I try to communicate with 2 RS485 sensors (pH and EC), but I have some troubles with my code. First, I tried to get data from just one:
The function in charge of request sending:
String GET_probe(String probe){
if(probe == "pH"){
Serial.println("Sending request pH...");
digitalWrite(modbusModuleRE, RS485Transmit); // Init Transceiver
digitalWrite(modbusModuleDE, RS485Transmit); // Init Transceiver
byte RTUrequest[] = {0x00, 0x03, 0x00, 0x00, 0x00, 0x01, 0x85, 0xDB};
RS485Serial.write(RTUrequest, sizeof(RTUrequest));
}
if(probe == "EC"){
Serial.println("Sending request EC...");
digitalWrite(modbusModuleRE, RS485Transmit); // Init Transceiver
digitalWrite(modbusModuleDE, RS485Transmit); // Init Transceiver
byte RTUrequest[] = {0x01, 0x03, 0x00, 0x00, 0x00, 0x02, 0xC4, 0x0B};
RS485Serial.write(RTUrequest, sizeof(RTUrequest));
}
RS485Serial.flush();
RS485Serial.listen();
}
The function in charge of reading sensor's answer:
void ReadRx() {
digitalWrite(modbusModuleRE, RS485Receive);
digitalWrite(modbusModuleDE, RS485Receive);
if(RS485Serial.available()) {
Serial.print("Answer: ");
for(int i=0; i<7; i++){
String myByte = String(RS485Serial.read(), HEX);// Read the byte
if (myByte.length() == 1)
{
myByte = "0"+myByte;
}
modbusHexBuffer[i] = myByte;
Serial.print(myByte);
Serial.print(" ");
}
Serial.println();
}
}
And the loop, sending the request each 5s:
void loop(){
static uint32_t ts = millis();
if (millis() - ts >= 5000) {
ts = millis();
GET_probe("pH");
}
ReadRx();
}
The loop runs fine once and fails the second time, as shown in terminal. Can you tell me why and help me to fix it?
Sending request pH...
Answer: 00 03 02 05 78 86 f6
Sending request pH...
RS485Serial.readBytes(RTUresponse, 7);
.readBytes
will wait for the next byte – Juraj Mar 8 '19 at 13:48