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

Added serial reads to flush spurious output from sensor #3

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

gitpeut
Copy link

@gitpeut gitpeut commented Feb 5, 2024

Used library to get Winsen ZH07 device working. Input and output are the same as the ZH06 . Spurious output from the sensor made sleep, wake and other commands fail unpredictably. Added serial reads to flush such unexpected sensor inputs

Used library to get Winsen ZH07 device. Input and output are the same as the ZH06 . Spurious output from the sensor
made  sleep, wake and other commands fail unpredictably. Added serial reads to flush such unexpected sensor inputs
@ShaggyDog18
Copy link
Owner

There is no need to modify the library. You may call a serial "clean up" function, for example:

void RX_flush(void){
  delay(100);   
  while(  _serial.available() ) {
     _serial.read();
  } 
}

after calling library functions that read from RX when you need it and for the troublesome sensors. The library is universal and works fine for most of existing sensors. Your case is kind of unique and you offered a simple solution to avoid an unpredictable garbage on RX. Having it as a separate function would also reduce your code size (you repeated the same code in multiple function methods).
So, my suggestion would be to keep the library as is, add clean up function RX_flush() to your code and call it after library functions whenever you have a troublesome sensor.

Use separate RX_flush function instead of repeating while() loops.
@gitpeut
Copy link
Author

gitpeut commented Feb 8, 2024

Thanks for your thoughts.

Not sure if the compiler wouldn't inline the one simple loop, but I agree style-wise
it would be neater to have a separate function.

To me it makes more sense to add the RX_flush function to the library, since _serial is private and no getter is available.

As I ran into this issue of spurous data, it's likely others will too in some cases, and I can't think of a scenario where
it would cause unwanted side effects.

See also my latest commit.

Your thoughts?

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

Successfully merging this pull request may close these issues.

2 participants