110 lines
2.3 KiB
C++
110 lines
2.3 KiB
C++
|
||
#include "if_uart.h"
|
||
#include "QThread"
|
||
#include "base/mycfg.h"
|
||
#include "QDebug"
|
||
|
||
void if_uart::init()
|
||
{
|
||
if (serial_ == nullptr)
|
||
{
|
||
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();
|
||
}
|
||
}
|
||
}
|
||
|
||
int if_uart::write(myarray data)
|
||
{
|
||
if ((nullptr == serial_) || (serial_open != true))
|
||
{
|
||
// qWarning() << "Can’t send data , TcpClient socket not connect.";
|
||
return 0;
|
||
}
|
||
// qDebug("uart send:%s", data.toHex(' ').data());
|
||
|
||
int wb = serial_->write(data);
|
||
if ((!serial_->flush()) || (wb != data.size()))
|
||
{
|
||
// qWarning() << "uart data:"<<data.toHex(' ')<<"not sent.";
|
||
}
|
||
return wb;
|
||
}
|
||
|
||
void if_uart::ready_read_cb()
|
||
{
|
||
QByteArray data = serial_->readAll();
|
||
// qDebug("uart recv:%s", data.toHex(' ').data());
|
||
if (irq_fun)
|
||
{
|
||
irq_fun(myarray(data));
|
||
}
|
||
}
|
||
|
||
InterFace *if_uart_hmi()
|
||
{
|
||
static InterFace *if_ = nullptr;
|
||
if (if_ == nullptr)
|
||
{
|
||
char *dev=nullptr;
|
||
if(syscfg()->device_type=="hmi_coder"){
|
||
dev="/dev/ttySTM3";
|
||
}else{
|
||
dev="/dev/ttySTM1";
|
||
}
|
||
if_ = new if_uart(dev, 115200);
|
||
// 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)
|
||
{
|
||
int bsp = 57600;
|
||
if (syscfg()->uart_bsp != 0)
|
||
{
|
||
bsp = syscfg()->uart_bsp;
|
||
}
|
||
if_ = new if_uart("/dev/ttySTM2", bsp);
|
||
// 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)
|
||
{
|
||
if_ = new if_uart("/dev/ttyRPMSG0", 115200);
|
||
// QTimer::singleShot(0, if_, &InterFace::init);
|
||
if_->init();
|
||
}
|
||
return if_;
|
||
}
|
||
|
||
if_export(uart_m4, if_uart_m4);
|