TCP Server

george32
2025-02-20
2025-02-20
  • george32 - 2025-02-20

    Dear people,

    I would like to create a TCP server in my PLC program. The TCP server needs to communicate with an external device. I have been searching the internet and found that the libraries Syssocket or CAA NetBaseSrv are two possible solutions to create a TCP server. From what I have read, CAA NetBaseSrv is more user-friendly to implement than the Syssocket variant.

    However, I am still struggling to actually implement this library. Foremost is my little knowledge about creating a TCP server and how to read/understand each function block, especially the outputs of some blocks and the types, e.g. server has an output CAA.Handle, but I cannot find the explanation of CAA.Handle. I tried to find a good document that can help me understand how to implement a TCP server, but I could not find it.

    Thank you in advance,
    George

     
  • george32 - 2025-02-20

    Thank you for your advice Rinie,

    I have tried to follow this example but found one problem.When I disconnect with Hercules the system does not acknowledge the lost of its connection this could result in critical errors.

    Because of this error I am trying to implement the Syssocket library. With this library I am having a problem which I could not solve.

    I have the following code:

    ip_adres : STRING :='0.0.0.0';
    port : WORD := 5000;    
    iStep : INT := 1;
    
    
    // Socket information
    
    
    Socket_handler : SysSocket_Interfaces.RTS_IEC_HANDLE; 
    Socket_handler_result : SysSocket_Interfaces.RTS_IEC_RESULT;
    
    
    Socket_adress : SysSocket_Interfaces.SOCKADDRESS;
    
    Addres_Convert : SysSocket_Implementation.RTS_IEC_RESULT; 
    
    // Bind information
    Bind_handler : SysSocket_Interfaces.RTS_IEC_RESULT;
    
    // Listener Information
    Listener_handler : SysSocket_Interfaces.RTS_IEC_RESULT;
    
    
    
    
    Addres_Convert := SysSocket_Implementation.SysSockInetAddr(szIPAddress:= ip_adres,pInAddr:= ADR(Socket_adress.sin_addr)); 
    Socket_adress.sin_family:= SysSocket_Interfaces.SOCKET_AF_INET; 
    Socket_adress.sin_port := SysSocket_Implementation.SysSockHtons(usHost:= port);
    
    CASE iStep OF
        1: 
        // Create Socket
        Socket_handler :=SysSockCreate(
            iAddressFamily :=SysSocket_Interfaces.SOCKET_AF_INET,
            diType:=SysSocket_Interfaces.SOCKET_STREAM,
            diProtocol:=SysSocket_Interfaces.SOCKET_IPPROTO_TCP,
            pResult:=ADR(Socket_handler_result)); 
        IF Socket_handler = SysSocket_Interfaces.RTS_INVALID_HANDLE THEN
                iStep := 1;
            ELSE
                iStep := 2;
            END_IF
    
        2:
            Bind_handler := SysSocket_Implementation.SysSockBind(
                                hSocket:=Socket_handler,
                                pSockAddr :=ADR(Socket_adress),
                                diSockAddrSize:=SIZEOF(Socket_adress) );
    
    
    
        3: 
            Listener_handler := SysSocket_Implementation.SysSockListen(
                                    hSocket :=Socket_handler,
                                    diMaxConnections :=1); 
    
    
    
    END_CASE
    

    When I am trying to get this working, the SysSockBind function gives a value of 16#207 which means adress already in use. I find this weird because of the IP-adress of 0.0.0.0. I think I am converting the Ip-adress in the wrong way, but the code does not give a error arround this. So therefore I cannot find the solution to this problem. I hope someone could help me with this problem.

    Thanks in advance
    George

     
  • rmaas - 2025-02-20

    Hi George,

    I do not know what the problem is with the syssocket implementation, but when i read the forums it seems the Net Base Services library is the recommended way to go.

    There is another more extensive example here:
    https://forge.codesys.com/prj/codesys-example/plc-chat-nbs/home/Home/

    I have just tested this example and it works very well, also with closing and opening the connection from hercules...

    good luck!

     

Log in to post a comment.