Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

pyserial script doesnt execute properly

26 views
Skip to first unread message

kishore

unread,
Mar 9, 2010, 3:39:15 AM3/9/10
to
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

News123

unread,
Mar 9, 2010, 4:19:56 AM3/9/10
to
Hi,


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

unread,
Mar 9, 2010, 10:01:22 AM3/9/10
to


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

unread,
Mar 9, 2010, 10:30:32 AM3/9/10
to

have found one more person with same problem but no solution

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

Message has been deleted

Andy

unread,
Mar 10, 2010, 12:01:48 AM3/10/10
to
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

unread,
Mar 10, 2010, 3:07:18 AM3/10/10
to pytho...@python.org
En Tue, 09 Mar 2010 12:01:22 -0300, kishore <kisho...@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

unread,
Mar 10, 2010, 3:39:36 AM3/10/10
to
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ó:

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

0 new messages