tags:

views:

30

answers:

1

Hi.

I have to read a stream which is sent from a homemade device over the serial port. The problem is that it should be deployed on a machine where I don't have access to install anything new, which means I have to use the python standard libraries to do this. Is this possible, and if so, how can I manage this.

If it turns out to be almost impossible, I will have to get someone to install pySerial, but I would really appreciate it if it could be done without this.

If there is differences in Linux/Windows, this is on a Windows machine, but I would really appreciate a cross platform solution.

+1  A: 

On Unix-like operating systems, the serial port work just like a file, and you simply open it and read or write bytes. There are some extra calls you can make to set the baud rate and whatnot, but that's essentially all there is.

On Windows, you open the serial port like a file, but you must use some particular ways of accessing it that are slightly different from what Python uses for normal files. Unfortunately it is difficult to successfully access a Windows serial port using only native Python libraries.

The pyserial library provides a uniform, cross-platform way of accessing serial ports. It relies on ctypes, which is in the standard library since Python 2.5, so you may be able to include pyserial with your application and just use that.

Greg Hewgill
Can you describe a bit more what the differences are, or maybe even better give me a hint as to where to look. Tried to google it, but couldn't find anything other then pySerial.
martiert
One of the differences is that on Windows, you must read and write the serial port device using the Win32 API functions `ReadFile` and `WriteFile`, in *overlapped I/O* mode. Doing this correctly usually involves creating separate threads to handle the reading and writing. Since `pyserial` does all this and is open source, you can look inside to see what it does. Your last resort can be "do exactly what `pyserial` does".
Greg Hewgill