Reputation: 1
After few successful request and responses, client sends a junk data request on line.
I am trying Modbus RTU communication between a x86 cpu having serial DB9 port and raspberry pi connected with TTL-232RG TTL to USB converter using RS485 communication protocol. My client code on x86 machine is written in c++ using libmodbus library and server code on raspberry pi is written in python which just responds back with same request frame by adding dummy data and actual computed CRC.
Issue is initially I am able to send correct requests and get response correctly, but after few such transactions there is junk request sent from client side. The junk request is more than 50 bytes longer.
I tried running the client code with the standard docklight software running on my my laptop, there also same issue is seen.
Need to know why this issue could be happening?
I am using following client code:
#include <modbus.h>
#include <iostream>
#include <unistd.h>
#include <cstring>
int main() {
while(true) {
modbus_t* ctx = modbus_new_rtu("/dev/ttyUSB0", 19200, 'N', 8, 1);
if (ctx == nullptr) {
std::cerr << "Unable to create modbus context" << std::endl;
return -1;
}
modbus_set_slave(ctx, 201);
modbus_rtu_set_serial_mode(ctx, MODBUS_RTU_RS485);
modbus_rtu_set_rts(ctx, 1);
if (modbus_connect(ctx) == -1) {
std::cerr << "Connection failed: " << modbus_strerror(errno) << std::endl;
modbus_free(ctx);
return -1;
}
uint16_t tab_reg[32];
bool flag = true;
while(flag == true) {
memset(tab_reg, 0, (32*2));
int rc = modbus_read_input_registers(ctx, 1, 1, tab_reg);
if (rc == -1) {
std::cerr << "Failed to read registers: " << modbus_strerror(errno) << std::endl;
modbus_close(ctx);
modbus_free(ctx);
flag = false;
}
for (int i = 0; i < rc; i++) {
std::cout << "Register " << i << ": " << tab_reg[i] << std::endl;
}
modbus_flush(ctx);
}
}
return 0;
}
Upvotes: -1
Views: 80