279 lines
7.5 KiB
C++
279 lines
7.5 KiB
C++
// TestMK3.cpp : This file contains the 'main' function. Program execution begins and ends there.
|
|
//
|
|
#include <wtypes.h>
|
|
#include <synchapi.h>
|
|
#include <chrono>
|
|
#include <iostream>
|
|
#include "EpdCommon.h"
|
|
#include "Epd3.h"
|
|
|
|
#include "TimeFunctions.h"
|
|
#include <cstdio> // if not already included
|
|
|
|
|
|
|
|
|
|
char comPort[] = "COM3";
|
|
CommsHandle epdComsHandle;
|
|
uint32_t EpdID;
|
|
EpdGeneration EpdGen;
|
|
DiscoveryId Epd;
|
|
int DeviceCount;
|
|
|
|
HANDLE Ev1 = INVALID_HANDLE_VALUE;
|
|
CompletionToken token;
|
|
StatusData_t status;
|
|
MeasValue_t doses[4];
|
|
uint8_t numDoses;
|
|
uint32_t utc,utc2;
|
|
uint8_t EC[]{ 0xE7 ,0xD3 ,0xE7 ,0x69 ,0xF3 ,0xF5 ,0x93 ,0xDA ,0xDC ,0xB8 ,0x63 ,0x4C ,0xC5 ,0xB0 ,0x9F ,0xC9 ,0x0D ,0xD3 ,0xA6 ,0x1C ,0x4A ,0x06 ,0xA7 ,0x9C ,0xB0 ,0x92 ,0x36 ,0x62 ,0xFE ,0x6F ,0xAE ,0x6B };
|
|
|
|
|
|
|
|
void WINAPI discovery2(CommsHandle hComms, DiscoveryId const discovered[], int32_t count)
|
|
{
|
|
EpdID = 0;
|
|
DeviceCount = count;
|
|
std::cout << "Discovered " << count << "\n";
|
|
if (count > 0) {
|
|
|
|
EpdID = discovered[0].Id;
|
|
EpdGen = discovered[0].Gen;
|
|
memcpy(&Epd, &discovered[0], sizeof(DiscoveryId));
|
|
}
|
|
else {
|
|
EpdID = 0;EpdGen = EpdGeneration::None;
|
|
memset(&Epd, 0, sizeof(DiscoveryId));
|
|
}
|
|
SetEvent(Ev1);
|
|
}
|
|
|
|
|
|
void WINAPI discovery(CommsHandle hComms, DiscoveryId const discovered[], int32_t count)
|
|
{
|
|
EpdID = 0;
|
|
DeviceCount = count;
|
|
if (count > 0) {
|
|
EpdID = discovered[0].Id;
|
|
EpdGen = discovered[0].Gen;
|
|
}
|
|
memcpy(&Epd,&discovered[0],sizeof(DiscoveryId));
|
|
SetEvent(Ev1);
|
|
}
|
|
|
|
void WINAPI completion(CommsHandle hComms, CompletionToken token)
|
|
{
|
|
SetEvent(Ev1);
|
|
}
|
|
|
|
|
|
void ReadTime()
|
|
{
|
|
DWORD r;
|
|
DWORD EpdClock;
|
|
utc = 0;
|
|
token = BeginReadRTC(epdComsHandle, nullptr);
|
|
r = Commit(epdComsHandle);
|
|
if (!r)
|
|
r = EndReadRTC(epdComsHandle, token, &utc); //EpdClock for MK2
|
|
EpdClock = utc;
|
|
utc2 = TimeFunctions::NowToEpdTime(false);
|
|
|
|
}
|
|
|
|
int SetTime() //Only R3
|
|
{
|
|
int r = 0;
|
|
if (Epd.Gen == EpdGeneration::Mk3)
|
|
{
|
|
|
|
utc = TimeFunctions::NowToEpdTime(false);
|
|
token = BeginWriteRTC_R3(epdComsHandle, utc, nullptr);
|
|
r = Commit(epdComsHandle);
|
|
if (!r)
|
|
r = EndWriteRTC_R3(epdComsHandle, token); //EpdClock for MK2
|
|
|
|
}
|
|
return r;
|
|
}
|
|
|
|
int startCom()
|
|
{
|
|
epdComsHandle = CreateReaderInterface();
|
|
int r = OpenReader(epdComsHandle, comPort);
|
|
Ev1 = CreateEvent(
|
|
NULL, // default security attributes
|
|
TRUE, // manual-reset event
|
|
FALSE, // initial state is nonsignaled
|
|
TEXT("WriteEvent") // object name
|
|
);
|
|
uint32_t ms1, ms, slots; bool state = true;
|
|
GetResponseTimeout(epdComsHandle, &ms1);
|
|
r = GetMultipleDiscoveryMode(epdComsHandle, &state);
|
|
r = GetDiscoveryTimeslots(epdComsHandle, &slots);
|
|
r = GetDiscoveryTimeout(epdComsHandle, &ms);
|
|
|
|
SetDiscoveryCacheTimeout(epdComsHandle, 2000);
|
|
SetResponseTimeout(epdComsHandle, 500);
|
|
return r;
|
|
}
|
|
|
|
void endCom()
|
|
{
|
|
CloseHandle(Ev1);
|
|
Ev1 = INVALID_HANDLE_VALUE;
|
|
int r = CloseReader(epdComsHandle);
|
|
r = DestroyReaderInterface(epdComsHandle);
|
|
}
|
|
|
|
//void detectepd()
|
|
//{
|
|
// using std::chrono::high_resolution_clock;
|
|
// using std::chrono::duration_cast;
|
|
// using std::chrono::duration;
|
|
// using std::chrono::milliseconds;
|
|
//
|
|
//
|
|
//
|
|
//
|
|
// int r,r2;
|
|
// int tries = 5; DeviceCount = 0;
|
|
// ResetEvent(Ev1);
|
|
//
|
|
// while (tries-- > 0 && DeviceCount == 0)
|
|
// {
|
|
// auto t1 = high_resolution_clock::now();
|
|
// //memset(DevceIDs, 0, sizeof(DeviceIDs));
|
|
// r = StartDiscovery(epdComsHandle, discovery2);
|
|
//
|
|
// if (r == 0) {
|
|
// r = WaitForSingleObject(Ev1, 10000);
|
|
//
|
|
// std::cout << "EPD " << EpdID << "\n";
|
|
// }
|
|
// else {
|
|
// std::cout << "StartDiscovery failed " << r << "\n";
|
|
// }
|
|
// r2 = StopDiscovery(epdComsHandle);
|
|
// auto t2 = high_resolution_clock::now();
|
|
// auto elapsed = duration_cast<milliseconds>(t2 - t1);
|
|
// std::cout << "Elapsed time: " << elapsed.count() << " ms\n";
|
|
// }
|
|
//
|
|
//
|
|
//
|
|
// //std::cout << "wacht effen!\n";
|
|
// //r = WaitForSingleObject(Ev1, 10000); // no time-out interval
|
|
//
|
|
// std::cout << "ok! r=" << r << ", r2=" << r2 << "\n";
|
|
//
|
|
//
|
|
//
|
|
//
|
|
//}
|
|
|
|
void GetEPD()
|
|
{
|
|
//epdComsHandle = CreateReaderInterface();
|
|
//int r = OpenReader(epdComsHandle, comPort);
|
|
Ev1 = CreateEvent(
|
|
NULL, // default security attributes
|
|
TRUE, // manual-reset event
|
|
FALSE, // initial state is nonsignaled
|
|
TEXT("WriteEvent") // object name
|
|
);
|
|
if (!Ev1) return;
|
|
|
|
int r;
|
|
int tries = 5; DeviceCount = 0;
|
|
while (tries-- > 0 && DeviceCount == 0)
|
|
{
|
|
//memset(DevceIDs, 0, sizeof(DeviceIDs));
|
|
r = StartDiscovery(epdComsHandle, discovery);
|
|
if (r == 0)
|
|
r = WaitForSingleObject(Ev1, 10000);
|
|
}
|
|
|
|
|
|
std::cout << "wacht discover!\n";
|
|
r = WaitForSingleObject(Ev1, 10000); // no time-out interval
|
|
|
|
std::cout << "ok! connect\n";
|
|
|
|
|
|
Sleep(1000);
|
|
|
|
|
|
|
|
r = Connect(epdComsHandle,Epd, true);
|
|
// -9 = r_OperationTimeout
|
|
std::cout << "Connect rv " << r << "\n";
|
|
|
|
SetResponseTimeout(epdComsHandle, 250);
|
|
SetDiscoveryCacheTimeout(epdComsHandle, 3000);
|
|
|
|
Sleep(1000);
|
|
|
|
std::cout << "get status\n";
|
|
auto start = std::chrono::steady_clock::now();
|
|
ResetEvent(Ev1);
|
|
token = BeginReadStatus(epdComsHandle, completion); //completion);
|
|
r = Commit(epdComsHandle);
|
|
r = WaitForSingleObject(Ev1, 1000); // no time-out interval
|
|
r = EndReadStatus(epdComsHandle, token, &status);
|
|
auto end = std::chrono::steady_clock::now();
|
|
|
|
// 4. Calculate duration in desired units
|
|
std::chrono::duration<double, std::milli> duration_ms = end - start;
|
|
|
|
std::cout << "API call took: " << duration_ms.count() << " ms\n";
|
|
std::cout << "got status "<<r<<"\n";
|
|
Sleep(11000);
|
|
|
|
//ReadTime();
|
|
//SetTime();
|
|
//ReadTime();
|
|
|
|
////r = Connect(epdComsHandle, Epd, true);
|
|
//token = BeginWriteAccessLevel(epdComsHandle, AccessLevel_t::Admin, EC, 32, nullptr);
|
|
//r = Commit(epdComsHandle);
|
|
////-7 r_InvalidState
|
|
//r = EndWriteAccessLevel(epdComsHandle, token);
|
|
//std::cout << "EndWriteAccessLevel rv " << r << "\n";
|
|
|
|
//numDoses = 0; utc = 0;
|
|
//ResetEvent(Ev1);
|
|
////token = BeginReadDoses(epdComsHandle, new uint8_t[]{ 0, 1 }, 2 , completion);
|
|
//token = BeginReadDoses(epdComsHandle, nullptr, 0 , completion);
|
|
//r = Commit(epdComsHandle);
|
|
//r = WaitForSingleObject(Ev1, 10000); // no time-out interval
|
|
//r = EndReadDoses(epdComsHandle, token, doses, 2, &numDoses, &utc);
|
|
//std::cout << "got doses " << r << "\n";
|
|
r = Disconnect(epdComsHandle);
|
|
std::cout << "Disconnect" << r << "\n";
|
|
|
|
}
|
|
|
|
|
|
int main()
|
|
{
|
|
startCom();
|
|
//while (true)
|
|
//detectepd();
|
|
|
|
GetEPD();
|
|
endCom();
|
|
(void)getchar();
|
|
}
|
|
|
|
// Run program: Ctrl + F5 or Debug > Start Without Debugging menu
|
|
// Debug program: F5 or Debug > Start Debugging menu
|
|
//C:\Program Files (x86)\Thermo Scientific\Unmanaged HtmlFiles
|
|
//
|
|
// Tips for Getting Started:
|
|
// 1. Use the Solution Explorer window to add/manage files
|
|
// 2. Use the Team Explorer window to connect to source control
|
|
// 3. Use the Output window to see build output and other messages
|
|
// 4. Use the Error List window to view errors
|
|
// 5. Go to Project > Add New Item to create new code files, or Project > Add Existing Item to add existing code files to the project
|
|
// 6. In the future, to open this project again, go to File > Open > Project and select the .sln file
|