-
Notifications
You must be signed in to change notification settings - Fork 0
/
modbusformat.h
208 lines (181 loc) · 4.32 KB
/
modbusformat.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
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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
#ifndef MODBUSFORMAT_H
#define MODBUSFORMAT_H
#include <type_traits>
//默认本机为大端或者小端 ,本机如果为其他字节序不支持转换
class ModBusFormat {
private:
union {
short value = 0x0001;
bool isLittleEndian : 1;
};
enum Endian {
Endian_ABCD,
Endian_CDAB,
Endian_DCBA,
Endian_BADC,
};
int serverEndian = Endian_CDAB;
public:
ModBusFormat() = default;
void setServerEndian(int e = Endian_CDAB)
{
serverEndian = e;
}
int getServerEndian()
{
return serverEndian;
}
int getClientEndian()
{
return isLittleEndian ? Endian_ABCD : Endian_DCBA;
}
template <typename T>
T toClient(T v) const
{
switch (serverEndian) {
case Endian_ABCD:
return fromABCD(v);
break;
case Endian_CDAB:
return fromCDAB(v);
break;
case Endian_DCBA:
return fromDCBA(v);
break;
case Endian_BADC:
return fromBADC(v);
break;
default:
return v;
}
}
template <typename T>
T toServer(T v) const
{
switch (serverEndian) {
case Endian_ABCD:
return toABCD(v);
break;
case Endian_CDAB:
return toCDAB(v);
break;
case Endian_DCBA:
return toDCBA(v);
break;
case Endian_BADC:
return toBADC(v);
break;
default:
return v;
}
}
template <typename T>
T toABCD(T v) const // to ABCD
{
if (isLittleEndian) { //DCBA => ABCD
return swapByte(v);
} else { // ABCD => ABCD
return v;
}
}
template <typename T>
T toCDAB(T v) const
{
if (isLittleEndian) {
return swapByte(swapByte(v), 2);
} else {
return swapByte(v, 2);
}
}
template <typename T>
T toDCBA(T v) const
{
if (isLittleEndian) {
return v;
} else {
return swapByte(v);
}
}
template <typename T, int N = sizeof(T)>
T toBADC(T v) const
{
if (N <= 2) {
return isLittleEndian ? swapByte(v) : swapByte(v);
}
if (isLittleEndian) { // DCBA ->BADC
return swapByte(v, 2);
} else { //ABCD => BADC
return swapByte(swapByte(v), 2);
}
}
template <typename T>
T fromDCBA(T v) const
{
if (isLittleEndian) { // DCBA => DCBA
return v;
} else { //DCBA => ABCD
return swapByte(v);
}
}
template <typename T>
T fromBADC(T v) const
{
if (isLittleEndian) { // BADC => DCBA
return swapByte(v, 2);
} else { //DCBA => ABCD
return swapByte(swapByte(v, 2));
}
}
template <typename T>
T fromCDAB(T v) const
{
if (isLittleEndian) { // CDAB => DCBA
return swapByte(swapByte(v, 2));
} else { //CDAB => ABCD
return swapByte(v, 2);
}
}
template <typename T>
T fromABCD(T v) const
{
if (isLittleEndian) { // ABCD => DCBA
return swapByte(v);
} else { //ABCD => ABCD
return v;
}
}
template <typename T, int N = sizeof(T)>
T swapByte(T v, int n = 1) const // n个字节为一组, 前n个字节和后面n个字节交换
{
if (N <= n)
return v;
int end = N / n - 1;
switch (n) {
case 1: {
char* byte = reinterpret_cast<char*>(&v);
int start = 0;
while (start < end) {
std::swap(byte[start++], byte[end--]);
}
} break;
case 2: {
short* byte = reinterpret_cast<short*>(&v);
int start = 0;
while (start < end) {
std::swap(byte[start++], byte[end--]);
}
} break;
case 4: {
int* byte = reinterpret_cast<int*>(&v);
int start = 0;
while (start < end) {
std::swap(byte[start++], byte[end--]);
}
} break;
default:
break;
}
return v;
}
};
#endif // MODBUSFORMAT_H