-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathUARTLink.cpp
More file actions
36 lines (32 loc) · 1.13 KB
/
UARTLink.cpp
File metadata and controls
36 lines (32 loc) · 1.13 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
#include "UARTLink.h"
/*
Tip: Learn more about the QSerialPort class' members here:
https://doc.qt.io/qt-6/qserialport-members.html
*/
UARTLink::UARTLink(QObject* p):QObject(p){
connect(&serial_, &QSerialPort::readyRead, this, &UARTLink::onReadyRead);
}
bool UARTLink::open(const QString& port, int baud){
if (serial_.isOpen()) serial_.close();
serial_.setPortName(port);
serial_.setBaudRate(baud);
serial_.setDataBits(QSerialPort::Data8);
serial_.setParity(QSerialPort::NoParity);
serial_.setStopBits(QSerialPort::OneStop);
serial_.setFlowControl(QSerialPort::NoFlowControl);
if(!serial_.open(QIODevice::ReadWrite)){
emit linkError(QString("Open failed: %1").arg(serial_.errorString()));
return false;
}
return true;
}
void UARTLink::close(){ serial_.close(); }
qint64 UARTLink::writeBytes(const QByteArray& b){
if (!serial_.isOpen()) return -1;
const qint64 n = serial_.write(b); // QSerialPort::write returns qint64
if (n == -1) emit linkError(serial_.errorString());
return n;
}
void UARTLink::onReadyRead(){
emit bytesReceived(serial_.readAll());
}