diff --git a/Railcom.cpp b/Railcom.cpp index 1b9225c..08d8632 100644 --- a/Railcom.cpp +++ b/Railcom.cpp @@ -147,7 +147,7 @@ Railcom::Railcom(uint16_t blockvpin) { } uint16_t Railcom::expectLoco=0; uint16_t Railcom::expectCV=0; -uint16_t Railcom::expectWait=0; +unsigned long Railcom::expectWait=0; ACK_CALLBACK Railcom::expectCallback=0; @@ -183,22 +183,20 @@ void Railcom::process(uint8_t * inbound, uint8_t length) { byte type=(packet>>8) & 0x0F; byte data= packet & 0xFF; if (type==RMOB_POM) { - DIAG(F("POM READ loco=%d cv(%d)=%d/0x%x"), expectLoco, expectCV,data,data); + // DIAG(F("POM READ loco=%d cv(%d)=%d/0x%x"), expectLoco, expectCV,data,data); expectCallback(data); expectCV=0; } } - if (expectCV) { // still waiting - expectWait--; - if (expectWait==0) { - DIAG(F("POM READ loco=%d cv(%d) FAIL"), expectLoco, expectCV); - expectCallback(-1); - expectCV=0; - } - } } + if (expectCV && (millis()-expectWait)> POM_READ_TIMEOUT) { // still waiting + expectCallback(-1); + expectCV=0; + } + + auto v1=GETHIGHFLASH(decode,inbound[0]); auto v2=(length>1) ? GETHIGHFLASH(decode,inbound[1]):INV; uint16_t packet=(v1<<6) | (v2 & 0x3f); diff --git a/Railcom.h b/Railcom.h index 63487ea..007a0ea 100644 --- a/Railcom.h +++ b/Railcom.h @@ -36,12 +36,14 @@ class Railcom { static void anticipate(uint16_t loco, uint16_t cv, ACK_CALLBACK callback) { expectLoco=loco; expectCV=cv; - expectWait=10; // channel3 packets + expectWait=millis(); // start of timeout expectCallback=callback; }; private: - static uint16_t expectCV,expectLoco,expectWait; + static const unsigned long POM_READ_TIMEOUT=500; // as per spec + static uint16_t expectCV,expectLoco; + static unsigned long expectWait; static ACK_CALLBACK expectCallback; void noData(); uint16_t vpin;