python - Read from two serial ports asynchronously -


i'd read 2 (or more) serial ports (/dev/ttyusb0 etc) @ same time in python on linux. want read complete lines each port (whichever has data) , process results in order received (without race conditions). simple example write lines single merged file.

i assume way based on pyserial, can't quite figure out how it. pyserial has non-blocking reads using asyncio , using threads. asyncio marked experimental. assume there wouldn't race conditions if processing done in asyncio.protocol.data_received(). in case of threads, processing have protected mutex.

perhaps can done not in pyserial. 2 serial ports can opened files , read when data available using select().

you try take values in order , memorise in variables:

a = data1.read () b = data2.read () 

and after process in order:

if len (a) != 0 or len (b ) != 0:              process              process b 

using method if 1 or both of value has data, process it


Comments

Popular posts from this blog

ios - RestKit 0.20 — CoreData: error: Failed to call designated initializer on NSManagedObject class (again) -

java - Digest auth with Spring Security using javaconfig -

laravel - PDOException in Connector.php line 55: SQLSTATE[HY000] [1045] Access denied for user 'root'@'localhost' (using password: YES) -