Welcome to our new forum
All users of the legacy CODESYS Forums, please create a new account at account.codesys.com. But make sure to use the same E-Mail address as in the old Forum. Then your posts will be matched. Close

SysSocket TCP server/client

JeloneK
2016-08-02
2018-08-17
  • JeloneK - 2016-08-02

    Hello Guys!

    I'm trying to communicate my robot controler with PLC using TCP server/client.

    First what i want to do, is write correct program on PLC, and test it using Herkules program (http://hercules-setup.soft32.com)

    In case PLC as server, my code:

    PROGRAM PLC_PRG
    VAR
            sa: SOCKADDRESS;
       saSize: DINT;
       port: WORD;
       hSocket, hClientSocket :UDINT;
       maxConnections: DINT := 10;
       result: UDINT;
       str: STRING:= 'Hello, Client!';
       str2: STRING(255);
       numberOfBytes: DINT;
       
    END_VAR
    
    hSocket := SysSockCreate(SOCKET_AF_INET, SOCKET_STREAM, SOCKET_IPPROTO_TCP, ADR(result));
    sa.sin_family := SOCKET_AF_INET;
    sa.sin_addr.ulAddr := SOCKET_INADDR_ANY;
    sa.sin_port := 999;
    result := SysSockBind(hSocket, ADR(sa), SIZEOF(sa));
    result := SysSockListen(hSocket, maxConnections);
    hClientSocket := SysSockAccept(hSocket, ADR(sa), ADR(saSize), ADR(result));
    //SysSockSend(hClientSocket, ADR(str), len(str) +1, 0, ADR(result));
    //str2:= '';
    //SysSockRecv(hClientSocket, ADR(str2), 256, 0, ADR(result));
    //SysSockClose(hClientSocket);
    //SysSockClose(hSocket);
    

    I got exception.
    And it doesnt work.
    I even cannot connect (using herkules).
    I just put in herkules 'tcp client' IP adress -> PLC IP, and port 999 (is it right?)
    And i cannot connect, and it doesnt ping.

    What am i doing wrong ?

    This is result:

    What does the "green C" mean ?

    IMG: Bild

     
  • Moreno - 2016-09-01

    1) the IP address and port must be converted to network format with SysSockHton* functions:
    sa.sin_addr.ulAddr := SysSockHtonl(SOCKET_INADDR_ANY);
    sa.sin_port := SysSockHtons(999);

    2) saSize must be initialized before performing the SysSockAccept:
    In the sa structure will be copied the information of the connected client, so SysSockAccept() must knows the size of the structure passed.
    saSize: = SIZEOF(sa);
    hClientSocket := SysSockAccept(hSocket, ADR(sa), ADR(saSize), ADR(result));

    3) Disable the task watchdog because SysSockAccept is blocking; by default sockets are blocking when created.
    The caller task is 'frozen' until someone connects.
    Otherwise to prevent the watchdog I suggest you to use the SysSockSelect to check if someone has requested a connection and only then use the SysSockAccept.

    The "green C" I think means Constant variable.

     
  • TimvH

    TimvH - 2016-09-02

    Maybe an option is to use the Use Case library: Network.
    It has a much friendlier interace than the syssocket library.

    See the store for examples.

     
  • aso3964 - 2016-12-18

    Moreno hat geschrieben:
    1) the IP address and port must be converted to network format with SysSockHton* functions:
    sa.sin_addr.ulAddr := SysSockHtonl(SOCKET_INADDR_ANY);
    sa.sin_port := SysSockHtons(999);
    2) saSize must be initialized before performing the SysSockAccept:
    In the sa structure will be copied the information of the connected client, so SysSockAccept() must knows the size of the structure passed.
    saSize: = SIZEOF(sa);
    hClientSocket := SysSockAccept(hSocket, ADR(sa), ADR(saSize), ADR(result));
    3) Disable the task watchdog because SysSockAccept is blocking; by default sockets are blocking when created.
    The caller task is 'frozen' until someone connects.
    Otherwise to prevent the watchdog I suggest you to use the SysSockSelect to check if someone has requested a connection and only then use the SysSockAccept.
    The "green C" I think means Constant variable.

    Dear Moreno

    Do you have a complete example of a tcp socket server for reading and writing?

     
  • Anonymous - 2017-01-24

    Originally created by: KevinR

    As TimvH said, please try out the CAA NetBaseSrv "CAA Net Base Services" Library.
    You will see - it's much more easier and it's also well documented. You can get it to work in a short time!

    The HTTP Client example (http://store.codesys.com/http-client-example.html

    Best Regards,
    Kevin

     
  • danyamian - 2018-01-29

    aso3964 hat geschrieben:
    Dear Moreno
    Do you have a complete example of a tcp socket server for reading and writing?

    I'm working on a similar project, does anyone have a complete project where a TCP server is configured using Sockets?

    Thank you

     
  • dFx

    dFx - 2018-03-02

    If you hardware supports CAA NetBaseServices Library, then this may help :
    l viewtopic.php?f=11&t=8641 l

     
  • zorgoz - 2018-08-14

    danyamian hat geschrieben:
    I'm working on a similar project, does anyone have a complete project where a TCP server is configured using Sockets?

    I am walking in the same shoes now, have you got any usable resources in this topic? If so, could you share?

     

Log in to post a comment.