Open
Description
Hi!
I'm testing the following code as a link between https://sourceforge.net/projects/qmodmaster/ and https://pypi.org/project/pyModSlave/. As hardware, I'm using a NodeMCU 1.0 and a USB/TTL converter.
#include <ESP8266WiFi.h>
#include <ModbusTCP.h>
#include <ModbusRTU.h>
ModbusRTU rtu;
ModbusTCP tcp;
IPAddress srcIp;
uint16_t transRunning = 0;
uint8_t slaveRunning = 0;
uint16_t ModbusBaudRate = 19200;
const char* ssid = "<SSID>";
const char* password = "<PASSWORD>";
Modbus::ResultCode cbTcpRaw(uint8_t* data, uint8_t len, void* custom) {
auto src = (Modbus::frame_arg_t*) custom;
srcIp = src->ipaddr;
slaveRunning = src->slaveId;
transRunning = src->transactionId;
rtu.rawRequest(slaveRunning, data, len);
return Modbus::EX_SUCCESS;
}
Modbus::ResultCode cbRtuRaw(uint8_t* data, uint8_t len, void* custom) {
tcp.setTransactionId(transRunning);
tcp.rawResponce(srcIp, data, len, slaveRunning);
transRunning = 0;
slaveRunning = 0;
srcIp = (0,0,0,0);
return Modbus::EX_PASSTHROUGH;
}
void setup() {
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED)
delay(1000);
tcp.server();
tcp.onRaw(cbTcpRaw);
Serial.begin(ModbusBaudRate, SERIAL_8E1);
rtu.begin(&Serial);
rtu.master();
rtu.onRaw(cbRtuRaw);
}
void loop() {
rtu.task();
tcp.task();
yield();
}
When I make any request from the TCP client, I receive an RTU Timeout.
I've already tried using hard and soft Serial, baudrates of 9600 and 19200 and powering the board with a regulated supply @ 5 V-1 A.
Both programs work normally when I communicate them directly with the ESP, either reading from the RTU slave or receiving reads from the TCP client.
When I use ESP as a bridge, the RTU slave receives the request normally and responds correctly in < 5 ms, but the timeout occurs.
Please, any hint on what to test or what else to investigate? Thanks.