Skip to content

Commit

Permalink
add RESPONSE_TIMEOUT
Browse files Browse the repository at this point in the history
and rename MAX_MILLIS_TO_WAIT to WAITING_TURNAROUND_DELAY
potential bug fix for #50
  • Loading branch information
reaper7 committed Dec 16, 2020
1 parent 0f3553d commit 68e89f1
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 12 deletions.
13 changes: 7 additions & 6 deletions SDM.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ float SDM::readVal(uint16_t reg, uint8_t node) {

dereSet(LOW); //receive from SDM -> DE Disable, /RE Enable (for control MAX485)

resptime = millis() + MAX_MILLIS_TO_WAIT;
resptime = millis() + WAITING_TURNAROUND_DELAY;

while (sdmSer.available() < FRAMESIZE) {
if (resptime < millis()) {
Expand Down Expand Up @@ -153,7 +153,7 @@ float SDM::readVal(uint16_t reg, uint8_t node) {
++readingsuccesscount;
}

flush(); //read serial if any old data is available
flush(RESPONSE_TIMEOUT); //read serial if any old data is available and wait for RESPONSE_TIMEOUT (in ms)

#if !defined ( USE_HARDWARESERIAL )
sdmSer.stopListening(); //disable softserial rx interrupt
Expand Down Expand Up @@ -210,10 +210,11 @@ uint16_t SDM::calculateCRC(uint8_t *array, uint8_t num) {
return _crc;
}

void SDM::flush() {
uint8_t _i = 0;
while (sdmSer.available() && _i++ < 10) { //read serial if any old data is available
sdmSer.read();
void SDM::flush(unsigned long _flushtime) {
unsigned long flushtime = millis() + _flushtime;
while (sdmSer.available() || flushtime <= millis()) {
if (sdmSer.available()) //read serial if any old data is available
sdmSer.read();
delay(1);
}
}
Expand Down
10 changes: 7 additions & 3 deletions SDM.h
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,12 @@

#endif

#if !defined ( MAX_MILLIS_TO_WAIT )
#define MAX_MILLIS_TO_WAIT 500 // default max time to wait for response from SDM
#if !defined ( WAITING_TURNAROUND_DELAY )
#define WAITING_TURNAROUND_DELAY 200 // time in ms to wait for process current request
#endif

#if !defined ( RESPONSE_TIMEOUT )
#define RESPONSE_TIMEOUT 500 // time in ms to wait for return response from all devices before next request
#endif

//------------------------------------------------------------------------------
Expand Down Expand Up @@ -274,7 +278,7 @@ class SDM {
uint32_t readingerrcount = 0; // total errors counter
uint32_t readingsuccesscount = 0; // total success counter
uint16_t calculateCRC(uint8_t *array, uint8_t num);
void flush(); // read serial if any old data is available
void flush(unsigned long _flushtime = 0); // read serial if any old data is available or for a given time in ms
void dereSet(bool _state = LOW); // for control MAX485 DE/RE pins, LOW receive from SDM, HIGH transmit to SDM
};
#endif // SDM_h
13 changes: 10 additions & 3 deletions SDM_Config_User.h
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,15 @@
//------------------------------------------------------------------------------

/*
* define user MAX_MILLIS_TO_WAIT to wait for response from SDM
* define user WAITING_TURNAROUND_DELAY time in ms to wait for process current request
*/
//#define MAX_MILLIS_TO_WAIT 500
//#define WAITING_TURNAROUND_DELAY 200

//------------------------------------------------------------------------------
//------------------------------------------------------------------------------

/*
* define user RESPONSE_TIMEOUT time in ms to wait for return response from all devices before next request
*/
//#define RESPONSE_TIMEOUT 500

//------------------------------------------------------------------------------

0 comments on commit 68e89f1

Please sign in to comment.