arduino-serial
arduino-serial copied to clipboard
Review sleep(2) in flush (Correction suggest)
Hi,
Problem
I use this lib to do some tasks and this next line give me lot of problems :
https://github.com/todbot/arduino-serial/blob/e5958dc3bfd44a303456ff22da53974424da646d/arduino-serial-lib.c#L148
- Blocking code too long
- Overflow in serial port (without this sleep)
Why ?
Only because write()
method on my posix system (RPi 3 - Linux 3 - Raspbian 8) is asynchrone and doesn't wait entire writing of string on serial port (seem legit). The reason of flush()
Solution
The solution is easy, program should wait entire writing (some milliseconds) :
millisecondsToWait = ceil( totalBytes * protocolBitsLength / baudsSpeed / 1000 )
-
millisecondsToWait : Result, that should use as
usleep( millisecondsToWait );
instead ofsleep 2;
- totalBytes : Trame length, 23bytes in case of Arduino with MySensors in serial gateway mode,
- protocolBitsLength : Linked to parity, stop bits, frame length, for example 8N1 = 10 bits,
- baudsSpeed : Your speed : 9600, 38400, 115200, ...
That give waiting of 6 milliseconds for a MySensors gateway with a speed of 38400 bauds.
This really works ?
Yeah, tested with sniffing our serial port from hardware, without sleep()
- Before : writing 2 messages gives an unexpected 95% of time,
- After : no error in one week of test.