-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathport.cpp
More file actions
135 lines (122 loc) · 3.25 KB
/
port.cpp
File metadata and controls
135 lines (122 loc) · 3.25 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
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
#include "port.h"
#include <qdebug.h>
/**
* @brief ComPort::ComPort constructor.
* @param parent
*/
ComPort::ComPort(QObject *parent) :
QObject(parent)
{
}
/**
* @brief ComPort::~ComPort destructor.
*/
ComPort::~ComPort()
{
emit quitComPort();
qDebug()<<"~By-by from"<<this;
}
/**
* @brief ComPort::onPortStart. Run at class start.
*/
void ComPort:: onPortStart()
{
qDebug("Hello from comPort Thread!");
/* Connect signals with slots */
connect(&serialPort, SIGNAL(readyRead()), this, SLOT(onReadyRead()));
}
/**
* @brief ComPort::onConnectClicked slot implementation.
* @param port - port name
*/
void ComPort:: onConnectClicked(const QString &port) {
if (!serialPort.isOpen()) {
serialPort.setPortName(port);
if (serialPort.open(QIODevice::ReadWrite)) {
serialPort.setBaudRate(QSerialPort::Baud1200);
serialPort.setDataBits(QSerialPort::Data8);
serialPort.setStopBits(QSerialPort::OneStop);
serialPort.setParity(QSerialPort::OddParity);
serialPort.setFlowControl(QSerialPort::NoFlowControl);
QString str = getPortParams();
emit portOpened(str);
}
else {
emit portError(tr("Порт не доступен. Возможно он занят другим приложением."));
}
}
else {
qDebug() << "ComPort::onConnectClicked()"
<< "serialPort.isOpen()";
}
}
/**
* @brief ComPort::onDisconnectClicked slot implementation.
*/
void ComPort::onDisconnectClicked()
{
if (serialPort.isOpen()) {
serialPort.close();
emit portClosed();
}
}
/**
* @brief ComPort::getPortParams. Gets the parameters of the Com Port settings.
* @return
*/
QString ComPort::getPortParams(void)
{
QString str, str1;
str = serialPort.portName();
str1.setNum(serialPort.baudRate());
str += "/" + str1 + "/";
str1.setNum(serialPort.dataBits());
str += str1 + "/";
str1.setNum(serialPort.stopBits());
str += str1 + "/";
str1.setNum(serialPort.parity());
str += str1 + "/";
str1.setNum(serialPort.flowControl());
str += str1;
return str;
}
/**
* @brief ComPort::onReadyRead slot implementation.
*/
void ComPort::onReadyRead()
{
static QByteArray rxData;
while(!serialPort.atEnd()) {
rxData.append(serialPort.readAll());
if (!serialPort.waitForReadyRead(30)) {
break;
}
}
if (rxData.size() > 0) {
while(static_cast<quint8>(rxData[0]) == 0xFF) {
rxData.remove(0, 1); // Delete the preamble
}
emit parsingPacket(rxData); // Pass packet to the protocol
rxData.clear();
}
}
/**
* @brief ComPort::onSendDataPacket slot implementation.
* @param prea_size - size of the HART preamble
* @param packet - data packet
*/
void ComPort::onSendDataPacket(const QByteArray &packet)
{
if (!serialPort.isOpen()) {
emit portError(tr("Порт не открыт"));
}
else {
QByteArray hartFrame;
for (quint16 i = 0; i < PREAMBLE_SIZE; i++) {
hartFrame.append(static_cast<char>(0xFF)); // Add the preamble
}
hartFrame.append(packet);
serialPort.write(hartFrame);
}
}
//eof