1)工程文件
QT = core
CONFIG += c++17 cmdline
# You can make your code fail to compile if it uses deprecated APIs.
# In order to do so, uncomment the following line.
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0
DEFINES += _DEBUG
DEFINES += TEST_MEASUREMENTS
SOURCES +=
cprecisetimer.cpp
main.cpp
# Default rules for deployment.
qnx: target.path = /tmp/$${TARGET}/bin
else: unix:!android: target.path = /opt/$${TARGET}/bin
!isEmpty(target.path): INSTALLS += target
HEADERS +=
cprecisetimer.h
win32:CONFIG(release, debug|release): LIBS += -L$$PWD/SDK/ -lACSCL_x64
INCLUDEPATH += $$PWD/SDK
DEPENDPATH += $$PWD/SDK
LIBS +=-L$$PWD/SDK -lACSCL_x64
win32:!win32-g++:CONFIG(release, debug|release): PRE_TARGETDEPS += $$PWD/SDK/ACSCL_x64.lib
2)精确定时器类
#ifndef CPRECISETIMER_H
#define CPRECISETIMER_H
#include
class CPreciseTimer
{
public:
CPreciseTimer();
bool SupportsHighResCounter();
void StartTimer();
void StopTimer();
__int64 GetTime();
private:
//Auxiliary Function
void UpdateElapsed();
//Member variables
bool m_bRunning;
__int64 m_i64Start;
__int64 m_i64Elapsed;
//Some auxiliary variables
__int64 m_i64Counts;
LARGE_INTEGER m_liCount;
//Static Variables
static bool sm_bInit;
static bool sm_bPerformanceCounter;
static __int64 sm_i64Freq;
};
inline bool CPreciseTimer::SupportsHighResCounter()
{
return sm_bPerformanceCounter;
}
//Auxiliary Function
inline void CPreciseTimer::UpdateElapsed()
{
if(true == sm_bPerformanceCounter)
{
QueryPerformanceCounter(&m_liCount);
m_i64Counts = ((__int64)m_liCount.HighPart m_i64Start)
m_i64Elapsed = m_i64Counts - m_i64Start;
else
//Eliminate possible number overflow (0x7fffffffffffffff is the maximal __int64 positive number)
m_i64Elapsed = (0x7fffffffffffffff - m_i64Start) + m_i64Counts;
}
#endif // CPRECISETIMER_H
#include "cprecisetimer.h"
bool CPreciseTimer::sm_bInit = false;
bool CPreciseTimer::sm_bPerformanceCounter;
__int64 CPreciseTimer::sm_i64Freq;
//CONSTRUCTOR
CPreciseTimer::CPreciseTimer() : m_i64Start(0), m_i64Elapsed(0), m_bRunning(false)
{
//Only if not already initialized
if(false == sm_bInit)
{
//Initializing some static variables dependent on the system just once
LARGE_INTEGER liFreq;
if(TRUE == QueryPerformanceFrequency(&liFreq))
{
//Only if the system is supporting High Performance
sm_i64Freq = ((__int64)liFreq.HighPart
3)主程序
// Measurements Demo.cpp : Defines the entry point for the console application.
//
#include
#include
#include "SDK/ACSC.h"
#include "cpreciseTimer.h"
extern "C" {
// CHANGE THOSE SETTINGS ACCORDING TO NEEDED PERFORMANCE MEASUREMENTS
// ------------------------------------------------------------------
#define SHARED_MEMORY // UNCOMMENT IF REQUIRED
#define READ_WRITE_VARIABLES_MEASUREMENTS // UNCOMMENT IF REQUIRED
#define CALLBACKS_MEASUREMENTS // UNCOMMENT IF REQUIRED
//#define TEST_MEASUREMENTS // UNCOMMENT IF REQUIRED
typedef enum
{
TCP_COMMUNICATION = 1,
UDP_COMMUNICATION = 2,
SIMULATOR_COMMUNICATION = 3,
#if !defined(SHARED_MEMORY)
SERIAL_COMMUNICATION,
PCI_COMMUNICATION,
#endif
REMOTE_COMMUNICATION,
EXIT
} COMMUNICATION_TYPE;
// DEFINE NUMBER OF REQUIRED ITERATIONS
// ------------------------------------
const int n_iterations = 100;
unsigned __int64 read_write_variables_results[n_iterations];
unsigned __int64 callbacks_results[n_iterations];
unsigned int counter = 0;
#if defined(TEST_MEASUREMENTS)
HANDLE CallbackReceivedEvent = NULL;
bool isRunning = true;
#endif
CPreciseTimer preciseTimer;
#if defined(CALLBACKS_MEASUREMENTS)
int WINAPI Callback_Motion64(UINT64 Param, void* UserParameter);
int WINAPI Callback_Motor64(UINT64 Param, void* UserParameter);
int WINAPI Callback_ProgramEx64(UINT64 Param, void* UserParameter);
int WINAPI Callback_SystemError64(UINT64 Param, void* UserParameter);
int WINAPI Callback_EtherCATError64(UINT64 Param, void* UserParameter);
int WINAPI Callback_Emergency(UINT64 Param, void* UserParameter);
#endif
#if defined(CALLBACKS_MEASUREMENTS) || defined(TEST_MEASUREMENTS)
int WINAPI Callback_Program64(UINT64 Param, void* UserParameter);
#endif
#if defined(TEST_MEASUREMENTS)
int WINAPI Callback_ProgramEnd64(UINT64 Param, void* UserParameter);
#endif
void perform_statistics_calculations(unsigned __int64* results, int n_samples)
{
// PRINT SOME STATISTICS
unsigned __int64 min = results[0], max = results[0];
double average = 0;
for (int i = 0; i results[i])
min = results[i];
if (max > 24, (Received >> 16) & 0xFF, (Received >> 8) & 0xFF, Received & 0xFF);
#if !defined(SHARED_MEMORY)
if (acsc_GetPCICards(Cards, 10, &ObtainedCards))
{
printf("SPiiPlus card found Bus %d, Slot %d, Function %dn", Cards[0].BusNumber, Cards[0].SlotNumber, Cards[0].Function);
}
#endif
/////////////////////////////////////////////////////////////////////////////////////
// OPEN COMMUNICATION
/////////////////////////////////////////////////////////////////////////////////////
do
{
printf("nnPlease select the communication type (1-5) that you want to test with:n");
printf("1. Press 1 for Ethernet Network (TCP) communicationn");
printf("2. Press 2 for Ethernet Point-to-Point (UDP) communicationn");
printf("3. Press 3 for Simulator communicationn");
#if !defined(SHARED_MEMORY)
printf("4. Press 4 for Serial communicationn");
printf("5. Press 5 for PCI communicationn");
printf("6. Press 6 for establishing connection to remote PCn");
printf("7. Press 7 to Exitnn");
#else
printf("4. Press 4 for establishing connection to remote PCn");
printf("5. Press 5 to Exitnn");
#endif
scanf_s("%d", &CommType);
if ((CommType 7))
{
printf("Wrong communication type !nn");
continue;
}
switch (CommType)
{
case TCP_COMMUNICATION:
printf("nPlease enter IP address:");
scanf_s("%s", IPAddress, _countof(IPAddress));
printf("Trying to communicate using Ethernet Network (TCP) Communication (IP:%s) . . .n", IPAddress);
hComm = acsc_OpenCommEthernetTCP(IPAddress, ACSC_SOCKET_STREAM_PORT);
if (hComm == ACSC_INVALID)
{
printf("acsc_OpenCommEthernetTCP(): Error while trying to open Ethernet Network (TCP) Communication: %dn",
acsc_GetLastError());
continue;
}
Connected = true;
break;
case UDP_COMMUNICATION:
printf("nPlease enter IP address:");
scanf_s("%s", IPAddress, _countof(IPAddress));
printf("Trying to communicate using Ethernet Point-to-Point (UDP) Communication (IP:%s) . . .n", IPAddress);
hComm = acsc_OpenCommEthernetUDP(IPAddress, ACSC_SOCKET_DGRAM_PORT);
if (hComm == ACSC_INVALID)
{
printf("acsc_OpenCommEthernetUDP(): Error while trying to open Ethernet Point-to-Point (UDP) Communication: %dn",
acsc_GetLastError());
continue;
}
Connected = true;
break;
case SIMULATO服务器托管网R_COMMUNICATION:
hComm = acsc_OpenCommSimulator();
if (hComm == ACSC_INVALID)
{
printf("Cannot open direct simulator communication!n");
continue;
}
Connected = true;
break;
#if !defined(SHARED_MEMORY)
case SERIAL_COMMUNICATION:
printf("nPlease enter COM port number:");
scanf_s("%d", COMPort, sizeof(int));
printf("nPlease enter COM baud rate number:");
scanf_s("%d", COMBaudRate, sizeof(int));
printf("Trying to communicate using Serial Communication (COM:%d, BAUD:%d) . . .n", COMPort, COMBaudRate);
hComm = acsc_OpenCommSerial(COMPort, COMBaudRate);
if (hComm == ACSC_INVALID)
{
printf("acsc_OpenCommSerial(): Error while trying to open Serial Communication: %dn",
acsc_GetLastError());
continue;
}
Connected = true;
break;
case PCI_COMMUNICATION:
printf("nPlease enter PCI slot number:");
scanf_s("%d", PCISlot, sizeof(int));
hComm = acsc_OpenCommPCI(PCISlot);
if (hComm == ACSC_INVALID)
{
printf("Cannot open PCI communication!n");
continue;
}
Connected = true;
break;
#endif
case REMOTE_COMMUNICATION:
printf("nPlease enter remote IP address:");
scanf_s("%s", IPAddress, _countof(IPAddress));
printf("Connecting to %s...", IPAddress);
if( !acsc_SetServerExtLogin(IPAddress, 9999, NULL, NULL, NULL) )
{
printf("Cannot establish remote connection to %s..n", IPAddress);
continue;
}
else
{
printf("Remote connection established successfully.n");
}
break;
case EXIT:
acsc_CloseComm(hComm);
return 1;
break;
}
} while (!Connected);
printf("Communication was opened successfully.n");
#if defined(_DEBUG)
getchar();
#endif
#if defined(CALLBACKS_MEASUREMENTS)
/////////////////////////////////////////////////////////////////////////////////////
// SET CALLBACKS
/////////////////////////////////////////////////////////////////////////////////////
#if defined(_DEBUG)
printf("nInitializing Callbacks . . .n");
#endif
//set callback for motion end
if (!acsc_InstallCallback(hComm, Callback_Motion64, NULL/*&preciseTimer*/, ACSC_INTR_PHYSICAL_MOTION_END))
{
printf("acsc_InstallCallback(): ACSC_INTR_PHYSICAL_MOTION_END Callback Registration Error: %dn", acsc_GetLastError());
goto END;
}
//set callback for motor error
if (!acsc_InstallCallback(hComm, Callback_Motor64, NULL/*&preciseTimer*/, ACSC_INTR_MOTOR_FAILURE))
{
printf("acsc_InstallCallback(): ACSC_INTR_MOTOR_FAILURE Callback Registration Error: %dn", acsc_GetLastError());
goto END;
}
//set callback for program interrupt_ex
if (!acsc_InstallCallback(hComm, Callback_ProgramEx64, NULL, ACSC_INTR_ACSPL_PROGRAM_EX))
{
printf("acsc_InstallCallback(): ACSC_INTR_ACSPL_PROGRAM_EX Callback Registration Error: %dn", acsc_GetLastError());
goto END;
}
//set callback for program interrupt
if (!acsc_InstallCallback(hComm, Callback_Program64, /*NULL*/&preciseTimer, ACSC_INTR_ACSPL_PROGRAM))
{
printf("acsc_InstallCallback(): ACSC_INTR_ACSPL_PROGRAM Callback Registration Error: %dn", acsc_GetLastError());
goto END;
}
//set callback for emergency stop
if (!acsc_InstallCallback(hComm, Callback_Emergency, NULL, ACSC_INTR_EMERGENCY))
{
printf("acsc_InstallCallback(): ACSC_INTR_EMERGENCY Callback Registration Error: %dn", acsc_GetLastError());
goto END;
}
//set callback for EtherCAT error
if (!acsc_InstallCallback(hComm, Callback_EtherCATError64, NULL, ACSC_INTR_ETHERCAT_ERROR))
{
printf("acsc_InstallCallback(): ACSC_INTR_ETHERCAT_ERROR Callback Registration Error: %dn", acsc_GetLastError());
goto END;
}
//set callback for system error
if (!acsc_InstallCallback(hComm, Callback_SystemError64, NULL, ACSC_INTR_SYSTEM_ERROR))
{
printf("acsc_InstallCallback(): ACSC_INTR_SYSTEM_ERROR Callback Registration Error: %dn", acsc_GetLastError());
goto END;
}
#if defined(_DEBUG)
printf("Callbacks were initialized successfully, Press any key to continue . . .n");
getchar();
#endif
#endif
#if defined(TEST_MEASUREMENTS)
//set callback for program end
if (!acsc_InstallCallback(hComm, Callback_ProgramEnd64, NULL/*&preciseTimer*/, ACSC_INTR_PROGRAM_END))
{
printf("acsc_InstallCallback(): ACSC_INTR_PROGRAM_END Callback Registration Error: %dn", acsc_GetLastError());
goto END;
}
//set callback for program interrupt
// if (!acsc_InstallCallback(hComm, Callback_Program64, /*NULL*/&preciseTimer, ACSC_INTR_ACSPL_PROGRAM))
// {
// printf("acsc_InstallCallback(): ACSC_INTR_ACSPL_PROGRAM Callback Registration Error: %dn", acsc_GetLastError());
// goto END;
// }
#if defined(_DEBUG)
printf("Callbacks were initialized successfully, Press any key to continue . . .n");
getchar();
#endif
#endif
/////////////////////////////////////////////////////////////////////////////////////
// LOAD D-BUFFER CONTENTS WITH VARIABLES DEFINITIONS
/////////////////////////////////////////////////////////////////////////////////////
#if defined(_DEBUG)
printf("nLoading D-Buffer contents . . .n");
#endif
if (!acsc_Transaction(hComm, d_buffer_index_query, (int)strlen(d_buffer_index_query), str, (DWORD)(sizeof(str)), &received, NULL))
{
printf("acsc_Transaction(): Transaction Error During Getting D-Buffer index: %dn", acsc_GetLastError());
goto END;
}
if (!received)
{
printf("acsc_Transaction(): Transaction Error During Getting D-Buffer index: %dn", acsc_GetLastError());
goto END;
}
str[received - 1] = '';
sscanf_s(str, "%d", &DBufferIndex);
if (!acsc_Command(hComm, stop_and_reset_all_buffers, (int)strlen(stop_and_reset_all_buffers), NULL))
{
printf("acsc_Transaction(): Transaction Error During Stopping and Reset all buffers: %dn", acsc_GetLastError());
goto END;
}
if (!acsc_LoadBuffer(hComm, DBufferIndex, d_buffer_contents, (int)strlen(d_buffer_contents), NULL))
{
printf("acsc_LoadBuffer(): Transaction Error During Loading D-Buffer contents: %dn", acsc_GetLastError());
goto END;
}
if (!acsc_CompileBuffer(hComm, DBufferIndex, NULL))
{
printf("acsc_CompileBuffer(): Transaction Error During Compiling D-Buffer contents: %dn", acsc_GetLastError());
goto END;
}
#if defined(_DEBUG)
printf("D-Buffer contents were loaded successfully, Press any key to continue . . .n");
getchar();
#endif
#if defined(READ_WRITE_VARIABLES_MEASUREMENTS)
#if defined(SHARED_MEMORY)
/////////////////////////////////////////////////////////////////////////////////////
// WRITE SHARED MEMORY (SINGLE VALUE)
/////////////////////////////////////////////////////////////////////////////////////
printf("nWriting Single Value to Shared Memory . . .n");
Address = 0;
sprintf(szTemp,"%s","HELLO_VAR");
if (!acsc_GetSharedMemoryAddress(hComm, ACSC_NONE, szTemp, &Address, NULL))
{
printf("acsc_GetSharedMemoryAddress(): Transaction Error During Getting Shared Memory Variable Address: %dn", acsc_GetLastError());
goto END;
}
for (iterator = 0; iterator 0 goto GG; end
//stop
END:
if (hComm != ACSC_INVALID)
{
#if defined(CALLBACKS_MEASUREMENTS)
//unset callback for motion end
if (!acsc_InstallCallback(hComm, NULL, NULL, ACSC_INTR_PHYSICAL_MOTION_END))
{
printf("acsc_InstallCallback(): ACSC_INTR_PHYSICAL_MOTION_END Callback Unregistration Error: %dn", acsc_GetLastError());
}
//unset callback for motor error
if (!acsc_InstallCallback(hComm, NULL, NULL, ACSC_INTR_MOTOR_FAILURE))
{
printf("acsc_InstallCallback(): ACSC_INTR_MOTOR_FAILURE Callback Unregistration Error: %dn", acsc_GetLastError());
}
//unset callback for program interrupt_ex
if (!acsc_InstallCallback(hComm, NULL, NULL, ACSC_INTR_ACSPL_PROGRAM_EX))
{
printf("acsc_InstallCallback(): ACSC_INTR_ACSPL_PROGRAM_EX Callback Unregistration Error: %dn", acsc_GetLastError());
}
//unset callback for program interrupt
if (!acsc_InstallCallback(hComm, NULL, NULL, ACSC_INTR_ACSPL_PROGRAM))
{
printf("acsc_InstallCallback(): ACSC_INTR_ACSPL_PROGRAM Callback Unregistration Error: %dn", acsc_GetLastError());
}
//unset callback for emergency stop
if (!acsc_InstallCallback(hComm, NULL, NULL, ACSC_INTR_EMERGENCY))
{
printf("acsc_InstallCallback(): ACSC_INTR_EMERGENCY Callback Unregistration Error: %dn", acsc_GetLastError());
}
//unset callback for EtherCAT error
if (!acsc_InstallCallback(hComm, NULL, NULL, ACSC_INTR_ETHERCAT_ERROR))
{
printf("acsc_InstallCallback(): ACSC_INTR_ETHERCAT_ERROR Callback Unregistration Error: %dn", acsc_GetLastError());
goto END;
}
//unset callback for system error
if (!acsc_InstallCallback(hComm, NULL, NULL, ACSC_INTR_SYSTEM_ERROR))
{
printf("acsc_InstallCallback(): ACSC_INTR_SYSTEM_ERROR Callback Unregistration Error: %dn", acsc_GetLastError());
goto END;
}
#endif
#if defined(TEST_MEASUREMENTS)
//unset callback for program end
if (!acsc_InstallCallback(hComm, NULL, NULL/*&preciseTimer*/, ACSC_INTR_PROGRAM_END))
{
printf("acsc_InstallCallback(): ACSC_INTR_PROGRAM_END Callback Registration Error: %dn", acsc_GetLastError());
}
//unset callback for program interrupt
// if (!acsc_InstallCallback(hComm, NULL, NULL, ACSC_INTR_ACSPL_PROGRAM))
// {
// printf("acsc_InstallCallback(): ACSC_INTR_ACSPL_PROGRAM Callback Unregistration Error: %dn", acsc_GetLastError());
// }
#endif
acsc_CloseComm(hComm);
hComm = ACSC_INVALID;
}
if (LONG_ARRAY_TO_WRITE != NULL)
{
delete[] LONG_ARRAY_TO_WRITE;
LONG_ARRAY_TO_WRITE = NULL;
}
if (LONG_ARRAY_TO_READ != NULL)
{
delete[] LONG_ARRAY_TO_READ;
LONG_ARRAY_TO_READ = NULL;
}
printf("Press any key to exit . . .n");
getchar();
return 0;
}
#if defined(CALLBACKS_MEASUREMENTS)
int WINAPI Callback_Motion64(UINT64 Param, void* UserParameter)
{
printf("Callback_Motion64(): INTERRUPT_PHYSICAL_MOTION_END Callback received. (Parameter: %I64u)n", Param);
return 0;
}
int WINAPI Callback_Motor64(UINT64 Param, void* UserParameter)
{
printf("Callback_Motor64(): INTERRUPT_MOTOR_FAILURE Callback received. (Parameter: %I64u)n", Param);
return 0;
}
int WINAPI Callback_ProgramEx64(UINT64 Param, void* UserParameter)
{
// Stop the timer
preciseTimer.StopTimer();
// Measure the time
unsigned __int64 i64Counter = preciseTimer.GetTime();
if (counter > 32;
UINT Param2 = Param & 0x00000000FFFFFFFF;
printf("Callback_ProgramEx64(): ACSC_INTR_ACSPL_PROGRAM_EX Callback received (counter = %d). (Parameter 1: 0x%x, Parameter 2: 0x%x), Time = %I64d microsecondsn", counter, Param1, Param2, i64Counter);
return 0;
}
int WINAPI Cal服务器托管网lback_SystemError64(UINT64 Param, void* UserParameter)
{
printf("Callback_SystemError64(): ACSC_INTR_SYSTEM_ERROR Callback received. (Parameter: %I64u)n", Param);
return 0;
}
int WINAPI Callback_EtherCATError64(UINT64 Param, void* UserParameter)
{
printf("Callback_EtherCATError64(): ACSC_INTR_ETHERCAT_ERROR Callback received. (Parameter: %I64u)n", Param);
return 0;
}
int WINAPI Callback_Emergency(UINT64 Param, void* UserParameter)
{
printf("Callback_Emergency(): ACSC_INTR_EMERGENCY Callback received. (Parameter: %I64u)n", Param);
return 0;
}
#endif
#if defined(CALLBACKS_MEASUREMENTS) || defined(TEST_MEASUREMENTS)
int WINAPI Callback_Program64(UINT64 Param, void* UserParameter)
{
// Stop the timer
preciseTimer.StopTimer();
// Measure the time
unsigned __int64 i64Counter = preciseTimer.GetTime();
if (counter
服务器托管,北京服务器托管,服务器租用 http://www.fwqtg.net
机房租用,北京机房租用,IDC机房托管, http://www.fwqtg.net
相关推荐: (10/1-10/31)10月摸鱼计划,挑战7/14/21天发博文,实体礼品包邮送!
10月摸鱼计划,来啦! 本月继续以【博主任务】形式,让大家自发选择更文任务!任务达标后即可兑奖!且任务间的奖品可同享! 【活动时间】 发文时间:2023年10月1日—2023年10月31日 【活动任务】 以下任务福利可同享!! 任务一:7天更文 任务要求 任务…