diff --git a/DCCEXParser.cpp b/DCCEXParser.cpp index 94f3a21..241da63 100644 --- a/DCCEXParser.cpp +++ b/DCCEXParser.cpp @@ -51,11 +51,11 @@ void DCCEXParser::loop(Stream & stream) { } } - int DCCEXParser::splitValues( int result[MAX_PARAMS]) { + int DCCEXParser::splitValues( int result[MAX_PARAMS], char * cmd) { byte state=1; byte parameterCount=0; int runningValue=0; - const char * remainingCmd=buffer+1; // skips the opcode + const char * remainingCmd=cmd+1; // skips the opcode bool signNegative=false; // clear all parameters in case not enough found @@ -96,12 +96,13 @@ void DCCEXParser::loop(Stream & stream) { // See documentation on DCC class for info on this section void DCCEXParser::parse(Print & stream, const char *com) { - // DIAG(F("\nPARSING:%s\n"),com); + DIAG(F("\nPARSING:%s\n"),com); (void) EEPROM; // tell compiler not to warn thi is unused int p[MAX_PARAMS]; - int params=splitValues(p); - + int params=splitValues(p, com); + if (com[0]=='<') com++; + // Functions return from this switch if complete, break from switch implies error to send switch(com[0]) { diff --git a/DCCEXParser.h b/DCCEXParser.h index e979ab1..5d0bd1c 100644 --- a/DCCEXParser.h +++ b/DCCEXParser.h @@ -14,7 +14,7 @@ struct DCCEXParser byte bufferLength=0; bool inCommandPayload=false; char buffer[MAX_BUFFER+2]; - int splitValues( int result[MAX_PARAMS]); + int splitValues( int result[MAX_PARAMS],char * command); bool parseT(Print & stream, int params, int p[]); bool parseZ(Print & stream, int params, int p[]); diff --git a/WifiInterface.cpp b/WifiInterface.cpp index bb7b86c..a1204ae 100644 --- a/WifiInterface.cpp +++ b/WifiInterface.cpp @@ -78,6 +78,7 @@ void WifiInterface::loop() { // read anything into a buffer, collecting info on the way while (loopstate!=99 && Serial1.available()) { int ch=Serial1.read(); + Serial.write(ch); switch (loopstate) { case 0: // looking for + connectionId=0; @@ -105,6 +106,7 @@ void WifiInterface::loop() { else datalength=datalength*10 + (ch-'0'); break; case 7: // reading data + streamer.write(ch); datalength--; if (datalength==0) loopstate=99; break; @@ -113,13 +115,19 @@ void WifiInterface::loop() { if (loopstate!=99) return; // TODO remove > in data streamer.write('\0'); - streamer.flush(); // reset write position to start of buffer + + DIAG(F("\nWifiRead:%d:%s\n"),connectionId,buffer); + streamer.setBufferContentPosition(0,0); // reset write position to start of buffer // SIDE EFFECT WARNING::: // We know that parser will read the entire buffer before starting to write to it. // Otherwise we would have to copy the buffer elsewhere and RAM is in short supply. // TODO ... tell parser that callbacks are diallowed because we dont want to handle the async - parser.parse(streamer,buffer+1); - StringFormatter::send(Serial1,F("AT+CIPSEND=%d,%d\r\n>%s"),connectionId,streamer.available(),buffer); - loopstate=0; + parser.parse(streamer,buffer); + if (streamer.available()) { // there is a reply to send + StringFormatter::send(Serial1,F("AT+CIPSEND=%d,%d\r\n"),connectionId,streamer.available()); + streamer.write('\0'); + if (checkForOK(1000,">")) Serial1.print((char *) buffer); + } + loopstate=0; // go back to looking for +IPD }