devicesbus read data
This commit is contained in:
@@ -8,7 +8,7 @@ namespace softbus::hardware::drivers::serial::capabilities
|
||||
|
||||
struct SerialDefaults
|
||||
{
|
||||
int baudRate = 115200;
|
||||
int baudRate = 9600;
|
||||
int dataBits = 8;
|
||||
const char* parity = "none";
|
||||
int stopBits = 1;
|
||||
|
||||
@@ -1,6 +1,8 @@
|
||||
#include "hardware/drivers/serial/SerialQtDriver.h"
|
||||
|
||||
#include <QByteArray>
|
||||
#include <QMetaObject>
|
||||
#include <QThread>
|
||||
|
||||
#include "utils/logs/logging.h"
|
||||
|
||||
@@ -22,46 +24,51 @@ SerialQtDriver::~SerialQtDriver()
|
||||
|
||||
bool SerialQtDriver::Open(const std::string& endpoint)
|
||||
{
|
||||
if (m_port.isOpen()) {
|
||||
return true;
|
||||
const QString ep = QString::fromStdString(endpoint);
|
||||
if (QThread::currentThread() == this->thread()) {
|
||||
return openOnOwnerThread(ep);
|
||||
}
|
||||
|
||||
m_port.setPortName(QString::fromStdString(endpoint));
|
||||
if (!m_port.open(QIODevice::ReadWrite)) {
|
||||
if (m_errorCallback) {
|
||||
m_errorCallback(m_port.errorString().toStdString());
|
||||
}
|
||||
LOG_WARNING() << "SerialQtDriver: failed to open port" << m_port.portName() << ":" << m_port.errorString();
|
||||
return false;
|
||||
}
|
||||
|
||||
LOG_DEBUG() << "SerialQtDriver: opened port" << m_port.portName();
|
||||
return true;
|
||||
bool ok = false;
|
||||
QMetaObject::invokeMethod(
|
||||
this,
|
||||
[this, ep, &ok]() { ok = openOnOwnerThread(ep); },
|
||||
Qt::BlockingQueuedConnection);
|
||||
return ok;
|
||||
}
|
||||
|
||||
void SerialQtDriver::Close()
|
||||
{
|
||||
if (m_port.isOpen()) {
|
||||
const QString name = m_port.portName();
|
||||
m_port.close();
|
||||
LOG_DEBUG() << "SerialQtDriver: closed port" << name;
|
||||
if (QThread::currentThread() == this->thread()) {
|
||||
closeOnOwnerThread();
|
||||
return;
|
||||
}
|
||||
QMetaObject::invokeMethod(this, [this]() { closeOnOwnerThread(); }, Qt::BlockingQueuedConnection);
|
||||
}
|
||||
|
||||
bool SerialQtDriver::Write(const std::vector<std::uint8_t>& data)
|
||||
{
|
||||
if (!m_port.isOpen()) {
|
||||
return false;
|
||||
return Write(data.data(), data.size());
|
||||
}
|
||||
|
||||
bool SerialQtDriver::Write(const std::uint8_t* data, std::size_t size)
|
||||
{
|
||||
if (!data || size == 0) {
|
||||
return true;
|
||||
}
|
||||
|
||||
const QByteArray bytes(reinterpret_cast<const char*>(data.data()), static_cast<int>(data.size()));
|
||||
const qint64 written = m_port.write(bytes);
|
||||
if (written != bytes.size()) {
|
||||
LOG_WARNING() << "SerialQtDriver: partial write on" << m_port.portName() << "written" << written << "of"
|
||||
<< bytes.size();
|
||||
return false;
|
||||
const QByteArray bytes(reinterpret_cast<const char*>(data), static_cast<int>(size));
|
||||
// QSerialPort 必须在其所属线程内访问;若从外部线程调用,切回 owner 线程执行。
|
||||
if (QThread::currentThread() == this->thread()) {
|
||||
return writeOnOwnerThread(bytes);
|
||||
}
|
||||
return true;
|
||||
|
||||
bool ok = false;
|
||||
QMetaObject::invokeMethod(
|
||||
this,
|
||||
[this, bytes, &ok]() { ok = writeOnOwnerThread(bytes); },
|
||||
Qt::BlockingQueuedConnection);
|
||||
return ok;
|
||||
}
|
||||
|
||||
void SerialQtDriver::SetReadCallback(ReadCallback cb)
|
||||
@@ -134,5 +141,47 @@ void SerialQtDriver::handleError(QSerialPort::SerialPortError error)
|
||||
}
|
||||
}
|
||||
|
||||
bool SerialQtDriver::writeOnOwnerThread(const QByteArray& bytes)
|
||||
{
|
||||
if (!m_port.isOpen()) {
|
||||
return false;
|
||||
}
|
||||
const qint64 written = m_port.write(bytes);
|
||||
if (written != bytes.size()) {
|
||||
LOG_WARNING() << "SerialQtDriver: partial write on" << m_port.portName() << "written" << written << "of"
|
||||
<< bytes.size();
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
bool SerialQtDriver::openOnOwnerThread(const QString& endpoint)
|
||||
{
|
||||
if (m_port.isOpen()) {
|
||||
return true;
|
||||
}
|
||||
|
||||
m_port.setPortName(endpoint);
|
||||
if (!m_port.open(QIODevice::ReadWrite)) {
|
||||
if (m_errorCallback) {
|
||||
m_errorCallback(m_port.errorString().toStdString());
|
||||
}
|
||||
LOG_WARNING() << "SerialQtDriver: failed to open port" << m_port.portName() << ":" << m_port.errorString();
|
||||
return false;
|
||||
}
|
||||
|
||||
LOG_DEBUG() << "SerialQtDriver: opened port" << m_port.portName();
|
||||
return true;
|
||||
}
|
||||
|
||||
void SerialQtDriver::closeOnOwnerThread()
|
||||
{
|
||||
if (m_port.isOpen()) {
|
||||
const QString name = m_port.portName();
|
||||
m_port.close();
|
||||
LOG_DEBUG() << "SerialQtDriver: closed port" << name;
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace softbus::hardware::drivers::serial
|
||||
|
||||
|
||||
@@ -3,6 +3,8 @@
|
||||
#include <QObject>
|
||||
#include <QSerialPort>
|
||||
#include <QString>
|
||||
#include <cstddef>
|
||||
#include <QByteArray>
|
||||
|
||||
#include "hardware/interfaces/IStreamDriver.h"
|
||||
|
||||
@@ -60,6 +62,7 @@ public:
|
||||
void Close() override;
|
||||
|
||||
bool Write(const std::vector<std::uint8_t>& data) override;
|
||||
bool Write(const std::uint8_t* data, std::size_t size) override;
|
||||
|
||||
void SetReadCallback(ReadCallback cb) override;
|
||||
void SetErrorCallback(ErrorCallback cb) override;
|
||||
@@ -75,6 +78,11 @@ private slots:
|
||||
void handleReadyRead();
|
||||
void handleError(QSerialPort::SerialPortError error);
|
||||
|
||||
private:
|
||||
bool openOnOwnerThread(const QString& endpoint);
|
||||
void closeOnOwnerThread();
|
||||
bool writeOnOwnerThread(const QByteArray& bytes);
|
||||
|
||||
private:
|
||||
QSerialPort m_port;
|
||||
ReadCallback m_readCallback;
|
||||
|
||||
@@ -23,6 +23,7 @@ public:
|
||||
virtual void Close() = 0;
|
||||
|
||||
virtual bool Write(const std::vector<std::uint8_t>& data) = 0;
|
||||
virtual bool Write(const std::uint8_t* data, std::size_t size) = 0;
|
||||
|
||||
virtual void SetReadCallback(ReadCallback cb) = 0;
|
||||
virtual void SetErrorCallback(ErrorCallback cb) = 0;
|
||||
|
||||
Reference in New Issue
Block a user