-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserialport.cpp
52 lines (44 loc) · 1.51 KB
/
serialport.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
#include <QMessageBox>
#include "serialport.h"
SerialPort::SerialPort(QWidget *parent) : QObject(parent), serialPort(parent) {}
void SerialPort::openPort() {
serialPort.setPortName("/dev/tnt0");
serialPort.setBaudRate(9600);
serialPort.setDataBits(QSerialPort::Data8);
serialPort.setParity(QSerialPort::NoParity);
serialPort.setStopBits(QSerialPort::OneStop);
serialPort.setFlowControl(QSerialPort::NoFlowControl);
if (serialPort.open(QIODevice::ReadWrite)) {
portOpen();
QObject::connect(&serialPort, SIGNAL(readyRead()), this, SLOT(serialReadReady()));
}
else {
QMessageBox::critical(parent, tr("Error"), serialPort.errorString());
}
}
SerialPort::~SerialPort() {
if(serialPort.isOpen()) {
serialPort.close();
}
}
void SerialPort::serialReadReady() {
while (serialPort.canReadLine()) {
QString string = serialPort.readLine();
auto packet = Packet::parsePacket(string);
if (packet->type == ERROR_PACKET) {
QMessageBox msgBox;
msgBox.setText("An error packet has been received: " + std::dynamic_pointer_cast<ErrorPacket>(packet)->error);
msgBox.exec();
}
newRawIncomingPacket(string);
newPacket(packet);
}
}
void SerialPort::sendRawText(QString string) {
newRawOutgoingPacket(string);
serialPort.write(string.toUtf8());
}
void SerialPort::sendPacket(std::shared_ptr<SendablePacket> packet) {
QString string = packet->serialize();
sendRawText(string);
}