-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathserialport.cpp
More file actions
40 lines (35 loc) · 919 Bytes
/
serialport.cpp
File metadata and controls
40 lines (35 loc) · 919 Bytes
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
#include "serialport.h"
#include <QIODevice>
SerialPort::SerialPort(QObject *parent) : BaseParent(parent)
{
serial = new QSerialPort(this);
connect(serial,&QSerialPort::readyRead,this,&SerialPort::handleReadyRead);
}
bool SerialPort::Open(Para *para)
{
if(serial->isOpen())
serial->close();
serial->setPortName(QString("COM%1").arg(para->com));
if(!serial->open(QIODevice::ReadWrite))
return false;
serial->setBaudRate(para->baud);
serial->setParity(para->parity);
serial->setDataBits(QSerialPort::Data8);
serial->setStopBits(QSerialPort::OneStop);
return true;
}
void SerialPort::Close()
{
serial->close();
}
void SerialPort::handleReadyRead()
{
emit sig_recv_data(serial->readAll());
}
void SerialPort::slot_send_data(QByteArray hex)
{
if(serial->isOpen() && serial->isWritable()) {
serial->write(hex);
serial->flush();
}
}