pySerial help please!

Grant Edwards grante at visi.com
Tue Feb 10 21:45:09 EST 2009


On 2009-02-10, bmaschino at gmail.com <bmaschino at gmail.com> wrote:
> Hello all,
>
> I am very new to Python and I am using it because I needed an
> easy language to control a piece of equipment that connects to
> my computer via a serial cable. I am running Python 2.6 with
> pySerial 2.4 under Windows. I can get Python to create a
> serial port on COM1, but when I try to write to the device,
> nothing happens. Here is an example:
>
> import serial
> ser = serial.Serial(0)
> ser.write("otpm 2 16 0")
> ser.close()

Are you sure you don't need some sort of line-terminator (e.g
\r or \n) to tell the device to go ahead and execute the command?

> If I connect to the device in Hyperterminal the device will
> behave as expected when I give it the command 'otpm 2 16 0'
> but not in Python.

In Hyperterminal did you have to press [enter] to get the
command to execute?

If you close the port immediately after calling write(), it's
possible that the data never actually got sent.  You should
probably wait until the data has been sent before closing the
port.

If you're running on Unix, I you should be able to call
ser.flush() to wait until the output buffer has been sent.

However, I wouldn't count too heavily on the underlying Unix
serial driver properly implimenting the tcdrain call.  And, I
think the flush() call on Windows is a noop.  So, putting an
appropriate delay between the write and the close is probably
the safest thing to do.

-- 
Grant




More information about the Python-list mailing list