diff --git a/Modbus/include/Common.cin b/Modbus/include/Common.cin index 4d991de..d28df3d 100644 --- a/Modbus/include/Common.cin +++ b/Modbus/include/Common.cin @@ -2,7 +2,7 @@ void bin_to_strhex(byte bin[], char result[]) { - char hex_str[17]= "0123456789ABCDEF"; + char hex_str[17] = "0123456789ABCDEF"; word i; word binsz; @@ -28,7 +28,7 @@ void bin_to_strhex(byte bin[], char result[]) void hbin_to_strhex(byte bin[], char result[]) { - char hex_str[17]= "0123456789ABCDEF"; + char hex_str[17] = "0123456789ABCDEF"; word i; word binsz; @@ -49,4 +49,33 @@ void hbin_to_strhex(byte bin[], char result[]) result[38] = '.'; } result[binsz * 2 - 1] = 0; +} + +void dbin_to_strhex(word bin[], char result[]) +{ + char hex_str[17] = "0123456789ABCDEF"; + word i; + word binsz; + byte offset; + + binsz = elCount(bin); + if (binsz > 20) + binsz = 20; + + for (i = 0; i < binsz; i++) + { + result[i * 5 + 0] = hex_str[(bin[i] >> 12) & 0x0F]; + result[i * 5 + 1] = hex_str[(bin[i] >> 8) & 0x0F]; + result[i * 5 + 2] = hex_str[(bin[i] >> 4) & 0x0F]; + result[i * 5 + 3] = hex_str[(bin[i] ) & 0x0F]; + result[i * 5 + 4] = ' '; + } + + if (elCount(bin) > 20) // trailing "..." + { + result[96] = '.'; + result[97] = '.'; + result[98] = '.'; + } + result[(byte)(binsz * 2.5) - 1] = 0; } \ No newline at end of file diff --git a/Modbus/include/ModbusCommonStructs.cin b/Modbus/include/ModbusCommonStructs.cin index 21020df..d13daec 100644 --- a/Modbus/include/ModbusCommonStructs.cin +++ b/Modbus/include/ModbusCommonStructs.cin @@ -2,7 +2,7 @@ variables { // A normal Modbus Application Header. Every Modbus Packet begins with these 7 (+FuncCode) Bytes - struct ModbusApHeader + _align(1) struct ModbusApHeader { word TxID; word Protocol; @@ -11,21 +11,21 @@ variables byte FuncCode; }; // Read Data from the host. We only need the start address and the number of bits/registers we want to read - struct ModbusReqRead + _align(1) struct ModbusReqRead { struct ModbusApHeader Header; word Address; word Count; }; // Write a single value to a bit/register - struct ModbusReqWriteSingle + _align(1) struct ModbusReqWriteSingle { struct ModbusApHeader Header; word Address; word Value; }; // Write several values to a bit/register starting with Address - struct ModbusReqWriteBits + _align(1) struct ModbusReqWriteBits { struct ModbusApHeader Header; word Address; @@ -34,41 +34,89 @@ variables byte Data[246]; // Max length: 1968 bits }; // Write several values to bits starting with Address - struct ModbusReqWriteRegisters + _align(1) struct ModbusReqWriteRegisters { struct ModbusApHeader Header; word Address; word Count; byte ByteCount; - int Data[123]; // Max length: 123 registers + word Data[123]; // Max length: 123 registers + }; + // Write AND and OR masks to a holding register + _align(1) struct ModbusReqWriteMasks + { + struct ModbusApHeader Header; + word Address; + word And; + word Or; + }; + // Read and write multiple registers + _align(1) struct ModbusReqReadWriteRegisters + { + struct ModbusApHeader Header; + word ReadAddress; + word ReadCount; + word WriteAddress; + word WriteCount; + byte ByteCount; + word Data[121]; // Max length: 123-2 registers }; + // Receive several bit values - struct ModbusResReceiveBits + _align(1) struct ModbusResReceiveBits { struct ModbusApHeader Header; byte ByteCount; byte Data[250]; // Max length: 2000 bits }; // Receive several register values - struct ModbusResReceiveRegisters + _align(1) struct ModbusResReceiveRegisters { struct ModbusApHeader Header; byte ByteCount; - int Data[125]; // Max length: 125 registers + word Data[125]; // Max length: 125 registers }; // Confirm the write of a single bit/register - struct ModbusResConfirmSingle + _align(1) struct ModbusResConfirmSingle { struct ModbusApHeader Header; word Address; int Value; }; // Confirm the write of several bits/registers - struct ModbusResConfirmMultiple + _align(1) struct ModbusResConfirmMultiple { struct ModbusApHeader Header; word Address; word Count; }; + // Confirm the write of AND and OR mask + _align(1) struct ModbusResConfirmMasks + { + struct ModbusApHeader Header; + word Address; + word And; + word Or; + }; + + + + enum ModbusRequestError + { + Exception, + Timeout + }; + enum ModbusException + { + None = 0x00, + IllegalFuncCode = 0x01, + IllegalDataAddress = 0x02, + IllegalDataValue = 0x03, + ServerFailure = 0x04, + Acknowledge = 0x05, + ServerBusy = 0x06, + GatewayPathsNA = 0x0A, + TargetOffline = 0x0B + }; } \ No newline at end of file diff --git a/Modbus/include/TcpUdpCommon.cin b/Modbus/include/TcpUdpCommon.cin index 173c2b7..d9cf84b 100644 --- a/Modbus/include/TcpUdpCommon.cin +++ b/Modbus/include/TcpUdpCommon.cin @@ -12,11 +12,11 @@ variables enum SocketState { NULL, OK, ERROR, CLOSED }; enum SocketState gSocketState = NULL; - dword gRemoteIP = INVALID_IP; + dword gRemoteIP = INVALID_IP; word gRemotePort = 0; } - +// Not needed dword SetupIp(char Local_IP[]) { int adapterIndex; @@ -29,7 +29,7 @@ dword SetupIp(char Local_IP[]) long error; adapterCount = IpGetAdapterCount(); - adapterIndex = @sysvar::TCPIP::AdapterIndex; + adapterIndex = @sysvar::Config::TcpIp::AdapterIndex; switch (adapterCount) { diff --git a/Modbus/include/UdpCommon.cin b/Modbus/include/UdpCommon.cin index 4a4669e..ea2d4ed 100644 --- a/Modbus/include/UdpCommon.cin +++ b/Modbus/include/UdpCommon.cin @@ -15,26 +15,23 @@ variables word UdpOpenSocket() { - char Local_IP[16]; - dword localIp; - word localPort; dword i = 0; CHAR errorText[200]; - localIp = SetupIp(Local_IP); - - if (localIp == INVALID_IP) + if (EthGetAdapterStatus() != 2) // Not connected + { + writeLineEx(0, 3, "<%NODE_NAME%> Error: Adapter status not ok: %d!", EthGetAdapterStatus()); return INVALID_IP; + } // Try to open socket do { - localPort = random(65536-10240)+10240; - gSocket = UdpSocket::Open(localIp, localPort); + gSocket = UdpSocket::Open(0, 0); // Open socket on any IP and Port if (gSocket.GetLastSocketError() != 0) { gSocket.GetLastSocketErrorAsString(errorText, elcount(errorText)); - writeLineEx(0, 1, "<%NODE_NAME%> Error: could not open Udp socket on %s:%d, %s (%d)!", Local_IP, localPort, errorText, gSocket.GetLastSocketError()); + writeLineEx(0, 1, "<%NODE_NAME%> Error: could not open Udp socket: %s (%d)!", errorText, gSocket.GetLastSocketError()); } } while (gSocket.GetLastSocketError() != 0 && i++ < 9); @@ -46,7 +43,7 @@ word UdpOpenSocket() } else { - writeLineEx(0, 1, "<%NODE_NAME%> Udp socket opened on %s:%d.", Local_IP, localPort); + writeLineEx(0, 1, "<%NODE_NAME%> Udp socket opened."); } return 0; diff --git a/Modbus/modbus.cfg b/Modbus/modbus.cfg index d6b280d..d163996 100644 --- a/Modbus/modbus.cfg +++ b/Modbus/modbus.cfg @@ -1,4 +1,4 @@ -;CANoe Version |4|7|1|58725 modbus +;CANoe Version |4|7|1|42353 modbus Version: 8.2.40 Build 40 32 PRO 10 @@ -425,25 +425,25 @@ VCaplOptionsStreamer 3 Begin_Of_Object End_Of_Object VCaplOptionsStreamer 3 VSVConfigurationStreamer 3 Begin_Of_Object 1 -1709 +1713 - - - + + + - + 2 1 - 1 "v.vsysvar" + 1 "..\Modbus\modbus.vsysvar" 1 End_Of_Object VSVConfigurationStreamer 3 @@ -722,11 +722,11 @@ Begin_Of_Multi_Line_String Copyright (c) 2001-2006 Actipro Software LLC. All rights reserved. http://www.ActiproSoftware.com/Products/DotNet/ ---> + TitleBarText="Symbol Explorer"> End_Of_Serialized_Data 3 End_Of_Object VDesktop 3 VDesktop 3 Begin_Of_Object @@ -1546,7 +1546,7 @@ End_Of_Serialized_Data 14 6 1 14 -ver=2: FT TF TF FF FT FT;F T Config;F T Ethernet;F T Ethernet1;F T GLLogger;T F _Statistics;F T sysvar +ver=2: FT TF TF FF FT FT;F T Config;F T Ethernet1;F T GLLogger;T F _Statistics End_Of_Serialized_Data 14 7 0 @@ -1572,10 +1572,7 @@ End_Of_Serialized_Data 14 16 0 17 -1 -14 -ver=2: FT -End_Of_Serialized_Data 14 +0 18 0 19 @@ -3441,7 +3438,7 @@ VTraceControlFixedModeExpansionItems 15 Begin_Of_Object 0 End_Of_Object VTraceControlFixedModeExpansionItems 15 14 -J:\HsKA\NB7\Bachelorthesis\CANoe\Modbus +J:\HsKA\NB7\Bachelorthesis\CANoe\Modbus - preStack End_Of_Serialized_Data 14 14 Trace Window @@ -4052,7 +4049,7 @@ VSysVarObject 14 Begin_Of_Object VHostSignal 15 Begin_Of_Object 2 3 -Ethernet1::Wago_3::Data::Bits +Bits 0 End_Of_Object VHostSignal 15 14 @@ -4088,7 +4085,7 @@ VSysVarObject 14 Begin_Of_Object VHostSignal 15 Begin_Of_Object 2 3 -Ethernet1::Wago_3::Data::Registers +Registers 0 End_Of_Object VHostSignal 15 14 @@ -4510,7 +4507,7 @@ VUniqueBox 4 Begin_Of_Object VBoxRoot 5 Begin_Of_Object 1 3 -0 0 0 1 -1 -1 -8 -30 0 0 890 487 +0 0 0 1 -1 -1 -1 -1 0 0 890 487 1 @@ -4518,7 +4515,7 @@ MDI_DOCK_INFO_END 5 1 6 -0 1 -1 -1 -8 -30 0 0 890 487 +0 1 -1 -1 -1 -1 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 @@ -4577,7 +4574,7 @@ End_Of_Object VGrMnBox 3 VDOLocalInfoStruct 3 Begin_Of_Object 3 1 -143 +147 VDAOBus 4 Begin_Of_Object 1 1 @@ -4599,7 +4596,7 @@ VConfigurationRoot 8 Begin_Of_Object End_Of_Object VConfigurationRoot 8 1 "ModbusClientUDP.can" 1 -J:\HsKA\NB7\Bachelorthesis\CANoe\Modbus\ModbusClientUDP.cbf +J:\HsKA\NB7\Bachelorthesis\CANoe\Modbus - preStack\ModbusClientUDP.cbf Wago_3 modbus 5 @@ -4778,7 +4775,7 @@ VBoxRoot 12 Begin_Of_Object 1 3 1 -1 2 3 -1 -1 -8 -30 171 76 688 306 -Debugger - ModbusClientUDP +Debugger - Wago_3 1 MDI_DOCK_INFO_END @@ -4969,7 +4966,8 @@ End_Of_Object VProgrammedNode 7 0 Startdelay 0 0 0 Jitter 0 0 1 0 0 0 0 -0 +1 +1 ETHERNET_IL.DLL EOF_NLDATA 3 VSimulinkModelViewerConfiguration 7 Begin_Of_Object @@ -4982,7 +4980,7 @@ VSimulinkModelViewerConfiguration 7 Begin_Of_Object End_Of_Object VSimulinkModelViewerConfiguration 7 1 0 -3696050849 +1861272737 0 NodeSignalPanelBustypeCount 0 End_Of_Object VSimulationNode 6 @@ -5015,7 +5013,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 @@ -5092,8 +5090,106 @@ EOF_MBSSDATA 1 0 0 - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + @@ -5104,104 +5200,6 @@ EOF_MBSSDATA - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - End_Of_Object VSSPlugInConfiguration 6 NULL @@ -5237,7 +5235,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 15 0 344227128 0 344808852 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 1809669784 0 0 3 SS_BEGIN_COMMON_INFO 1 0 @@ -5249,7 +5247,7 @@ Ether1 11 1 1 -344891952 1 0 1 0 1 1 0 0 0 2000 1 +338794296 1 0 1 0 1 1 0 0 0 2000 1 SS_BEGIN_COMMON_INFO 1 3 @@ -5360,7 +5358,7 @@ End_Of_Serialized_Data 2 End_Of_Object VWriteBox 2 VWinStore 2 Begin_Of_Object 1 -22 2 3 -32088 -32000 -1 -1 89 45 1107 589 +22 2 3 -32088 -32000 -1 -1 -10088 -10000 -9070 -9233 End_Of_Child_List End_Of_Object VWinStore 2 VWinStore 2 Begin_Of_Object @@ -5636,10 +5634,10 @@ FiltersBegin Begin 3 0 0 2 -Netzwerke -( 0) Busstatistik Signale ( 1 ( 0 ) 0 ) +Netzwerke +( 0) SymbSelHeaderMgrBegin 1 6 0 1 200 0 0 @@ -5651,12 +5649,12 @@ SymbSelHeaderMgrBegin SymbSelHeaderMgrEnd End Begin -3 0 15 +3 0 -1 2 modbus Systemvariablen - ( 3 ( 1 ( 1 ( 0 ) 2 ( 0 ) 3 ( 0 ) 0 ) 0 ) 0 ) + ( 0 ) SymbSelHeaderMgrBegin 1 4 0 1 200 0 0 @@ -5987,7 +5985,7 @@ VUniqueBox 4 Begin_Of_Object VBoxRoot 5 Begin_Of_Object 1 3 -1 1 0 1 -1 -1 -8 -30 199 118 815 556 +1 1 0 1 -1 -1 -1 -1 199 118 815 556 Startwerte 1 @@ -5995,7 +5993,7 @@ MDI_DOCK_INFO_END 5 1 6 -0 1 -1 -1 -8 -30 199 118 815 556 +0 1 -1 -1 -1 -1 199 118 815 556 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