-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathqcodesysnv.h
417 lines (341 loc) · 11.6 KB
/
qcodesysnv.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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
#ifndef QCODESYSNV_H
#define QCODESYSNV_H
#include <QList>
#include <QVector>
#include <vector>
#include <QObject>
#include <QUdpSocket>
#include <QByteArray>
#include <type_traits>
#include <QDataStream>
using namespace std;
//Socket which handles QCodesysNVTelegrams
class QCodesysNVSocket;
//CoDeSys network variable telegram
class QCodesysNVTelegram;
//types of CoDeSys network variables
class QCodesysNVType;
//this class includes functions which convert
template <typename Type> class QCodesysNVConvert;
//variable types in the computer
enum class QCodesysNVcmpType {_int8,_uint8,_int16,_uint16,_int32,_uint32,_int64,_uint64,_float,_double};
//this class handles receiving and sending of QCodesysNVTelegrams
class QCodesysNVSocket : public QObject
{
Q_OBJECT
public:
explicit QCodesysNVSocket(QHostAddress ip,quint16 localPort,QObject *parent = 0);
//telegram can be broadcasted by using pointer to telegram or element of telegramList
//note that index is might not be constant if removeTelegram(int index) is used
int broadcastTelegram(QCodesysNVTelegram* telegram);
int broadcastTelegram(int i);
//this adds the telegram to telegramList
void addTelegram(QCodesysNVTelegram* telegram);
//return pointer to telegram
//note that index is might not be constant if removeTelegram(int index) is used
QCodesysNVTelegram* getTelegram(int i);
//number of telegrams
int telegramCount();
//clear telegramList
void clearTelegrams();
void replaceTelegram(int i,QCodesysNVTelegram* telegram);
//removes telegram from teh list
//note that index is might not be constant if removeTelegram(int index) is used
void removeTelegram(int i);
signals:
//these signals are emitted when UDP package or any codesys telegram is received
void udpPackageReceived(QString IP, unsigned int port, unsigned int bytes);
void codesysTelegramReceived(QString IP, unsigned int port, unsigned int bytes, unsigned int cobid);
public slots:
//data available in UDP socket
void readyRead();
private:
//this is a list of pointers to telegrams. This class check if a CobId of incoming telegram corresponds to any of those in the list
QList <QCodesysNVTelegram*> telegramList;
//identity (first bytes) of the real CoDeSys telegram
QByteArray telegramIdentity;
//standard udp socket in QT
QUdpSocket *socket;
//IP address and port limitations for incoming telegrams
QHostAddress ip;
quint16 port;
};
//this is the telegram which is received or broadcasted via UDP
class QCodesysNVTelegram : public QObject
{
Q_OBJECT
public:
explicit QCodesysNVTelegram(bool telegramWritable = 0, QObject *parent = 0);
int setVariableTypes(const QList<QCodesysNVType>& variables);
int setVariableTypes(QString variables);
void clearVariableTypes(void);
QList<QCodesysNVType> readVariableTypes(void);
void readVariableTypes(QString& variables);
void readVariableTypes(QList<QCodesysNVType>& variables);
int variableCount(void);
int stringToData(QString dataString);
void dataToString(QString& dataString);
int stringListToData(QStringList& dataStringList);
void dataToStringList(QStringList& dataStrings);
void parseTelegram(QByteArray telegram, QHostAddress IP, quint16 port);
//read properties of the telegram
QHostAddress readIP(void);
quint16 readPort(void);
QString readIdentity(void);
quint32 readId(void);
quint16 readCobId(void);
quint16 readSubIndex(void);
quint16 readItems(void);
quint16 readLength(void);
quint16 readCounter(void);
quint8 readFlags(void);
quint8 readChecksum(void);
void readTelegram(QByteArray& data);
//set properties of the telegram
//part of them can be set only if the telegram is writable
int setIP(QHostAddress IP);
int setPort(quint16 port);
int setId(quint32 ID);
void setCobId(quint16 cobID);
int setSubIndex(quint16 subIndex);
int setCounter(quint16 counter);
int setFlags(quint8 flags);
int setChecksum(quint8 checksum);
//check if telegram is writable
bool isBroadcastable(void);
//check if telegram is updated
bool isUpdated(void);
//read data from variable
//this SHOULD be safe so that you can use any Type
template <typename Type>
void readData(int index, Type &variable);
//read data from variable
//this SHOULD be safe so that you can use any variable Type
template <typename Type>
void setData(int index, Type variable);
private:
void recalculateDataPositions();
void initializeTelegram(void);
QHostAddress IP;
quint16 port;
QByteArray identity;
quint32 id;
quint16 cob_id;
quint16 subindex;
quint16 items;
quint16 length;
quint16 counter;
quint8 flags;
quint8 checksum;
quint8 totalDataLength;
QList<int> dataPositions;
QList<int> dataLengths;
bool broadcastable;
bool dataUpdated;
QByteArray data;
void dataVarToString(int index,QString& variableString);
void stringToDataVar(int index,QString variableString);
QList <QCodesysNVType> variableTypeList;
signals:
void updated(void);
};
template <typename Type>
void QCodesysNVTelegram::readData(int index, Type &variable){
if(index<variableTypeList.length())
{
QCodesysNVConvert<Type>::convertFromBytes(data.mid((20+dataPositions[index]),dataLengths[index]), variableTypeList[index],variable);
}
else
{
qDebug() << "QCodesysNVTelegram::readData: wrong index number";
}
}
template <typename Type>
void QCodesysNVTelegram::setData(int index, Type variable){
if(index<variableTypeList.length())
{
QByteArray buffer;
QCodesysNVConvert<Type>::convertToBytes(buffer, variableTypeList[index], variable);
data.replace((20+dataPositions[index]),dataLengths[index],buffer);
}
else
{
qDebug() << "QCodesysNVTelegram::readData: wrong index number";
}
}
class QCodesysNVType {
public:
//codesys variable types
static const QCodesysNVType BOOL;
static const QCodesysNVType BYTE;
static const QCodesysNVType WORD;
static const QCodesysNVType DWORD;
static const QCodesysNVType SINT;
static const QCodesysNVType USINT;
static const QCodesysNVType INT;
static const QCodesysNVType UINT;
static const QCodesysNVType DINT;
static const QCodesysNVType UDINT;
static const QCodesysNVType REAL;
static const QCodesysNVType LREAL;
//list of all codesys varirable types
static const QList <QCodesysNVType> typeList;
//overloaded operators for comparing variable types
friend bool operator== (const QCodesysNVType& lhs, const QCodesysNVType& rhs){
return (lhs.index == rhs.index);
}
friend bool operator!= (const QCodesysNVType& lhs, const QCodesysNVType& rhs){
return !(lhs == rhs);
}
quint32 index;
quint32 size;
QCodesysNVcmpType varType;
QString name;
private:
//variable type is defined as object which has following properties
QCodesysNVType(quint32 i,QString name, quint8 size,QCodesysNVcmpType varType){
this->index=i; //unique index number
this->name=name; //string name for parsing types from string list
this->size=size; //length in bytes
this->varType=varType; //variable type in standard computer variables
}
};
//Static class which convert variables to bytearray and vice versa
template <class Type>
class QCodesysNVConvert {
public:
static void convertFromBytes(QByteArray bytes, QCodesysNVType netType, Type& variable);
static void convertToBytes(QByteArray& bytes, QCodesysNVType netType, Type variable);
};
//convert bytearray from telegram to any variable type (by using casting if necessary)
template<typename Type>
void QCodesysNVConvert<Type>::convertFromBytes(QByteArray bytes, QCodesysNVType cmpType, Type& variable)
{
QDataStream stream(bytes);
stream.setByteOrder(QDataStream::LittleEndian);
if(cmpType.varType==QCodesysNVcmpType::_uint8)
{
quint8 tempVar;
stream >> tempVar;
variable = (Type) tempVar;
}
else if(cmpType.varType==QCodesysNVcmpType::_int8)
{
qint8 tempVar;
stream >> tempVar;
variable = (Type) tempVar;
}
else if(cmpType.varType==QCodesysNVcmpType::_uint16)
{
quint16 tempVar;
stream >> tempVar;
variable = (Type) tempVar;
}
else if(cmpType.varType==QCodesysNVcmpType::_int16)
{
qint16 tempVar;
stream >> tempVar;
variable = (Type) tempVar;
}
else if(cmpType.varType==QCodesysNVcmpType::_uint32)
{
quint32 tempVar;
stream >> tempVar;
variable = (Type) tempVar;
}
else if(cmpType.varType==QCodesysNVcmpType::_int32)
{
qint32 tempVar;
stream >> tempVar;
variable = (Type) tempVar;
}
else if(cmpType.varType==QCodesysNVcmpType::_uint64)
{
quint64 tempVar;
stream >> tempVar;
variable = (Type) tempVar;
}
else if(cmpType.varType==QCodesysNVcmpType::_int64)
{
qint64 tempVar;
stream >> tempVar;
variable = (Type) tempVar;
}
else if(cmpType.varType==QCodesysNVcmpType::_float)
{
stream.setFloatingPointPrecision(QDataStream::SinglePrecision);
float tempVar;
stream >> tempVar;
variable = (Type) tempVar;
}
else if(cmpType.varType==QCodesysNVcmpType::_double)
{
stream.setFloatingPointPrecision(QDataStream::DoublePrecision);
double tempVar;
stream >> tempVar;
variable = (Type) tempVar;
}
}
//convert any variable type (by using casting if necessary) to QByteArray format which is suitable for Codesys UDP telegram
template <typename Type>
void QCodesysNVConvert<Type>::convertToBytes(QByteArray& bytes, QCodesysNVType cmpType, Type variable)
{
bytes.clear();
//Creates writeable QDataStream and changes byte order
QDataStream stream(&bytes, QIODevice::WriteOnly);
stream.setByteOrder(QDataStream::LittleEndian);
//the conversion depends on the data type
if(cmpType.varType==QCodesysNVcmpType::_uint8)
{
quint8 tempVar = (quint8) variable;
stream << tempVar;
}
else if(cmpType.varType==QCodesysNVcmpType::_int8)
{
qint8 tempVar = (qint8) variable;
stream << tempVar;
}
else if(cmpType.varType==QCodesysNVcmpType::_uint16)
{
quint16 tempVar = (quint16) variable;
stream << tempVar;
}
else if(cmpType.varType==QCodesysNVcmpType::_int16)
{
qint16 tempVar = (qint16) variable;
stream << tempVar;
}
else if(cmpType.varType==QCodesysNVcmpType::_uint32)
{
quint32 tempVar = (quint32) variable;
stream << tempVar;
}
else if(cmpType.varType==QCodesysNVcmpType::_int32)
{
qint32 tempVar = (qint32) variable;
stream << tempVar;
}
else if(cmpType.varType==QCodesysNVcmpType::_uint64)
{
quint64 tempVar = (quint64) variable;
stream << tempVar;
}
else if(cmpType.varType==QCodesysNVcmpType::_int64)
{
qint64 tempVar = (qint64) variable;
stream << tempVar;
}
else if(cmpType.varType==QCodesysNVcmpType::_float)
{
stream.setFloatingPointPrecision(QDataStream::SinglePrecision);
float tempVar = (float) variable;
stream << tempVar;
}
else if(cmpType.varType==QCodesysNVcmpType::_double)
{
stream.setFloatingPointPrecision(QDataStream::DoublePrecision);
double tempVar = (double) variable;
stream << tempVar;
}
}
#endif // QCODESYSNV_H