Hello
I'm new to libmodbus and I recently use it for my project.
I have a problem with modbus client in TCP mode.
my platform is raspberry pi zero with Linux raspbian 4.9 stretch.
I try to use multiple connection from my modbus tcp client program and read from multiple servers
here is my code:
----------------------------
modbus_t * ctx0;
modbus_t * ctx1;
modbus_t * ctx2;
Read_0xxxx_bits = (uint8_t *) malloc(10 * sizeof(uint8_t));
memset(Read_0xxxx_bits, 0, 10 * sizeof(uint8_t));
Read_3xxxx_registers = (uint16_t *) malloc(10 * sizeof(uint16_t));
memset(Read_3xxxx_registers, 0, 10 * sizeof(uint16_t));
Read_4xxxx_registers = (uint16_t *) malloc(10 * sizeof(uint16_t));
memset(Read_4xxxx_registers, 0, 10 * sizeof(uint16_t));
for(i=0;i<2;i++)
{
switch(i)
{
case(0):
ctx0 = modbus_new_tcp("192.168.10.10","502");
modbus_set_debug(ctx0, TRUE);
modbus_connect(ctx0);
break;
case(1):
ctx1 = modbus_new_tcp("192.168.10.11","502");
modbus_set_debug(ctx1, TRUE);
modbus_connect(ctx1);
break;
case(2):
ctx2 = modbus_new_tcp("192.168.10.12","502");
modbus_set_debug(ctx2, TRUE);
modbus_connect(ctx2);
break;
}
}
modbus_read_bits(ctx0,0,10, Read_0xxxx_bits);
modbus_read_input_registers(ctx1, 0, 10, Read_3xxxx_registers );
modbus_read_registers(ctx2, 0, 10, Read_4xxxx_registers);
modbus_close(ctx0);
modbus_close(ctx1);
modbus_close(ctx2);
modbus_free(ctx0);
modbus_free(ctx1);
modbus_free(ctx2);
-------------------------------------------------------------
strange thing happens:
there is only one of the connection (ctx2) is successful and all three of the request go to the ctx2 server!
the reason that I want to stay connected to all my server is because my servers do not handle socket disconnection well and still stay connected for a long time and leave socket in "FIN_WAIT2" statuse
I try to use a modbys_t * array but the result was the same.
would you please help me on this?
Best regards