views:

630

answers:

2

Good afternoon,

I would ask some suggestion about the best way to monitor events over the serial port.

I'm using PySerial to write "commands" over the serial port towards some devices and

I would like to receive feedback about the status of this devices.

Wich is the best way: 1) fullfill a pipe and read into, 2) a new thread delegated to read only, or what?

Can I also ask for a simple code to implement the solution?

+2  A: 

For general tips on working with pyserial, look at the search S.Lott suggested in the comment.

Regarding the best strategy to implement your application - it all depends on how your protocols are defined. Do the devices immediately respond to queries? Or do they continually send data that must be monitored? This is important to define, as it certainly affects the way you'll want to handle the communication.

Generally, I've found it simple and stable to have a separate thread reading everything from the serial port and just pumping the data into a Queue. The main application logic then can query this queue whenever it needs to and read the data.

Eli Bendersky
Yes devices respond immediately, but sometimes it's possible they send data "randomically".Anyway the search search suggested by S.Lott wasn't helpfull unfortunatelly
DrFalk3n
What about the separate thread solution I suggested in the last paragraph? Would it work for you? It's a very general pattern that provides good performance.
Eli Bendersky
A: 

The strategy choosen is to use python multiprocessing and queue see:

  1. http://www.ibm.com/developerworks/aix/library/au-threadingpython/index.html and

  2. http://www.ibm.com/developerworks/aix/library/au-multiprocessing/index.html?ca=dgr-lnxw9dPython-Multi&S_TACT=105AGX59&S_CMP=grsitelnxw9d

for reference

DrFalk3n
Just curious - why did you decide multiprocessing instead of threading?
Eli Bendersky
to avoid GIL problem and, I hope :-) , better performance
DrFalk3n