TCP Stack implemented
This commit is contained in:
		
							parent
							
								
									83103d679e
								
							
						
					
					
						commit
						4fceb602a5
					
				
					 7 changed files with 13909 additions and 0 deletions
				
			
		
							
								
								
									
										458
									
								
								Modbus TCP/IPClient.can
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										458
									
								
								Modbus TCP/IPClient.can
									
									
									
									
									
										Normal file
									
								
							| 
						 | 
					@ -0,0 +1,458 @@
 | 
				
			||||||
 | 
					/*@@includes:*/
 | 
				
			||||||
 | 
					includes
 | 
				
			||||||
 | 
					{
 | 
				
			||||||
 | 
					  #include "IPCommon.can"
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					/*@@end*/
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					/*@@var:*/
 | 
				
			||||||
 | 
					variables
 | 
				
			||||||
 | 
					{
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					/*@@end*/
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					/*@@caplFunc:SetupIp():*///function
 | 
				
			||||||
 | 
					void SetupIp()
 | 
				
			||||||
 | 
					{
 | 
				
			||||||
 | 
					  int   adapterIndex = 1;
 | 
				
			||||||
 | 
					  char  text[512] = "";
 | 
				
			||||||
 | 
					  char  info[512] = "";
 | 
				
			||||||
 | 
					  int   size = 512;
 | 
				
			||||||
 | 
					  long  result = 0;
 | 
				
			||||||
 | 
					  dword addresses[1];
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  writeClear(0);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  if (1 > IpGetAdapterCount())
 | 
				
			||||||
 | 
					  {
 | 
				
			||||||
 | 
					    writelineex(0, 3, "Error: There is no network interface available!");
 | 
				
			||||||
 | 
					    
 | 
				
			||||||
 | 
					    stop();
 | 
				
			||||||
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  if (0 != IpGetAdapterAddress(adapterIndex, addresses, 1))
 | 
				
			||||||
 | 
					  {
 | 
				
			||||||
 | 
					    writelineex(0, 3, "Error: Could not retrieve ip address!");
 | 
				
			||||||
 | 
					    
 | 
				
			||||||
 | 
					    stop();
 | 
				
			||||||
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  gIpAddress = addresses[0]; // the interface used
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  if (INVALID_IP == gIpAddress)
 | 
				
			||||||
 | 
					  {
 | 
				
			||||||
 | 
					    writelineex(0, 3, "Error: ip address to be used is invalid!");
 | 
				
			||||||
 | 
					    
 | 
				
			||||||
 | 
					    stop();
 | 
				
			||||||
 | 
					  }
 | 
				
			||||||
 | 
					 
 | 
				
			||||||
 | 
					  IpGetAdapterDescription(adapterIndex, text, size);
 | 
				
			||||||
 | 
					  snprintf(info, size, "Interface: %s", text);
 | 
				
			||||||
 | 
					  writelineex(0, 1, info);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  IpGetAdapterAddressAsString(adapterIndex, text, size);
 | 
				
			||||||
 | 
					  snprintf(info, size, "Ip address: %s", text);
 | 
				
			||||||
 | 
					  writelineex(0, 1, info);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  SysSetVariableString(sysvar::TCPIP::TcpClientIp, text);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  SysSetVariableString(sysvar::UDP::UdpClientIp, text);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  IpGetAdapterMaskAsString(adapterIndex, text, size);
 | 
				
			||||||
 | 
					  snprintf(info, size, "Subnet mask: %s", text);
 | 
				
			||||||
 | 
					  writelineex(0, 1, info);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  IpGetAdapterGatewayAsString(adapterIndex, text, size);
 | 
				
			||||||
 | 
					  snprintf(info, size, "Gateway address: %s", text);
 | 
				
			||||||
 | 
					  writelineex(0, 1, info);
 | 
				
			||||||
 | 
					 
 | 
				
			||||||
 | 
					  gStatus = gkSTATUS_INITIALISED;
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					/*@@end*/
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					/*@@startStart:Start:*/
 | 
				
			||||||
 | 
					on start
 | 
				
			||||||
 | 
					{
 | 
				
			||||||
 | 
					  SetupIp();  
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					/*@@end*/
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					/*@@stop:StopMeasurement:*/
 | 
				
			||||||
 | 
					on stopMeasurement
 | 
				
			||||||
 | 
					{
 | 
				
			||||||
 | 
					  ResetIp();
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					/*@@end*/
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					/*@@caplFunc:OnTcpReceive(dword,long,dword,dword,char[],dword):*///function
 | 
				
			||||||
 | 
					void OnTcpReceive( dword socket, long result, dword address, dword port, char buffer[], dword size)
 | 
				
			||||||
 | 
					{
 | 
				
			||||||
 | 
					  char  addressString[64] = "";
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  if ( gTcpDataSocket != socket)
 | 
				
			||||||
 | 
					  {
 | 
				
			||||||
 | 
					    writelineex(0, 2, "OnTcpReceive called for unknown socket 0x%X", socket);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    return;
 | 
				
			||||||
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  if (0 != result)
 | 
				
			||||||
 | 
					  {
 | 
				
			||||||
 | 
					    IpGetLastSocketErrorAsString( socket, gIpLastErrStr, elcount( gIpLastErrStr));
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    writelineex( 0, 2, "OnTcpReceive error (%d): %s", IpGetLastSocketError( socket), gIpLastErrStr);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    return;
 | 
				
			||||||
 | 
					  }
 | 
				
			||||||
 | 
					 
 | 
				
			||||||
 | 
					  IpGetAddressAsString(address, addressString, elcount(addressString));
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  SysSetVariableString(sysvar::TCPIP::TcpData, buffer);
 | 
				
			||||||
 | 
					 
 | 
				
			||||||
 | 
					  TcpRecv( socket);
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					/*@@end*/
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					/*@@caplFunc:OnTcpSend(dword,long,char[],dword):*///function
 | 
				
			||||||
 | 
					void OnTcpSend( dword socket, long result, char buffer[], dword size)
 | 
				
			||||||
 | 
					{
 | 
				
			||||||
 | 
					  if ( gTcpDataSocket != socket)
 | 
				
			||||||
 | 
					  {
 | 
				
			||||||
 | 
					    writelineex(0, 2, "OnTcpSend called for unknown socket 0x%X", socket);
 | 
				
			||||||
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  if (0 != result)
 | 
				
			||||||
 | 
					  {
 | 
				
			||||||
 | 
					    IpGetLastSocketErrorAsString( socket, gIpLastErrStr, elcount( gIpLastErrStr));
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    writelineex( 0, 2, "OnTcpSend error (%d): %s", IpGetLastSocketError( socket), gIpLastErrStr);
 | 
				
			||||||
 | 
					  }
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					/*@@end*/
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					/*@@caplFunc:ConnectTcp():*///function
 | 
				
			||||||
 | 
					void ConnectTcp()
 | 
				
			||||||
 | 
					{ 
 | 
				
			||||||
 | 
					  char buffer[64];
 | 
				
			||||||
 | 
					  dword serverIp;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  SysGetVariableString(sysvar::TCPIP::TcpServerIp, buffer, elcount(buffer));
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  serverIp = IpGetAddressAsNumber(buffer);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  if (INVALID_IP == serverIp)
 | 
				
			||||||
 | 
					  {
 | 
				
			||||||
 | 
					    writelineex(0, 1, "Error: invalid server Ip address!");
 | 
				
			||||||
 | 
					    
 | 
				
			||||||
 | 
					    return;
 | 
				
			||||||
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  gTcpPort = @sysvar::TCPIP::TcpClientPort;
 | 
				
			||||||
 | 
					  
 | 
				
			||||||
 | 
					  gTcpDataSocket = TcpOpen(gIpAddress, gTcpPort);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  if ( INVALID_SOCKET == gTcpDataSocket)
 | 
				
			||||||
 | 
					  {
 | 
				
			||||||
 | 
					    writelineex(0, 1, "Error: could not open Tcp socket!");
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    return;
 | 
				
			||||||
 | 
					  }
 | 
				
			||||||
 | 
					  else
 | 
				
			||||||
 | 
					  {
 | 
				
			||||||
 | 
					    writelineex(0, 1, "Tcp socket opened.");
 | 
				
			||||||
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  if (0 == TcpConnect(gTcpDataSocket, serverIp, @sysvar::TCPIP::TcpServerPort))
 | 
				
			||||||
 | 
					  {
 | 
				
			||||||
 | 
					    writelineex(0, 1, "Successfully connected to server %s:%d", buffer, @sysvar::TCPIP::TcpServerPort);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    TcpRecv( gTcpDataSocket);
 | 
				
			||||||
 | 
					  }
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					/*@@end*/
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					/*@@caplFunc:DisconnectTcp():*///function
 | 
				
			||||||
 | 
					void DisconnectTcp()
 | 
				
			||||||
 | 
					{
 | 
				
			||||||
 | 
					  if (INVALID_SOCKET != gTcpDataSocket)
 | 
				
			||||||
 | 
					  {
 | 
				
			||||||
 | 
					    TcpClose(gTcpDataSocket);
 | 
				
			||||||
 | 
					    
 | 
				
			||||||
 | 
					    gTcpDataSocket = INVALID_SOCKET;
 | 
				
			||||||
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  writelineex(0, 1, "Tcp socket is closed.");
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					/*@@end*/
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					/*@@caplFunc:SendTcpData():*///function
 | 
				
			||||||
 | 
					void SendTcpData()
 | 
				
			||||||
 | 
					{
 | 
				
			||||||
 | 
					  char buffer[8192];
 | 
				
			||||||
 | 
					  
 | 
				
			||||||
 | 
					  SysGetVariableString(sysvar::TCPIP::TcpClientData, buffer, elcount(buffer));
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  if (INVALID_SOCKET == gTcpDataSocket)
 | 
				
			||||||
 | 
					  {
 | 
				
			||||||
 | 
					    writelineex( 0, 2, "Tcp socket is invalid!");
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    return;
 | 
				
			||||||
 | 
					  }
 | 
				
			||||||
 | 
					  
 | 
				
			||||||
 | 
					  if (0 != TcpSend( gTcpDataSocket, buffer, elcount(buffer)))
 | 
				
			||||||
 | 
					  {
 | 
				
			||||||
 | 
					    gIpLastErr = IpGetLastSocketError( gTcpDataSocket);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    if ( WSA_IO_PENDING != gIpLastErr)
 | 
				
			||||||
 | 
					    {
 | 
				
			||||||
 | 
					      IpGetLastSocketErrorAsString( gTcpDataSocket, gIpLastErrStr, elcount( gIpLastErrStr));
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					      writelineex( 0, 2, "Tcp send error (%d): %s", gIpLastErr, gIpLastErrStr);
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					  }
 | 
				
			||||||
 | 
					  else
 | 
				
			||||||
 | 
					  {
 | 
				
			||||||
 | 
					    writelineex( 0, 1, "Tcp data sent successfully!");
 | 
				
			||||||
 | 
					  }
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					/*@@end*/
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					/*@@caplFunc:SendUdpData():*///function
 | 
				
			||||||
 | 
					void SendUdpData()
 | 
				
			||||||
 | 
					{
 | 
				
			||||||
 | 
					  char buffer[64]; // used for ip address
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  char data[4096]; // user data
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  dword serverIp;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  int serverPort;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  if ( INVALID_SOCKET == gUdpSocket)
 | 
				
			||||||
 | 
					  {
 | 
				
			||||||
 | 
					    writelineex(0, 1, "Error: Udp socket is not opened!");
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    return;
 | 
				
			||||||
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  SysGetVariableString(sysvar::UDP::UdpServerIp, buffer, elcount(buffer));
 | 
				
			||||||
 | 
					  
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  serverIp = IpGetAddressAsNumber(buffer);
 | 
				
			||||||
 | 
					  
 | 
				
			||||||
 | 
					  if (INVALID_IP == serverIp)
 | 
				
			||||||
 | 
					  {
 | 
				
			||||||
 | 
					    writelineex(0, 1, "Error: invalid server Ip address!");
 | 
				
			||||||
 | 
					    return;
 | 
				
			||||||
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  SysGetVariableString(sysvar::UDP::UdpClientData, data, elcount(data));
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  if (0 == UdpSendTo(gUdpSocket, serverIp, @sysvar::UDP::UdpServerPort, data, elcount(data)))
 | 
				
			||||||
 | 
					  {
 | 
				
			||||||
 | 
					    writelineex(0, 1, "Successfully sent Udp data.");
 | 
				
			||||||
 | 
					  }
 | 
				
			||||||
 | 
					  else
 | 
				
			||||||
 | 
					  {
 | 
				
			||||||
 | 
					    writelineex(0, 1, "Error: an error occured while connecting to server %s:%d", serverIp, @sysvar::TCPIP::TcpServerPort);
 | 
				
			||||||
 | 
					  }
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					/*@@end*/
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					/*@@caplFunc:OpenUdpPort():*///function
 | 
				
			||||||
 | 
					void OpenUdpPort()
 | 
				
			||||||
 | 
					{
 | 
				
			||||||
 | 
					  gUdpPort = @sysvar::UDP::UdpClientPort;
 | 
				
			||||||
 | 
					  
 | 
				
			||||||
 | 
					  gUdpSocket = UdpOpen( gIpAddress, gUdpPort);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  if ( INVALID_SOCKET == gUdpSocket)
 | 
				
			||||||
 | 
					  {
 | 
				
			||||||
 | 
					    writelineex(0, 1, "Error: could not create Udp socket!");
 | 
				
			||||||
 | 
					  }
 | 
				
			||||||
 | 
					  else
 | 
				
			||||||
 | 
					  {
 | 
				
			||||||
 | 
					    writelineex(0, 1, "Udp socket is opened successfully.");
 | 
				
			||||||
 | 
					    
 | 
				
			||||||
 | 
					    UdpRecv( gUdpSocket);
 | 
				
			||||||
 | 
					  }
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					/*@@end*/
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					/*@@caplFunc:CloseUdpPort():*///function
 | 
				
			||||||
 | 
					void CloseUdpPort()
 | 
				
			||||||
 | 
					{
 | 
				
			||||||
 | 
					  if (INVALID_SOCKET != gUdpSocket)
 | 
				
			||||||
 | 
					  {
 | 
				
			||||||
 | 
					    UdpClose(gUdpSocket);
 | 
				
			||||||
 | 
					    
 | 
				
			||||||
 | 
					    gUdpSocket = INVALID_SOCKET;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    writelineex(0, 2, "Udp port is closed.");
 | 
				
			||||||
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  SysSetVariableString(sysvar::UDP::UdpServerIp, "");
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  @sysvar::UDP::UdpServerPort = 0;
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					/*@@end*/
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					/*@@caplFunc:ResetIp():*///function
 | 
				
			||||||
 | 
					void ResetIp()
 | 
				
			||||||
 | 
					{
 | 
				
			||||||
 | 
					  if (INVALID_SOCKET != gTcpDataSocket)
 | 
				
			||||||
 | 
					  {
 | 
				
			||||||
 | 
					    TcpClose(gTcpDataSocket);
 | 
				
			||||||
 | 
					    
 | 
				
			||||||
 | 
					    gTcpDataSocket = INVALID_SOCKET;
 | 
				
			||||||
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  if (INVALID_SOCKET != gUdpSocket)
 | 
				
			||||||
 | 
					  {
 | 
				
			||||||
 | 
					    UdpClose(gUdpSocket);
 | 
				
			||||||
 | 
					    
 | 
				
			||||||
 | 
					    gUdpSocket = INVALID_SOCKET;
 | 
				
			||||||
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					/*@@end*/
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					/*@@caplFunc:OnUdpReceiveFrom(dword,long,dword,dword,char[],dword):*///function
 | 
				
			||||||
 | 
					void OnUdpReceiveFrom( dword socket, long result, dword address, dword port, char buffer[], dword count)
 | 
				
			||||||
 | 
					{
 | 
				
			||||||
 | 
					  char  addressString[64] = "";
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  if ( gUdpSocket != socket)
 | 
				
			||||||
 | 
					  {
 | 
				
			||||||
 | 
					    writelineex(0, 2, "OnUdpReceiveFrom called for unknown socket!");
 | 
				
			||||||
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  if (0 != result)
 | 
				
			||||||
 | 
					  {
 | 
				
			||||||
 | 
					    IpGetLastSocketErrorAsString( socket, gIpLastErrStr, elcount( gIpLastErrStr));
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    writelineex( 0, 2, "OnUdpReceiveFrom error (%d): %s", IpGetLastSocketError( socket), gIpLastErrStr);
 | 
				
			||||||
 | 
					  }
 | 
				
			||||||
 | 
					 
 | 
				
			||||||
 | 
					  IpGetAddressAsString(address, addressString, elcount(addressString));
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  SysSetVariableString(sysvar::UDP::UdpServerIp, addressString);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  @sysvar::UDP::UdpServerPort = port;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  SysSetVariableString(sysvar::UDP::UdpData, buffer);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					 
 | 
				
			||||||
 | 
					  UdpRecv( socket);
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					/*@@end*/
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					/*@@caplFunc:OnUdpSendTo(dword,long,char[],dword):*///function
 | 
				
			||||||
 | 
					void OnUdpSendTo( dword socket, long result, char buffer[], dword size)
 | 
				
			||||||
 | 
					{
 | 
				
			||||||
 | 
					  if ( gUdpSocket != socket)
 | 
				
			||||||
 | 
					  {
 | 
				
			||||||
 | 
					    writelineex(0, 2, "OnUdpSendTo called for unknown socket 0x%X", socket);
 | 
				
			||||||
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  if (0 != result)
 | 
				
			||||||
 | 
					  {
 | 
				
			||||||
 | 
					    IpGetLastSocketErrorAsString( socket, gIpLastErrStr, elcount( gIpLastErrStr));
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    writelineex( 0, 2, "OnUdpSendTo error (%d): %s", IpGetLastSocketError( socket), gIpLastErrStr);
 | 
				
			||||||
 | 
					  }
 | 
				
			||||||
 | 
					  else
 | 
				
			||||||
 | 
					  {
 | 
				
			||||||
 | 
					    writelineex(0, 1, "Successfully sent Udp data.");
 | 
				
			||||||
 | 
					  }
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					/*@@end*/
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					/*@@caplFunc:OnTcpConnect(dword,long):*///function
 | 
				
			||||||
 | 
					void OnTcpConnect( dword socket, long result)
 | 
				
			||||||
 | 
					{
 | 
				
			||||||
 | 
					  if ( gTcpDataSocket != socket)
 | 
				
			||||||
 | 
					  {
 | 
				
			||||||
 | 
					    writelineex(0, 2, "OnTcpConnect called for unknown socket 0x%X", socket);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    return;
 | 
				
			||||||
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  if (0 != result)
 | 
				
			||||||
 | 
					  {
 | 
				
			||||||
 | 
					    IpGetLastSocketErrorAsString( socket, gIpLastErrStr, elcount( gIpLastErrStr));
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    writelineex( 0, 2, "OnTcpConnect error (%d): %s", IpGetLastSocketError( socket), gIpLastErrStr);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    return;
 | 
				
			||||||
 | 
					  }
 | 
				
			||||||
 | 
					  else
 | 
				
			||||||
 | 
					  {
 | 
				
			||||||
 | 
					    writelineex(0, 1, "Successfully connected to server via Tcp");
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    TcpRecv( socket);
 | 
				
			||||||
 | 
					  }
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					/*@@end*/
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					/*@@sysvarUpdate:TCPIP::TcpConnect:*/
 | 
				
			||||||
 | 
					on sysvar_update sysvar::TCPIP::TcpConnect
 | 
				
			||||||
 | 
					{
 | 
				
			||||||
 | 
					  if (@this)
 | 
				
			||||||
 | 
					  {
 | 
				
			||||||
 | 
					    ConnectTcp();
 | 
				
			||||||
 | 
					  }
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					/*@@end*/
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					/*@@sysvarUpdate:TCPIP::TcpDisconnect:*/
 | 
				
			||||||
 | 
					on sysvar_update sysvar::TCPIP::TcpDisconnect
 | 
				
			||||||
 | 
					{
 | 
				
			||||||
 | 
					  if (@this)
 | 
				
			||||||
 | 
					  {
 | 
				
			||||||
 | 
					    DisconnectTcp();
 | 
				
			||||||
 | 
					  }
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					/*@@end*/
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					/*@@sysvarUpdate:TCPIP::TcpSend:*/
 | 
				
			||||||
 | 
					on sysvar_update sysvar::TCPIP::TcpSend
 | 
				
			||||||
 | 
					{
 | 
				
			||||||
 | 
					  if (@this)
 | 
				
			||||||
 | 
					  {
 | 
				
			||||||
 | 
					    SendTcpData();
 | 
				
			||||||
 | 
					  }
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					/*@@end*/
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					/*@@sysvarUpdate:UDP::UdpClose:*/
 | 
				
			||||||
 | 
					on sysvar_update sysvar::UDP::UdpClose
 | 
				
			||||||
 | 
					{
 | 
				
			||||||
 | 
					  if (@this)
 | 
				
			||||||
 | 
					  {
 | 
				
			||||||
 | 
					    CloseUdpPort();
 | 
				
			||||||
 | 
					  }
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					/*@@end*/
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					/*@@sysvarUpdate:UDP::UdpOpen:*/
 | 
				
			||||||
 | 
					on sysvar_update sysvar::UDP::UdpOpen
 | 
				
			||||||
 | 
					{
 | 
				
			||||||
 | 
					  if (@this)
 | 
				
			||||||
 | 
					  {
 | 
				
			||||||
 | 
					    OpenUdpPort();
 | 
				
			||||||
 | 
					  }
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					/*@@end*/
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					/*@@sysvarUpdate:UDP::UdpSend:*/
 | 
				
			||||||
 | 
					on sysvar_update sysvar::UDP::UdpSend
 | 
				
			||||||
 | 
					{
 | 
				
			||||||
 | 
					  if (@this)
 | 
				
			||||||
 | 
					  {
 | 
				
			||||||
 | 
					    SendUdpData();
 | 
				
			||||||
 | 
					  }
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					/*@@end*/
 | 
				
			||||||
 | 
					
 | 
				
			||||||
							
								
								
									
										397
									
								
								Modbus TCP/IPServer.can
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										397
									
								
								Modbus TCP/IPServer.can
									
									
									
									
									
										Normal file
									
								
							| 
						 | 
					@ -0,0 +1,397 @@
 | 
				
			||||||
 | 
					includes
 | 
				
			||||||
 | 
					{
 | 
				
			||||||
 | 
					  #include "IPCommon.can"
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					variables
 | 
				
			||||||
 | 
					{
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					void SetupIp()
 | 
				
			||||||
 | 
					{
 | 
				
			||||||
 | 
					  int   adapterIndex = 4;
 | 
				
			||||||
 | 
					  char  text[512] = "";
 | 
				
			||||||
 | 
					  char  info[512] = "";
 | 
				
			||||||
 | 
					  int   size = 512;
 | 
				
			||||||
 | 
					  long  result = 0;
 | 
				
			||||||
 | 
					  dword addresses[1];
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  writeClear(0);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  if (1 > IpGetAdapterCount())
 | 
				
			||||||
 | 
					  {
 | 
				
			||||||
 | 
					    writelineex(0, 3, "Error: There is no network interface available!");
 | 
				
			||||||
 | 
					    
 | 
				
			||||||
 | 
					    stop();
 | 
				
			||||||
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  if (0 != IpGetAdapterAddress(adapterIndex, addresses, 1))
 | 
				
			||||||
 | 
					  {
 | 
				
			||||||
 | 
					    writelineex(0, 3, "Error: Could not retrieve Ip address!");
 | 
				
			||||||
 | 
					    
 | 
				
			||||||
 | 
					    stop();
 | 
				
			||||||
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  gIpAddress = addresses[0]; // the interface used
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  if (INVALID_IP == gIpAddress)
 | 
				
			||||||
 | 
					  {
 | 
				
			||||||
 | 
					    writelineex(0, 3, "Error: Ip address to be used is invalid!");
 | 
				
			||||||
 | 
					    
 | 
				
			||||||
 | 
					    stop();
 | 
				
			||||||
 | 
					  }
 | 
				
			||||||
 | 
					 
 | 
				
			||||||
 | 
					  IpGetAdapterDescription(adapterIndex, text, size);
 | 
				
			||||||
 | 
					  snprintf(info, size, "Interface: %s", text);
 | 
				
			||||||
 | 
					  writelineex(0, 1, info);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  IpGetAdapterAddressAsString(adapterIndex, text, size);
 | 
				
			||||||
 | 
					  snprintf(info, size, "Ip address: %s", text);
 | 
				
			||||||
 | 
					  writelineex(0, 1, info);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  SysSetVariableString(sysvar::TCPIP::TcpServerIp, text);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  SysSetVariableString(sysvar::UDP::UdpServerIp, text);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  IpGetAdapterMaskAsString(adapterIndex, text, size);
 | 
				
			||||||
 | 
					  snprintf(info, size, "Subnet mask: %s", text);
 | 
				
			||||||
 | 
					  writelineex(0, 1, info);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  IpGetAdapterGatewayAsString(adapterIndex, text, size);
 | 
				
			||||||
 | 
					  snprintf(info, size, "Gateway address: %s", text);
 | 
				
			||||||
 | 
					  writelineex(0, 1, info);
 | 
				
			||||||
 | 
					 
 | 
				
			||||||
 | 
					  gStatus = gkSTATUS_INITIALISED;
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					on start
 | 
				
			||||||
 | 
					{
 | 
				
			||||||
 | 
					  SetupIp();  
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					on stopMeasurement
 | 
				
			||||||
 | 
					{
 | 
				
			||||||
 | 
					  ResetIp();
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					void OnTcpListen( dword socket, long result)
 | 
				
			||||||
 | 
					{
 | 
				
			||||||
 | 
					  if (gTcpSocket != socket)
 | 
				
			||||||
 | 
					  {
 | 
				
			||||||
 | 
					    writelineex( 0, 2, "OnTcpListen called for unexpected socket (%d).", socket);
 | 
				
			||||||
 | 
					    
 | 
				
			||||||
 | 
					    return;
 | 
				
			||||||
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  if ( 0 != result)
 | 
				
			||||||
 | 
					  {
 | 
				
			||||||
 | 
					    IpGetLastSocketErrorAsString( socket, gIpLastErrStr, elcount( gIpLastErrStr));
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    writelineex( 0, 2, "OnTcpListen error (%d, %s).", 
 | 
				
			||||||
 | 
					                 IpGetLastSocketError( socket), gIpLastErrStr);
 | 
				
			||||||
 | 
					    return;
 | 
				
			||||||
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  if (gTcpDataSocket != INVALID_SOCKET)
 | 
				
			||||||
 | 
					  {
 | 
				
			||||||
 | 
					    TcpClose(gTcpDataSocket);
 | 
				
			||||||
 | 
					  }
 | 
				
			||||||
 | 
					  
 | 
				
			||||||
 | 
					  gTcpDataSocket = TcpAccept( socket);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  if ( INVALID_SOCKET == gTcpDataSocket)
 | 
				
			||||||
 | 
					  {
 | 
				
			||||||
 | 
					    IpGetLastSocketErrorAsString( socket, gIpLastErrStr, elcount( gIpLastErrStr));
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    writelineex( 0, 2, "Error: TcpAccept (%d): %s", 
 | 
				
			||||||
 | 
					                 IpGetLastSocketError( socket), gIpLastErrStr);
 | 
				
			||||||
 | 
					    
 | 
				
			||||||
 | 
					    return;
 | 
				
			||||||
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  TcpRecv( gTcpDataSocket);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  writelineex( 0, 1, "Status: Tcp connection established.");  
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					long OnTcpReceive( dword socket, long result, dword address, dword port, char buffer[], dword size)
 | 
				
			||||||
 | 
					{
 | 
				
			||||||
 | 
					  char  addressString[64] = "";
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  if ( gTcpDataSocket != socket)
 | 
				
			||||||
 | 
					  {
 | 
				
			||||||
 | 
					    writelineex(0, 2, "OnTcpReceive called for unknown socket 0x%X", socket);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    return 0;
 | 
				
			||||||
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  if (0 != result)
 | 
				
			||||||
 | 
					  {
 | 
				
			||||||
 | 
					    IpGetLastSocketErrorAsString( socket, gIpLastErrStr, elcount( gIpLastErrStr));
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    writelineex( 0, 2, "OnTcpReceive error (%d): %s", IpGetLastSocketError( socket), gIpLastErrStr);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    return 0;
 | 
				
			||||||
 | 
					  }
 | 
				
			||||||
 | 
					 
 | 
				
			||||||
 | 
					  IpGetAddressAsString(address, addressString, elcount(addressString));
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					   SysSetVariableString(sysvar::TCPIP::TcpClientIp, addressString);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  @sysvar::TCPIP::TcpClientPort = port;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  SysSetVariableString(sysvar::TCPIP::TcpClientData, buffer);
 | 
				
			||||||
 | 
					 
 | 
				
			||||||
 | 
					  TcpRecv( socket);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  return 0;
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					void OnTcpSend( dword socket, long result, char buffer[], dword size)
 | 
				
			||||||
 | 
					{
 | 
				
			||||||
 | 
					  if ( gTcpDataSocket != socket)
 | 
				
			||||||
 | 
					  {
 | 
				
			||||||
 | 
					    writelineex(0, 2, "OnTcpSend called for unknown socket 0x%X", socket);
 | 
				
			||||||
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  if (0 != result)
 | 
				
			||||||
 | 
					  {
 | 
				
			||||||
 | 
					    IpGetLastSocketErrorAsString( socket, gIpLastErrStr, elcount( gIpLastErrStr));
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    writelineex( 0, 2, "OnTcpSend error (%d): %s", IpGetLastSocketError( socket), gIpLastErrStr);
 | 
				
			||||||
 | 
					  }
 | 
				
			||||||
 | 
					  else
 | 
				
			||||||
 | 
					  {
 | 
				
			||||||
 | 
					    writelineex( 0, 1, "Tcp data sent successfully!");
 | 
				
			||||||
 | 
					  }
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					void StartListenTcp()
 | 
				
			||||||
 | 
					{ 
 | 
				
			||||||
 | 
					  gTcpPort = @sysvar::TCPIP::TcpServerPort;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  gTcpSocket = TcpOpen(gIpAddress, gTcpPort);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  if ( INVALID_SOCKET == gTcpSocket)
 | 
				
			||||||
 | 
					  {
 | 
				
			||||||
 | 
					    writelineex(0, 1, "Error: could not create Tcp socket!");
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    return;
 | 
				
			||||||
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  writelineex(0, 1, "Successfully created Tcp socket.");
 | 
				
			||||||
 | 
					  
 | 
				
			||||||
 | 
					  TcpListen(gTcpSocket);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  writelineex( 0, 1, "Listening for incoming Tcp connections on port %d", gTcpPort);
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					void StopListenTcp()
 | 
				
			||||||
 | 
					{
 | 
				
			||||||
 | 
					  if (INVALID_SOCKET != gTcpDataSocket)
 | 
				
			||||||
 | 
					  {
 | 
				
			||||||
 | 
					    TcpClose(gTcpDataSocket);
 | 
				
			||||||
 | 
					    
 | 
				
			||||||
 | 
					    gTcpDataSocket = INVALID_SOCKET;
 | 
				
			||||||
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  if (INVALID_SOCKET != gTcpSocket)
 | 
				
			||||||
 | 
					  {
 | 
				
			||||||
 | 
					    TcpClose(gTcpSocket);
 | 
				
			||||||
 | 
					    
 | 
				
			||||||
 | 
					    gTcpSocket = INVALID_SOCKET;
 | 
				
			||||||
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  writelineex(0, 1, "Tcp socket is closed.");
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					void SendTcpData()
 | 
				
			||||||
 | 
					{
 | 
				
			||||||
 | 
					  char buffer[4096];
 | 
				
			||||||
 | 
					   
 | 
				
			||||||
 | 
					  SysGetVariableString(sysvar::TCPIP::TcpData, buffer, elcount(buffer));
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  if (INVALID_SOCKET != gTcpDataSocket)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  if (0 != TcpSend( gTcpDataSocket, buffer, elcount(buffer)))
 | 
				
			||||||
 | 
					  {
 | 
				
			||||||
 | 
					    gIpLastErr = IpGetLastSocketError( gTcpDataSocket);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    if ( WSA_IO_PENDING != gIpLastErr)
 | 
				
			||||||
 | 
					    {
 | 
				
			||||||
 | 
					      IpGetLastSocketErrorAsString( gTcpDataSocket, gIpLastErrStr, elcount( gIpLastErrStr));
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					      writelineex( 0, 2, "TcpSend error (%d): %s", gIpLastErr, gIpLastErrStr);
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					  }
 | 
				
			||||||
 | 
					  else
 | 
				
			||||||
 | 
					  {
 | 
				
			||||||
 | 
					    writelineex( 0, 1, "Tcp data sent successfully!");
 | 
				
			||||||
 | 
					  }
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					void SendUdpData()
 | 
				
			||||||
 | 
					{
 | 
				
			||||||
 | 
					  char buffer[64]; // used for ip address
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  char data[4096]; // user data
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  dword clientIp;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  if ( INVALID_SOCKET == gUdpSocket)
 | 
				
			||||||
 | 
					  {
 | 
				
			||||||
 | 
					    writelineex(0, 1, "Error: Udp socket is not opened!");
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    return;
 | 
				
			||||||
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  SysGetVariableString(sysvar::UDP::UdpClientIp, buffer, elcount(buffer));
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  clientIp = IpGetAddressAsNumber(buffer);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  if (INVALID_IP == clientIp)
 | 
				
			||||||
 | 
					  {
 | 
				
			||||||
 | 
					    writelineex(0, 1, "Error: invalid client Ip address!");
 | 
				
			||||||
 | 
					    
 | 
				
			||||||
 | 
					    return;
 | 
				
			||||||
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  SysGetVariableString(sysvar::UDP::UdpData, data, elcount(data));
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  UdpSendTo(gUdpSocket, clientIp, @sysvar::UDP::UdpClientPort, data, elcount(data));
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					void OpenUdpPort()
 | 
				
			||||||
 | 
					{
 | 
				
			||||||
 | 
					  gUdpPort = @sysvar::UDP::UdpServerPort;
 | 
				
			||||||
 | 
					  
 | 
				
			||||||
 | 
					  gUdpSocket = UdpOpen( gIpAddress, gUdpPort);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  if ( INVALID_SOCKET == gUdpSocket)
 | 
				
			||||||
 | 
					  {
 | 
				
			||||||
 | 
					    writelineex(0, 1, "Error: could not create Udp socket!");
 | 
				
			||||||
 | 
					  }
 | 
				
			||||||
 | 
					  else
 | 
				
			||||||
 | 
					  {
 | 
				
			||||||
 | 
					    writelineex(0, 1, "Udp socket is opened successfully.");
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    UdpRecv( gUdpSocket);
 | 
				
			||||||
 | 
					  }
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					void CloseUdpPort()
 | 
				
			||||||
 | 
					{
 | 
				
			||||||
 | 
					  if (INVALID_SOCKET != gUdpSocket)
 | 
				
			||||||
 | 
					  {
 | 
				
			||||||
 | 
					    UdpClose(gUdpSocket);
 | 
				
			||||||
 | 
					    
 | 
				
			||||||
 | 
					    gUdpSocket = INVALID_SOCKET;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    writelineex(0, 1, "Udp port is closed.");
 | 
				
			||||||
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  SysSetVariableString(sysvar::UDP::UdpClientIp, "");
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  @sysvar::UDP::UdpClientPort = 0;
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					void ResetIp()
 | 
				
			||||||
 | 
					{
 | 
				
			||||||
 | 
					  if (INVALID_SOCKET != gTcpDataSocket)
 | 
				
			||||||
 | 
					  {
 | 
				
			||||||
 | 
					    TcpClose(gTcpDataSocket);
 | 
				
			||||||
 | 
					    
 | 
				
			||||||
 | 
					    gTcpDataSocket = INVALID_SOCKET;
 | 
				
			||||||
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  if (INVALID_SOCKET != gTcpSocket)
 | 
				
			||||||
 | 
					  {
 | 
				
			||||||
 | 
					    TcpClose(gTcpSocket);
 | 
				
			||||||
 | 
					    
 | 
				
			||||||
 | 
					    gTcpSocket = INVALID_SOCKET;
 | 
				
			||||||
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  if (INVALID_SOCKET != gUdpSocket)
 | 
				
			||||||
 | 
					  {
 | 
				
			||||||
 | 
					    UdpClose(gUdpSocket);
 | 
				
			||||||
 | 
					    
 | 
				
			||||||
 | 
					    gUdpSocket = INVALID_SOCKET;
 | 
				
			||||||
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					void OnUdpReceiveFrom( dword socket, long result, dword address, dword port, char buffer[], dword count)
 | 
				
			||||||
 | 
					{
 | 
				
			||||||
 | 
					  char  addressString[64] = "";
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  if ( gUdpSocket != socket)
 | 
				
			||||||
 | 
					  {
 | 
				
			||||||
 | 
					    writelineex(0, 2, "OnUdpReceiveFrom called for unknown socket!");
 | 
				
			||||||
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  if (0 != result)
 | 
				
			||||||
 | 
					  {
 | 
				
			||||||
 | 
					    IpGetLastSocketErrorAsString( socket, gIpLastErrStr, elcount( gIpLastErrStr));
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    writelineex( 0, 2, "OnUdpReceiveFrom error (%d): %s", IpGetLastSocketError( socket), gIpLastErrStr);
 | 
				
			||||||
 | 
					  }
 | 
				
			||||||
 | 
					 
 | 
				
			||||||
 | 
					  IpGetAddressAsString(address, addressString, elcount(addressString));
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  SysSetVariableString(sysvar::UDP::UdpClientIp, addressString);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  @sysvar::UDP::UdpClientPort = port;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  SysSetVariableString(sysvar::UDP::UdpClientData, buffer);
 | 
				
			||||||
 | 
					 
 | 
				
			||||||
 | 
					  UdpRecv( socket);
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					on sysvar_update sysvar::TCPIP::TcpSend
 | 
				
			||||||
 | 
					{
 | 
				
			||||||
 | 
					  if (@this)
 | 
				
			||||||
 | 
					  {
 | 
				
			||||||
 | 
					    SendTcpData();
 | 
				
			||||||
 | 
					  }
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					on sysvar_update sysvar::TCPIP::TcpStartListen
 | 
				
			||||||
 | 
					{
 | 
				
			||||||
 | 
					  if (@this)
 | 
				
			||||||
 | 
					  {
 | 
				
			||||||
 | 
					    StartListenTcp();
 | 
				
			||||||
 | 
					  }
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					on sysvar_update sysvar::TCPIP::TcpStopListen
 | 
				
			||||||
 | 
					{
 | 
				
			||||||
 | 
					  if (@this)
 | 
				
			||||||
 | 
					  {
 | 
				
			||||||
 | 
					    StopListenTcp();
 | 
				
			||||||
 | 
					  }
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					on sysvar_update sysvar::UDP::UdpClose
 | 
				
			||||||
 | 
					{
 | 
				
			||||||
 | 
					  if (@this)
 | 
				
			||||||
 | 
					  {
 | 
				
			||||||
 | 
					    CloseUdpPort();
 | 
				
			||||||
 | 
					  }
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					on sysvar_update sysvar::UDP::UdpOpen
 | 
				
			||||||
 | 
					{
 | 
				
			||||||
 | 
					  if (@this)
 | 
				
			||||||
 | 
					  {
 | 
				
			||||||
 | 
					    OpenUdpPort();
 | 
				
			||||||
 | 
					  }
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					on sysvar_update sysvar::UDP::UdpSend
 | 
				
			||||||
 | 
					{
 | 
				
			||||||
 | 
					  if (@this)
 | 
				
			||||||
 | 
					  {
 | 
				
			||||||
 | 
					    SendUdpData();
 | 
				
			||||||
 | 
					  }
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
							
								
								
									
										241
									
								
								Modbus TCP/TcpCommon.can
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										241
									
								
								Modbus TCP/TcpCommon.can
									
									
									
									
									
										Normal file
									
								
							| 
						 | 
					@ -0,0 +1,241 @@
 | 
				
			||||||
 | 
					/*@!Encoding:1252*/
 | 
				
			||||||
 | 
					includes
 | 
				
			||||||
 | 
					{
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					variables
 | 
				
			||||||
 | 
					{
 | 
				
			||||||
 | 
						const long	WSA_IO_PENDING		= 997;
 | 
				
			||||||
 | 
						const long	WSAEWOULDBLOCK		= 10035;
 | 
				
			||||||
 | 
						const dword INVALID_IP			= 0xffffffff;
 | 
				
			||||||
 | 
						
 | 
				
			||||||
 | 
						long gIpLastErr					= 0;
 | 
				
			||||||
 | 
						char gIpLastErrStr[512]			= "";
 | 
				
			||||||
 | 
						TcpSocket gSocket;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						char gTcpRxBuffer[8192];
 | 
				
			||||||
 | 
						char gUdpRxBuffer[4096];
 | 
				
			||||||
 | 
					/*
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						dword		gIpAddress			= INVALID_IP;
 | 
				
			||||||
 | 
						char		gIpLastErrStr[1024]	= "";
 | 
				
			||||||
 | 
						char		gIpAddressStr[32]	= "";
 | 
				
			||||||
 | 
						int			gIpLastErr			= 0;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						dword		gUdpPort			= 0;
 | 
				
			||||||
 | 
						long		gUdpSocket			= INVALID_SOCKET;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						dword		gTcpPort			= 0;
 | 
				
			||||||
 | 
						long		gTcpSocket			= INVALID_SOCKET;
 | 
				
			||||||
 | 
						long		gTcpDataSocket		= INVALID_SOCKET;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						// status
 | 
				
			||||||
 | 
						int				 gStatus = 0;
 | 
				
			||||||
 | 
						const int	 gkSTATUS_UNINITIALISED = 0;
 | 
				
			||||||
 | 
						const int	 gkSTATUS_INITIALISED	 = 1;
 | 
				
			||||||
 | 
					*/
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					/*
 | 
				
			||||||
 | 
					long UdpRecv()
 | 
				
			||||||
 | 
					{
 | 
				
			||||||
 | 
						int result = 0;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						result = gSocket.UdpReceiveFrom(gUdpRxBuffer, elcount( gUdpRxBuffer));
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						if ( 0 != result)
 | 
				
			||||||
 | 
						{
 | 
				
			||||||
 | 
							gIpLastErr = gSocket.GetLastSocketError();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
							if ( WSA_IO_PENDING != gIpLastErr)
 | 
				
			||||||
 | 
							{
 | 
				
			||||||
 | 
								gSocket.GetLastSocketErrorAsString(gIpLastErrStr, elcount( gIpLastErrStr));
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
								writelineex( 0, 2, "UdpReceive error (%d): %s", gIpLastErr, gIpLastErrStr);
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						return result;
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					*/
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					dword SetupIp(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();
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						adapterIndex = @sysvar::TCPIP::AdapterIndex;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						if (IpGetAdapterAddress(adapterIndex, addresses, 1) != 0)
 | 
				
			||||||
 | 
						{
 | 
				
			||||||
 | 
							writelineex(0, 3, "<%BASE_FILE_NAME%> Error: Could not retrieve ip address!");
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
							stop();
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						address = addresses[0]; // the interface used
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						if (address == INVALID_IP)
 | 
				
			||||||
 | 
						{
 | 
				
			||||||
 | 
							writelineex(0, 3, "<%BASE_FILE_NAME%> Error: ip address to be used is invalid!");
 | 
				
			||||||
 | 
							
 | 
				
			||||||
 | 
							stop();
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					 
 | 
				
			||||||
 | 
						IpGetAdapterDescription(adapterIndex, text, size);
 | 
				
			||||||
 | 
						snprintf(info, size, "<%BASE_FILE_NAME%> Interface: %s", text);
 | 
				
			||||||
 | 
						writelineex(0, 1, info);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						IpGetAdapterAddressAsString(adapterIndex, text, size);
 | 
				
			||||||
 | 
						snprintf(info, size, "<%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);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						IpGetAdapterGatewayAsString(adapterIndex, text, size);
 | 
				
			||||||
 | 
						snprintf(info, size, "<%BASE_FILE_NAME%> Gateway address: %s", text);
 | 
				
			||||||
 | 
						writelineex(0, 1, info);
 | 
				
			||||||
 | 
						
 | 
				
			||||||
 | 
						return address;
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					word OpenSocket()
 | 
				
			||||||
 | 
					{
 | 
				
			||||||
 | 
						char Local_IP[16];
 | 
				
			||||||
 | 
						dword localIp;
 | 
				
			||||||
 | 
						word localPort;
 | 
				
			||||||
 | 
						dword i = 0;
 | 
				
			||||||
 | 
						CHAR errorText[200];
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						localIp = SetupIp(Local_IP);
 | 
				
			||||||
 | 
						localPort = random(65535-10240)+10240;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						// Try to open socket
 | 
				
			||||||
 | 
						do
 | 
				
			||||||
 | 
						{
 | 
				
			||||||
 | 
							gSocket = TcpSocket::Open(localIp, localPort);
 | 
				
			||||||
 | 
							if (gSocket.GetLastSocketError() != 0)
 | 
				
			||||||
 | 
							{
 | 
				
			||||||
 | 
								gSocket.GetLastSocketErrorAsString(errorText, elcount(errorText));
 | 
				
			||||||
 | 
								writelineex(0, 1, "<%BASE_FILE_NAME%> Error: could not open Tcp socket on %s:%d, %s (%d)!", Local_IP, localPort, errorText, gSocket.GetLastSocketError());
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						while (gSocket.GetLastSocketError() != 0 && i++ < 9);
 | 
				
			||||||
 | 
						
 | 
				
			||||||
 | 
						if (gSocket.GetLastSocketError() != 0)
 | 
				
			||||||
 | 
						{
 | 
				
			||||||
 | 
							writelineex(0, 1, "<%BASE_FILE_NAME%> Error: could not open Tcp socket!");
 | 
				
			||||||
 | 
							return gSocket.GetLastSocketError();
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						else
 | 
				
			||||||
 | 
						{
 | 
				
			||||||
 | 
							writelineex(0, 1, "<%BASE_FILE_NAME%> Tcp socket opened on %s:%d.", Local_IP, localPort);
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						return 0;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					word TcpConnectTo(char Remote_IP[], word remotePort)
 | 
				
			||||||
 | 
					{
 | 
				
			||||||
 | 
						dword remoteIp;
 | 
				
			||||||
 | 
						long fehler;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						// Try to open a socket
 | 
				
			||||||
 | 
						fehler = OpenSocket();
 | 
				
			||||||
 | 
						if (fehler != 0)
 | 
				
			||||||
 | 
						{
 | 
				
			||||||
 | 
							return fehler;
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						// Convert IP string to Number
 | 
				
			||||||
 | 
						remoteIp = IpGetAddressAsNumber(Remote_IP);
 | 
				
			||||||
 | 
						if (remoteIp == INVALID_IP)
 | 
				
			||||||
 | 
						{
 | 
				
			||||||
 | 
							writelineex(0, 1, "<%BASE_FILE_NAME%> Error: invalid server Ip address!");
 | 
				
			||||||
 | 
							
 | 
				
			||||||
 | 
							return 1;
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						// Connect to Server
 | 
				
			||||||
 | 
						if (gSocket.Connect(remoteIp, remotePort) != 0)
 | 
				
			||||||
 | 
						{
 | 
				
			||||||
 | 
							fehler = gSocket.GetLastSocketError();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
							if (fehler != WSAEWOULDBLOCK)		// OnTcpConnect will be called otherwise
 | 
				
			||||||
 | 
							{
 | 
				
			||||||
 | 
								write("<%BASE_FILE_NAME%> No Port-Connection: %d", fehler);
 | 
				
			||||||
 | 
								return fehler;
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
							return 0;
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						else
 | 
				
			||||||
 | 
						{
 | 
				
			||||||
 | 
							writelineex(0, 1, "<%BASE_FILE_NAME%> Successfully connected to server %s:%d", Remote_IP, remotePort);
 | 
				
			||||||
 | 
							TcpRecv();
 | 
				
			||||||
 | 
							return 0;
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					void OnTcpConnect(dword socket, long result)
 | 
				
			||||||
 | 
					{
 | 
				
			||||||
 | 
						if (result != 0)
 | 
				
			||||||
 | 
						{
 | 
				
			||||||
 | 
							gSocket.GetLastSocketErrorAsString(gIpLastErrStr, elcount(gIpLastErrStr));
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
							writelineex(0, 2, "<%BASE_FILE_NAME%> OnTcpConnect error (%d): %s", gSocket.GetLastSocketError(), gIpLastErrStr);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
							return;
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						else
 | 
				
			||||||
 | 
						{
 | 
				
			||||||
 | 
							writelineex(0, 1, "<%BASE_FILE_NAME%> Successfully connected to server");
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					long TcpRecv()
 | 
				
			||||||
 | 
					{
 | 
				
			||||||
 | 
						int result;
 | 
				
			||||||
 | 
						
 | 
				
			||||||
 | 
						result = gSocket.Receive(gTcpRxBuffer, elcount(gTcpRxBuffer));
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						if (result != 0)
 | 
				
			||||||
 | 
						{
 | 
				
			||||||
 | 
							gIpLastErr = gSocket.GetLastSocketError();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
							if (WSA_IO_PENDING != gIpLastErr)
 | 
				
			||||||
 | 
							{
 | 
				
			||||||
 | 
								gSocket.GetLastSocketErrorAsString(gIpLastErrStr, elcount( gIpLastErrStr));
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
								writelineex(0, 2, "<%BASE_FILE_NAME%> TcpReceive error (%d): %s", gIpLastErr, gIpLastErrStr);
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
							else
 | 
				
			||||||
 | 
							{
 | 
				
			||||||
 | 
								writelineex(0, 1, "<%BASE_FILE_NAME%> TcpReceive was IO pending", gTcpRxBuffer);
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						else
 | 
				
			||||||
 | 
						{
 | 
				
			||||||
 | 
							writelineex(0, 1, "<%BASE_FILE_NAME%> TcpReceive: %s", gTcpRxBuffer);
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						return result;
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
							
								
								
									
										6710
									
								
								Modbus TCP/modbus.cfg
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										6710
									
								
								Modbus TCP/modbus.cfg
									
									
									
									
									
										Normal file
									
								
							
										
											
												File diff suppressed because it is too large
												Load diff
											
										
									
								
							
							
								
								
									
										18
									
								
								Modbus TCP/sender.can
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										18
									
								
								Modbus TCP/sender.can
									
									
									
									
									
										Normal file
									
								
							| 
						 | 
					@ -0,0 +1,18 @@
 | 
				
			||||||
 | 
					includes
 | 
				
			||||||
 | 
					{
 | 
				
			||||||
 | 
					  
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					variables
 | 
				
			||||||
 | 
					{
 | 
				
			||||||
 | 
					  int gedrueckt=0;
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					on sysvar senden
 | 
				
			||||||
 | 
					{
 | 
				
			||||||
 | 
					  if (gedrueckt==1) {return;};
 | 
				
			||||||
 | 
					  if (@senden==0) {gedrueckt=0;}; // Rücksetzen wenn Button losgelassen
 | 
				
			||||||
 | 
					  
 | 
				
			||||||
 | 
					  write ("Gedrueckt");
 | 
				
			||||||
 | 
					    
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
							
								
								
									
										79
									
								
								Modbus TCP/socket.can
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										79
									
								
								Modbus TCP/socket.can
									
									
									
									
									
										Normal file
									
								
							| 
						 | 
					@ -0,0 +1,79 @@
 | 
				
			||||||
 | 
					/*@!Encoding:1252*/
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					includes
 | 
				
			||||||
 | 
					{
 | 
				
			||||||
 | 
						#include "TcpCommon.can"
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					variables
 | 
				
			||||||
 | 
					{
 | 
				
			||||||
 | 
						int state = 0;
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					// Get information of local network interface such like ip address
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					on preStart
 | 
				
			||||||
 | 
					{
 | 
				
			||||||
 | 
						setStartdelay(1000);
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					on start
 | 
				
			||||||
 | 
					{
 | 
				
			||||||
 | 
						long fehler;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						fehler = TcpConnectTo("192.168.1.3", 502);
 | 
				
			||||||
 | 
						state = fehler == 0;
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					on key 'd'
 | 
				
			||||||
 | 
					{
 | 
				
			||||||
 | 
						SendTcpData();
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					void SendTcpData()
 | 
				
			||||||
 | 
					{
 | 
				
			||||||
 | 
						const word length = 12;
 | 
				
			||||||
 | 
						char buffer[length];
 | 
				
			||||||
 | 
						
 | 
				
			||||||
 | 
						if (state == 0)
 | 
				
			||||||
 | 
						{
 | 
				
			||||||
 | 
							writelineex(0, 2, "Tcp socket is invalid!");
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
							return;
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						// Modbus Application Header
 | 
				
			||||||
 | 
						buffer[00] = 0x23;			// [2] Transaction ID
 | 
				
			||||||
 | 
						buffer[01] = 0x45;
 | 
				
			||||||
 | 
						buffer[02] = 0x00;			// [2] Protocol ID = 0
 | 
				
			||||||
 | 
						buffer[03] = 0x00;
 | 
				
			||||||
 | 
						buffer[04] = (length-6)>>8;	// [2] Length; Number of bytes following
 | 
				
			||||||
 | 
						buffer[05] = (length-6);
 | 
				
			||||||
 | 
						buffer[06] = 0xFF;			// [1] Unit identifier; not relevant
 | 
				
			||||||
 | 
						// Payload
 | 
				
			||||||
 | 
						buffer[07] = 0x01;			// [1] Function Code; 1:Read DI; 2:Read DIO; 3:Read AIO; 4:Read AI; 5:Write 1 DO; 6:Write 1 AO; 7: Read Exeption, 15:Write n DO; 16:Write n AO;
 | 
				
			||||||
 | 
						buffer[08] = 0x00;			// [2] Start address
 | 
				
			||||||
 | 
						buffer[09] = 0x00;
 | 
				
			||||||
 | 
						buffer[10] = 0x01;			// [2] Number of items; 1:max 2000=0x7D0
 | 
				
			||||||
 | 
						buffer[11] = 0xFF;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						write("Sende '%s' (Länge: %d)", buffer, length);
 | 
				
			||||||
 | 
						
 | 
				
			||||||
 | 
						if (gSocket.Send(buffer, elCount(buffer)) != 0)
 | 
				
			||||||
 | 
						{
 | 
				
			||||||
 | 
							gIpLastErr = gSocket.GetLastSocketError();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
							if (gIpLastErr != WSA_IO_PENDING)
 | 
				
			||||||
 | 
							{
 | 
				
			||||||
 | 
								gSocket.GetLastSocketErrorAsString(gIpLastErrStr, elcount(gIpLastErrStr));
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
								writelineex(0, 2, "Tcp send error (%d): %s", gIpLastErr, gIpLastErrStr);
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						else
 | 
				
			||||||
 | 
						{
 | 
				
			||||||
 | 
							writelineex(0, 1, "Tcp data sent successfully!");
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					/**/
 | 
				
			||||||
							
								
								
									
										6006
									
								
								Modbus TCP/socket.cfg
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										6006
									
								
								Modbus TCP/socket.cfg
									
									
									
									
									
										Normal file
									
								
							
										
											
												File diff suppressed because it is too large
												Load diff
											
										
									
								
							
		Loading…
	
		Reference in a new issue