I'm trying to send commands from a device via a SoftwareSerial port to the arduino to talk to command sensors which will then respond with data to be sent back to the peripheral. I'm having a bit of trouble getting the commands from the peripheral to the Arduino.
I have a pH sensor, which is selected by sending 'pH\r' to the Arduino from the peripheral. This is coming in fine, and the pH probe is 'selected' in the Arduino program. However, when I want to send a command to the selected pH probe to tell it to give me sensor data (the command is 'r\r'), The SoftwareSerial port on the Arduino is not showing that there is new data (the 'r\r' command to tell the pH sensor to update the sensor data) available on the SoftwareSerial port. You can see how I'm checking this below. I am also listening on the SoftwareSerial port and have tested that I am doing so.
PeripheralSerial.listen();
//if (PeripheralSerial.isListening()) {
//Serial.println("PeripheralSerial is listening!"); //debug to see if port is listening
//}
while (PeripheralSerial.available()) { //while a char is holding in the serial buffer
Serial.println(PeripheralSerial.available()); //debug to see if the PeripheralSerial port is available
char inchar = (char)PeripheralSerial.read(); //get the new char
Serial.println(inchar);
inputstring += inchar; //add it to the inputstring
if (inchar == '\r') {
input_stringcomplete = true;
//Serial.print("set");
Serial.println("set"); //debug
} //if the incoming character is a <CR>, set the flag
}
Note that the input_stringcomplete flag needs to be set so that i can go into some if statements and select the correct probe to talk to.
if (input_stringcomplete){ //if a string from the PC has been received in its entirety
//Serial.print(inputstring);
if(inputstring.equals("pH\r")){
Serial.println(inputstring);
probe_select = 1;
Serial.print(probe_select); //debug
}
And I'm checking to see that the input_stringcomplete flag is being reset (which it is).
... // after the probe has been selected and has sent data back to the peripheral
inputstring = "";
input_stringcomplete = false;
Serial.println(input_stringcomplete);
}
I am not sure why the SoftwareSerial port is not seeing the second command. I don't know if it's something with the Arduino program or if it's with how I'm sending things from the peripheral. Does the serial port need to be shut down and reopened somehow before it can become available again? I haven't had problems with this in the past. I'm sending the first command (pH\r), wait a few seconds, then send the second command (r\r).
I checked to see if the correct commands are being sent to the arduino in a separate program like this:
//if (PeripheralSerial.available())
// Serial.write(PeripheralSerial.read()); // to the hardware serial
and I'm seeing all of the commands come in, so I know they're being sent.
This program works fine if I just use the serial monitor. Why is the PeripheralSerial port not available?