forked from UAVGCSTeam/GCS
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathXbeeLink.cpp
More file actions
31 lines (28 loc) · 1 KB
/
XbeeLink.cpp
File metadata and controls
31 lines (28 loc) · 1 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
#include "XbeeLink.h"
XbeeLink::XbeeLink(QObject* p):QObject(p){
connect(&serial_, &QSerialPort::readyRead, this, &XbeeLink::onReadyRead);
}
bool XbeeLink::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 XbeeLink::close(){ serial_.close(); }
qint64 XbeeLink::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 XbeeLink::onReadyRead(){
emit bytesReceived(serial_.readAll());
}