如何在linux中创建高分辨率计时器

时间:2014-02-25 18:13:57

标签: c linux timer

char buf[256];
char buf2[30];


SOCKET sock;
SOCKET fd;
uint16 port = 18017;





VOID CALLBACK TASK1(Task2ms_Raster)
{

    uint32 startTime, stopTime; 
    startTime= GetStopWatch();

    /* Trigger DAQ for the 2ms XCP raster. */
    if( XCPEVENT_DAQ_OVERLOAD & Xcp_DoDaqForEvent_2msRstr( ))
    {
        ++numDaqOverload2ms;
    }

    /* Update those variables which are modified every 2ms. */
 counter32 += slope32;

    /* Trigger STIM for the 2ms XCP raster. */
  if( enableBypass2ms )
   {
        if( XCPEVENT_MISSING_DTO & Xcp_DoStimForEvent_2msRstr( ) )
        {
     ++numMissingDto2ms;
        }
        }
     stopTime = GetStopWatch(); 
   duration10ms = (uint32)(stopTime - startTime );
}

VOID CALLBACK TASK2(Task10ms_Raster)
{
    uint32 startTime, stopTime;
    startTime = GetStopWatch();
    //printf("hi1\n");

    /* Trigger DAQ for the 10ms XCP raster. */
    if( XCPEVENT_DAQ_OVERLOAD & Xcp_DoDaqForEvent_10msRstr( ))
    {
        ++numDaqOverload10ms;
    }

    /* Update those variables which are modified every 10ms. */
    counter16 += slope16;

    /* Trigger STIM for the 10ms XCP raster. */
    if( enableBypass10ms )
    {
        if( XCPEVENT_MISSING_DTO & Xcp_DoStimForEvent_10msRstr( ) )
        {
            ++numMissingDto10ms;
        }
    }

    /* Perform XCP command processing. */
    //Xcp_CmdProcessor();
   stopTime = GetStopWatch(); 
   duration10ms = (uint32)(stopTime - startTime );
}


VOID CALLBACK TASK3(Task100ms_Raster)
{
    uint32 startTime, stopTime;
    startTime = GetStopWatch();

    /* Trigger DAQ for the 100ms XCP raster. */
    if( XCPEVENT_DAQ_OVERLOAD & Xcp_DoDaqForEvent_100msRstr( ))
    {
        ++numDaqOverload100ms;
    }

    /* Update those variables which are modified every 100ms. */
    counter8 += slope8;


    /* Trigger STIM for the 100ms XCP raster. */
    if( enableBypass100ms )
    {
        if( XCPEVENT_MISSING_DTO & Xcp_DoStimForEvent_100msRstr( ) )
        {
            ++numMissingDto100ms;
        }
    }

    stopTime = GetStopWatch(); 
    Xcp_CmdProcessor(); 
   duration100ms = (uint32)(stopTime - startTime );
}




HANDLE TimerTask(unsigned int period,WAITORTIMERCALLBACK task,void* arg)  
{


    HANDLE HTimer =NULL;

    //set the timer to call the timer routine in 2ms
    if(!CreateTimerQueueTimer( &HTimer, NULL, (WAITORTIMERCALLBACK)task,(PVOID) arg, 0, period*1000, 0))
    {
        printf("CreateTimerQueueTimer failed (%d)\n", GetLastError());
        return NULL;
    }


    return HTimer;
}


int startWinsock(void)

{

  WSADATA wsa;

  return WSAStartup(MAKEWORD(2,0),&wsa);

}


int CreateSocket()
{
struct sockaddr_in server, client;  // creating a socket address structure: structure contains ip address and port number
WORD wVersionRequested;
WSADATA wsaData;
long rc, len;
double Task2ms_Raster, Task10ms_Raster, Task100ms_Raster ;
HANDLE h1, h2, h3;
//u_long iMode = 1;
//int sockoptval = 1;


rc=startWinsock();

 if(rc!=0)

  {

    printf("Fehler: startWinsock, fehler code: %d\n",rc);

    return 1;

  }

  else

  {

    printf("Winsock gestartet!\n");

  }

    // create socket
    sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
    if (sock ==INVALID_SOCKET)    {
        printf("Could not Create Socket\n");
        return 1;

  }

  else

  {

    printf("Socket created\n");

  }





    // create socket address of the server
    memset( &server, 0, sizeof(server));


    // IPv4 - connection
    server.sin_family = AF_INET;
    // accept connections from any ip adress
    server.sin_addr.s_addr = htonl(INADDR_ANY);
    // set port
    server.sin_port = htons(port);


    //Binding between the socket and ip address
    rc= bind (sock, (struct sockaddr *)&server, sizeof(server));
    if(rc==SOCKET_ERROR)

{

  printf("Fehler: bind, fehler code: %d\n",WSAGetLastError());

  return 1;

}

else

{

  printf("Socket an port %d gebunden\n",port);

}


    //Listen to incoming connections
   rc=listen(sock, 10);

    if(rc==SOCKET_ERROR)

{

  printf("Fehler: listen, fehler code: %d\n",WSAGetLastError());

  return 1;

}

else

{

  printf("acceptSocket ist im listen Modus....\n");

}

        len = sizeof(client);
        fd = accept(sock, (struct sockaddr*) &client, &len);


        if(fd ==INVALID_SOCKET)
        {

  printf("Fehler: accept, fehler code: %d\n",WSAGetLastError());

  return 1;

}

else

{

  printf("Neue Verbindung wurde akzeptiert!\n");

        while(rc!=SOCKET_ERROR)
  {
    rc=recv(fd,buf,256,0);
    if(rc==0)
    {
      printf("Server hat die Verbindung getrennt..\n");
      break;
    }
    if(rc==SOCKET_ERROR)
    {
      printf("Fehler: recv, fehler code: %d\n",WSAGetLastError());
      break;
    }
    XcpIp_RxCallback( (uint16) rc, (uint8*) buf, (uint16) port );



 h1=TimerTask(2,TASK1,&Task2ms_Raster);
 h2=TimerTask(10,TASK2,&Task10ms_Raster);
 h3=TimerTask(100,TASK3,&Task100ms_Raster);

//    buf[rc]='\0';
//    printf("Client sendet: %s\n",buf);
//    sprintf(buf2,"Du mich auch %s",buf);
//    rc=send(connectedSocket,buf2,strlen(buf2),0);jsd

  }
}
  closesocket(sock);
  closesocket(fd);
  XcpIp_OnTcpCxnClosed( (uint16) port );
  WSACleanup();
  return 0;

}






int main()
{


Xcp_Initialize();
CreateSocket();

return 0;
}



void XcpApp_IpTransmit( uint16 XcpPort,  Xcp_StatePtr8 pBytes, uint16 numBytes )
{
    long sentbytes;


    if ((long)XcpPort==port){
            sentbytes=send(fd,(char*)pBytes,(long)numBytes,0);
    }
    XcpIp_TxCallback(port,(uint16)sentbytes);
}

通过使用Windows操作系统的处理程序,我创建了一个用于每2ms,10ms和100ms调用任务的计时器。有人帮助我为linux OS创建一个计时器吗?上面的代码适用于Windows操作系统。

1 个答案:

答案 0 :(得分:0)

这方面的核心功能是nanosleep()clock_gettime()。包含文件是time.h。