pyserial script doesnt execute properly
Gabriel Genellina
gagsl-py2 at yahoo.com.ar
Wed Mar 10 03:07:18 EST 2010
En Tue, 09 Mar 2010 12:01:22 -0300, kishore <kishoreinme at 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
More information about the Python-list
mailing list