diff --git a/Modbus/MakeConfig.cfg b/Modbus/MakeConfig.cfg index 7d245f4..022c23e 100644 --- a/Modbus/MakeConfig.cfg +++ b/Modbus/MakeConfig.cfg @@ -1,4 +1,4 @@ -;CANoe Version |4|7|1|52129 MakeConfig +;CANoe Version |4|7|1|52133 MakeConfig Version: 8.2.40 Build 40 32 PRO 5 @@ -2933,7 +2933,7 @@ End_Of_Object VGrMnBox 3 VDOLocalInfoStruct 3 Begin_Of_Object 3 1 -71 +74 VDAOBus 4 Begin_Of_Object 1 1 @@ -3063,7 +3063,7 @@ VSimulinkModelViewerConfiguration 7 Begin_Of_Object End_Of_Object VSimulinkModelViewerConfiguration 7 1 0 -3836594437 +3737284963 0 NodeSignalPanelBustypeCount 0 End_Of_Object VSimulationNode 6 @@ -3101,7 +3101,7 @@ NULL End_Of_Object VDOLocalInfoStruct 3 0.000000 0 0 -1 1 0 59420 1 233 1 2882400001 98 331 309 611 2882400002 0 0 0 0 0 0 1 2882400001 1270 1270 311 311 2882400002 0 0 0 576526008 0 344968884 3 +1 1 0 59420 1 233 1 2882400001 98 331 309 611 2882400002 0 0 0 0 0 0 1 2882400001 1270 1270 311 311 2882400002 0 0 0 526434840 0 526135044 3 SS_BEGIN_COMMON_INFO 1 0 @@ -3113,7 +3113,7 @@ Ethernet 11 1 1 -575153512 1 0 1 0 0 1 0 0 0 2000 1 +345908232 1 0 1 0 0 1 0 0 0 2000 1 SS_BEGIN_COMMON_INFO 1 3 @@ -3224,7 +3224,7 @@ End_Of_Serialized_Data 2 End_Of_Object VWriteBox 2 VWinStore 2 Begin_Of_Object 1 -22 2 3 -32088 -32000 -1 -1 -10088 -10000 -9070 -9233 +22 2 3 -1 -1 -1 -1 -10088 -10000 -9070 -9233 End_Of_Child_List End_Of_Object VWinStore 2 VWinStore 2 Begin_Of_Object diff --git a/Modbus/ModbusNet.cfg b/Modbus/ModbusNet.cfg index c63308c..fcd318c 100644 --- a/Modbus/ModbusNet.cfg +++ b/Modbus/ModbusNet.cfg @@ -1,4 +1,4 @@ -;CANoe Version |4|7|1|38832 ModbusNet +;CANoe Version |4|7|1|55200 ModbusNet Version: 8.2.40 Build 40 32 PRO 10 @@ -4530,7 +4530,7 @@ VUniqueBox 4 Begin_Of_Object VBoxRoot 5 Begin_Of_Object 1 3 -0 0 0 1 -1 -1 -1 -1 0 0 890 487 +0 0 0 1 -1 -1 -8 -30 0 0 890 487 1 @@ -4538,7 +4538,7 @@ MDI_DOCK_INFO_END 5 1 6 -0 1 -1 -1 -1 -1 0 0 890 487 +0 1 -1 -1 -8 -30 0 0 890 487 6 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 -1 -1 32767 0 0 0 0 0 0 0 0 0 0 -1 -1 0 0 0 0 0 0 END_OF_DOCK_INFO 1 @@ -4597,7 +4597,7 @@ End_Of_Object VGrMnBox 3 VDOLocalInfoStruct 3 Begin_Of_Object 3 1 -183 +187 VDAOBus 4 Begin_Of_Object 1 1 @@ -4669,7 +4669,7 @@ VIPBAdapterSetting 9 Begin_Of_Object 02:4C:5C:2B:26:00 End_Of_Serialized_Data 9 9 -192.168.1.3 +192.168.1.2 End_Of_Serialized_Data 9 9 255.255.255.0 @@ -4687,7 +4687,7 @@ VPersistentAddress 12 Begin_Of_Object End_Of_Object VPersistentAddress 12 End_Of_Object VPersistentIFAddress 11 10 -192.168.1.3 +192.168.1.2 End_Of_Serialized_Data 10 10 255.255.255.0 @@ -4787,11 +4787,6 @@ NDebugger::VFile 9 Begin_Of_Object End_Of_Object NDebugger::VFile 9 NDebugger::VFile 9 Begin_Of_Object 1 - 1 "include\CAPL\ModbusClient.can" -27 -End_Of_Object NDebugger::VFile 9 -NDebugger::VFile 9 Begin_Of_Object -1 1 "include\CAPL\include\Common.cin" 28 End_Of_Object NDebugger::VFile 9 @@ -4825,6 +4820,11 @@ NDebugger::VFile 9 Begin_Of_Object 1 "include\CAPL\include\UdpCommon.cin" 34 End_Of_Object NDebugger::VFile 9 +NDebugger::VFile 9 Begin_Of_Object +1 + 1 "include\CAPL\ModbusClient.can" +35 +End_Of_Object NDebugger::VFile 9 VNETStandaloneComponent 9 Begin_Of_Object 1 VNETControlBox 10 Begin_Of_Object @@ -4901,7 +4901,7 @@ End_Of_Object VUniqueBox 11 1 -1 0 0 0 0 0 0 0 0 0 0 0 End_Of_Object VNETControlBox 10 -114 +156 APPDIR Vector.CANoe.Debugger.DLL Vector.CANoe.Debugger, Version=8.2.40.0, Culture=neutral, PublicKeyToken=null Vector.CANoe.Debugger.DebuggerComponent @@ -4943,16 +4943,58 @@ TypeRef:3 3 Int32 NrOfFiles -1 +8 Int32 FileID0 -27 +35 Int32 CurrentLine0 0 Int32 +FileID1 +28 +Int32 +CurrentLine1 +0 +Int32 +FileID2 +29 +Int32 +CurrentLine2 +0 +Int32 +FileID3 +30 +Int32 +CurrentLine3 +0 +Int32 +FileID4 +31 +Int32 +CurrentLine4 +0 +Int32 +FileID5 +32 +Int32 +CurrentLine5 +0 +Int32 +FileID6 +33 +Int32 +CurrentLine6 +0 +Int32 +FileID7 +34 +Int32 +CurrentLine7 +0 +Int32 SelectedFileID -27 +35 Int32 NrOfWatchedVariables 0 @@ -5040,7 +5082,7 @@ VSimulinkModelViewerConfiguration 7 Begin_Of_Object End_Of_Object VSimulinkModelViewerConfiguration 7 1 0 -741650535 +1202903787 0 NodeSignalPanelBustypeCount 0 End_Of_Object VSimulationNode 6 @@ -5073,7 +5115,7 @@ VBoxRoot 9 Begin_Of_Object 1 3 1 1 2 3 -1 -1 -8 -30 114 0 1146 491 - +Ethernet Packet Builder 1 MDI_DOCK_INFO_END @@ -5150,106 +5192,8 @@ EOF_MBSSDATA 1 0 0 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + + @@ -5260,6 +5204,104 @@ EOF_MBSSDATA + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + End_Of_Object VSSPlugInConfiguration 6 NULL @@ -5295,7 +5337,7 @@ NULL End_Of_Object VDOLocalInfoStruct 3 0.000000 0 0 -1 1 0 59420 1 233 1 2882400001 323 556 331 782 2882400002 0 0 0 0 0 0 1 2882400001 1197 1197 333 333 2882400002 0 0 0 1630953112 0 0 3 +1 1 0 59420 1 233 1 2882400001 323 556 331 782 2882400002 0 0 0 0 0 0 1 2882400001 1197 1197 333 333 2882400002 0 0 0 346407864 0 345757012 3 SS_BEGIN_COMMON_INFO 1 0 @@ -5307,7 +5349,7 @@ Ether1 11 1 1 -574156496 1 0 1 0 1 1 0 0 0 2000 1 +525967224 1 0 1 0 1 1 0 0 0 2000 1 SS_BEGIN_COMMON_INFO 1 3 diff --git a/Modbus/include/CAPL/MakeConfig.can b/Modbus/include/CAPL/MakeConfig.can index 45816fe..faa819a 100644 --- a/Modbus/include/CAPL/MakeConfig.can +++ b/Modbus/include/CAPL/MakeConfig.can @@ -47,6 +47,7 @@ on preStart */ // Scan a range of IPs for devices. Start and Stop go here + // Please note: Currelty .255 will be skipped! strncpy(gScanFirstIp, "192.168.1.1", 16); strncpy(gScanLastIp, "192.168.1.10", 16); @@ -107,16 +108,20 @@ void DetectDevicesNext() { gScanFirst &= 0x000000FF; gScanFirst += 0x00000001; + write("%d.%d.%d.%d ", gScanFirst & 0xFF, (gScanFirst >> 8) & 0xFF, (gScanFirst >> 16) & 0xFF, gScanFirst >> 24); } else if ((gScanFirst & 0xFFFF0000) == 0xFEFF0000) { gScanFirst &= 0x0000FFF; gScanFirst += 0x00000100; + write("%d.%d.%d.%d ", gScanFirst & 0xFF, (gScanFirst >> 8) & 0xFF, (gScanFirst >> 16) & 0xFF, gScanFirst >> 24); } else if ((gScanFirst & 0xFF000000) == 0xFE000000) { gScanFirst &= 0x00FFFFFF; gScanFirst += 0x00010000; + + write("%d.%d.%d.%d ", gScanFirst & 0xFF, (gScanFirst >> 8) & 0xFF, (gScanFirst >> 16) & 0xFF, gScanFirst >> 24); } else { @@ -128,6 +133,7 @@ void DetectDevicesNext() MakeIpNets(); return; } + writeEx(0, 1, "."); gRemoteIP = gScanFirst; // Don't open new socket, it takes too much time. ModbusReadBits(0, 1); } diff --git a/Modbus/include/CAPL/ModbusClient.can b/Modbus/include/CAPL/ModbusClient.can index 13fdb58..4393a19 100644 --- a/Modbus/include/CAPL/ModbusClient.can +++ b/Modbus/include/CAPL/ModbusClient.can @@ -2,7 +2,7 @@ includes { - #include "include\ModbusUdpClientCommon.cin" + #include "include\ModbusEilClientCommon.cin" #include "include\ModbusFunctions.cin" } diff --git a/Modbus/include/CAPL/include/EilCommon.cin b/Modbus/include/CAPL/include/EilCommon.cin new file mode 100644 index 0000000..847f736 --- /dev/null +++ b/Modbus/include/CAPL/include/EilCommon.cin @@ -0,0 +1,155 @@ +/*@!Encoding:1252*/ +includes +{ + #include "Common.cin" + #include "TcpUdpCommon.cin" +} + +variables +{ + long gPacket; + + byte gLocalMac[6]; + byte gRemoteMac[6]; + dword gLocalIP = 0xC0A80101; +} + + +word EilConnectTo(char Remote_IP[], word remotePort) +{ + dword remoteIp; + + // Convert IP string to Number + remoteIp = IpGetAddressAsNumber(Remote_IP); + if (remoteIp == INVALID_IP) + { + writeLineEx(0, 1, "<%NODE_NAME%> Error: invalid server Ip address!"); + + return ipGetLastError(); + } + + return EilConnectTo(remoteIp, remotePort); +} + +word EilConnectTo(dword remoteIp, word remotePort) +{ + long error; + byte broadcastMac[6] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF}; + + if (EthGetMacId(gLocalMac) != 0) + { + gSocketState = ERROR; + error = EthGetLastError(); + writeLineEx(0, 3, "<%NODE_NAME%> EthGetMacId Error: Could not get local MAC! %d", error); + return error; + } + + // TCP/IP API gives IP in little endian but EIL uses big endian + gRemotePort = remotePort; + gRemoteIP = (remoteIp >> 24) | (remoteIp >> 8) & 0x0000FF00 | (remoteIp << 8) & 0x00FF0000 | (remoteIp << 24); + + if (gPacket != 0) + ModbusDisconnect(); + // Try to create an ARP packet that gets the MAC from remote server + gPacket = EthInitPacket("arp"); + if (gPacket == 0) + { + gSocketState = ERROR; + error = EthGetLastError(); + writeLineEx(0, 3, "<%NODE_NAME%> EthInitPacket Error: Could not create ARP package! %d", error); + return error; + } + + EthSetTokenData(gPacket, "eth", "source" , elCount(gLocalMac), gLocalMac); + EthSetTokenData(gPacket, "eth", "destination" , elCount(broadcastMac), broadcastMac); + EthSetTokenInt(gPacket, "arp", "hwType" , 1); // Ethernet + EthSetTokenInt(gPacket, "arp", "protType" , 0x0800); // IPv4 + EthSetTokenInt(gPacket, "arp", "hwSize" , 6); // Ethernet addr size + EthSetTokenInt(gPacket, "arp", "protSize" , 4); // IP addr size + EthSetTokenInt(gPacket, "arp", "operation" , 1); + EthSetTokenData(gPacket, "arp", "hwSourceAddr" , elCount(gLocalMac), gLocalMac); + EthSetTokenInt(gPacket, "arp", "protSourceAddr" , gLocalIP); + //EthSetTokenData(gPacket, "arp", "hwDestinationAddr" , elCount(gLocalMac), gLocalMac); + EthSetTokenInt(gPacket, "arp", "protDestinationAddr" , gRemoteIP); + + EthReceivePacket("OnEthReceivePacket"); + + EthCompletePacket(gPacket); + EthOutputPacket(gPacket); + EthReleasePacket(gPacket); + gSocketState = NULL; + return 0; +} + +void EilConnectTo2() +{ + gPacket = EthInitPacket("udp"); + if (gPacket == 0) + { + gSocketState = ERROR; + writeLineEx(0, 3, "<%NODE_NAME%> EthInitPacket Error: Could not create udp packet: %d", EthGetLastError()); + return; + } + + EthSetTokenData(gPacket, "eth", "source" , elCount(gLocalMac), gLocalMac); + EthSetTokenData(gPacket, "eth", "destination" , elCount(gRemoteMac), gRemoteMac); + EthSetTokenInt(gPacket, "ipv4", "source" , gLocalIP); + EthSetTokenInt(gPacket, "ipv4", "destination" , gRemoteIP); + EthSetTokenInt(gPacket, "udp", "source" , 23456); + EthSetTokenInt(gPacket, "udp", "destination" , 502); + + gSocketState = OK; +} + +void EilDisconnect() +{ + if (gPacket != 0) + { + EthReleasePacket(gPacket); + gPacket = 0; + } + gSocketState = CLOSED; +} + + +void EilRecv() +{ +} + +byte EilSnd(byte buffer[], word length) +{ + //char str[20*3]; + + switch (gSocketState) + { + case CLOSED: + EilConnectTo(gRemoteIP, gRemotePort); + if (gSocketState != OK) + { + writeLineEx(0, 2, "EilSnd: Reconnecting failed!"); + return 1; + } + case OK: + break; + default: + writeLineEx(0, 2, "EilSnd: Socket status is not OK!"); + return 1; + } + + //bin_to_strhex(buffer, str); + //writeLineEx(0, 1, "<%NODE_NAME%> EilSnd: %s (Länge: %d)", str, length); + + EthResizeToken(gPacket, "udp", "data" , length*8); + EthSetTokenData(gPacket, "udp", "data" , length, buffer); + + EthCompletePacket(gPacket); + EthOutputPacket(gPacket); + + return 0; +} + +long EilGetLastConnectionError(char string[]) +{ + EthGetLastErrorText(elCount(string), string); + return EthGetLastError(); +} \ No newline at end of file diff --git a/Modbus/include/CAPL/include/ModbusClientCommon.cin b/Modbus/include/CAPL/include/ModbusClientCommon.cin index 3fe605d..b674a06 100644 --- a/Modbus/include/CAPL/include/ModbusClientCommon.cin +++ b/Modbus/include/CAPL/include/ModbusClientCommon.cin @@ -461,9 +461,8 @@ void OnModbusReceive(dword socket, long result, dword address, dword port, byte if (size == 0) { // Size of zero indicates that the socket was closed by the communication peer. - writeLineEx(0, 2, "<%NODE_NAME%> OnTcpReceive: Socket closed by peer"); - gSocket.Close(); - gSocketState = CLOSED; + writeLineEx(0, 2, "<%NODE_NAME%> OnModbusReceive: Socket closed by peer"); + ModbusDisconnect(); } else { @@ -473,11 +472,9 @@ void OnModbusReceive(dword socket, long result, dword address, dword port, byte } else { - gIpLastErr = gSocket.GetLastSocketError(); - gSocket.GetLastSocketErrorAsString(gIpLastErrStr, elcount(gIpLastErrStr)); - writeLineEx(0, 2, "<%NODE_NAME%> OnTcpReceive error (%d): %s", gIpLastErr, gIpLastErrStr); - gSocket.Close(); - gSocketState = CLOSED; + gIpLastErr = ModbusGetLastConnectionError(gIpLastErrStr); + writeLineEx(0, 2, "<%NODE_NAME%> OnModbusReceive error (%d): %s", gIpLastErr, gIpLastErrStr); + ModbusDisconnect(); } } @@ -743,11 +740,12 @@ on timer gtRobin continue; - ModbusSnd(gQueuePending[TxID].Buffer, gQueuePending[TxID].Length); // send packet - - memcpy(gQueueSent[TxID], gQueuePending[TxID]); // move packet to sent queue - gQueuePending.Remove(TxID); - ModbusRecv(); + if (ModbusSnd(gQueuePending[TxID].Buffer, gQueuePending[TxID].Length) == 0) // if packet was sent + { + memcpy(gQueueSent[TxID], gQueuePending[TxID]); // move packet to sent queue + gQueuePending.Remove(TxID); + ModbusRecv(); + } } if (gQueueSent.Size() == 0) // Stop timer to reduce latency of first packet diff --git a/Modbus/include/CAPL/include/ModbusEilClientCommon.cin b/Modbus/include/CAPL/include/ModbusEilClientCommon.cin new file mode 100644 index 0000000..232b736 --- /dev/null +++ b/Modbus/include/CAPL/include/ModbusEilClientCommon.cin @@ -0,0 +1,59 @@ +/*@!Encoding:1252*/ + +includes +{ + #include "EilCommon.cin" + #include "ModbusClientCommon.cin" +} + +void ModbusConnectTo(char Remote_IP[], word Remote_Port) +{ + EilConnectTo(Remote_IP, Remote_Port); +} + +void ModbusConnectTo(dword Remote_IP, word Remote_Port) +{ + EilConnectTo(Remote_IP, Remote_Port); +} + +byte ModbusSnd(byte buffer[], word length) +{ + return EilSnd(buffer, length); +} + +void ModbusRecv() +{ + EilRecv(); +} + +void ModbusDisconnect() +{ + EilDisconnect(); +} + +long ModbusGetLastConnectionError(char string[]) +{ + return EilGetLastConnectionError(string); +} + +void OnEthReceivePacket(long channel, long dir, long packet) +{ + byte buffer[gMaxPacketLength]; + long size; + + if (dir == TX) + return; + + if (EthGetTokenInt(packet, "arp", "protSourceAddr") == gRemoteIP) // this was our ARP package + { + if (EthGetTokenData(packet, "arp", "hwSourceAddr", elCount(gRemoteMac), gRemoteMac) == 6) + { + write("remote Mac: %02X:%02X:%02X:%02X:%02X:%02X", gRemoteMac[0], gRemoteMac[1], gRemoteMac[2], gRemoteMac[3], gRemoteMac[4], gRemoteMac[5]); + EilConnectTo2(); // create the UDP package + } + return; + } + + size = EthGetThisData(0, gMaxPacketLength, buffer); + OnModbusReceive(0, 0, EthGetTokenInt(packet, "ipv4", "source"), EthGetTokenInt(packet, "udp", "source"), buffer, size); +} \ No newline at end of file diff --git a/Modbus/include/CAPL/include/ModbusTcpClientCommon.cin b/Modbus/include/CAPL/include/ModbusTcpClientCommon.cin index e0bb62d..cb7ceae 100644 --- a/Modbus/include/CAPL/include/ModbusTcpClientCommon.cin +++ b/Modbus/include/CAPL/include/ModbusTcpClientCommon.cin @@ -16,9 +16,14 @@ void ModbusConnectTo(dword Remote_IP, word Remote_Port) TcpConnectTo(Remote_IP, Remote_Port); } -void ModbusSnd(byte buffer[], word length) +void ModbusDisconnect() { - TcpSnd(buffer, length); + TcpDisconnect(); +} + +byte ModbusSnd(byte buffer[], word length) +{ + return TcpSnd(buffer, length); } void ModbusRecv() @@ -26,6 +31,11 @@ void ModbusRecv() TcpRecv(); } +long ModbusGetLastConnectionError(char string[]) +{ + return TcpGetLastConnectionError(string); +} + void OnTcpReceive(dword socket, long result, dword address, dword port, byte buffer[], dword size) { OnModbusReceive(socket, result, address, port, buffer, size); diff --git a/Modbus/include/CAPL/include/ModbusUdpClientCommon.cin b/Modbus/include/CAPL/include/ModbusUdpClientCommon.cin index ffb8e5c..3c0fd9a 100644 --- a/Modbus/include/CAPL/include/ModbusUdpClientCommon.cin +++ b/Modbus/include/CAPL/include/ModbusUdpClientCommon.cin @@ -16,9 +16,14 @@ void ModbusConnectTo(dword Remote_IP, word Remote_Port) UdpConnectTo(Remote_IP, Remote_Port); } -void ModbusSnd(byte buffer[], word length) +void ModbusDisconnect() { - UdpSnd(buffer, length); + UdpDisconnect(); +} + +byte ModbusSnd(byte buffer[], word length) +{ + return UdpSnd(buffer, length); } void ModbusRecv() @@ -26,6 +31,11 @@ void ModbusRecv() UdpRecv(); } +long ModbusGetLastConnectionError(char string[]) +{ + return UdpGetLastConnectionError(string); +} + void OnUdpReceiveFrom(dword socket, long result, dword address, dword port, byte buffer[], dword size) { OnModbusReceive(socket, result, address, port, buffer, size); diff --git a/Modbus/include/CAPL/include/TcpCommon.cin b/Modbus/include/CAPL/include/TcpCommon.cin index 0a81980..ae91df8 100644 --- a/Modbus/include/CAPL/include/TcpCommon.cin +++ b/Modbus/include/CAPL/include/TcpCommon.cin @@ -113,6 +113,12 @@ void OnTcpConnect(dword socket, long result) } } +void TcpDisconnect() +{ + gSocket.Close(); + gSocketState = CLOSED; +} + void TcpRecv() { int result; @@ -141,7 +147,7 @@ void TcpRecv() return; } -void TcpSnd(byte buffer[], word length) +word TcpSnd(byte buffer[], word length) { //char str[20*3]; @@ -152,13 +158,13 @@ void TcpSnd(byte buffer[], word length) if (gSocketState != OK) { writeLineEx(0, 2, "<%NODE_NAME%> TcpSnd: Reconnecting failed!"); - return; + return 1; } case OK: break; default: writeLineEx(0, 2, "<%NODE_NAME%> TcpSnd: Socket status is not OK!"); - return; + return 1; } //bin_to_strhex(buffer, str); @@ -172,8 +178,15 @@ void TcpSnd(byte buffer[], word length) { gSocket.GetLastSocketErrorAsString(gIpLastErrStr, elcount(gIpLastErrStr)); writeLineEx(0, 2, "<%NODE_NAME%> TcpSnd error (%d): %s", gIpLastErr, gIpLastErrStr); - gSocket.Close(); - gSocketState = CLOSED; + TcpDisconnect(); + return 1; } } + return 0; +} + +long TcpGetLastConnectionError(char string[]) +{ + gSocket.GetLastSocketErrorAsString(string, elCount(string)); + return gSocket.GetLastSocketError(); } \ No newline at end of file diff --git a/Modbus/include/CAPL/include/UdpCommon.cin b/Modbus/include/CAPL/include/UdpCommon.cin index cd6e661..8b8f1e6 100644 --- a/Modbus/include/CAPL/include/UdpCommon.cin +++ b/Modbus/include/CAPL/include/UdpCommon.cin @@ -82,13 +82,19 @@ word UdpConnectTo(dword remoteIp, word remotePort) return 0; } +void UdpDisconnect() +{ + gSocket.Close(); + gSocketState = CLOSED; +} + void UdpRecv() { int result; if (gSocketState != OK) { - writeLineEx(0, 2, "UdpRecv: Socket status is not OK!"); + writeLineEx(0, 2, "<%NODE_NAME%> UdpRecv: Socket status is not OK!"); return; } @@ -120,14 +126,14 @@ void UdpSnd(byte buffer[], word length) UdpConnectTo(gRemoteIP, gRemotePort); if (gSocketState != OK) { - writeLineEx(0, 2, "UdpSnd: Reconnecting failed!"); - return; + writeLineEx(0, 2, "<%NODE_NAME%> UdpSnd: Reconnecting failed!"); + return 1; } case OK: break; default: - writeLineEx(0, 2, "UdpSnd: Socket status is not OK!"); - return; + writeLineEx(0, 2, "<%NODE_NAME%> UdpSnd: Socket status is not OK!"); + return 1; } //bin_to_strhex(buffer, str); @@ -141,8 +147,15 @@ void UdpSnd(byte buffer[], word length) { gSocket.GetLastSocketErrorAsString(gIpLastErrStr, elcount(gIpLastErrStr)); writeLineEx(0, 2, "<%NODE_NAME%> UdpSnd error (%d): %s", gIpLastErr, gIpLastErrStr); - gSocket.Close(); - gSocketState = CLOSED; + UdpDisconnect(); + return 1; } } + return 0; +} + +long UdpGetLastConnectionError(char string[]) +{ + gSocket.GetLastSocketErrorAsString(string, elCount(string)); + return gSocket.GetLastSocketError(); } \ No newline at end of file