/
P1MeterSerial.cpp
168 lines (146 loc) · 3.6 KB
/
P1MeterSerial.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
#include "stdafx.h"
#include "P1MeterSerial.h"
#include "../main/Logger.h"
#include "../main/Helper.h"
#include "../main/SQLHelper.h"
#include "../main/mainworker.h"
#include "../main/WebServer.h"
#include "../webserver/cWebem.h"
//NOTE!!!, this code is partly based on the great work of RHekkers:
//https://github.com/rhekkers
#include <string>
#include <algorithm>
#include <iostream>
#include <ctime>
//
//Class P1MeterSerial
//
P1MeterSerial::P1MeterSerial(const int ID, const std::string& devname, const unsigned int baud_rate, const bool disable_crc, const int ratelimit, const std::string& DecryptionKey):
m_szSerialPort(devname)
{
m_HwdID=ID;
m_iBaudRate=baud_rate;
m_bDisableCRC = disable_crc;
m_ratelimit = ratelimit;
if (!DecryptionKey.empty())
{
m_bIsEncrypted = true;
m_szHexKey = HexToBytes(DecryptionKey);
}
}
P1MeterSerial::P1MeterSerial(const std::string& devname,
unsigned int baud_rate,
boost::asio::serial_port_base::parity opt_parity,
boost::asio::serial_port_base::character_size opt_csize,
boost::asio::serial_port_base::flow_control opt_flow,
boost::asio::serial_port_base::stop_bits opt_stop)
:AsyncSerial(devname,baud_rate,opt_parity,opt_csize,opt_flow,opt_stop),
m_iBaudRate(baud_rate)
{
}
#ifdef _DEBUG
//#define DEBUG_FROM_FILE
#endif
bool P1MeterSerial::StartHardware()
{
RequestStart();
#ifdef DEBUG_FROM_FILE
FILE *fIn=fopen("E:\\meter.txt","rb+");
BYTE buffer[1400];
int ret=fread((BYTE*)&buffer,1,sizeof(buffer),fIn);
fclose(fIn);
ParseData((const BYTE*)&buffer, ret, 1);
#endif
//Try to open the Serial Port
try
{
Log(LOG_STATUS,"Using serial port: %s", m_szSerialPort.c_str());
if (m_iBaudRate==9600)
{
open(
m_szSerialPort,
m_iBaudRate,
boost::asio::serial_port_base::parity(
boost::asio::serial_port_base::parity::even),
boost::asio::serial_port_base::character_size(7)
);
}
else
{
//DSMRv4
open(
m_szSerialPort,
m_iBaudRate,
boost::asio::serial_port_base::parity(
boost::asio::serial_port_base::parity::none),
boost::asio::serial_port_base::character_size(8)
);
if (m_bDisableCRC) {
Log(LOG_STATUS,"CRC validation disabled through hardware control");
}
}
}
catch (boost::exception & e)
{
Log(LOG_ERROR,"Error opening serial port!");
#ifdef _DEBUG
Log(LOG_ERROR,"-----------------\n%s\n-----------------",boost::diagnostic_information(e).c_str());
#else
(void)e;
#endif
return false;
}
catch ( ... )
{
Log(LOG_ERROR,"Error opening serial port!!!");
return false;
}
Init();
m_bIsStarted=true;
m_thread = std::make_shared<std::thread>([this] { Do_Work(); });
SetThreadNameInt(m_thread->native_handle());
setReadCallback([this](auto d, auto l) { readCallback(d, l); });
sOnConnected(this);
return true;
}
bool P1MeterSerial::StopHardware()
{
if (m_thread)
{
RequestStop();
m_thread->join();
m_thread.reset();
}
m_bIsStarted = false;
return true;
}
void P1MeterSerial::readCallback(const char *data, size_t len)
{
if (!m_bEnableReceive)
return; //receiving not enabled
ParseP1Data((const unsigned char*)data, static_cast<int>(len), m_bDisableCRC, m_ratelimit);
}
bool P1MeterSerial::WriteToHardware(const char *pdata, const unsigned char length)
{
return false;
}
void P1MeterSerial::Do_Work()
{
int sec_counter = 0;
int msec_counter = 0;
Log(LOG_STATUS, "Worker started...");
while (!IsStopRequested(200))
{
msec_counter++;
if (msec_counter == 5)
{
msec_counter = 0;
sec_counter++;
if (sec_counter % 12 == 0) {
m_LastHeartbeat = mytime(nullptr);
}
}
}
terminate();
Log(LOG_STATUS, "Worker stopped...");
}