weider weider - 8 months ago 49
Linux Question

Input from serial port to Python3

I get data from serial port by package.
A few package a second, like below:

275 242 280 263 235 179 234 236 233 195 203 190 202...

Aproximatelly 250 digits.
Every time diffrent amount.

My main purpose is real-time picture from this data.
I use python like below:

ser = serial.Serial('/dev/ttyACM0', 921600, timeout=2, xonxoff=False, rtscts=False, dsrdtr=False) #Tried with and without the last 3 parameters, and also at 1Mbps, same happens.
while true:
data_raw = ser.readline()
*some calculation*

My first question is: Is it possible to miss some package, if the rate of packages too fast(e.g ~1000 package per second)?
Or I must use pipe,like:

cat /dev/ttyS0 | python3 script.py

My second question is: What the language I must to use to avoid any losses? Matlab is enough? or maybe C++?
I use Linux.

Answer Source

You are reading from a serial port with a bit rate of 921600 bps. Call it 100,000 characters per second, or 10┬Ás / character. 250 characters will take at least 2.5ms to arrive - you can do a lot in 2.5ms. (Incidentally, this means you can't get more than 400 packets per second - you just can't stuff the bits along the wire any faster!)

The device driver will do some buffering, so I wouldn't worry about it.