-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathpdu.h
82 lines (70 loc) · 2.01 KB
/
pdu.h
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
#ifndef PDU_H
#define PDU_H
#include <QtCore>
inline quint16 toUInt16(const QByteArray &array, int offset)
{
return static_cast<quint16>(
(static_cast<quint8>(array[offset]) << 8) |
static_cast<quint8>(array[offset + 1]));
}
inline void appendUInt16(QByteArray &array, quint16 v) {
array.append(static_cast<char>(v >> 8));
array.append(static_cast<char>(v & 0xFF));
}
class PDU
{
public:
PDU();
PDU(const QByteArray & pduRequest);
enum FunctionCode
{
ReadCoils = 1,
ReadDiscreteInputs = 2,
ReadHoldingRegisters = 3,
ReadInputRegisters = 4,
WriteSingleCoil = 5,
WriteSingleRegister = 6,
WriteMultipleCoils = 15,
WriteMultipleRegisters = 16,
ReadFileRecord = 20,
WriteFileRecord = 21,
MaskWriteRegister = 22,
ReadWriteMultipleRegisters = 23,
ReadFIFOQueue = 24,
EncapsulatedInterfaceTransport = 43,
};
enum ExceptionCode
{
NoExeption = 0,
IllegalFunction = 1,
IllegalDataAddress = 2,
IllegalDataValue = 3,
SlaveDeviceFailure = 4,
Acknowledge = 5,
SlaveDeviceBusy = 6,
MemoryParityError = 7,
GatewayPathUnavailable = 10,
GatewayTargetDeviceFailedToRespond = 11
};
quint8 getFunctionCode() const { return mFunctionCode; }
QByteArray & getData() { return mData; }
int getDataSize() const { return mData.size(); }
quint16 getAddres() const { return toUInt16(mData, 0); }
quint16 getQuantity() const { return toUInt16(mData, 2); }
quint8 getByteCount() const { return static_cast<quint8>(mData[4]); }
ExceptionCode getExceptionCode() const { return mExeptionCode; }
void setData(const QByteArray & data);
void setExceptionCode(ExceptionCode code);
//Helpers
QString pduToString() const;
private:
quint8 mFunctionCode;
ExceptionCode mExeptionCode;
QByteArray mData;
// Helpers
static const QMap<int,QString> initFunctionMap();
static const QMap<int,QString> functionMap;
static const QMap<int,QString> initExceptionMap();
static const QMap<int,QString> exceptionMap;
};
#endif // PDU_H