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.lib questions about netwerk communication

ltmini
2011-07-15
2011-07-27
  • ltmini - 2011-07-15

    Dear all,

    I have a few questions about the sysSocket library and about the TCPip example found in the website download area.

    First I will tell you guys what I want to reach.
    I'm trying to setup a communication between a plc (Wago module) and a computer (windows 7 / application). I searched for some while and i found the sysSocket library. I'm not sure for 150% but with this library my goal should be reached, not?

    1) I download and wanted to test the TCPip example (in simulation mode) to get more knowledge about the use of the socket library. But i came with a first disappointment, it didn't work. I couldn't get connection between the Server and Client. If run the program step by step i see the SysSocketOption() gives me a FALSE and the SysSocketBind() also gives me a FALSE, so it doesn't go further. Further debugging doesn't gives me a working example.

    What did I do? Load the project, Set simulation mode on, click online, click run, start the client and the server. IP= 127.0.0.1 port 4444 default settings of the example.

    2) Is working with sockets the best way to transfer some data (not much) over the network? Like I told before, from the PLC to a Computer and visa versa?

    Kind Regards,
    Glenn

     
  • ltmini - 2011-07-27

    HI all,

    I managed to get the socket communication working. Only i run in to a small but important issue.

    When i send some chars to my tcp server (PLC), I split up my received chars. The first char followed by nothing and the next run in the repeat loop the other chars.

    For example:
    I declared a variable "" when I send these chars "+0011-" to my PLC I get the folowing.
    abyRecvRaw[1] = +
    abyRecvRaw[2] = nothing
    ..
    abyRecvRaw[8] = nothing

    the secund repeat gives me this
    abyRecvRaw[1] = 0
    abyRecvRaw[2] = 0
    abyRecvRaw[3] = 1
    abyRecvRaw[4] = 1
    abyRecvRaw[5] = -

    As you can see the send data isn't received well. it should be like this.
    abyRecvRaw[1] = +
    abyRecvRaw[2] = 0
    abyRecvRaw[3] = 0
    abyRecvRaw[4] = 1
    abyRecvRaw[5] = 1
    abyRecvRaw[6] = -

    You can find a peice of code below.
    Before the core parts you find **, so you can easy track the receiving data part.

    If any of you guys have some explaination for it, it should be great.

    Kind Regards,
    Glenn

    Variables

    abyRecvRaw: ARRAY [1..8] OF BYTE;
    
    (*Open listening socket*)
       IF diSocket = SOCKET_INVALID THEN
          diSocket := TcpServerOpenSocket(uiPort, 1);
       ELSE
          (*Wait for a client connection*)
          IF Client.diSocket = SOCKET_INVALID THEN
             Client := TcpServerWaitForConnect(diSocket, 250);
          ELSE
             (*Receive*)
             REPEAT
    **            diReceived:= TcpReceiveData(Client.diSocket, ADR(abyRecvRaw), SIZEOF(abyRecvRaw), 10);
                IF diReceived < 0 THEN (*Problem occured*)
                   CloseSocket(ADR(Client.diSocket));
                   Client.stIPAddress:= '';
                   LifeSignTimeoutTimer(IN:= FALSE);
                ELSIF diReceived > 0 THEN (*Something received*)
                   LifeSignTimeoutTimer(IN:= FALSE);
                   bReceived:= TRUE;
                   IF NOT (diReceived = 1 AND abyRecvRaw[1] = 0) THEN
                      (* Received Protocol data to Protocol_Reader *)
                      abyRecv:= Filter_Protocol_Data(abyRecvRaw);
                      (* Check and Set protocol *)
                      Result := Protocol_Reader(abyRecv);
                   END_IF
                END_IF
             UNTIL
                diReceived <= 0
             END_REPEAT;
    

    TCPReceiveDAta (FUN)

    IF SocketIsReadable(diSocket, diTimeout) THEN
    **       TcpReceiveData:= SysSockRecv(diSocket, pbyData, diDataSize, 0); (* Returns 0 if gracefully closed or -1 for an error*)
       IF TcpReceiveData <= 0 THEN
          TcpReceiveData:= TcpReceiveData - 1;
       END_IF
    END_IF
    
     

    Related

    Talk.ru: 1
    Talk.ru: 2
    Talk.ru: 3
    Talk.ru: 5
    Talk.ru: 8


Log in to post a comment.