eDosStation/EpdBase/EpdBase.cpp

422 lines
11 KiB
C++
Raw Normal View History

2019-02-04 11:48:08 +01:00
#include "stdafx.h"
#include "EpdBase.h"
2020-02-05 14:21:53 +01:00
#define DiscoveryExpirationPeriod 1000
2019-02-04 11:48:08 +01:00
EpdBase::Epd2U::Epd2U(int comport)
{
port = comport;
2019-02-27 07:22:39 +01:00
CommsInitialized = 0;
2019-02-04 11:48:08 +01:00
SetAutoCommit(0);
Error = 123;
//SetTimeouts(port, 20, 1000, 1000);
2019-02-25 09:12:08 +01:00
}
DWORD EpdBase::Epd2U::StartCom()
{
Error = 0;
InitRv = InitComms(port, 9600, 8, 0, 0);
2019-02-04 11:48:08 +01:00
if (InitRv == 0) Error = GetErrorDetails(&ErrorSource, &ErrorReason, &ErrorData);
2019-02-27 07:22:39 +01:00
else {
CommsInitialized = 1;
2020-02-05 14:21:53 +01:00
SetDiscoveryExpirationPeriod(port, DiscoveryExpirationPeriod);
2019-02-27 07:22:39 +01:00
}
2019-02-25 09:12:08 +01:00
return Error;
2019-02-04 11:48:08 +01:00
}
EpdBase::Epd2U::~Epd2U()
{
DeinitComms(port);
2019-02-11 11:34:52 +01:00
2019-02-04 11:48:08 +01:00
}
2019-02-27 07:22:39 +01:00
int EpdBase::Epd2U::StartDiscover()
2019-02-04 11:48:08 +01:00
{
2019-02-27 07:22:39 +01:00
Error = 0;
2020-02-05 14:21:53 +01:00
Sleep(200);
2019-02-04 11:48:08 +01:00
DiscRV= Discover(port, DeviceIDs, MaxDevices, &DeviceCount);
2019-02-14 15:56:23 +01:00
Commit();
2019-02-27 07:22:39 +01:00
if (DiscRV == 1 && DeviceCount == 0)
2019-02-19 15:08:06 +01:00
{
2019-02-27 07:22:39 +01:00
DeinitComms(port);
2020-02-05 14:21:53 +01:00
FlushDiscoveryCache(port);
Sleep(200);
2019-02-27 07:22:39 +01:00
InitRv = InitComms(port, 9600, 8, 0, 0);
if (InitRv == 1) {
CommsInitialized = 1;
2020-02-05 14:21:53 +01:00
SetDiscoveryExpirationPeriod(port, DiscoveryExpirationPeriod);
2019-02-27 07:22:39 +01:00
DiscRV = Discover(port, DeviceIDs, MaxDevices, &DeviceCount);
Commit();
}
else
{
Error = GetErrorDetails(&ErrorSource, &ErrorReason, &ErrorData);
}
}
return DiscRV;
2019-02-04 11:48:08 +01:00
}
void EpdBase::Epd2U::Open(DWORD DeviceID)
{
CurrentDeviceID = DeviceID;
ConRV = Connect(DeviceID);
if (ConRV == 0) Error = GetErrorDetails(&ErrorSource, &ErrorReason, &ErrorData);
else {
OpenRV = OpenDevice(port, CurrentDeviceID);
if (OpenRV == 0) Error = GetErrorDetails(&ErrorSource, &ErrorReason, &ErrorData);
}
}
void EpdBase::Epd2U::Close()
{
CloseDevice();
DeinitComms(port);
}
void EpdBase::Epd2U::DecodeFunctionFlags()
{
2019-02-21 10:11:58 +01:00
EpdType = (FunctionFlags & 0x1F00) >> 8;
2019-02-04 11:48:08 +01:00
}
void EpdBase::Epd2U::DecodeStatus()
{
DetectorsOn = (OperatingStatus & 1) != 0;
EPDIssued = (OperatingStatus & 4) != 0;
EPDLocked = (OperatingStatus & 0x10)!=0;
EPDTesting = (OperatingStatus & 0x20) != 0;
EPDProcSec = (OperatingStatus & 0x40) != 0;
EPDFaulty = (OperatingStatus & 0x80) != 0;
EPDNewFault = (ErrorStatus & 1) != 0;
EPDComFault = (ErrorStatus & 2) != 0;
EPDCalFault = (ErrorStatus & 4) != 0;
EPDDetFault = (ErrorStatus & 8) != 0;
EPDEprFault = (ErrorStatus & 0x10) != 0;
EPDTrhFault = (ErrorStatus & 0x20) != 0;
EPDOthFault = (ErrorStatus & 0x80) != 0;
AlarmDoseHP101G = (AlarmStatus & 1) != 0;
AlarmDoseHP102G = (AlarmStatus & 2) != 0;
AlarmDoseHP07N = (AlarmStatus & 4) != 0;
AlarmRateHP101G = (AlarmStatus & 8) != 0;;
AlarmRateHP102G = (AlarmStatus & 0x10) != 0;
AlarmRateHP07N = (AlarmStatus & 0x20) != 0;
//OtherAlarm
OtherAlarmRateHp101G = (OtherAlarms & 1) != 0;
OtherAlarmRateHp102G = (OtherAlarms & 2) != 0;
OtherAlarmRateHp07N = (OtherAlarms & 4) != 0;
OtherAlarmReturn = (OtherAlarms & 8) != 0;
OtherAlarmBatLow = (OtherAlarms & 0x10) != 0;
OtherAlarmCarrier = (OtherAlarms & 0x20) != 0;
OtherAlarmBatVolt = (OtherAlarms & 0x40) != 0;
}
WORD EpdBase::Epd2U::ReadStatus()
{
2020-02-05 14:21:53 +01:00
Error = 0; ReadStatusRV = 0;
2019-02-04 11:48:08 +01:00
memset(WearerID, 0, WearerIDLength+1);
memset(WearerName, 0, WearerNameLength+1);
EpdClock = RealTime = IssueCount = OffTime = OperatingStatus = ErrorStatus = AlarmStatus = OtherAlarms = EEPROMBadSectors = 0;
ErrorSource = ErrorReason = ErrorData = 0;
2019-02-14 15:56:23 +01:00
EpdID = HardVersion = SoftVersion = FunctionFlags = 0;
2019-02-04 11:48:08 +01:00
2019-02-14 15:56:23 +01:00
CommitRV = ReadEpdStatus(&EpdClock, &RealTime, &IssueCount, &OffTime, &OperatingStatus, &ErrorStatus, &AlarmStatus, &OtherAlarms, &EEPROMBadSectors);
if (CommitRV==1) CommitRV= ReadEpdID(
2019-02-04 11:48:08 +01:00
&EpdID,
&HardVersion,
&SoftVersion,
&FunctionFlags);
2019-02-14 15:56:23 +01:00
if (CommitRV==1) CommitRV = Commit();
2019-02-04 11:48:08 +01:00
if(CommitRV == 0) {
Error = GetErrorDetails(&ErrorSource, &ErrorReason, &ErrorData);
2020-02-05 14:21:53 +01:00
ReadStatusRV= ErrorReason;
return 0;
2019-02-04 11:48:08 +01:00
}
else {
2019-02-14 15:56:23 +01:00
if (CurrentDeviceID == 0) CurrentDeviceID = EpdID;
2019-02-04 11:48:08 +01:00
time_t t = RealTime;
TimeFunctions::UnixTimeToSystemTime(t, &tRealTime);
DecodeStatus();
DecodeFunctionFlags();
}
2019-02-14 15:56:23 +01:00
if (CommitRV == 1 && EPDIssued == 1)
{
CommitRV = ReadWearer(
2019-02-04 11:48:08 +01:00
WearerID,
WearerIDLength,
WearerName,
WearerNameLength);
2019-02-14 15:56:23 +01:00
if(CommitRV==1) CommitRV = Commit();
2019-02-04 11:48:08 +01:00
if (CommitRV == 0) {
Error = GetErrorDetails(&ErrorSource, &ErrorReason, &ErrorData);
2020-02-05 14:21:53 +01:00
ReadStatusRV = ErrorReason;
return 0;
2019-02-04 11:48:08 +01:00
}
2019-02-14 15:56:23 +01:00
else {
for (int i = 0; i < WearerIDLength; i++)
if (WearerID[i] == 0xb) WearerID[i] = '0'; else WearerID[i] |= 0x30;
}
2019-02-04 11:48:08 +01:00
}
2020-02-05 14:21:53 +01:00
return CommitRV;
2019-02-04 11:48:08 +01:00
}
2019-02-12 16:01:47 +01:00
WORD EpdBase::Epd2U::EPDReadExtraStatus()
{
2019-02-14 15:56:23 +01:00
byBatADC = bySupplyADC = 0;
/*CommitRV = ReadA2ConfigStatus(
2019-02-12 16:01:47 +01:00
&byBatADC,
&bySupplyADC,
&byStatus3,
&byStatus4);
2019-02-14 15:56:23 +01:00
if (CommitRV == 1) CommitRV = Commit();*/
2019-02-12 16:01:47 +01:00
2019-02-14 15:56:23 +01:00
if (CommitRV == 1) CommitRV = ReadCounts(&D1, &D2, &D3, &D4, &CountsClock);
2019-02-12 16:01:47 +01:00
if (CommitRV == 1) CommitRV = Commit();
2019-02-14 15:56:23 +01:00
if (CommitRV == 1) CommitRV = ReadBaseCounts(&BaseD1, &BaseD2, &BaseD3, &BaseD4, &BaseCountsClock);
if (CommitRV == 1) CommitRV = Commit();
if (CommitRV == 0)
Error = GetErrorDetails(&ErrorSource, &ErrorReason, &ErrorData);
2019-02-12 16:01:47 +01:00
return CommitRV;
}
2019-02-04 11:48:08 +01:00
WORD EpdBase::Epd2U::EPDReadConfig()
{
ReadConfigRV = ReadConfig(
&CountDownTime,
&ReturnTime,
&ChirpRate,
&TeleBaudSetting,
&SelfTestInterval,
&BatteryLoadTestInt,
&GeneralControl,
&CommsTimeout,
&InhibitTimeout,
&MinTxInterval,
&MaxTxIntervals,
&RandomiseWindow,
&DoseIncThresh);
if (ReadConfigRV == 0) Error = GetErrorDetails(&ErrorSource, &ErrorReason, &ErrorData);
CommitRV = Commit();
if (CommitRV == 0) Error = GetErrorDetails(&ErrorSource, &ErrorReason, &ErrorData);
return CommitRV;
}
WORD EpdBase::Epd2U::TurnOff()
{
OffRV = EpdOff();
if (OffRV == 0) Error = GetErrorDetails(&ErrorSource, &ErrorReason, &ErrorData);
EndSessionRV = EndSession(AlarmCtrl,10);
if (EndSessionRV == 0) Error = GetErrorDetails(&ErrorSource, &ErrorReason, &ErrorData);
else {
CommitRV = Commit();
if (CommitRV == 0) Error = GetErrorDetails(&ErrorSource, &ErrorReason, &ErrorData);
}
return CommitRV;
}
WORD EpdBase::Epd2U::TurnOn()
{
//OnRV = EpdOn();
//if (OnRV == 0) Error = GetErrorDetails(&ErrorSource, &ErrorReason, &ErrorData);
EndSessionRV = EndSession(AlarmCtrl, 10);
if (EndSessionRV == 0) {
Error = GetErrorDetails(&ErrorSource, &ErrorReason, &ErrorData);
return 0;
}
/*
CommitRV = Commit();
if (CommitRV == 0) Error = GetErrorDetails(&ErrorSource, &ErrorReason, &ErrorData);*/
return 0;
}
2019-02-12 16:01:47 +01:00
WORD EpdBase::Epd2U::WriteCal()
{
2019-02-14 15:56:23 +01:00
if (K6 > 2000)
{ CommitRV = 0;
return 0;
}
2019-02-12 16:01:47 +01:00
Password = 9;
//((BYTE *)&Password)[0] = 9;
CommitRV = MFRLogin(Password);
if (CommitRV == 1) CommitRV = Commit();
2019-02-14 15:56:23 +01:00
2019-02-12 16:01:47 +01:00
if (CommitRV == 1) CommitRV = MFRWriteCalFactors(K1, K2, K3, K4, K5, K6);
if (CommitRV == 1) CommitRV = Commit();
if (CommitRV == 0) Error = GetErrorDetails(&ErrorSource, &ErrorReason, &ErrorData);
return CommitRV;
}
2019-02-04 11:48:08 +01:00
WORD EpdBase::Epd2U::ReadTresHolds()
{
Hp10THDose1 = Hp10THDose2 = Hp07THDose = 0;
ReadDoseTresholdRV = ReadAlarmDoseThresholds(&Hp10THDose1, &Hp10THDose2, &Hp07THDose);
CommitRV = Commit();
if (CommitRV == 0) Error = GetErrorDetails(&ErrorSource, &ErrorReason, &ErrorData);
ReadAlarmRateThresholdsRV = ReadAlarmRateThresholds(
&Hp10Rate1On,
&Hp10Rate2On,
&Hp07RateOn,
&Hp10Rate1Off,
&Hp10Rate2Off,
&Hp07RateOff);
ReadCalibrationDataRV = ReadCalibrationData(
&K1,
&K2,
&K3,
&K4,
&K5,
&K6,
&SG,
&BC,
&FB,
&HG);
CommitRV = Commit();
if (CommitRV == 0) Error = GetErrorDetails(&ErrorSource, &ErrorReason, &ErrorData);
return CommitRV;
}
WORD EpdBase::Epd2U::ReadDoses()
{
ReadDosesQualityRV = ReadPeaksRV = CommitRV = 0;
2019-02-01 17:35:18 +01:00
ReadDosesQualityRV = ReadDosesQuality(
&Hp10Dose,
&Hp07Dose,
&Hp10Total,
&Hp07Total,
&Knocks,
&Resets,
2019-02-04 11:48:08 +01:00
&QualityData);
if (ReadDosesQualityRV == 0) {
Error = GetErrorDetails(&ErrorSource, &ErrorReason, &ErrorData);
return 0;
}
2019-02-01 17:35:18 +01:00
ReadPeaksRV = ReadPeaks(
&Hp10Peak,
&Hp07Peak,
2019-02-04 11:48:08 +01:00
&Hp10Time,
&Hp07Time);
if (ReadPeaksRV == 0) {
Error = GetErrorDetails(&ErrorSource, &ErrorReason, &ErrorData);
return 0;
}
CommitRV = Commit();
if (CommitRV == 0) Error = GetErrorDetails(&ErrorSource, &ErrorReason, &ErrorData);
2019-02-11 11:34:52 +01:00
ReadCalibrationData(
&K1,
&K2,
&K3,
&K4,
&K5,
&K6,
&SG,
&BC,
&FB,
&HG);
CommitRV = Commit();
if (CommitRV == 0) Error = GetErrorDetails(&ErrorSource, &ErrorReason, &ErrorData);
2019-02-04 11:48:08 +01:00
return CommitRV;
}
WORD EpdBase::Epd2U::PrepareForIssue()
{
CommitRV = ClearDose();
if (CommitRV) CommitRV = ClearTotalDose();
if (CommitRV) CommitRV = ClearPeakRates();
if (CommitRV) CommitRV = Commit();
return CommitRV;
}
2019-02-04 11:48:08 +01:00
WORD EpdBase::Epd2U::DeIssue()
{
ClearDoseRV = ClearTotalDoseRV = ClearPeakRatesRV = DeIssueEpdRV = 0;
CommitRV = ClearFailFlag();
if (CommitRV) CommitRV = ClearFaultFlags();
if (CommitRV) CommitRV = ClearLatchedAlarms();
if (CommitRV) CommitRV = Commit();
if (CommitRV) CommitRV = ClearDose();
if (CommitRV) CommitRV = ClearTotalDose();
if (CommitRV) CommitRV = ClearPeakRates();
if (CommitRV) CommitRV = DeIssueEpd();
if (CommitRV) CommitRV = Commit();
if (CommitRV && DetectorsOn) {
CommitRV = EpdOff();
CommitRV= Commit();
Sleep(1000);// Versie < .. Commit & Sleep
}
if (CommitRV) { EndSession(0x16, 12); Commit(); }// Continuous high tone for 1.2 seconds
else
{
FlushBuffers(port);
Error = GetErrorDetails(&ErrorSource, &ErrorReason, &ErrorData); EndSession(0x5E, 40); return 0;
}
2019-02-11 11:34:52 +01:00
//DeinitComms(port);
2019-02-04 11:48:08 +01:00
return CommitRV;
}
WORD EpdBase::Epd2U::Issue()
{
IssueEpdRV = WriteAlarmDoseThresholdsRV = WriteAlarmRateThresholdsRV = WriteWearerNameRV = OnRV = CommitRV = 0;
DWORD OKRV = SetFailFlag(); // all actions have been completed
if (OKRV) OKRV = Commit();
if (OKRV) OKRV = WriteAlarmDoseThresholds(Hp10THDose1, Hp10THDose2, Hp07THDose);
if (OKRV) OKRV = WriteAlarmRateThresholds(Hp10Rate1On, Hp10Rate2On, Hp07RateOn, Hp10Rate1Off, Hp10Rate2Off, Hp07RateOff);
if (OKRV) OKRV = Commit();
if (OKRV) OKRV = IssueEpd(WearerID, WearerIDLength);
if (OKRV) OKRV = Commit();
2019-02-12 16:01:47 +01:00
if (OKRV) OKRV = WriteWearerName(WearerName,strlen((char *)WearerName));
2019-02-11 11:34:52 +01:00
if (OKRV) OKRV = Commit();
2019-02-14 15:56:23 +01:00
if (OKRV) OKRV = BaseLineCounts();
if (OKRV == 0)
{
Error = GetErrorDetails(&ErrorSource, &ErrorReason, &ErrorData);
}
ClearFailFlag(); // all actions have been completed
Commit();
2019-02-04 11:48:08 +01:00
if (!DetectorsOn) {
2020-02-05 14:21:53 +01:00
OKRV = EpdOn(); OKRV=Commit(); Sleep(1500);
2019-02-04 11:48:08 +01:00
}
if (OKRV) {
EndSession(0x16, 12);
Commit();
} // Continuous high tone for 1.2 seconds
else
{
FlushBuffers(port);
2019-02-14 15:56:23 +01:00
Error = GetErrorDetails(&ErrorSource, &ErrorReason, &ErrorData);
EndSession(0x5E, 40);
2019-02-04 11:48:08 +01:00
}
2019-02-11 11:34:52 +01:00
//DeinitComms(port);
2019-02-04 11:48:08 +01:00
return OKRV;
2019-02-01 09:54:48 +01:00
}