Cross-platform serial port access

Grant Edwards grante at visi.com
Thu Mar 22 12:44:15 EST 2001


In article <99d7g3$qed$1 at news.netpower.no>, David Brown wrote:

>>Currently, serial port stuff in Python is not portable between
>>Mac/MS/Unix.
>
>I suppose it should be easy enough to make a small wrapper around the
>different platforms' code.

Yup -- somebody should do that someday...

>>>Does anyone know if Linux supports RTS toggle handshaking
>>>(i.e., the RTS line is high while transmitting, for convenient
>>>connection to RS-485 convertors),
>>
>>Not with the standard PC serial port driver.  I hacked the
>>16x50 driver to do RTS toggle several years ago, but it never
>>worked reliably.  It's very difficult to do it reliably in
>>software at high baud rates.

Back when I tried to do it with the Linux driver, I was trying
to use extra transmit data bytes (didn't work well) and later
the shift-register empty bit to shut off RTS.  I should have
done it using loop-back: enable loopback in the UART and shut
off RTS in the receive handling code when the end of the tx
message is seen.  That only works if loopback in the UART
doesn't disable the transmit data pin...

>>Your best bet is to buy a board that supports RTS toggle in
>>hardware or use a 232-485 converter that handles it
>>automatically by detecting the presence of data.

>This is definitely a shame.  The RTS toggle works well under NT
>(no idea about Win9x) - I have written a Delphi program that
>uses it to run a RS-485 convertor at 57600.  I don't know for
>sure how fast RTS is turned off after the last byte - the other
>nodes on the bus wait 10 ms before answering anyway.

That's the trick.  10ms is long enough that it can be done in
software.  I had to get it shut off within 1 byte-time of the
last stop bit.  At 57.6K that's 17us.

>I guess under Linux I would have to toggle RTS manually.
>Turning it on before sending is easy enough, it is turning it
>off at the right time that is more of a hassle.

Yup - it's extremely tricky.  I finally gave up and bought a
board that did it in hardware.  If you've got a converter that
loops back transmit data, you can use that: do a write(), then
do a read() -- when you've seen the right number of bytes, shut
off RTS.

Some of the middle and upper level serial driver layers in
Linux have a 10ms granularity -- so you might not be able do
anything from user-space faster than 10ms.

>So-called "intelligent" RS-485 convertors that read the
>incomming data and change direction appropriately have their
>own problems.  We have delivered systems that communicated at
>300 baud, and "intelligent" convertors frequently do not work
>at such slow rates.  Additionally, they generally need
>configured with DIP-switches and the like, which is an extra
>hassle if you need to change the baud rate.

Yup.  The ones without dip-switches (fixed tx data timeout)
will only work well at certain baud rates.

>Simple RTS-controlled convertors are much easier in that
>respect.

I agree.

>Although it is not really Python-specific, do you know if there
>are serial drivers for Linux that simulate 9-bit UARTs (i.e.,
>swapping the parity bit from space to mark as appropriate)?

I don't think so.

-- 
Grant Edwards                   grante             Yow!  .. Do you like
                                  at               "TENDER VITTLES?"?
                               visi.com            



More information about the Python-list mailing list