-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fixed serial communication issues between ESP and Arduino
- Loading branch information
Showing
10 changed files
with
393 additions
and
283 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,25 +1,58 @@ | ||
#include <Arduino.h> // To get millis() | ||
#include "common.h" | ||
|
||
void checkSerial(HardwareSerial *serial, char *message, void (*processMsgFunc)(char *)) { | ||
char incomingChar; | ||
// Serial has a ~60 bytes buffer. If messages longer than 60 bytes are not read fast enough, they can be lost. | ||
boolean readFromSerial(HardwareSerial *serial, char *message, unsigned long timeOut) { | ||
int incomingChar; | ||
int length; | ||
unsigned long now = millis(); | ||
|
||
while (serial->available() > 0) { | ||
while (true) { | ||
incomingChar = serial->read(); | ||
if(incomingChar > 0) { | ||
if((incomingChar == '\r') || (incomingChar == '\n')) { | ||
processMsgFunc(message); | ||
if(strlen(message) > 1) { // do not process the extra \n or \r when println was used | ||
return true; | ||
} | ||
message[0] = 0; | ||
return false; // Message should not be processed | ||
} else { | ||
length = strlen(message); | ||
if(length < MAX_SERIAL_INPUT_MESSAGE - 2) { | ||
message[length] = incomingChar; | ||
message[length + 1] = 0; | ||
} else { | ||
// Ignore message, for now | ||
// Ignore message | ||
message[0] = 0; | ||
Serial.println("Serial message too big"); | ||
return false; | ||
} | ||
} | ||
} else { | ||
if((millis() - now) > timeOut) { | ||
// Keep what was read, we'll get the rest at next call... ? | ||
//Serial.println("Time out reading on serial"); | ||
return false; | ||
} | ||
} | ||
} | ||
} | ||
|
||
// Write to serial, insuring message is read to not overload the buffer | ||
boolean writeToSerial(HardwareSerial *serial, char *message, long int timeOut) { | ||
char *charPtr; | ||
int length; | ||
unsigned long now = millis(); | ||
charPtr = message; | ||
while(*charPtr) { | ||
if(serial->availableForWrite()) { | ||
serial->write(charPtr++, 1); | ||
} else { | ||
if((millis() - now) > timeOut) { | ||
//Serial.println("Time out writing on serial"); | ||
return false; | ||
} | ||
} | ||
} | ||
serial->write(0x10); // line feed to end message | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
#include <HardwareSerial.h> | ||
#define MAX_SERIAL_INPUT_MESSAGE 161 | ||
#define MAX_SERIAL_INPUT_MESSAGE 600 // some "big" Json strings | ||
|
||
void checkSerial(HardwareSerial *serial, char *message, void (*processMsgFunc)(char *)); | ||
boolean readFromSerial(HardwareSerial *serial, char *message, unsigned long timeOut); | ||
boolean writeToSerial(HardwareSerial *serial, char *message, unsigned long timeOut); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.