jari-koivuluoma - 14 hours ago

I have a need to send messages between 2 PLCs and I cant use network variables (because of size limit) so I tried writing this simple test program.

This seems to work fine. I can send messages back and forth when a first "Start server" and then "Connect client". See the attached image.

However, if I disconnect the client by setting ClientConnect to false and try to re-connect then the TCP_Client just gives me TIMEOUT error.

When I stop and start the server again, then Im able to reconnect.

How is this supposed to work? Why reconnecting wont work. There is not other way of disconnecting the client than setting xEnable of the TCP_Client to false.

This is just a testing program and I will try it on 2 seperate devices once this works.

PROGRAM PLC_PRG
VAR CONSTANT
    mySize : UDINT := 255;
END_VAR
VAR
    Server: NBS.TCP_Server;
    ServerIpStr: STRING := '127.0.0.1';
    ServerIP: NBS.IP_ADDR;
    ServerPort: UINT := 50000;

    ServerConnection: NBS.TCP_Connection;

    Client: NBS.TCP_Client;

    ServerRead: NBS.TCP_Read;
    ServerWrite: NBS.TCP_Write;
    ServerSend: STRING(mySize);
    ServerReceive: STRING(mySize);
    ServerConnect: BOOL;
    bServerSend: BOOL;
    IP: NBS.INADDR;
    ConnectedClientIP: STRING;

    ClientPort: UINT := 50000;
    ClientIpStr: STRING := '192.168.1.49';
    ClientIP: NBS.IP_ADDR;

    ClientRead: NBS.TCP_Read;
    ClientWrite: NBS.TCP_Write;
    ClientSend: STRING(mySize);
    ClientReceive: STRING(mySize);
    ClientConnect: BOOL;
    bClientSend: BOOL;

    Error: BOOL;
    Message: BOOL;
END_VAR
// Server

ServerIP.sAddr := ServerIpStr;
Server(
    ipAddr := ServerIP,
    uiPort := ServerPort
);

ServerConnection(
    xEnable := Server.xEnable,
    hServer := Server.hServer,
);

IP := ServerConnection.IPAddress;

ConnectedClientIP := F_Concat7(
                        BYTE_TO_STRING(IP.S_un_b.s_b1),'.',
                        BYTE_TO_STRING(IP.S_un_b.s_b2),'.',
                        BYTE_TO_STRING(IP.S_un_b.s_b3),'.',
                        BYTE_TO_STRING(IP.S_un_b.s_b4));

ServerRead(
    xEnable := ServerConnection.xActive,
    hConnection := ServerConnection.hConnection,
    szSize := SIZEOF(ServerReceive),
    pData := ADR(ServerReceive)
);

IF ServerRead.xReady AND ServerRead.szCount > 0 THEN
    Message := TRUE;
END_IF

IF ServerRead.eError > 0 THEN
    Error := TRUE;
END_IF

ServerWrite(
    xExecute := ServerConnection.xActive AND bServerSend,
    hConnection := ServerConnection.hConnection,
    szSize := LEN(ServerSend)+1,
    pData := ADR(ServerSend)
);

IF ServerWrite.xDone THEN
    bServerSend := FALSE;
END_IF

IF ServerWrite.eError > 0 THEN
    Error := TRUE;
END_IF

// Client
ClientIP.sAddr := ClientIpStr;
Client(
    xEnable := ClientConnect,
    ipAddr := ClientIP,
    uiPort := ServerPort,
    udiTimeOut := 10000000
);

ClientRead(
    xEnable := Client.xActive,
    hConnection := Client.hConnection,
    szSize := SIZEOF(ClientReceive),
    pData := ADR(ClientReceive)
);

IF ClientRead.xReady AND ClientRead.szCount > 0 THEN
    Message := TRUE;
END_IF

IF ClientRead.eError > 0 THEN
    Error := TRUE;
END_IF

ClientWrite(
    xExecute := Client.xActive AND bClientSend,
    hConnection := Client.hConnection,
    szSize := LEN(ClientSend)+1,
    pData := ADR(ClientSend)
);

IF ClientWrite.xDone THEN
    bClientSend := FALSE;
END_IF

IF ClientWrite.eError > 0 THEN
    Error := TRUE;
END_IF
 

Last edit: jari-koivuluoma 14 hours ago