This code is on the slave side of an Uno with a soft serial port connected by a RS-485 bus. Receiving commands S,1 (Open Shade) or S,2 (Close Shade) or S,3 (Status of shade position) determines the action to take.
I was seeing a second duplicate command which caused issues until I started clearing the serial buffer after a command was executed. It is not being sent twice. I am scratching my head as to how the duplicate command is being generated. The flush is working but why might I be needing it?
/******* READ SERIAL PORT FOR DATA COMMAND FROM XOJO *********/
void RS485SerialCheck() {
inputString = "";// Clear string to start
while (RS485Serial.available()) {
// get the new byte:
char inChar = (char)RS485Serial.read();
// add it to the inputString:
inputString += inChar;
if ((inChar == '\r') && ((inputString.charAt(0)) == 'S')) { // Carriage Return CR
//******Open Shutter Command******
if ((inputString.charAt(2)) == '1') {
findWestClosedlimit(); //Find closed first
delay(400);
findWestOpenlimit();
delay(400);
findEastClosedlimit(); //Find closed first
findEastOpenlimit();
while (RS485Serial.available ()) //Flush port
RS485Serial.read ();
}
//******Close Shutter Command******
else if ((inputString.charAt(2)) == '2') {
findWestClosedlimit();
delay(400);
findEastClosedlimit();
while (RS485Serial.available ()) //Flush port
RS485Serial.read ();
}
//******Data Return******
if ((inputString.charAt(2)) == '3') {
ReturnDataToXOJO();
while (RS485Serial.available ()) //Flush port
RS485Serial.read ();
}
}
}
}
The Serial Basics post was a jewel for me and it solved my need to clear out the buffer. My question is since we're now working with C Strings how do I find what a particular Chr is in a c string as you might with a string?
I did assign a (String inputString = "") to the chr string and the below works but there must be a better approach.
Yes, I just discovered it and it works. I'll check the link out to get better acquainted. Thanks for all your help. I believe I'm back on track for the moment. C++ is very new to me.