-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserial.cpp
More file actions
64 lines (48 loc) · 1.52 KB
/
serial.cpp
File metadata and controls
64 lines (48 loc) · 1.52 KB
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
#include "serial.h"
Serial::Serial(QObject *parent) : QObject(parent) {}
#define CRC16 0x8005
QString Serial::hashID(const char* data) {
uint16_t out = 0;
int bits_read = 0,
bit_flag,
size = strlen(data);
if(data == NULL)
return 0;
while(size > 0) {
bit_flag = out >> 15;
out <<= 1;
out |= (*data >> (7 - bits_read)) & 1;
bits_read++;
if(bits_read > 7) {
bits_read = 0;
data++;
size--;
}
if(bit_flag)
out ^= CRC16;
}
return QString(QByteArray::number(out, 16));
}
QVariantList Serial::getConnectedDevices() {
const auto allSerial = QSerialPortInfo::availablePorts();
QVariantList list;
for(const QSerialPortInfo &serialInfo : allSerial) {
QJsonObject currentSerial;
// Ignore if no serial number
if(!serialInfo.serialNumber().isEmpty()) {
currentSerial["port"] = serialInfo.portName();
if(serialInfo.hasProductIdentifier())
currentSerial["id"] = serialInfo.productIdentifier();
else
currentSerial["id"] = -1;
currentSerial["number"] = serialInfo.serialNumber();
currentSerial["busy"] = serialInfo.isBusy();
currentSerial["deviceName"] = serialInfo.description();
list.append(currentSerial.toVariantMap());
}
}
return list;
}
void Serial::serialInit() {
qDebug() << Serial::getConnectedDevices();
}