[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.python

pyserial script doesnt execute properly

kishore

3/9/2010 8:39:00 AM

hello there

Iam using python 2.5.4
pyserial 2.4
pywin32-214

on windows 7


i hav a small test script written to query a serial device (arduino)
and get back reply appropriately



////file: test.py

import serial
print 'hi'
ser=serial.Serial(port='\\.\COM2', baudrate=9600)
ser.close()
ser.open()
ser.write('1')
ser.readline()
ser.readline()
ser.close()



the device waits for '1' through its serial interface and print two
lines if it gets '1'

"Some Data found" and "Header received"


the script works on IDLE well when given one line at a time

but when given in command line as python test.py it prints hi and wait
forever

can anyone help?
thanks in advance

7 Answers

News123

3/9/2010 9:20:00 AM

0

Hi,

kishore wrote:
> hello there
>
> Iam using python 2.5.4
> pyserial 2.4
> pywin32-214
>
> on windows 7
>
>
> i hav a small test script written to query a serial device (arduino)
> and get back reply appropriately
>
>
>
> ////file: test.py
>
> import serial
> print 'hi'
> ser=serial.Serial(port='\\.\COM2', baudrate=9600)
> ser.close()
> ser.open()
> ser.write('1')
> ser.readline()
> ser.readline()
> ser.close()
>
>
>
> the device waits for '1' through its serial interface and print two
> lines if it gets '1'
>
> "Some Data found" and "Header received"
>
>
> the script works on IDLE well when given one line at a time
>
> but when given in command line as python test.py it prints hi and wait
> forever
>


Unfortunately I don't remember exacty, but try following:

close IDLE and try then to start the script from the command line.
I remember having had a problem with idle, that it did not always close
the UART port
(especially, when an error (e.g. syntax) occured before the close statement)


bye

N

kishore

3/9/2010 3:01:00 PM

0

On Mar 9, 2:19 pm, News123 <news...@free.fr> wrote:
> Hi,
>
>
>
> kishore wrote:
> > hello there
>
> > Iam using python 2.5.4
> > pyserial  2.4
> > pywin32-214
>
> > on windows 7
>
> > i hav a small test script written to query a serial device (arduino)
> > and get back reply appropriately
>
> > ////file: test.py
>
> > import serial
> > print 'hi'
> > ser=serial.Serial(port='\\.\COM2', baudrate=9600)
> > ser.close()
> > ser.open()
> > ser.write('1')
> > ser.readline()
> > ser.readline()
> > ser.close()
>
> > the device waits for '1' through its serial interface and print two
> > lines if it gets '1'
>
> > "Some Data found" and "Header received"
>
> > the script works on IDLE well when given one line at a time
>
> > but when given in command line as python test.py it prints hi and wait
> > forever
>
> Unfortunately I don't remember exacty, but try following:
>
> close IDLE and try then to start the script from the command line.
> I remember having had a problem with idle, that it did not always close
> the UART port
> (especially, when an error (e.g. syntax) occured before the close statement)
>
> bye
>
> N


Thanks for your response
i tried closing idle and the following code prints
port opened
Write failed

code:

import serial
import time
ser=serial.Serial(port='\\.\COM2', baudrate=9600)
if ser:
print 'port opened'
ser.open()
if ser.write('1'):
print 'Write success'
else:
print 'write failed'

time.sleep(1)

a=ser.readline()
time.sleep(1)
b=ser.readline()
print b
ser.close()



I believe this might be a serial port access error.
how to solve this?
Any suggestions?

kishore

3/9/2010 3:31:00 PM

0

On Mar 9, 8:01 pm, kishore <kishorei...@gmail.com> wrote:
> On Mar 9, 2:19 pm, News123 <news...@free.fr> wrote:
>
>
>
> > Hi,
>
> > kishore wrote:
> > > hello there
>
> > > Iam using python 2.5.4
> > > pyserial  2.4
> > > pywin32-214
>
> > > on windows 7
>
> > > i hav a small test script written to query a serial device (arduino)
> > > and get back reply appropriately
>
> > > ////file: test.py
>
> > > import serial
> > > print 'hi'
> > > ser=serial.Serial(port='\\.\COM2', baudrate=9600)
> > > ser.close()
> > > ser.open()
> > > ser.write('1')
> > > ser.readline()
> > > ser.readline()
> > > ser.close()
>
> > > the device waits for '1' through its serial interface and print two
> > > lines if it gets '1'
>
> > > "Some Data found" and "Header received"
>
> > > the script works on IDLE well when given one line at a time
>
> > > but when given in command line as python test.py it prints hi and wait
> > > forever
>
> > Unfortunately I don't remember exacty, but try following:
>
> > close IDLE and try then to start the script from the command line.
> > I remember having had a problem with idle, that it did not always close
> > the UART port
> > (especially, when an error (e.g. syntax) occured before the close statement)
>
> > bye
>
> > N
>
> Thanks for your response
> i tried closing idle and the following code prints
> port opened
> Write failed
>
> code:
>
> import serial
> import time
> ser=serial.Serial(port='\\.\COM2', baudrate=9600)
> if ser:
>         print 'port opened'
> ser.open()
> if ser.write('1'):
>         print 'Write success'
> else:
>         print 'write failed'
>
> time.sleep(1)
>
> a=ser.readline()
> time.sleep(1)
> b=ser.readline()
> print b
> ser.close()
>
> I believe this might be a serial port access error.
> how to solve this?
> Any suggestions?

have found one more person with same problem but no solution

http://mail.python.org/pipermail/python-win32/2009-January/0...

kishore

3/9/2010 3:42:00 PM

0

On Mar 9, 8:01 pm, kishore <kishorei...@gmail.com> wrote:
> On Mar 9, 2:19 pm, News123 <news...@free.fr> wrote:
>
>
>
> > Hi,
>
> > kishore wrote:
> > > hello there
>
> > > Iam using python 2.5.4
> > > pyserial  2.4
> > > pywin32-214
>
> > > on windows 7
>
> > > i hav a small test script written to query a serial device (arduino)
> > > and get back reply appropriately
>
> > > ////file: test.py
>
> > > import serial
> > > print 'hi'
> > > ser=serial.Serial(port='\\.\COM2', baudrate=9600)
> > > ser.close()
> > > ser.open()
> > > ser.write('1')
> > > ser.readline()
> > > ser.readline()
> > > ser.close()
>
> > > the device waits for '1' through its serial interface and print two
> > > lines if it gets '1'
>
> > > "Some Data found" and "Header received"
>
> > > the script works on IDLE well when given one line at a time
>
> > > but when given in command line as python test.py it prints hi and wait
> > > forever
>
> > Unfortunately I don't remember exacty, but try following:
>
> > close IDLE and try then to start the script from the command line.
> > I remember having had a problem with idle, that it did not always close
> > the UART port
> > (especially, when an error (e.g. syntax) occured before the close statement)
>
> > bye
>
> > N
>
> Thanks for your response
> i tried closing idle and the following code prints
> port opened
> Write failed
>
> code:
>
> import serial
> import time
> ser=serial.Serial(port='\\.\COM2', baudrate=9600)
> if ser:
>         print 'port opened'
> ser.open()
> if ser.write('1'):
>         print 'Write success'
> else:
>         print 'write failed'
>
> time.sleep(1)
>
> a=ser.readline()
> time.sleep(1)
> b=ser.readline()
> print b
> ser.close()
>
> I believe this might be a serial port access error.
> how to solve this?
> Any suggestions?

have found one more person with same problem but no solution

http://mail.python.org/pipermail/python-win32/2009-January/0...

Andy

3/10/2010 5:02:00 AM

0

Hi Kishore,

Have you tried "ser=serial.Serial(port='COM2', baudrate=9600)" instead
of "port='\\.\COM2'"?

Also, I'd suggest you temporarily define some other parameters that
now you're leaving to default values. From the documentation of
pyserial:
readline(size=None, eol='\n')

You're sure that your Arduino device is sending a '\n' right after the
'1', aren't you? Or is it two consecutive '1'?

And another idea: If this is your first experience with PySerial, I
suggest you first use some free software as counterpart to your
Arduino device in order to confirm the microcontroller is sending
exactly what you think it is sending. In other words, is it sending
to your Python program '11' or '1\n1\n'? Also to confirm other
details such as parity bits, baud, etc.

Cheers,
Andy

Gabriel Genellina

3/10/2010 8:07:00 AM

0

En Tue, 09 Mar 2010 12:01:22 -0300, kishore <kishoreinme@gmail.com>
escribió:
>>
>> > Iam using python 2.5.4
>> > pyserial 2.4
>> > pywin32-214
>> > on windows 7
>>
>> > i hav a small test script written to query a serial device (arduino)
>> > and get back reply appropriately
>>
>
> Thanks for your response
> i tried closing idle and the following code prints
> port opened
> Write failed
>
> code:
>
> import serial
> import time
> ser=serial.Serial(port='\\.\COM2', baudrate=9600)

If you want a string containing these 8 characters \\.\COM2 you have to
write it either as r'\\.\COM2' or '\\\\.\\COM2'

> if ser:
> print 'port opened'

Either the Serial object is constructed and returned, or an exception is
raised. 'if ser:' has no sense; Python is not C...

> ser.open()
> if ser.write('1'):
> print 'Write success'
> else:
> print 'write failed'

The write method, when successful, implicitly returns None. None has a
false boolean value, so your code will always print 'write failed'.

Usually, in Python, error conditions are marked by raising an exception.
Using return values to indicate success/failure is uncommon.

Also, are you sure the device doesn't expect a newline character? '1\n'?

You may need to call ser.flushOutput() to ensure the output buffer is
actually emptied.

> time.sleep(1)
> a=ser.readline()

print repr(a)

> time.sleep(1)
> b=ser.readline()

print repr(b)

> ser.close()

> I believe this might be a serial port access error.
> how to solve this?
> Any suggestions?

I don't think so. If you could not access the serial port, you'd have seen
an IOError exception or similar.

--
Gabriel Genellina

kishore

3/10/2010 8:40:00 AM

0

On Mar 10, 1:07 pm, "Gabriel Genellina" <gagsl-...@yahoo.com.ar>
wrote:
> En Tue, 09 Mar 2010 12:01:22 -0300, kishore <kishorei...@gmail.com>
> escribió:
>
>
>
>
>
> >> > Iam using python 2.5.4
> >> > pyserial  2.4
> >> > pywin32-214
> >> > on windows 7
>
> >> > i hav a small test script written to query a serial device (arduino)
> >> > and get back reply appropriately
>
> > Thanks for your response
> > i tried closing idle and the following code prints
> > port opened
> > Write failed
>
> > code:
>
> > import serial
> > import time
> > ser=serial.Serial(port='\\.\COM2', baudrate=9600)
>
> If you want a string containing these 8 characters \\.\COM2 you have to
> write it either as r'\\.\COM2' or '\\\\.\\COM2'
>
> > if ser:
> >    print 'port opened'
>
> Either the Serial object is constructed and returned, or an exception is
> raised. 'if ser:' has no sense; Python is not C...
>
> > ser.open()
> > if ser.write('1'):
> >    print 'Write success'
> > else:
> >    print 'write failed'
>
> The write method, when successful, implicitly returns None. None has a
> false boolean value, so your code will always print 'write failed'.
>
> Usually, in Python, error conditions are marked by raising an exception.
> Using return values to indicate success/failure is uncommon.
>
> Also, are you sure the device doesn't expect a newline character? '1\n'?
>
> You may need to call ser.flushOutput() to ensure the output buffer is
> actually emptied.
>
> > time.sleep(1)
> > a=ser.readline()
>
> print repr(a)
>
> > time.sleep(1)
> > b=ser.readline()
>
> print repr(b)
>
> > ser.close()
> > I believe this might be a serial port access error.
> > how to solve this?
> > Any suggestions?
>
> I don't think so. If you could not access the serial port, you'd have seen
> an IOError exception or similar.
>
> --
> Gabriel Genellina

thanks Gabriel & Andy..ll get back soon here after trying ur
suggestions