This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

Sending time information inside the udp packets

Hi ,

There is a function for sending time information ??

I would like to send in each packets an information about the time of sending for making easier for the receiver recognize the correct order of the packets..

There is a function that it makes possible to insert the value of the time inside the buffer ?

  • Hi ,

    How can I add timestamp on the udp packets ?

    I can put timestamp in the data field ?

    There is the somebody that he tried to put it..

    Best Regards

    Luca Rossini

  • Hi Luca,

    You can add timestamp as part of the UDP payload (in data buffer) and send it. Please check the sample UDP examples in the SDK. In the example we are sending dummy bytes. Instead of these dummy bytes you can send these time stamps.

    Regards,
    Raghavendra
  • Hi ,

    I can do this using the library time.h ??

    I am seeing but for example if I want to use

    _CODE_ACCESS time_t mktime(struct tm *_tptr);

    and I have put it before the calling to udpClient..

    In the buffer I put

    for (iCounter=BUF_SIZE/2; iCounter< (3*BUF_SIZE/4) ; iCounter++)
    {

    uBuf.BsdBuf[iCounter] = (char)(tm.tm_sec);
    UART_PRINT("The value in the buffer is BsdBuf[%d]=%d \n\r",iCounter,uBuf.BsdBuf[iCounter]);
    }

    but i have an error..

    How can I modify this ???

    thanks for your help


    Regards

    Luca Rossini
  • Hi,

    Depends on what time you would want to use. You can use the time from SNTP server. You can see the get_time example.
    Or you can use the SlowClockCounter. More about RTC (SlowClockCounter) is discussed here (e2e.ti.com/.../1277990).

    Regards,
    Raghavendra
  • Hi ,

    I need to insert a time of sending of each packets.. so a very precise time..

    I can use it ?
  • Hi,

    Yes, you can use RTC for that.

    Regards,
    Raghavendra
  • how can use the SNTP server without connection ??
  • Hi,

    You would need a connection for that. Please check the get_time example from the SDK and go through some articles/documents on SNTP.
    If you don't need a connection then use RTC (SlowClockCounter).

    Regards,
    Raghavendra
  • Hi ,

    i am using wifi-direct and I can't use get-time example for it.. right ??

    Without the connection I can use only RTC ??
  • Hi ,

    I need an interrupt to use RTC ?

    I don't understand well how I can use this slowclockcounter..
    I should call a function each time that I want to send one packets ??

    Sorry for the continuing questions ..

    Regards
    Luca
  • Hi Luca,

    Yes, please use RTC for this. Please take a look at prcm.c and prcm.h in driverlib in the SDK.

    In general, to initialize, you would need to call PRCMRTCInUseSet. And to get the time periodically, you would need to call PRCMRTCGet.

    Regards,
    Raghavendra
  • Hi ,

    I haven't found the functions that you said..
    I can use this for enable RTC

    unsigned long PRCMSysResetCauseGet() ??

    And for the time ?

    ! Gets slow clock counter match value.
    //!
    //! This function gets the match value for slow clock counter. This is use
    //! to interrupt the processor when RTC counts to the specified value.
    //!
    //! \return None.
    //
    //*****************************************************************************
    unsigned long long PRCMSlowClkCtrMatchGet()
    {
    unsigned long long ullValue;

    //
    // Get RTC match value
    //
    ullValue = PRCMHIBRegRead(HIB3P3_BASE + HIB3P3_O_MEM_HIB_RTC_IRQ_MSW_CONF);
    ullValue = ullValue<<32;
    ullValue |= PRCMHIBRegRead(HIB3P3_BASE + HIB3P3_O_MEM_HIB_RTC_IRQ_LSW_CONF);

    //
    // Return the value
    //
    return ullValue;
    }


    This ?

    or

    //*****************************************************************************
    //
    //! Get the instantaneous calendar time from the device.
    //!
    //! \param ulSecs refers to the seconds part of the calendar time
    //! \param usMsec refers to the fractional (ms) part of the second
    //!
    //! This function fetches the instantaneous value of the ticking calendar time
    //! from the device. The calendar time is outlined in terms of seconds and
    //! milliseconds.
    //!
    //! The device provides the calendar value that has been maintained across
    //! active and low power states.
    //!
    //! The function PRCMRTCSet() must have been invoked once to set a reference.
    //!
    //! \return None.
    //
    //*****************************************************************************
    void PRCMRTCGet(unsigned long *ulSecs, unsigned short *usMsec)
    {
    unsigned long long ullMsec = 0;

    if(IS_RTC_USED()) {
    ullMsec = RTC_U64MSEC_MK(RTC_U32SECS_REG_RD(),
    RTC_U16MSEC_REG_RD());
    ullMsec += SCC_U64MSEC_GET();
    }

    *ulSecs = RTC_SECS_IN_U64MSEC(ullMsec);
    *usMsec = RTC_MSEC_IN_U64MSEC(ullMsec);

    return;
    }


    In this second casa the return of the function is void.. I haven't found your prototype but I am in prcm.c like you said..

    What is your advice ??

    Best Regards

    Luca Rossini
  • Hi,

    The function PRCMRTCInUseSet should be present in prcm.c. Please re-check it.
    PRCMRTCGet returns void but the values are stored in the pointers passed to it.

    Regards,
    Raghavendra
  • I have enabled the RTC with the function :

    PRCMRTCInUseSet();

    I put :

    unsigned long *secs;
    unsigned short *msecs;

    I put it before calling the Client UDP..

    After inside the client udp I put

    while (lLoopCount<UDP_PACKET_COUNT )
    {
    // sending packet

    PRCMRTCMatchGet(&secs, &msecs);

    uBuf.BsdBuf[iCounter] = (char)(number);
    UART_PRINT("The value in the buffer is BsdBuf[%d]=%d \n\r",iCounter,uBuf.BsdBuf[iCounter]);

    for (iCounter=1 ; iCounter<BUF_SIZE ; iCounter++)
    {
    uBuf.BsdBuf[iCounter] = (char)(*msecs);
    UART_PRINT("The value in the buffer is BsdBuf[%d]=%d \n\r",iCounter,uBuf.BsdBuf[iCounter]);
    }

    iStatus = sl_SendTo(iSockID, uBuf.BsdBuf, sTestBufLen, 0,
    (SlSockAddr_t *)&sAddr, iAddrSize);


    if( iStatus <= 0 )
    {
    // error
    sl_Close(iSockID);
    ASSERT_ON_ERROR(-1);

    }
    lLoopCount++;
    number++;
    }

    Number is only in the first byte like sequence number of the packets.. But I can't see the time.. I have always zero..
    Maybe one byte for the time is few ?

    Or maybe I am doing some mistake with the pointers..
    What do you advice ??

    Best Regards

    Luca Rossini
  • Hi ,

    I modified the calling of the function

    PRCMRTCGet(secs, msecs);

    Because I had defined secs and msecs as a pointer.. So for the address I don't need " &".
    I see always zero in the buffer..
    It is correct to take the result of the time with " *secs " .

    uBuf.BsdBuf[iCounter] = *secs;

    Where is the mistake for you ?

    Secs and msecs are unsigned long.. How Can I put inside the buffer ?
    Must I activate a conversion or something ?
    For example I can separate in parties the variable secs ( I don't know if it is correct..but for example I have the buffer with dimension 32 byte.. How Can I use 16 byte for the variable secs ?? )

    The general Question is how can I put inside the buffer the information about the time ??

    Thanks for your help

    Best Regards

    Luca Rossini
  • Hi to everybody ,

    I'm trying to put inside the data field of my Udp packets a timestamp..

    I'm using RTC for this with PRCMRTCInUseSet ( to  initialize) and when I am sending the packets ( before calling sendto) I call this PRCMRTCGet.

    I have enabled the RTC with the function :

    PRCMRTCInUseSet(); 


    I put it before calling the Client UDP..

    I put :

    unsigned long *secs;
    unsigned short *msecs;




    while (lLoopCount<UDP_PACKET_COUNT )
    {
    // sending packet

    PRCMRTCMatchGet(secs, msecs);

    for (iCounter=0 ; iCounter<8 ; iCounter++)
    {
    uBuf.BsdBuf[iCounter] = (number);
    UART_PRINT("The value in the buffer is BsdBuf[%d]=%d \n\r",iCounter,uBuf.BsdBuf[iCounter]);

    }

    for (iCounter=16 ; iCounter<BUF_SIZE/2 ; iCounter++)
    {
    uBuf.BsdBuf[iCounter] = *secs;
    UART_PRINT("The value in the buffer is BsdBuf[%d]=%d \n\r",iCounter,uBuf.BsdBuf[iCounter]);
    }

    for (iCounter=16 ; iCounter<BUF_SIZE ; iCounter++)
    {
    uBuf.BsdBuf[iCounter] = *msecs;
    UART_PRINT("The value in the buffer is BsdBuf[%d]=%d \n\r",iCounter,uBuf.BsdBuf[iCounter]);
    }



    iStatus = sl_SendTo(iSockID, uBuf.BsdBuf, sTestBufLen, 0,
    (SlSockAddr_t *)&sAddr, iAddrSize);


    if( iStatus <= 0 )
    {
    // error
    sl_Close(iSockID);
    ASSERT_ON_ERROR(-1);

    }
    lLoopCount++;
    number++;
    }


    Number is only for the first 8 bytes like sequence number of the packets.. But I can't see the time.. I have always zero..

    Where is the mistake for you ?

    Secs and msecs are unsigned long.. How Can I put inside the buffer ?
    Must I activate a conversion or something ?
    For example I can separate in parties the variable secs ( I don't know if it is correct..but for example I have the buffer with dimension 32 byte.. How Can I use 16 byte for the variable secs ?? )

    The general Question is how can I put inside the buffer the information about the time ??

    Thanks for your help

    Best Regards

    Luca Rossini

  • Hi ,

    I tried in this mode for filling the buffer ( the first buffer is for the number of packets , the second buffer is for the seconds and the third is for the milliseconds )

    while (lLoopCount<UDP_PACKET_COUNT )
    {
    // sending packet



    PRCMRTCGet(&secs, &msecs);

    const int num = snprintf(NULL, 0, "%lu", number);

    char number_cBsdBuf[num+1];
    int z = snprintf(number_cBsdBuf, 2, "%lu", number);

    const int n = snprintf(NULL, 0, "%lu", secs);

    char s_cBsdBuf[n+1];
    int c = snprintf(s_cBsdBuf, n+1, "%lu", secs);

    const int d = snprintf(NULL, 0, "%lu", msecs);

    char ms_cBsdBuf[d+1];
    int h = snprintf(ms_cBsdBuf, d+1, "%lu", msecs);

    char g_cBsdBuf[n+d+2+2];


    memcpy(g_cBsdBuf, number_cBsdBuf, num+1);
    memcpy(&g_cBsdBuf[num+1], s_cBsdBuf, n+1);
    memcpy(&g_cBsdBuf[n+num+2], ms_cBsdBuf, d+1);


    iStatus = sl_SendTo(iSockID, g_cBsdBuf, n+d+2, 0,
    (SlSockAddr_t *)&sAddr, iAddrSize);



    if( iStatus <= 0 )
    {
    // error
    sl_Close(iSockID);
    ASSERT_ON_ERROR(-1);

    }
    lLoopCount++;
    number++;
    }

    What do you think ??

    It is correct for you for the buffer ??

    I think that I am doing some mistake because I receive always the same valors for each packets..

    I called in wrong mode the function PRCMRTCGet ?
  • Hi,

    Try using only the PRCMRTC APIs first in a different project.

    ===
    PRCMRTCInUseSet();

    unsigned long ulSecs;
    unsigned short usMsec;

    while(1)
    {
    PRCMRTCGet(&ulSecs, &usMsec);
    Report("Seconds %ld MSecs %d \n\r", ulSecs, usMsec);

    //Random delay
    MAP_UtilsDelay(8000000);
    }
    ===

    After you see the values changing, you can integrate this portion into the UDP application.

    Regards,
    Raghavendra

  • Hi ,

    Before I tried but I can't see something that increases.. I can see strange numbers..

    Look at the attached file ( ok the number changes but How can I read the values ? )

    Best Regards

    Lucavalori_tera term.pdf

  • Hi ,

    I have seen that you added one delay ( 5 seconds).. It is a big delay.. Maybe the clock is not enough fast for having information about udp packets.. It can be correct ??
    I think that for sending udp packets The board need not a lof of time...
    For you I can use for my application this ??
  • In the easy code the function for the time is ok but here I have problem.
    Do you any idea ?? I haven't filled well the buffer ??

    I put the enable for the RTC
    int main()
    {
    long lRetVal = -1;

    //
    // Initialize Board configurations
    //
    BoardInit();


    //
    // Pinmuxing for GPIO, UART
    //
    PinMuxConfig();

    // ENABLE RTC CLOCK FOR TIMESTAMP
    PRCMRTCInUseSet();





    -I inserted inside the calling PRCMRTCGet(&ulSecs, &usMsec);

    int BsdUdpClient(unsigned short usPort)
    {
    // int iCounter ;
    // short sTestBufLen;
    int BUF_DIM;
    SlSockAddrIn_t sAddr;

    int iAddrSize;
    int iSockID;
    int iStatus;
    long lLoopCount = 0;
    unsigned long ulSecs;
    unsigned short usMsec;
    long number = 0 ;

    // filling the buffer


    //sTestBufLen = BUF_SIZE;


    //filling the UDP server socket address
    sAddr.sin_family = SL_AF_INET;
    sAddr.sin_port = sl_Htons((unsigned short)5001);
    sAddr.sin_addr.s_addr = sl_Htonl((unsigned int)0xC0A83101);

    iAddrSize = sizeof(SlSockAddrIn_t);

    // creating a UDP socket
    GPIO_IF_LedOn(MCU_RED_LED_GPIO);
    iSockID = sl_Socket(SL_AF_INET,SL_SOCK_DGRAM, 0);
    GPIO_IF_LedOff(MCU_RED_LED_GPIO);
    if( iSockID < 0 )
    {
    // error
    ASSERT_ON_ERROR(-1);
    }

    // for a UDP connection connect is not required

    while (lLoopCount<UDP_PACKET_COUNT )
    {
    // sending packet




    // calling function for getting information about time ( seconds and milliseconds)
    PRCMRTCGet(&ulSecs, &usMsec);

    UART_PRINT("Seconds %ld MSecs %d \n\r", ulSecs, usMsec);

    //delay 5 seconds
    // MAP_UtilsDelay(8000000);

    // first buffer di character for taking the sequence number of packets..there is
    // a variable number inside the while

    const int num = snprintf(NULL, 0, "%lu", number);
    char number_cBsdBuf[num+1];
    int z = snprintf(number_cBsdBuf, num+1, "%lu", number);

    // second buffer for taking information about seconds.. Before filling the buffer I convert
    // the variable secs ( unsigned long ) to a string.. So now I can know the dimension of the buffer

    const int n = snprintf(NULL, 0, "%lu", ulSecs);
    char s_cBsdBuf[n+1];
    int c = snprintf(s_cBsdBuf, n+1, "%lu", ulSecs);

    // third buffer for taking information about milliseconds

    const int d = snprintf(NULL, 0, "%lu", usMsec);
    char ms_cBsdBuf[d+1];
    int h = snprintf(ms_cBsdBuf, d+1, "%lu", usMsec);

    /// buffer that I want to set.. I put together sequence number , seconds and milliseconds informations


    BUF_DIM = n+d+num+3;
    char g_cBsdBuf[BUF_DIM];


    memcpy(g_cBsdBuf, number_cBsdBuf, num+1);
    memcpy(&g_cBsdBuf[num+1], s_cBsdBuf, n+1);
    memcpy(&g_cBsdBuf[n+num+2], ms_cBsdBuf, d+1);


    iStatus = sl_SendTo(iSockID, g_cBsdBuf, BUF_DIM, 0,
    (SlSockAddr_t *)&sAddr, iAddrSize);

    if( iStatus <= 0 )
    {
    // error
    sl_Close(iSockID);
    ASSERT_ON_ERROR(-1);

    }
    lLoopCount++;
    number++;
    }

    UART_PRINT("Sent %u packets successfully\n\r",lLoopCount);

    //closing the socket after sending 1000 packets
    sl_Close(iSockID);

    return SUCCESS;
    }
  • one call function.pdfHi ,

    I called only one time the function but I have lot of datas of seconds and milliseconds( look at the attache file ).

    Why with only one calling of function I have a lot of datas ?

    It is normal ? I want only one data of time ( sec and millisec stop )..

    Advice for this ??

  • Hi,

    That is a random delay. That was to show that RTC was indeed showing the updated ticks. Please use this as per your application needs.
    UDP packets are not related to these RTC clock ticks.

    Regards,
    Raghavendra

  • Hi ,

    if i decrease the interval packet ( 5 ms between each packet ) this function is not so precise.. I have some mistakes in the time information...
    Is there a function more precise ??