• codesys TCP客户端程序


    CASE nClientStep OF
    	ENUM_tcp_client.nInitial://复位信号
    		TCP_Client.xEnable:=FALSE;
    		TCP_Read.xEnable:=FALSE;
    		TCP_Write.xExecute:=FALSE;
    
    		TcpClientStatus.bConnectDone:=FALSE;
    		TcpClientStatus.bReceiveDone:=FALSE;
    		TcpClientStatus.bSendDone:=FALSE;
    		TcpClientStatus.bCloseDone:=FALSE;
    
    		IF TcpClientControl.bConnect THEN
    			nClientStep:=ENUM_tcp_client.nConnect;
    		END_IF
    		
    	ENUM_tcp_client.nConnect://连接服务器
    		TCP_Client.xEnable:=TRUE;
    		nClientStep:=ENUM_tcp_client.nWhetherConnect;
    
    	ENUM_tcp_client.nWhetherConnect://判断是否连接成功
    		IF TCP_Client.xError THEN
    			nClientStep:=ENUM_tcp_client.nInitial;
    		END_IF
    		
    		IF TCP_Client.xActive AND NOT TCP_Client.xError THEN
    			TcpClientStatus.bConnectDone:=TRUE;
    			nClientStep:=ENUM_tcp_client.nDetermineAction;
    		END_IF
    		
    	ENUM_tcp_client.nDetermineAction://判断动作
    		TcpClientStatus.bSendDone:=FALSE;
    		TcpClientStatus.bReceiveDone:=FALSE;
    		
    		IF TcpClientControl.bClose THEN
    			nClientStep:=ENUM_tcp_client.nInitial;
    		ELSIF TcpClientControl.bSend THEN
    			nClientStep:=ENUM_tcp_client.nSend;
    		ELSE
    			nClientStep:=ENUM_tcp_client.nReceive;
    		END_IF
    		
    	ENUM_tcp_client.nReceive://接收数据
    		TCP_Read.xEnable:=TRUE;
    		nClientStep:=ENUM_tcp_client.nWhetherReceive;
    
    	ENUM_tcp_client.nWhetherReceive://判断是否接收成功
    		TCP_Read.xEnable:=FALSE;
    		IF TCP_Read.xError THEN
    			nClientStep:=ENUM_tcp_client.nInitial;
    		END_IF
    		
    		IF NOT TCP_Read.xBusy AND NOT TCP_Read.xError THEN
    			IF nReceiveBytes>0 THEN
    				TcpClientStatus.bReceiveDone:=TRUE;
    				nClientStep:=ENUM_tcp_client.nDetermineAction;
    			ELSE
    				nClientStep:=ENUM_tcp_client.nDetermineAction;
    			END_IF
    		END_IF
    		
    	ENUM_tcp_client.nSend://发送数据
    		TCP_Write.xExecute:=TRUE;
    		nClientStep:=ENUM_tcp_client.nWhetherSend;
    
    	ENUM_tcp_client.nWhetherSend://判断是否发送成功
    		TCP_Write.xExecute:=FALSE;
    		IF TCP_Write.xError THEN
    			nClientStep:=ENUM_tcp_client.nClose;
    		END_IF
    		
    		IF NOT TCP_Write.xBusy AND NOT TCP_Write.xError THEN
    			TcpClientStatus.bSendDone:=TRUE;
    			nClientStep:=ENUM_tcp_client.nDetermineAction;
    		END_IF
    END_CASE
    
    //功能块
    TCP_Client(
    	xEnable:= , 
    	xDone=> , 
    	xBusy=> , 
    	xError=> , 
    	udiTimeOut:= , 
    	ipAddr:=TcpClientParameter.sIp , 
    	uiPort:=TcpClientParameter.nPort , 
    	eError=> , 
    	xActive=> , 
    	hConnection=>hSocket );
    
    TCP_Read(
    	xEnable:= , 
    	xDone=> , 
    	xBusy=> , 
    	xError=> , 
    	hConnection:=hSocket , 
    	szSize:=TcpClientParameter.nReceiveDataLength , 
    	pData:=ADR(TcpClientParameter.ReceiveData) , 
    	eError=> , 
    	xReady=> , 
    	szCount=>nReceiveBytes );
    
    IF nReceiveBytes>0 THEN
    	TcpClientParameter.ReceiveData.sString:=LEFT(TcpClientParameter.ReceiveData.sString,ULINT_TO_INT(nReceiveBytes));
    	FOR i:=INT_TO_UINT(LEN(TcpClientParameter.ReceiveData.sString)) TO 255 BY 1 DO
    		TcpClientParameter.ReceiveData.nByte[i]:=0;
    	END_FOR
    END_IF
    
    IF TCP_Write.xExecute THEN
    	FOR i:=TcpClientParameter.nSendDataLength TO 255 BY 1 DO
    		TcpClientParameter.SendData.nByte[i]:=0;
    	END_FOR
    END_IF
    
    TCP_Write(
    	xExecute:= , 
    	udiTimeOut:= , 
    	xDone=> , 
    	xBusy=> , 
    	xError=> , 
    	hConnection:=hSocket , 
    	szSize:=TcpClientParameter.nSendDataLength , 
    	pData:=ADR(TcpClientParameter.SendData) , 
    	eError=> );
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    • 117
    • 118
    • 119
    • 120
    • 121
    • 122
    • 123
    • 124
    //远程IP和端口号
    IF tcpClient.parameter.sIp.sAddr='' THEN
    	tcpClient.parameter.sIp.sAddr:='192.168.150.204';
    	tcpClient.parameter.nPort:=55;
    END_IF
    
    tcpClient.parameter.nSendDataLength:=INT_TO_UINT(LEN(tcpClient.parameter.SendData.sString));
    
    //功能块
    tcpClient.FB(
    	TcpClientControl:=tcpClient.control , 
    	TcpClientParameter:=tcpClient.parameter , 
    	TcpClientStatus=>tcpClient.status );
    
    //复位
    IF tcpClient.status.bSendDone THEN
    	tcpClient.control.bSend:=FALSE;
    END_IF
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
  • 相关阅读:
    Error: error:0308010C:digital envelope routines::unsupported
    Redis关闭持久化
    AQS之CountDownLatch分析 (八)
    USB device ‘FTDI Dual RS232-HS‘ with UUID
    python数据结构与算法-07_哈希表
    3D WEB轻量化引擎HOOPS助力3D测量应用蓬勃发展:效率、精度显著提升
    几个简单的JavaScript面试题
    Shell脚本编程(一) —— 变量定义(用户自定义变量、位置变量、预定义变量、环境变量)
    【音视频|ALSA】基于alsa-lib开发ALSA应用层程序--附带源码
    鸿蒙:实现两个Page页面跳转
  • 原文地址:https://blog.csdn.net/weixin_58619062/article/details/126175095