Files
checker_host/interface/if_uart.cpp

104 lines
2.2 KiB
C++
Raw Normal View History

2023-11-29 15:36:45 +08:00
#include "if_uart.h"
#include "QThread"
#include "base/mycfg.h"
#include "QDebug"
void if_uart::init()
{
2023-12-02 11:27:25 +08:00
if (serial_ == nullptr)
2023-11-29 15:36:45 +08:00
{
2023-12-02 11:27:25 +08:00
serial_ = new QSerialPort();
serial_->setPortName(com);
serial_->setBaudRate(bsp);
serial_->setParity(QSerialPort::NoParity);
serial_->setDataBits(QSerialPort::Data8);
serial_->setStopBits(QSerialPort::OneStop);
serial_->setFlowControl(QSerialPort::NoFlowControl);
if (serial_->open(QIODevice::ReadWrite))
{
connect(serial_, &QSerialPort::readyRead, this, &if_uart::ready_read_cb);
qDebug("uart \"%s\" bsp=%d", serial_->portName().toLocal8Bit().data(), bsp);
serial_open = true;
}
else
{
qWarning() << "uart open failed." << serial_->portName();
}
2023-11-29 15:36:45 +08:00
}
}
int if_uart::write(myarray data)
{
2023-12-02 11:27:25 +08:00
if ((nullptr == serial_) || (serial_open != true))
2023-11-29 15:36:45 +08:00
{
2023-12-02 11:27:25 +08:00
// qWarning() << "Cant send data , TcpClient socket not connect.";
2023-11-29 15:36:45 +08:00
return 0;
}
2023-12-02 11:27:25 +08:00
qDebug("uart send:%s", data.toHex(' ').data());
2023-11-29 15:36:45 +08:00
int wb = serial_->write(data);
2023-12-02 11:27:25 +08:00
if ((!serial_->flush()) || (wb != data.size()))
2023-11-29 15:36:45 +08:00
{
2023-12-02 11:27:25 +08:00
// qWarning() << "uart data:"<<data.toHex(' ')<<"not sent.";
2023-11-29 15:36:45 +08:00
}
return wb;
}
void if_uart::ready_read_cb()
{
QByteArray data = serial_->readAll();
// qDebug("uart recv:%s", data.toHex(' ').data());
2023-11-29 15:36:45 +08:00
if (irq_fun)
{
irq_fun(myarray(data));
}
}
InterFace *if_uart_hmi()
{
static InterFace *if_ = nullptr;
if (if_ == nullptr)
{
2023-12-02 11:27:25 +08:00
if_ = new if_uart("/dev/ttySTM1", 115200);
2023-11-29 15:36:45 +08:00
// QTimer::singleShot(0, if_, &InterFace::init);
if_->init();
}
return if_;
}
if_export(uart_hmi, if_uart_hmi);
InterFace *if_uart_host()
{
static InterFace *if_ = nullptr;
if (if_ == nullptr)
{
2023-12-02 11:27:25 +08:00
int bsp = 57600;
if (syscfg()->uart_bsp != 0)
{
bsp = syscfg()->uart_bsp;
2023-11-29 15:36:45 +08:00
}
2023-12-02 11:27:25 +08:00
if_ = new if_uart("/dev/ttySTM2", bsp);
2023-11-29 15:36:45 +08:00
// QTimer::singleShot(0, if_, &InterFace::init);
if_->init();
}
return if_;
}
if_export(uart_host, if_uart_host);
InterFace *if_uart_m4()
{
static InterFace *if_ = nullptr;
if (if_ == nullptr)
{
2023-12-02 11:27:25 +08:00
if_ = new if_uart("/dev/ttyRPMSG0", 115200);
2023-11-29 15:36:45 +08:00
// QTimer::singleShot(0, if_, &InterFace::init);
if_->init();
}
return if_;
}
if_export(uart_m4, if_uart_m4);