Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to read from the serial port in python without using external APIs?

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.

like image 569
martiert Avatar asked Jun 24 '10 07:06

martiert


1 Answers

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.

like image 170
Greg Hewgill Avatar answered Nov 10 '22 07:11

Greg Hewgill