Skip to content

Commit

Permalink
TCP-to-RTU example: Fix cbTcpRaw
Browse files Browse the repository at this point in the history
  • Loading branch information
emelianov committed Jul 22, 2022
1 parent 336d56c commit dc1a79a
Showing 1 changed file with 10 additions and 8 deletions.
18 changes: 10 additions & 8 deletions examples/Bridge/TCP-to-RTU-Simulator/TCP-to-RTU-Simulator.ino
Original file line number Diff line number Diff line change
Expand Up @@ -52,24 +52,24 @@ bool cbRtuTrans(Modbus::ResultCode event, uint16_t transactionId, void* data) {
Modbus::ResultCode cbTcpRaw(uint8_t* data, uint8_t len, void* custom) {
auto src = (Modbus::frame_arg_t*) custom;

Serial.print("TCP IP: ");
Serial.print("TCP IP in - ");
Serial.print(IPAddress(src->ipaddr));
Serial.printf(" Fn: %02X, len: %d \n\r", data[0], len);

if (transRunning) { // Note that we can't process new requests from TCP-side while waiting for responce from RTU-side.
tcp.setTransactionId(transRunning); // Set transaction id as per incoming request
tcp.setTransactionId(src->transactionId); // Set transaction id as per incoming request
tcp.errorResponce(IPAddress((src->ipaddr), (Modbus::FunctionCode)data[0], Modbus::EX_SLAVE_DEVICE_BUSY);
return Modbus::EX_SLAVE_DEVICE_BUSY;
}

rtu.rawRequest(src->unitId, data, len, cbRtuTrans);

if (src->unitId) {
tcp.setTransactionId(transRunning); // Set transaction id as per incoming request

//uint16_t succeed = tcp.rawResponce(src->ipaddr, data, len, slaveRunning);

if (!src->unitId) { // If broadcast request (no responce from slave is expected)
tcp.setTransactionId(src->transactionId); // Set transaction id as per incoming request
tcp.errorResponce(IPAddress(src->ipaddr), (Modbus::FunctionCode)data[0], Modbus::EX_ACKNOWLEDGE);

transRunning = 0;
slaveRunning = 0;
return Modbus::EX_ACKNOWLEDGE;
}

Expand All @@ -87,10 +87,12 @@ Modbus::ResultCode cbTcpRaw(uint8_t* data, uint8_t len, void* custom) {
// Callback receives raw data from ModbusTCP and sends it on behalf of slave (slaveRunning) to master
Modbus::ResultCode cbRtuRaw(uint8_t* data, uint8_t len, void* custom) {
auto src = (Modbus::frame_arg_t*) custom;
if (!transRunning) // Unexpected incoming data
return Modbus::EX_PASSTHROUGH;
tcp.setTransactionId(transRunning); // Set transaction id as per incoming request
uint16_t succeed = tcp.rawResponce(srcIp, data, len, slaveRunning);
if (!succeed){
Serial.print("fail");
Serial.print("TCP IP out - failed");
}
Serial.printf("RTU Slave: %d, Fn: %02X, len: %d, ", src->slaveId, data[0], len);
Serial.print("Response TCP IP: ");
Expand Down

0 comments on commit dc1a79a

Please sign in to comment.