Vorac
Vorac

Reputation: 9114

Serial communication over USB converter in Python - how to aproach this?

A python program needs to accept a string every second from a serial port. I plan on using a RS-232 to USB converter. The application is going to work under Ubuntu 10.04.

How do I approach this? Do I use pySerial or libusb?

There needs to be done some processing in the meantime, so synchronous communication is not viable. Do I use some kind of interrupts or do I need to open separate threads? Or do I use blocking reads, believing that 1s is enough for my computations (it is plenty ... for now)?

I know, RTFM, but heading in the correct direction from the start will save me a lot of time! Thanks for bearing with me.

Upvotes: 3

Views: 6544

Answers (1)

Preston
Preston

Reputation: 2653

If your RS232-USB converter has a driver in Ubuntu that makes it look like a COM port then you will want to use pySerial (the interface is the same as any other COM port). If there is no driver for your device then you might have to use libusb and find the protocol for your specific device. Most major RS232-USB converters these days have usbserial based drivers committed and maintained in the Linux kernel. Simply check with your vendor for this.

There are many ways to do parallel processing but typically I write my applications in two ways:

  • Have a read thread that does nothing but read and fill a local thread safe buffer so data is ready for other threads when needed.

  • Have a read thread that reads data, determines where it goes and delivers it via messaging/event processing to the component that needs it.

The decisions here will depend on what your goals are, and how much processing is needed outside the reading.

Upvotes: 4

Related Questions