diff --git a/Modbus TCP/TcpCommon.cin b/Modbus TCP/TcpCommon.cin index e2d36c8..4211ff9 100644 --- a/Modbus TCP/TcpCommon.cin +++ b/Modbus TCP/TcpCommon.cin @@ -22,63 +22,80 @@ variables } -dword SetupIp(char Local_IP[]) +dword TcpSetupIp(char Local_IP[]) { - int adapterIndex; - char text[512] = ""; - char info[512] = ""; - int size = 512; - dword addresses[1]; - dword address; - - writeClear(0); - - if (IpGetAdapterCount() < 1) - { - writelineex(0, 3, "<%BASE_FILE_NAME%> Error: There is no network interface available!"); - - stop(); - } + int adapterIndex; + const int size = 512; + char text[size] = ""; + dword addresses[1]; + dword address; + word adapterCount; + word i; + adapterCount = IpGetAdapterCount(); adapterIndex = @sysvar::TCPIP::AdapterIndex; + switch (adapterCount) + { + case 0: + writelineex(0, 3, "<%BASE_FILE_NAME%> Error: There is no network interface available!"); + stop(); + return INVALID_IP; + break; + case 1: + writelineex(0, 1, "<%BASE_FILE_NAME%> Info: There is 1 network interface available!"); + if (adapterIndex != 1) + { + writelineex(0, 3, "<%BASE_FILE_NAME%> Error: You have not selected the first adapter!"); + stop(); + return INVALID_IP; + } + break; + default: + writelineex(0, 1, "<%BASE_FILE_NAME%> Info: There are %d network interfaces available!", adapterCount); + // // // // TEST \\ \\ \\ \\ + for (i = 1; i <= adapterCount; i++) + { + IpGetAdapterDescription(i, text, size); + writelineex(0, 1, "<%BASE_FILE_NAME%> Info: Interface %d: %s", i, text); + } + break; + } + if (IpGetAdapterAddress(adapterIndex, addresses, 1) != 0) { - writelineex(0, 3, "<%BASE_FILE_NAME%> Error: Could not retrieve ip address!"); - + writelineex(0, 3, "<%BASE_FILE_NAME%> Error: Could not retrieve IP address!"); stop(); + return INVALID_IP; } address = addresses[0]; // the interface used if (address == INVALID_IP) { - writelineex(0, 3, "<%BASE_FILE_NAME%> Error: ip address to be used is invalid!"); - + writelineex(0, 3, "<%BASE_FILE_NAME%> Error: IP address to be used is invalid!"); stop(); + return INVALID_IP; } IpGetAdapterDescription(adapterIndex, text, size); - snprintf(info, size, "<%BASE_FILE_NAME%> Interface: %s", text); - writelineex(0, 1, info); + writelineex(0, 1, "<%BASE_FILE_NAME%> Interface: %s", text); + writelineex(0, 1, "<%BASE_FILE_NAME%> Wrong interface? Change sysvar::TCPIP::AdapterIndex"); IpGetAdapterAddressAsString(adapterIndex, text, size); - snprintf(info, size, "<%BASE_FILE_NAME%> Ip address: %s", text); + writelineex(0, 1, "<%BASE_FILE_NAME%> Ip address: %s", text); strncpy(Local_IP, text, 16); - writelineex(0, 1, info); IpGetAdapterMaskAsString(adapterIndex, text, size); - snprintf(info, size, "<%BASE_FILE_NAME%> Subnet mask: %s", text); - writelineex(0, 1, info); + writelineex(0, 1, "<%BASE_FILE_NAME%> Subnet mask: %s", text); IpGetAdapterGatewayAsString(adapterIndex, text, size); - snprintf(info, size, "<%BASE_FILE_NAME%> Gateway address: %s", text); - writelineex(0, 1, info); + writelineex(0, 1, "<%BASE_FILE_NAME%> Gateway address: %s", text); return address; } -word OpenSocket() +word TcpOpenSocket() { char Local_IP[16]; dword localIp; @@ -86,9 +103,12 @@ word OpenSocket() dword i = 0; CHAR errorText[200]; - localIp = SetupIp(Local_IP); + localIp = TcpSetupIp(Local_IP); localPort = random(65535-10240)+10240; + if (localIp == INVALID_IP) + return INVALID_IP; + // Try to open socket do { @@ -120,7 +140,7 @@ word TcpConnectTo(char Remote_IP[], word remotePort) long fehler; // Try to open a socket - fehler = OpenSocket(); + fehler = TcpOpenSocket(); if (fehler != 0) { return fehler; @@ -236,10 +256,16 @@ void TcpSnd(byte buffer[]) { char str[20*3]; - if (g_%NODE_NAME%_TcpState != OK) + switch (g_%NODE_NAME%_TcpState) { - writelineex(0, 2, "TcpSnd: Socket status is not OK!"); - return; + case CLOSED: + TcpOpenSocket(); + break; + case OK: + break; + default: + writelineex(0, 2, "TcpSnd: Socket status is not OK!"); + return; } bin_to_strhex(buffer, str);