uint64_t GetTimeStamp()
{
struct timespec start;
if ((clock_gettime( CLOCK_MONOTONIC, &start)) == -1)
{
perror("clock gettime\n");
}
return(start.tv_sec + start.tv_nsec * 1e-9); // seconds
}
const struct sigevent *intHandler(void *arg, int id)
{
start_clock = ClockCycles();
// printf("start clock: %lld\n", start_clock);
return(&event);
}
void *ConfigureISR()
{
// Get IO privilege
ThreadCtl( _NTO_TCTL_IO, 0 );
// Setup COID and event
SIGEV_INTR_INIT( &event);
interruptID = InterruptAttach(intrNum, intHandler, NULL, 0, 0);
if (interruptID == -1)
{
fprintf(stderr, "can't attach to IRQ %d\n", intrNum );
perror(NULL);
exit(EXIT_FAILURE);
}
while (loop)
{
InterruptWait(0, NULL);
end_clock = ClockCycles();
InterruptLatency = (uint32) ((end_clock - start_clock) * 1000000 / (SYSPAGE_ENTRY(qtime)->cycles_per_sec));
printf("Current Interrupt Latency microseconds: %ld\n", InterruptLatency);
InterruptUnmask(intrNum, interruptID);
measurements[17] = InterruptLatency;
}
return(EXIT_SUCCESS);
}
int CreateSocket()
{
// pthread_attr_t attr;
// 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));
fcntl(acceptSocket, O_NONBLOCK);
if (rc == -1)
{
printf("Oh dear, something went wrong with bind()! %s\n", strerror(errno));
return -1;
}
else
{
printf("Socket an port %d \n", port);
}
return acceptSocket;
}
int main(int argc, char *argv[])
{
pthread_t thread_id, thread_id1;
Xcp_Initialize();
pthread_create(&thread_id1, NULL, &ConfigureISR, NULL);
if ((sock = CreateSocket()) < 0)
{
perror("Create_socket");
exit(1);
}
if (pthread_create(&thread_id, NULL, &rastertask, NULL))
{
perror("pthread_create");
exit(1);
}
do
{
socklen_t len;
len = sizeof(client);
printf("NEW DATA ARRIVED\n");
// non blocking mode : MSG_DONTWAIT
rc = recvfrom(sock, buf, 256, 0, (struct sockaddr *) &client, &len);
Receive = GetTimeStamp();
receiveTime = (uint32) (Receive / 1000000);
printf("Receive time: %lu\n", receiveTime);
// printf("RECEIVE from Time in microseconds: %lu\n", ReceiveTimestamp);
// measurements[19] = ReceiveTimestamp;
if (rc == 0)
{
printf("Server has no connection..\n");
loop = 0;
break;
}
if (rc == -1)
{
if (errno == SIGINT)
continue;
printf("Oh dear, something went wrong with read()! s\n", strerror(errno));
loop = 0;
break;
}
XcpIp_RxCallback( (uint16) rc, (uint8 *) buf, (uint16) port );
} while (1);
close(sock);
return 0;
}
以上是服务器代码,它通过IP地址和端口号接收数据。
稍后将数据发送回客户端。我收到数据但收到数据后,取时间戳(您可以在上面的代码中看到:Send= GetTimeStamp()
)。
为什么不打印任何东西?我通过套接字接收数据,然后我给了printf("new data arrived\n");
,然后它也没有打印。我也没有在那里接受任何时间!有人可以告诉我可能是什么原因?
答案 0 :(得分:0)
您的GetTimeStamp()
函数返回uint64_t
; return语句是:
return (start.tv_sec + start.tv_nsec * 1e-9);
这会创建一个double
值(1e-9
是double
常量),然后将其截断为整数,就像您返回时一样:
return (start.tv_sec);
请注意,这将为您提供以秒为单位的时间(自纪元 - 1970-01-01 00:00:00 +00:00以来)。
也许你想要:
return (start.tv_sec * 1E9 + start.tv_nsec);
这为您提供了自纪元以来的纳秒总数。这可能与您遇到的其他问题相关,但是没有获得合理的发送值意味着您将无法获得合理的值,因此您应该确定您对GetTimeStamp()
的期望值。例如:
Receive = GetTimeStamp();
receiveTime = (uint32) (Receive / 1000000);
printf("Receive time: %lu\n", receiveTime);
这将打印一个1400之类的值(因为当前时间戳约为1400755363 = 2014-05-22 10:42:43),因此该值除以1百万是1400.