以下代码用于通过udp层的套接字从主设备接收数据。我的项目中支持一些API,我创建了一个计时器任务,用于每2ms,10ms等调用任务。
我正在使用远程C / C ++应用程序在eclipse IDE中的嵌入式PC目标上调试此程序。我在maketimer (alarm)
函数中调用CreateSocket
。但是如何检查在后台每2ms,10ms和100ms调用函数TASK1,TASK2和TASK3的定时器任务?
#include "MAIN.h"
#define BILLION 1000000L
timer_t firstTimerID, secondTimerID, thirdTimerID;
double Task2ms_Raster, Task10ms_Raster, Task100ms_Raster ;
int connectedSocket, acceptSocket;
struct sockaddr_in addr;
struct sockaddr client, dest;
char buf[128];
long rc, sentbytes;
int port = 18017;
void TASK1(Task2ms_Raster)
{
struct timespec start, stop;
uint32 StartTime, StopTime;
if( (StartTime = clock_gettime( CLOCK_REALTIME, &start)) == -1 )
{
perror("clock gettime");
}
// return EXIT_SUCCESS;
/* 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;
}
}
if( (StopTime = clock_gettime( CLOCK_REALTIME, &stop)) == -1 )
{
perror( "clock gettime" );
}
duration2ms = ( stop.tv_sec - start.tv_sec )
+ (double)( stop.tv_nsec - start.tv_nsec )
/ (double)BILLION;
printf( "time difference is= %ld\n", duration2ms );
}
void TASK2(Task10ms_Raster)
{
struct timespec start, stop;
if( clock_gettime( CLOCK_REALTIME, &start) == -1 )
{
perror( "clock gettime" );
}
/* 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;
}
}
if( clock_gettime( CLOCK_REALTIME, &stop) == -1 )
{
perror( "clock gettime" );
}
XCP_FN_TYPE Xcp_CmdProcessor ( );
duration10ms = ( stop.tv_sec - start.tv_sec )
+ (double)( stop.tv_nsec - start.tv_nsec )
/ (double)BILLION;
printf( "time difference is= %ld\n", duration10ms );
}
void TASK3(Task100ms_Raster)
{
struct timespec start, stop;
if( clock_gettime( CLOCK_REALTIME, &start) == -1 )
{
perror( "clock gettime" );
}
/* 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;
}
}
if((clock_gettime( CLOCK_REALTIME, &stop)) == -1 )
{
perror( "clock gettime" );
}
duration100ms = ( stop.tv_sec - start.tv_sec )
+ (double)( stop.tv_nsec - start.tv_nsec )
/ (double)BILLION;
printf( "time difference is= %ld\n", duration100ms );
}
static void timerHandler( int sig, siginfo_t *si, void *uc )
{
timer_t *tidp;
tidp = si->si_value.sival_ptr;
if ( *tidp == firstTimerID )
TASK1(Task2ms_Raster);
else if ( *tidp == secondTimerID )
TASK2(Task10ms_Raster);
else if ( *tidp == thirdTimerID )
TASK3(Task100ms_Raster);
}
static int makeTimer( char *name, timer_t *timerID, int expireMS, int intervalMS )
{
struct sigevent te;
struct itimerspec its;
struct sigaction sa;
int sigNo = SIGRTMIN;
/* Set up signal handler. */
sa.sa_flags = SA_SIGINFO;
sa.sa_sigaction = timerHandler;
sigemptyset(&sa.sa_mask);
if (sigaction(sigNo, &sa, NULL) == -1)
{
perror("sigaction");
}
/* Set and enable alarm */
te.sigev_notify = SIGEV_SIGNAL;
te.sigev_signo = sigNo;
te.sigev_value.sival_ptr = timerID;
timer_create(CLOCK_REALTIME, &te, timerID);
its.it_interval.tv_sec = 0;
its.it_interval.tv_nsec = intervalMS * 1000000;
its.it_value.tv_sec = 0;
its.it_value.tv_nsec = expireMS * 1000000;
timer_settime(*timerID, 0, &its, NULL);
return 1;
}
int CreateSocket()
{
if(rc!=0)
{
printf("socket failure code: %ld\n",rc);
return 1;
}
else
{
printf("socket started!\n");
}
// Socket creation for UDP
acceptSocket=socket(AF_INET,SOCK_DGRAM,0);
if(acceptSocket==-1)
{
printf("Failure: socket creation is failed, failure code\n");
return 1;
}
else
{
printf("Socket started!\n");
}
memset(&addr, 0, sizeof(addr));
addr.sin_family=AF_INET;
addr.sin_port=htons(port);
addr.sin_addr.s_addr=htonl(INADDR_ANY);
rc=bind(acceptSocket,(struct sockaddr*)&addr,sizeof(addr));
if(rc== -1)
{
printf("Failure: listen, failure code:\n");
return 1;
}
else
{
printf("Socket an port %d \n",port);
}
while(rc!=-1)
{
rc= recvfrom(acceptSocket,buf,128,0,(struct sockaddr*)&client, sizeof(client));
if(rc==0)
{
printf("Server has no connection..\n");
break;
}
if(rc==-1)
{
printf("failure: recv, failure code\n");
break;
}
XcpIp_RxCallback( (uint16) rc, (uint8*) buf, (uint16) port );
makeTimer("First Timer", &firstTimerID, 2, 2); //2ms
makeTimer("Second Timer", &secondTimerID, 10, 10); //10ms
makeTimer("Third Timer", &thirdTimerID, 100, 100); //100ms
// buf[rc]='\0';
// printf("Client sendet: %s\n",buf);
// sprintf(buf2,"Du mich auch %s",buf);
// rc=sendto(connectedSocket,buf2,strlen(buf2),0);
}
close(acceptSocket);
return 0;
}
int main()
{
Xcp_Initialize();
CreateSocket();
return 0;
}
void XcpApp_IpTransmit( uint16 XcpPort, Xcp_StatePtr8 pBytes, uint16 numBytes )
{
if ((long)XcpPort==port)
{
sentbytes = sendto(acceptSocket,(char*)pBytes,(long)numBytes,0, (struct sockaddr*) &dest, sizeof(dest));
}
XcpIp_TxCallback(port,(uint16)sentbytes);
}
答案 0 :(得分:1)
BILLION
错了。你可能应该
#define BILLION 1.0e9
(因为您只在浮点表达式中使用它)
你可以有三个线程(读一个pthreads tutorial),一个用于TASK1,一个用于TASK2,一个用于TASK3(可能使用clock_nanosleep(2) ...)。或者(最好)你可以有一个event loop(大约例如poll(2)或ppoll(2)
多路复用呼叫....),也许也可以使用timerfd_create(2)来获取定时器事件,并且还管理套接字。
见this example of poll
-based event loop。您应该调整它以使用使用timerfd_create
获得的文件描述符。您还可以使用某些事件循环库,如libevent或libev
请注意,2毫秒是一个非常小的延迟。 (你希望它有多可靠?)