-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathserialcontroller.cpp
93 lines (80 loc) · 2.06 KB
/
serialcontroller.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
#include "serialcontroller.h"
SerialController::SerialController(QObject *parent) : QObject(parent)
{
serial = new QSerialPort(this);
connect(serial, QSerialPort::readyRead, this, handleRecv);
}
void SerialController::openSerial(QString name)
{
serial->setPortName(name);
if (serial->open(QIODevice::ReadWrite))
{
//串口打开成功
serial->setDataTerminalReady(false);
serial->setDataTerminalReady(false);
emit openSuccess();
}
else
{
//串口打开失败
emit openFailed();
}
}
void SerialController::closeSerial()
{
if (serial->isOpen())
{
serial->close();
emit closeSuccess();
}
}
void SerialController::getBaudrate(QString baudrate)
{
serial->setBaudRate(baudrate.toInt());
}
void SerialController::getStopbits(QString stopbits)
{
if (stopbits == "1")
serial->setStopBits(QSerialPort::OneStop);
else if (stopbits == "1.5")
serial->setStopBits(QSerialPort::OneAndHalfStop);
else if (stopbits == "2")
serial->setStopBits(QSerialPort::TwoStop);
}
void SerialController::getDatabits(QString databits)
{
serial->setDataBits(QSerialPort::DataBits(databits.toInt()));
}
void SerialController::getParity(QString parity)
{
if (parity == tr("无"))
serial->setParity(QSerialPort::NoParity);
else if (parity == tr("奇校验"))
serial->setParity(QSerialPort::OddParity);
else if (parity == tr("偶校验"))
serial->setParity(QSerialPort::EvenParity);
}
void SerialController::writeData(QString content)
{
if (serial->isWritable())
{
qint64 result = serial->write(content.toLocal8Bit().data());
if (result == -1)
emit writeFailed();
else
emit writeSuccess(result);
}
}
void SerialController::handleRecv()
{
QByteArray data = serial->readAll();
emit recvData(data);
}
void SerialController::contrloRTS(bool set)
{
serial->setRequestToSend(set);
}
void SerialController::controlDTR(bool set)
{
serial->setDataTerminalReady(set);
}