汇川AM401的TCP服务端通信

VAR
    x建立连接: BOOL;
    abySendData_0 : ARRAY[0..3] OF BYTE := [76, 79, 78, 13];
    abyRecvData_0 : ARRAY[0..19] OF BYTE;
    TCP_Server_0: TCP_Server;
    TCP_Connect_0: TCP_Connect;
    TCP_Receive_0: TCP_Receive;
    TCP_Send_0: TCP_Send;
    iState: INT;
END_VAR

(* 注意:TCP_Connect_0.hConnection客户端连接上后,只要再断开,必须重新使能一次, 不然下次客户端会连不上*)
(* 功能: 接收到客户端的任意数据后原路返回字符串"LON"  *)

//创建TCP服务器
TCP_Server_0(
    xEnable:= x建立连接 , 
    strIpAddrLocal:= '192.168.1.88', 
    uiPortLocal:= 1000, 
    xDone=> , 
    xBusy=> , 
    xError=> , 
    dwErrorID=> , 
    hServer=> );

//建立连接
TCP_Connect_0(
    xEnable:= TCP_Server_0.hServer<>0, 
    hServer:= TCP_Server_0.hServer, 
    xNotKeepAlive:= , 
    xDone=> , 
    xBusy=> , 
    xError=> , 
    dwErrorID=> , 
    xActive=> , 
    hConnection=> );    

//接收数据
TCP_Receive_0(
    xEnable:= , 
    hConnection:= TCP_Connect_0.hConnection, 
    uiSize:= SIZEOF(abyRecvData_0), 
    pbyData:= ADR(abyRecvData_0), 
    xDone=> , 
    xBusy=> , 
    xError=> , 
    dwErrorID=> , 
    xReady=> , 
    uiCount=> );

//发送数据
TCP_Send_0(
    xExecute:= , 
    hConnection:= TCP_Connect_0.hConnection, 
    uiSize:= SIZEOF(abySendData_0), 
    pbyData:= ADR(abySendData_0), 
    udiTimeOut:= 10000, 
    xDone=> , 
    xBusy=> , 
    xError=> , 
    dwErrorID=> );
    
CASE iState OF
    00 :
        IF TCP_Connect_0.xActive THEN
            iState:=10;
        END_IF;
    10 :
        TCP_Receive_0.xEnable:=TRUE;
        IF TCP_Receive_0.xReady THEN
            iState:=15;
        END_IF
    15 :
        TCP_Receive_0.xEnable:=FALSE;    
        IF NOT TCP_Receive_0.xReady THEN
            iState:=20;
        END_IF
    20 :
        TCP_Send_0.xExecute:=TRUE;
        IF TCP_Send_0.xDone THEN
            iState:=25;
        END_IF
    25 :
        TCP_Send_0.xExecute:=FALSE;    
        IF NOT TCP_Send_0.xDone THEN
            iState:=30;
        END_IF
    30 :
        iState:=0;
END_CASE    
    

 

posted @ 2024-07-31 17:13  CSF践行  阅读(48)  评论(0编辑  收藏  举报