Hello,
I have to read/write data from any MODBUS TCP/IP. And I am using visual studio 2012 to make every thing.
I can write a data in address 40001-40010, but I got a return error with "rc = modbus_write_register(ctx, index, *tab_rq_registers);" function. After I trying to write one more time, I got a error like in following figure.
In case of read a data, I can not read anything even having a return error. And the error cited above, I got the same error when I try to read data by second time.
I am using the following function to read and write data.
//function to connect libmodbus
bool connect_Modbus(char *ip, int port, const char *erro){
ctx = modbus_new_tcp(ip, port);
if (ctx == NULL){
success = false;
}
if (modbus_connect(ctx) == -1) {
modbus_free(ctx);
success = false;
}else {
success = true;
}
return success;
}
//function to write data
bool writedata(int index, String^ value){
uint16_t *tab_rq_registers;
tab_rq_registers = (uint16_t *) malloc(1 * sizeof(uint16_t));
memset(tab_rq_registers, 0, 1 * sizeof(uint16_t));
tab_rq_registers[0] = Convert::ToInt16(value);
rc = modbus_write_register(ctx, index, *tab_rq_registers);
if (rc < 0) {
MessageBox::Show("ERROR modbus_write_registers");
return false;
} else{
return true;
}
}
//function to read data
uint16_t readdata(int index){
uint16_t *tab_rp_registers;
tab_rp_registers = (uint16_t *) malloc(1 * sizeof(uint16_t));
memset(tab_rp_registers, 0, 1 * sizeof(uint16_t));
rc = modbus_read_registers(ctx, index, 1, tab_rp_registers);
if (rc > 0)
return tab_rp_registers[0];
else
MessageBox::Show("ERROR modbus_read_registers");
}
Anyone can help me?
thanks
luis