forked from 21pages/Transmit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
serialport.cpp
63 lines (57 loc) · 1.66 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
53
54
55
56
57
58
59
60
61
62
63
#include "serialport.h"
#include "widget.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()
{
Widget* wid = (Widget*)parent();
QByteArray data = serial->readAll();
QString logString = "";
logString = serial->portName();
logString += QString::asprintf(" read(%d):", data.size());
for(int i = 0; i < data.size(); i++)
{
logString += QString::asprintf("%02X ", (unsigned char)data.at(i));
}
logString += "\n";
wid->appendLog(logString);
emit sig_recv_data(data);
}
void SerialPort::slot_send_data(QByteArray hex)
{
Widget* wid = (Widget*)parent();
if(serial->isOpen() && serial->isWritable()) {
QString logString = "";
logString = serial->portName();
logString += QString::asprintf(" write(%d):", hex.size());
for(int i = 0; i < hex.size(); i++)
{
logString += QString::asprintf("%02X ", (unsigned char)hex.at(i));
}
logString += "\n";
wid->appendLog(logString);
serial->write(hex);
serial->flush();
}
}