-
Notifications
You must be signed in to change notification settings - Fork 0
/
serialcom.cpp
111 lines (99 loc) · 3.28 KB
/
serialcom.cpp
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
// https://github.com/Enderceliik
// Ender CELIK
#include "serialcom.h"
QSharedPointer<SerialCom>ext_serial_obj;
QSharedPointer<SerialCom>glob_pointer_array[1] = {nullptr};
SerialCom::SerialCom(QJsonObject port_config)
{
serial_port_obj = new QSerialPort();
port_definer(port_config);
protocol_definer();
open_serial_port();
}
void SerialCom::port_definer(QJsonObject port_config)
{
QString portName = port_config["portName"].toString();
QString baudRate = port_config["baudRate"].toString();
QSerialPort::DataBits dataBits = static_cast<QSerialPort::DataBits>(port_config["dataBits"].toString().toInt());
QString parityStr = port_config["parity"].toString();
QSerialPort::Parity parity;
if (parityStr == "EvenParity") {
parity = QSerialPort::EvenParity;
} else if (parityStr == "OddParity") {
parity = QSerialPort::OddParity;
} else if (parityStr == "NoParity") {
parity = QSerialPort::NoParity;
} else {
parity = QSerialPort::NoParity;
}
QString stopBitsStr = port_config["stopBits"].toString();
QSerialPort::StopBits stopBits;
if (stopBitsStr == "OneStop") {
stopBits = QSerialPort::OneStop;
} else if (stopBitsStr == "OneAndHalfStop") {
stopBits = QSerialPort::OneAndHalfStop;
} else if (stopBitsStr == "TwoStop") {
stopBits = QSerialPort::TwoStop;
} else {
stopBits = QSerialPort::OneStop;
}
QString flowControlStr = port_config["flowControl"].toString();
QSerialPort::FlowControl flowControl;
if (flowControlStr == "NoFlowControl") {
flowControl = QSerialPort::NoFlowControl;
} else if (flowControlStr == "HardwareControl") {
flowControl = QSerialPort::HardwareControl;
} else if (flowControlStr == "SoftwareControl") {
flowControl = QSerialPort::SoftwareControl;
} else {
flowControl = QSerialPort::NoFlowControl;
}
serial_port_obj->setPortName(portName);
serial_port_obj->setBaudRate(baudRate.toInt());
serial_port_obj->setDataBits(dataBits);
serial_port_obj->setParity(parity);
serial_port_obj->setStopBits(stopBits);
serial_port_obj->setFlowControl(flowControl);
}
void SerialCom::protocol_definer()
{
for (int num = 0; num < prtclString.length(); ++num) {
if (num%2==0) {
protocolList+=prtclString.mid(num,2);
}
}
for (int var = 0; var < #; ++var) {
protocolByteArray.append(QString(protocolList.value(var)).toUInt(nullptr,16));
}
}
void SerialCom::open_serial_port()
{
if (serial_port_obj->open(QIODevice::ReadWrite)) {
qDebug() << "Serial Port open successfuly!";
}
else
{
qDebug() << "Serial Port didn't open!" << serial_port_obj->error();
}
}
QByteArray SerialCom::communication()
{
QByteArray response;
if (serial_port_obj->isOpen()) {
serial_port_obj->write(protocolByteArray);
if (serial_port_obj->waitForBytesWritten(3000)) {
QThread::sleep(3);
//response = ext_serial_obj->read_data();
response = read_data();
}
}
return response;
}
QByteArray SerialCom::read_data()
{
QByteArray response;
if (serial_port_obj->waitForReadyRead(5000)) {
response.append(serial_port_obj->readAll());
}
return response;
}