diff --git a/modbus/modbus_tools/modbusConnect.ini b/modbus/modbus_tools/modbusConnect.ini index 83f0dcc..0976d43 100644 --- a/modbus/modbus_tools/modbusConnect.ini +++ b/modbus/modbus_tools/modbusConnect.ini @@ -1,5 +1,5 @@ [MODBUS_SLAVE] -SLAVE_IP_ADDR=192.168.50.155 +SLAVE_IP_ADDR=192.168.1.16 SLAVE_IP_PORT=502 diff --git a/modbus/modbus_tools/ysp_modbus_master.cpp b/modbus/modbus_tools/ysp_modbus_master.cpp index 9df7994..0b38609 100644 --- a/modbus/modbus_tools/ysp_modbus_master.cpp +++ b/modbus/modbus_tools/ysp_modbus_master.cpp @@ -92,19 +92,27 @@ DWORD WINAPI xModbus_master_Proc(LPVOID lp) { } modbus_set_response_timeout(ctx, 20, 0); - sockt = modbus_connect(ctx); - if (sockt == -1) { - fprintf(stderr, "Connection failed: %s\n", modbus_strerror(errno)); - modbus_free(ctx); - return -1; - } + while (1) { + if (sockt <= 0) + { + sockt = modbus_connect(ctx); + if (sockt == -1) { + fprintf(stderr, "Connection failed: %s\n", modbus_strerror(errno)); + //modbus_free(ctx); + Sleep(1); + continue; + } + else + { + printf("Connection Success! socket=%d\n", sockt); + } + } - unsigned char* src = (unsigned char*)tab_rp_registers; - while (1) - { -#if 0 - if (sockt < 0) - return -1; + unsigned char* src = (unsigned char*)tab_rp_registers; + +#if 1 + if (sockt <= 0) + continue; FD_ZERO(&fdset_ro); FD_ZERO(&fdset_wr); FD_ZERO(&fdset_ex); @@ -124,18 +132,25 @@ DWORD WINAPI xModbus_master_Proc(LPVOID lp) { sockt = -1; break; } - if (FD_ISSET(sockt, &fdset_ro)) + if (FD_ISSET(sockt, &fdset_wr)) { +#endif + rc = modbus_read_registers(ctx, start_hold_register, number_regs, tab_rp_registers); +#if 1 + if (rc == -1) + { + closesocket(sockt); + sockt = -1; + printf("End ThreadŁĄ\n"); + continue; + } #endif - rc = modbus_read_registers(ctx, start_hold_register, number_regs, tab_rp_registers); -#if 0 - if (rc == -1) + } + else { - closesocket(sockt); - sockt = -1; - printf("End ThreadŁĄ\n"); - return -1; + Sleep(1000); + continue; } -#endif + Sleep(3 * 1000); //xUnsigned_char_hex_out("recv", src, 0, number_regs*2); xRead_reg_hex_out(start_hold_register, "master recv", src, 0, number_regs * 2);