Hi All,
I've been working on a project where the circuitry allows for either Ethernet or Serial connectivity based on a jumper position. The code is essentially identical between either mode of communication, the only difference being some ternary operators set up to make sure I'm addressing the correct server:
I'm using QNEthernet and ArduinoModbus libraries.
Everything works great! No issues with the circuit or function of the modbus server with the jumper set in either position. The concerning bit is that my code responds to modbus calls over Serial in about 25ms (that's at 9600 Baud), and about 240ms when using Ethernet.
Here's the loop I'm using:
You can see that I've copied the example code nearly 1:1 (the only difference in structure is that I changed a 'while' to an 'if' as noted, maybe there's a better way to deal with the issue I had there.) Of course there are the extra things that need to be done to work with the Ethernet server, but the extra latency seems excessive.
Is this normal behavior, or am I doing something wrong? I believe I've posted the relevant code, but let me know if you want to see more details.
Thanks for any insight that can be given on this matter.
I've been working on a project where the circuitry allows for either Ethernet or Serial connectivity based on a jumper position. The code is essentially identical between either mode of communication, the only difference being some ternary operators set up to make sure I'm addressing the correct server:
I'm using QNEthernet and ArduinoModbus libraries.
C++:
// Here's an example of how I'm getting data from the chosen connection to set my output:
for (uint8_t i=0; i<outputChannelsNum; i++) {
boolean state = (jumperMode==2)?modbusTCPServer.coilRead(i+1):ModbusRTUServer.coilRead(i+1); // Get target state from coils 1-24
if (digitalRead(outputPins[i])!=state) digitalWrite(outputPins[i], state); // write new value if changed
}
Everything works great! No issues with the circuit or function of the modbus server with the jumper set in either position. The concerning bit is that my code responds to modbus calls over Serial in about 25ms (that's at 9600 Baud), and about 240ms when using Ethernet.
Here's the loop I'm using:
C++:
void loop() {
if (jumperMode==2) { // Ethernet loop
qn::EthernetClient client = EthServer.available();
if (client) {
modbusTCPServer.accept(client);
if (client.connected()) { // Examples shows while() here. This prevents the loop from recovering when the cable is disconnected. if() resolves the issue.
if (modbusTCPServer.poll()) { // poll() returns true when a request is present
if (modbusTCPServer.holdingRegisterRead(44)) saveDefaults();
if (modbusTCPServer.holdingRegisterRead(45)) closeAll();
if (modbusTCPServer.holdingRegisterRead(46)) FSVAll();
updateOutput();
}
}
}
}
if (jumperMode==3 && ModbusRTUServer.poll()) { // Serial loop
if (ModbusRTUServer.holdingRegisterRead(44)) saveDefaults();
if (ModbusRTUServer.holdingRegisterRead(45)) closeAll();
if (ModbusRTUServer.holdingRegisterRead(46)) FSVAll();
updateOutput();
}
}
You can see that I've copied the example code nearly 1:1 (the only difference in structure is that I changed a 'while' to an 'if' as noted, maybe there's a better way to deal with the issue I had there.) Of course there are the extra things that need to be done to work with the Ethernet server, but the extra latency seems excessive.
Is this normal behavior, or am I doing something wrong? I believe I've posted the relevant code, but let me know if you want to see more details.
Thanks for any insight that can be given on this matter.
Last edited: