Qt TwinCAT3中的变量回调函数的时间戳读取方式

发布时间 2023-08-04 09:03:33作者: 一杯清酒邀明月

官网提供了例程,官网真是个宝库。基本ADS的操作都里面有例程了,但是可能会稍微分散一点,不过多看几遍,也就慢慢整理你所需要的东西出来了。

https://infosys.beckhoff.com/index_en.htm

 1 #include <Windows.h>
 2 #include <conio.h>
 3 #include <winbase.h>
 4 
 5 #include <TcAdsDef.h>
 6 #include <TcAdsAPI.h>
 7 
 8 void myPAdsNotificationFuncEx(AmsAddr*    pAddr,
 9                               AdsNotificationHeader* pNotification,
10                               unsigned long hUser
11                               )
12 {
13     SYSTEMTIME              SystemTime, LocalTime;
14     FILETIME                FileTime;
15     LARGE_INTEGER           LargeInteger;
16     TIME_ZONE_INFORMATION   TimeZoneInformation;
17 
18     // Convert the timestamp into SYSTEMTIME
19     LargeInteger.QuadPart = pNotification->nTimeStamp;
20     FileTime.dwLowDateTime = (DWORD)LargeInteger.LowPart;
21     FileTime.dwHighDateTime = (DWORD)LargeInteger.HighPart;
22     FileTimeToSystemTime(&FileTime, &SystemTime);
23 
24     // Convert the time value Zeit to local time
25     GetTimeZoneInformation(&TimeZoneInformation);
26     SystemTimeToTzSpecificLocalTime(&TimeZoneInformation, &SystemTime, &LocalTime);
27 
28     // Print out the timestamp
29     qDebug() << LocalTime.wHour << ":"
30              << LocalTime.wMinute << ":"
31              << LocalTime.wSecond << '.'
32              << LocalTime.wMilliseconds;
33 
34     //这样子不行,貌似Qt不支持这种timeStamp
35     //qDebug() << QDateTime::fromTime_t(pNotification->nTimeStamp);
36 }