pyserial script doesnt execute properly

K

kishore

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
 
N

News123

Hi,
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
 
K

kishore

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


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?
 
K

kishore

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/008613.html
 
K

kishore

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/008613.html
 
A

Andy

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
 
G

Gabriel Genellina

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.
 
K

kishore

En Tue, 09 Mar 2010 12:01:22 -0300, kishore <[email protected]>
escribió:







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


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


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.


print repr(a)


print repr(b)


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

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

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,743
Messages
2,569,478
Members
44,899
Latest member
RodneyMcAu

Latest Threads

Top