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

CAA NBS - NTP request

e.kislov
2017-10-12
2022-05-09
  • e.kislov - 2017-10-12

    Hi. I try to use CAA Net Base Servises (3.5.5.0) to get current time from NTP-server. My Device - CODESYS Control Win V3 (3.5.5.0).

    NTP-server: 129.6.15.28 (time-a.nist.gov)
    http://tf.nist.gov/tf-cgi/servers.cgi

    I take example of NTP-request (0x1B and 47 NULL bytes) from this:
    https://stackoverflow.com/questions/119 ... er-using-c

    I test it with Hercules utility, and it works:

    Now I want to make this with CODESYS Control Win V3 - and I can't get response from server.
    And no errors in all NBS fb's.

    Maybe somebody will be able to found my mistakes and describe it.
    I will be really grateful.

    My code:

       iStep:            INT;
       fbUdpPeer:          NBS.UDP_Peer;
       hPeer:             NBS.CAA.HANDLE;
       fbUdpSend:          NBS.UDP_Send;
       fbUdpReceive:          NBS.UDP_Receive;
       stNtpAddress:         NBS.IP_ADDR;
       
       abyNtpRequest:       ARRAY [0..47] OF BYTE:=[16#1B, 47(0)]; 
       abyNtpResponse:      ARRAY [0..47] OF BYTE;
       
       fbSynchroPeriod:      TON;
       fbTimeout:          TON;
    
    fbSynchroPeriod
    (
       IN:=NOT(fbSynchroPeriod.Q),
       PT:=T#15S
    );
    stNtpAddress.sAddr:='129.6.15.28';
    CASE iStep OF
       
    0:
       fbUdpPeer
       (
          xEnable      :=   TRUE, 
          ipAddr      :=      ,
          uiPort      :=   4023,
          ipMultiCast   :=       ,
          hPeer      =>   hPeer
       );
       IF fbUdpPeer.xActive THEN
          iStep:=10;
       END_IF
       
       
    10:
       fbTimeout(IN:=FALSE);
       fbUdpSend
       (
          xExecute   :=    fbSynchroPeriod.Q, 
          udiTimeOut   :=    ,  
          hPeer      :=    hPeer, 
          ipAddr      :=    stNtpAddress, 
          uiPort      :=    123,  
          pData      :=    ADR(abyNtpRequest), 
          szSize      :=    SIZEOF(abyNtpRequest)
       );   
       
       IF fbUdpSend.xDone THEN
          fbUdpSend(xExecute:=FALSE);
          iStep:=20;
       END_IF
       
       
       20:
       
       fbTimeout(IN:=TRUE, PT:=T#6S);
       
       fbUdpReceive(
          xEnable   :=   TRUE ,  
          hPeer   :=   hPeer , 
          pData   :=   ADR(abyNtpResponse) ,         // fixed
          szSize   :=   SIZEOF(abyNtpResponse) ,       // fixed
        );
       IF fbUdpReceive.xReady THEN
          iStep:=10;
       END_IF
       
       IF fbTimeout.Q THEN
          iStep:=10;
       END_IF
       
    END_CASE
    

    IMG: Bild

     
  • e.kislov - 2017-10-13

    I found my mistake and edit first post. Now it works.

     
  • necati - 2022-05-09

    Is above code working as stable?

     

Log in to post a comment.