Who worked with the library libmodbus please tell me . I want to send the packet to the device that receives the data using the MODBUS protocol.
I will give a piece of code with the structure of the package itself:
struct input_t { union { uint16_t raw[9]; struct { uint16_t mode; float position; float time; float ampl; float velocity; } __attribute__((packed)); }; static constexpr int addr = 1; static constexpr int count = sizeof(raw) / 2; }; In the main part of the application, it is enough for me to simply send this package via COM PORT via RTU:
modbus_t* ctx; input_t ifield; output_t ofield; std::cout << "Sizeof input : " << sizeof(input_t) << std::endl; std::cout << "Sizeof output: " << sizeof(output_t) << std::endl; //ctx = modbus_new_tcp("192.168.0.3", 502); ctx = modbus_new_rtu("COM4", 115200, 'N', 8, 1); if( ctx == NULL ) { std::cout << "Unable to create the libmodbus context"; return -1; } modbus_set_slave(ctx, 1); modbus_set_debug(ctx, FALSE); std::cout << "Connect: " << modbus_connect(ctx) << std::endl; uint16_t tab_reg[64]; if (ctx ==NULL) printf("%s\n","check null not ok" ); int a = modbus_read_registers(ctx, input_t::addr, input_t::count, ofield.raw); std::cout << a << std::endl; std::cout << *ofield.raw << std::endl; ifield.mode = 3; ifield.position = -9; ifield.time = 6; int b = modbus_write_registers(ctx, output_t::addr, output_t::count, ofield.raw); std::cout << a << std::endl; modbus_close(ctx); modbus_free(ctx); return 0; In this program, the connection to this port and to the slave is correct. Errors occur on the packet sending and register reading functions. With what can this be related?
modbus_strerrorfunction. For example:if (modbus_...(ctx) == -1) fprintf(stderr, modbus_strerror(errno));- Embedder