UniSet
2.32.1
extensions
include
VTypes.h
1
/*
2
* Copyright (c) 2015 Pavel Vainerman.
3
*
4
* This program is free software: you can redistribute it and/or modify
5
* it under the terms of the GNU Lesser General Public License as
6
* published by the Free Software Foundation, version 2.1.
7
*
8
* This program is distributed in the hope that it will be useful, but
9
* WITHOUT ANY WARRANTY; without even the implied warranty of
10
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11
* Lesser General Lesser Public License for more details.
12
*
13
* You should have received a copy of the GNU Lesser General Public License
14
* along with this program. If not, see <http://www.gnu.org/licenses/>.
15
*/
16
// --------------------------------------------------------------------------
17
// --------------------------------------------------------------------------
18
#ifndef _RTUTypes_H_
19
#define _RTUTypes_H_
20
// -----------------------------------------------------------------------------
21
#include <string>
22
#include <stdint.h>
23
#include <cmath>
24
#include <cstring>
25
#include <ostream>
26
#include "modbus/ModbusTypes.h"
27
// -----------------------------------------------------------------------------
28
namespace
uniset
29
{
30
// -----------------------------------------------------------------------------
31
namespace
VTypes
32
{
34
enum
VType
35
{
36
vtUnknown,
37
vtF2,
38
vtF2r,
39
vtF4,
40
vtByte,
41
vtUnsigned,
42
vtSigned,
43
vtI2,
44
vtI2r,
45
vtU2,
46
vtU2r
47
};
48
49
std::ostream& operator<<( std::ostream& os,
const
VType& vt );
50
51
// -------------------------------------------------------------------------
52
std::string type2str( VType t )
noexcept
;
53
VType str2type(
const
std::string& s )
noexcept
;
54
int
wsize( VType t )
noexcept
;
55
// -------------------------------------------------------------------------
56
class
F2
57
{
58
public
:
59
60
// ------------------------------------------
61
static
const
size_t
f2Size = 2;
63
typedef
union
64
{
65
uint16_t v[f2Size];
66
float
val;
//
67
}
F2mem
;
68
// ------------------------------------------
69
// конструкторы на разные случаи...
70
F2
() noexcept
71
{
72
memset(raw.v, 0,
sizeof
(raw.v));
73
}
74
75
F2
(
const
float
& f )
noexcept
76
{
77
raw.val = f;
78
}
79
F2(
const
ModbusRTU::ModbusData* data,
size_t
size )
noexcept
80
{
81
for
(
size_t
i = 0; i <
wsize
() && i < size; i++ )
82
raw.v[i] = data[i];
83
}
84
85
~F2() noexcept {}
86
// ------------------------------------------
88
static
size_t
wsize
()
89
{
90
return
f2Size;
91
}
93
static
VType
type
()
94
{
95
return
vtF2;
96
}
97
// ------------------------------------------
98
operator
float()
99
{
100
return
raw.val;
101
}
102
operator
long()
103
{
104
return
lroundf(raw.val);
105
}
106
operator
int()
107
{
108
return
lroundf(raw.val);
109
}
110
111
F2mem raw;
112
};
113
// --------------------------------------------------------------------------
114
class
F2r
:
115
public
F2
116
{
117
public
:
118
119
// ------------------------------------------
120
// конструкторы на разные случаи...
121
F2r
()
noexcept
122
{
123
raw_backorder.val = 0;
124
}
125
126
F2r
(
const
float
& f )
noexcept
:
F2
(f)
127
{
128
raw_backorder = raw;
129
std::swap(raw_backorder.v[0], raw_backorder.v[1]);
130
}
131
F2r
(
const
ModbusRTU::ModbusData* data,
size_t
size )
noexcept
:
F2
(data, size)
132
{
133
// принимаем в обратном порядке.. поэтому переворачиваем raw
134
raw_backorder = raw;
135
std::swap(raw.v[0], raw.v[1]);
136
}
137
138
~F2r
()
noexcept
{}
139
140
F2mem
raw_backorder;
141
};
142
// --------------------------------------------------------------------------
143
class
F4
144
{
145
public
:
146
// ------------------------------------------
147
static
const
size_t
f4Size = 4;
149
typedef
union
150
{
151
uint16_t v[f4Size];
152
double
val;
//
153
}
F4mem
;
154
// ------------------------------------------
155
// конструкторы на разные случаи...
156
F4
() noexcept
157
{
158
memset(raw.v, 0,
sizeof
(raw.v));
159
}
160
161
F4
(
const
double
& f )
noexcept
162
{
163
memset(raw.v, 0,
sizeof
(raw.v));
164
raw.val = f;
165
}
166
F4(
const
ModbusRTU::ModbusData* data,
size_t
size )
noexcept
167
{
168
for
(
size_t
i = 0; i <
wsize
() && i < size; i++ )
169
raw.v[i] = data[i];
170
}
171
172
~F4() noexcept {}
173
// ------------------------------------------
175
static
size_t
wsize
()
176
{
177
return
f4Size;
178
}
180
static
VType
type
()
181
{
182
return
vtF4;
183
}
184
// ------------------------------------------
185
operator
double()
186
{
187
return
raw.val;
188
}
189
operator
long()
190
{
191
return
lroundf(raw.val);
192
}
193
194
F4mem raw;
195
};
196
// --------------------------------------------------------------------------
197
class
Byte
198
{
199
public
:
200
201
static
const
size_t
bsize = 2;
202
203
// ------------------------------------------
205
typedef
union
206
{
207
uint16_t w;
208
uint8_t b[bsize];
209
}
Bytemem
;
210
// ------------------------------------------
211
// конструкторы на разные случаи...
212
Byte
() noexcept
213
{
214
raw.w = 0;
215
}
216
217
Byte
( uint8_t b1, uint8_t b2 )
noexcept
218
{
219
raw.b[0] = b1;
220
raw.b[1] = b2;
221
}
222
223
Byte(
const
ModbusRTU::ModbusData dat )
noexcept
224
{
225
raw.w = dat;
226
}
227
228
~Byte() noexcept {}
229
// ------------------------------------------
231
static
size_t
wsize
()
232
{
233
return
1;
234
}
236
static
VType
type
()
237
{
238
return
vtByte;
239
}
240
// ------------------------------------------
241
operator
uint16_t()
242
{
243
return
raw.w;
244
}
245
246
uint8_t operator[](
const
size_t
i )
247
{
248
return
raw.b[i];
249
}
250
251
Bytemem raw;
252
};
253
// --------------------------------------------------------------------------
254
class
Unsigned
255
{
256
public
:
257
258
// ------------------------------------------
259
// конструкторы на разные случаи...
260
Unsigned
()
noexcept
: raw(0) {}
261
262
Unsigned
(
const
long
& val )
noexcept
263
{
264
raw = val;
265
}
266
267
Unsigned
(
const
ModbusRTU::ModbusData dat )
noexcept
268
{
269
raw = dat;
270
}
271
272
~Unsigned
()
noexcept
{}
273
// ------------------------------------------
275
static
size_t
wsize
()
276
{
277
return
1;
278
}
280
static
VType
type
()
281
{
282
return
vtUnsigned;
283
}
284
// ------------------------------------------
285
operator
long()
286
{
287
return
raw;
288
}
289
// ------------------------------------------
290
bool
operator==(
const
int
& a)
const
291
{
292
return
(
int
)raw == a;
293
}
294
bool
operator==(
const
long
& a)
const
295
{
296
return
(
long
)raw == a;
297
}
298
299
uint16_t raw;
300
};
301
// --------------------------------------------------------------------------
302
class
Signed
303
{
304
public
:
305
306
// ------------------------------------------
307
// конструкторы на разные случаи...
308
Signed
()
noexcept
: raw(0) {}
309
310
Signed
(
const
long
& val )
noexcept
311
{
312
raw = val;
313
}
314
315
Signed
(
const
ModbusRTU::ModbusData dat )
noexcept
316
{
317
raw = dat;
318
}
319
320
~Signed
()
noexcept
{}
321
// ------------------------------------------
323
static
size_t
wsize
()
324
{
325
return
1;
326
}
328
static
VType
type
()
329
{
330
return
vtSigned;
331
}
332
// ------------------------------------------
333
operator
long()
334
{
335
return
raw;
336
}
337
// ------------------------------------------
338
bool
operator== (
const
int
& a)
const
339
{
340
return
(
int
)raw == a;
341
}
342
bool
operator== (
const
long
& a)
const
343
{
344
return
(
long
)raw == a;
345
}
346
347
int16_t raw;
348
};
349
// --------------------------------------------------------------------------
350
class
I2
351
{
352
public
:
353
354
// ------------------------------------------
355
static
const
size_t
i2Size = 2;
357
typedef
union
358
{
359
uint16_t v[i2Size];
360
int32_t val;
//
361
}
I2mem
;
362
// ------------------------------------------
363
// конструкторы на разные случаи...
364
I2
() noexcept
365
{
366
memset(raw.v, 0,
sizeof
(raw.v));
367
}
368
369
I2
(
const
int32_t& v )
noexcept
370
{
371
raw.val = v;
372
}
373
I2(
const
ModbusRTU::ModbusData* data,
size_t
size )
noexcept
374
{
375
for
(
size_t
i = 0; i <
wsize
() && i < size; i++ )
376
raw.v[i] = data[i];
377
}
378
379
~I2() noexcept {}
380
// ------------------------------------------
382
static
size_t
wsize
()
383
{
384
return
i2Size;
385
}
387
static
VType
type
()
388
{
389
return
vtI2;
390
}
391
// ------------------------------------------
392
operator
int32_t()
393
{
394
return
raw.val;
395
}
396
397
I2mem raw;
398
};
399
// --------------------------------------------------------------------------
400
class
I2r
:
401
public
I2
402
{
403
public
:
404
I2r
()
noexcept
405
{
406
raw_backorder.val = 0;
407
}
408
409
I2r
(
const
int32_t& v )
noexcept
:
I2
(v)
410
{
411
raw_backorder = raw;
412
std::swap(raw_backorder.v[0], raw_backorder.v[1]);
413
}
414
415
I2r
(
const
ModbusRTU::ModbusData* data,
size_t
size )
noexcept
:
I2
(data, size)
416
{
417
// принимаем в обратном порядке.. поэтому переворачиваем raw
418
raw_backorder = raw;
419
std::swap(raw.v[0], raw.v[1]);
420
}
421
422
~I2r
()
noexcept
{}
423
424
I2mem
raw_backorder;
425
};
426
// --------------------------------------------------------------------------
427
class
U2
428
{
429
public
:
430
431
// ------------------------------------------
432
static
const
size_t
u2Size = 2;
434
typedef
union
435
{
436
uint16_t v[u2Size];
437
uint32_t val;
//
438
}
U2mem
;
439
// ------------------------------------------
440
// конструкторы на разные случаи...
441
U2
() noexcept
442
{
443
memset(raw.v, 0,
sizeof
(raw.v));
444
}
445
446
U2
(
const
uint32_t& v )
noexcept
447
{
448
raw.val = v;
449
}
450
U2(
const
ModbusRTU::ModbusData* data,
size_t
size )
noexcept
451
{
452
for
(
size_t
i = 0; i <
wsize
() && i < size; i++ )
453
raw.v[i] = data[i];
454
}
455
456
~U2() noexcept {}
457
// ------------------------------------------
459
static
size_t
wsize
()
460
{
461
return
u2Size;
462
}
464
static
VType
type
()
465
{
466
return
vtU2;
467
}
468
// ------------------------------------------
469
operator
uint32_t()
470
{
471
return
raw.val;
472
}
473
474
operator
long()
475
{
476
return
raw.val;
477
}
478
479
operator
unsigned
long()
480
{
481
return
(uint32_t)raw.val;
482
}
483
484
U2mem raw;
485
};
486
// --------------------------------------------------------------------------
487
class
U2r
:
488
public
U2
489
{
490
public
:
491
U2r
()
noexcept
492
{
493
raw_backorder.val = 0;
494
}
495
496
U2r
(
const
uint32_t& v )
noexcept
:
U2
(v)
497
{
498
raw_backorder = raw;
499
std::swap(raw_backorder.v[0], raw_backorder.v[1]);
500
}
501
502
U2r
(
const
ModbusRTU::ModbusData* data,
size_t
size )
noexcept
:
U2
(data, size)
503
{
504
// принимаем в обратном порядке.. поэтому переворачиваем raw
505
raw_backorder = raw;
506
std::swap(raw.v[0], raw.v[1]);
507
}
508
509
~U2r
() {}
510
511
U2mem
raw_backorder;
512
};
513
// --------------------------------------------------------------------------
514
515
}
// end of namespace VTypes
516
// --------------------------------------------------------------------------
517
}
// end of namespace uniset
518
// --------------------------------------------------------------------------
519
#endif
// _RTUTypes_H_
520
// -----------------------------------------------------------------------------
uniset::VTypes::Byte
Definition
VTypes.h:198
uniset::VTypes::Byte::type
static VType type()
Definition
VTypes.h:236
uniset::VTypes::Byte::wsize
static size_t wsize()
Definition
VTypes.h:231
uniset::VTypes::F2
Definition
VTypes.h:57
uniset::VTypes::F2::wsize
static size_t wsize()
Definition
VTypes.h:88
uniset::VTypes::F2::type
static VType type()
Definition
VTypes.h:93
uniset::VTypes::F2r
Definition
VTypes.h:116
uniset::VTypes::F4
Definition
VTypes.h:144
uniset::VTypes::F4::wsize
static size_t wsize()
Definition
VTypes.h:175
uniset::VTypes::F4::type
static VType type()
Definition
VTypes.h:180
uniset::VTypes::I2
Definition
VTypes.h:351
uniset::VTypes::I2::wsize
static size_t wsize()
Definition
VTypes.h:382
uniset::VTypes::I2::type
static VType type()
Definition
VTypes.h:387
uniset::VTypes::I2r
Definition
VTypes.h:402
uniset::VTypes::Signed
Definition
VTypes.h:303
uniset::VTypes::Signed::type
static VType type()
Definition
VTypes.h:328
uniset::VTypes::Signed::wsize
static size_t wsize()
Definition
VTypes.h:323
uniset::VTypes::U2
Definition
VTypes.h:428
uniset::VTypes::U2::type
static VType type()
Definition
VTypes.h:464
uniset::VTypes::U2::wsize
static size_t wsize()
Definition
VTypes.h:459
uniset::VTypes::U2r
Definition
VTypes.h:489
uniset::VTypes::Unsigned
Definition
VTypes.h:255
uniset::VTypes::Unsigned::type
static VType type()
Definition
VTypes.h:280
uniset::VTypes::Unsigned::wsize
static size_t wsize()
Definition
VTypes.h:275
uniset
Definition
Calibration.h:27
uniset::VTypes::Byte::Bytemem
Definition
VTypes.h:206
uniset::VTypes::F2::F2mem
Definition
VTypes.h:64
uniset::VTypes::F4::F4mem
Definition
VTypes.h:150
uniset::VTypes::I2::I2mem
Definition
VTypes.h:358
uniset::VTypes::U2::U2mem
Definition
VTypes.h:435
Документация по UniSet. Последние изменения: Ср 19 Июл 2023 21:17:14. Создано системой
1.10.0