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
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 31 additions & 2 deletions SD_ZH03B.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,14 @@ bool SD_ZH03B::readData(void) {
return true;
}

int SD_ZH03B::RX_flush(void){
delay(100);
int spurious_count;
for(spurious_count = 0; _serial.available(); ++spurious_count ) {
_serial.read();
}
return spurious_count;
}

void SD_ZH03B::setMode( const mode_t _mode ) {
switch( _mode ) {
Expand All @@ -180,6 +188,8 @@ void SD_ZH03B::setQandAmode(void) {
_currentMode = QA_MODE;
_sizeFrame = SIZEOF_QA_FRAME;
_sendCmd( 0x78, 0x41, 0x46 );

RX_flush();
}


Expand All @@ -192,6 +202,8 @@ void SD_ZH03B::setInitiativeMode(void) { // default mode
_sizeFrame = ZH06_SIZEOF_IU_FRAME;

_sendCmd( 0x78, 0x40, 0x47 );

RX_flush();
}


Expand All @@ -215,15 +227,32 @@ bool SD_ZH03B::_getCmdConfirmation(void) {

_serial.readBytes( _unionFrame.buffer, 9 ); // read the response to a buffer

#ifdef DEBUG
Serial.println("Command confirmation");
for( uint8_t i = 0; i < 9; i++ ) {
Serial.print( _unionFrame.buffer[i], HEX); Serial.print( ":");
}
Serial.println();
#endif

// check up the received response
if( _unionFrame.buffer[1] == 0xA7 && _unionFrame.buffer[2] == 0x01 && _unionFrame.buffer[8] == 0x58 )
if( _unionFrame.buffer[1] == 0xA7 && _unionFrame.buffer[2] == 0x01 && _unionFrame.buffer[8] == 0x58 ){
return true;

}

Serial.println("Command confirmation invalid:");
for( uint8_t i = 0; i < 9; i++ ) {
Serial.print( _unionFrame.buffer[i], HEX); Serial.print( ":");
}
Serial.println();

return false;
}

void SD_ZH03B::_sendCmd(const uint8_t ch1, const uint8_t ch2, const uint8_t ch3) {

_serial.flush(); // clear buffer
RX_flush();

// send command header
_serial.write((uint8_t)0xFF);
Expand Down
7 changes: 7 additions & 0 deletions SD_ZH03B.h
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,13 @@ enum type_t : uint8_t {
uint16_t getPM10_0(void) const {
return _currentMode == IU_MODE ? _unionFrame.ZH03_IUframe.concPM10_0 : _unionFrame.ZHxx_QAframe.concPM10_0;
}

/**
* @brief read spurious data on RX before sending
* @note unpredicatble a sensor may havesent spurious data,which can confuse later data reads
* @return nunmber of spurious bytes read */

int RX_flush(void);

#define ZH03_SIZEOF_IU_FRAME 24
#define ZH06_SIZEOF_IU_FRAME 32
Expand Down