Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Review sleep(2) in flush (Correction suggest) #14

Open
Servuc opened this issue Sep 25, 2017 · 0 comments
Open

Review sleep(2) in flush (Correction suggest) #14

Servuc opened this issue Sep 25, 2017 · 0 comments

Comments

@Servuc
Copy link

Servuc commented Sep 25, 2017

Hi,

Problem

I use this lib to do some tasks and this next line give me lot of problems :

sleep(2); //required to make flush work, for some reason

  • 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 of sleep 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.
@Servuc Servuc changed the title Magic code (Correction suggest) Review sleep(2) in flush (Correction suggest) Sep 26, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant