I’ve had some mixed results while trying to figure out my Modbus interface.
My application essentially does this:
TCPClient ysi;
byte server[] = {192,168,0,21};
if (ysi.connect(IPAddress(192,168,0,21),1502)) {
Serial.println("Connected!!!");
ysi.write(readSensor,12);
delay(99);
int i = 1;
while (ysi.available()) {
char c = ysi.read();
Serial.println(i);
i++;
Serial.println(c,HEX);
}
if (!ysi.connected()) {
Serial.println();
Serial.println("disconnecting.");
ysi.stop();
//for(;;);
}
}
else {
Serial.println("Connection failed. Damn it!");
}
I’m planning on putting most of the above (starting with the first “if”) in a loop and running it every minute to pull data and then push it to my cloud service.
The above worked many times and then seemed to stop. Powering down/up the Photon does not help. I am checking my Modbus simulator to see if it’s working OK, but thought I’d post a question to see if I’m doing anything wrong fundamentally (with regard to managing my TCP connection) with the above code.
Thanks for any feedback!