401 lines
13 KiB
C++
401 lines
13 KiB
C++
#include "CommunicationWgt.h"
|
|
#include "ui_CommunicationWgt.h"
|
|
|
|
#include <QMessageBox>
|
|
#include <QSerialPortInfo>
|
|
#include "log_utils.h"
|
|
#include "DataBus.h"
|
|
#include "GuideMQTTInline.h"
|
|
|
|
static const char blankString[] = QT_TRANSLATE_NOOP("CommunicationWgt", "N/A");
|
|
|
|
CommunicationWgt::CommunicationWgt(QWidget* parent)
|
|
: QMainWindow(parent)
|
|
, ui(new Ui::CommunicationWgtClass)
|
|
{
|
|
ui->setupUi(this);
|
|
|
|
this->statusBar()->hide();
|
|
|
|
// 串口初始化
|
|
m_timer.start(1000);
|
|
|
|
FillPortsParameters();
|
|
FillPortsInfo();
|
|
SerialPortTools::SerialPortList = std::make_shared<std::map<int, std::shared_ptr<SerialPort>>>();
|
|
SPDLOG_INFO("SerialPortsMap instance address in main is {}", reinterpret_cast<void*>(SerialPortTools::SerialPortList.get()));
|
|
|
|
InitStyles();
|
|
/*
|
|
*MQTT初始化
|
|
*/
|
|
{
|
|
MQTT_GUIDE::m_pMQTTOpenration = std::make_shared<MQTTOpenration>();
|
|
//m_topicList.append("cpu_track/RPC_Reply");
|
|
m_topicList.append("cpu_track/RPC_Reply");
|
|
}
|
|
// connect
|
|
{
|
|
LoadConnect();
|
|
}
|
|
initUi();
|
|
}
|
|
|
|
CommunicationWgt::~CommunicationWgt()
|
|
{
|
|
// connect
|
|
{
|
|
disconnect(ui->m_pushButton_OpenOrCloseSerialPort, &QPushButton::clicked, this, &CommunicationWgt::OnButtonClicked);
|
|
}
|
|
|
|
delete ui;
|
|
}
|
|
void CommunicationWgt::sltMQTTSendMessage(QString strTopic, QString strMessage)
|
|
{
|
|
MQTT_GUIDE::m_pMQTTOpenration->PublishDataByMQTT(strTopic, strMessage);
|
|
}
|
|
void CommunicationWgt::sltMQTTSendMessage(QString strTopic, QByteArray arrayMessage)
|
|
{
|
|
MQTT_GUIDE::m_pMQTTOpenration->PublishDataByMQTT(strTopic, arrayMessage);
|
|
}
|
|
|
|
void CommunicationWgt::sltMQTTSubcribe(QString strTopic)
|
|
{
|
|
MQTT_GUIDE::m_pMQTTOpenration->Subcribe(strTopic);
|
|
}
|
|
|
|
void CommunicationWgt::sltMQTTCancelTopic(QString strTopic)
|
|
{
|
|
MQTT_GUIDE::m_pMQTTOpenration->UnSubcribe(strTopic);
|
|
}
|
|
|
|
void CommunicationWgt::revMQTTSendMessage(QString strTopic, QByteArray arrayMessage)
|
|
{
|
|
int sp = 0;
|
|
}
|
|
|
|
void CommunicationWgt::initUi()
|
|
{
|
|
ui->m_TxCombox->addItem("推出机构", 0);
|
|
ui->m_TxCombox->addItem("多光谱", 1);
|
|
ui->m_TxCombox->addItem("DSP", 2);
|
|
ui->m_TxCombox->addItem("红外", 3);
|
|
ui->m_TxCombox->addItem("8511", 4);
|
|
ui->m_TxCombox->setCurrentIndex(0);
|
|
}
|
|
|
|
|
|
void CommunicationWgt::InitStyles()
|
|
{
|
|
QString fileName = ":/skin/qss/psblack.css";
|
|
if (!fileName.isEmpty())
|
|
{
|
|
QFile file(fileName);
|
|
|
|
if (file.open(QFile::ReadOnly))
|
|
{
|
|
QString str = file.readAll();
|
|
this->setStyleSheet(str);
|
|
}
|
|
}
|
|
}
|
|
void CommunicationWgt::setOpenStatus(bool isOpen)
|
|
{
|
|
m_currentBtnIsOpen = isOpen;
|
|
ui->m_TxCombox->setEnabled(!isOpen);
|
|
ui->m_label_ConnectFlag->setScaledContents(true);
|
|
if (isOpen)
|
|
{
|
|
ui->m_pushButton_OpenOrCloseSerialPort->setText("关闭串口");
|
|
// 点亮图标
|
|
ui->m_label_ConnectFlag->setPixmap(QPixmap(":/image/connect.png"));
|
|
ui->m_label_ConnectFlag->setScaledContents(true);
|
|
}
|
|
else {
|
|
ui->m_pushButton_OpenOrCloseSerialPort->setText("打开串口");
|
|
ui->m_label_ConnectFlag->setPixmap(QPixmap(":/image/disconnect.png"));
|
|
|
|
}
|
|
}
|
|
|
|
void CommunicationWgt::LoadConnect()
|
|
{
|
|
connect(ui->m_pushButton_OpenOrCloseSerialPort, &QPushButton::clicked, this, &CommunicationWgt::OnButtonClicked);
|
|
connect(ui->pushButton_ConnectMqtt, &QPushButton::clicked, this, &CommunicationWgt::OnButtonClicked);
|
|
|
|
QObject::connect(ui->m_comboBox_SerialPort, QOverload<int>::of(&QComboBox::currentIndexChanged), this, [this](int index)
|
|
{
|
|
OnSerialPortComboxCurIndexChanged(index);
|
|
});
|
|
connect(MQTT_GUIDE::m_pMQTTOpenration.get(),SIGNAL(messageReceived(QString,QByteArray)),this,SLOT(revMQTTSendMessage(QString, QByteArray)));
|
|
|
|
}
|
|
|
|
void CommunicationWgt::ShowMsg(QString title, QString msg)
|
|
{
|
|
QMessageBox msgBox(QMessageBox::Icon::Warning, title, msg);
|
|
QColor parentBgColor = this->palette().color(QWidget::backgroundRole());
|
|
QString styleSheet = QString(
|
|
"QMessageBox {"
|
|
" background-color: %1;"
|
|
" color: %2;"
|
|
"}"
|
|
"QMessageBox QLabel {"
|
|
" color: %2;"
|
|
"}"
|
|
"QMessageBox QPushButton {"
|
|
" background-color: palette(button);"
|
|
" border: 1px solid #8f8f91;"
|
|
" border-radius: 4px;"
|
|
" padding: 6px 12px;"
|
|
"}"
|
|
"QMessageBox QPushButton:hover {"
|
|
" background-color: palette(light);"
|
|
"}"
|
|
"QMessageBox QPushButton:pressed {"
|
|
" background-color: palette(dark);"
|
|
"}"
|
|
).arg(parentBgColor.name())
|
|
.arg(this->palette().color(QPalette::WindowText).name());
|
|
|
|
msgBox.setStyleSheet(styleSheet);
|
|
msgBox.exec();
|
|
}
|
|
|
|
void CommunicationWgt::InitMQTTtopic()
|
|
{
|
|
foreach (auto item , m_topicList)
|
|
{
|
|
bool rel = MQTT_GUIDE::m_pMQTTOpenration->Subcribe(item);
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void CommunicationWgt::FillPortsParameters()
|
|
{
|
|
|
|
ui->m_comboBox_BaudRate->addItem(QStringLiteral("19200"), 19200);
|
|
ui->m_comboBox_BaudRate->addItem(QStringLiteral("38400"), 38400);
|
|
ui->m_comboBox_BaudRate->addItem(QStringLiteral("115200"), 115200);
|
|
ui->m_comboBox_BaudRate->addItem(QStringLiteral("921600"), 921600);
|
|
|
|
ui->m_comboBox_BaudRate->setCurrentIndex(1);
|
|
|
|
//
|
|
ui->m_comboBox_Parity->addItem(tr("None"), asio::serial_port::parity::type::none);
|
|
ui->m_comboBox_Parity->addItem(tr("Even"), asio::serial_port::parity::type::even);
|
|
ui->m_comboBox_Parity->addItem(tr("Odd"), asio::serial_port::parity::type::odd);
|
|
ui->m_comboBox_Parity->setCurrentIndex(0);
|
|
|
|
//
|
|
ui->m_comboBox_DataBits->addItem(QStringLiteral("5"), 5);
|
|
ui->m_comboBox_DataBits->addItem(QStringLiteral("6"), 6);
|
|
ui->m_comboBox_DataBits->addItem(QStringLiteral("7"), 7);
|
|
ui->m_comboBox_DataBits->addItem(QStringLiteral("8"), 8);
|
|
ui->m_comboBox_DataBits->setCurrentIndex(3);
|
|
|
|
ui->m_comboBox_StopBits->addItem(QStringLiteral("1"), asio::serial_port::stop_bits::type::one);
|
|
#ifdef Q_OS_WIN
|
|
ui->m_comboBox_StopBits->addItem(tr("1.5"), asio::serial_port::stop_bits::type::onepointfive);
|
|
#endif
|
|
ui->m_comboBox_StopBits->addItem(QStringLiteral("2"), asio::serial_port::stop_bits::type::two);
|
|
ui->m_comboBox_StopBits->setCurrentIndex(0);
|
|
|
|
ui->m_comboBox_FlowControl->addItem(tr("None"), asio::serial_port::flow_control::none);
|
|
ui->m_comboBox_FlowControl->addItem(tr("Software"), asio::serial_port::flow_control::software);
|
|
ui->m_comboBox_FlowControl->addItem(tr("Hardware"), asio::serial_port::flow_control::hardware);
|
|
ui->m_comboBox_FlowControl->setCurrentIndex(0);
|
|
}
|
|
|
|
void CommunicationWgt::FillPortsInfo()
|
|
{
|
|
ui->m_comboBox_SerialPort->clear();
|
|
m_portNameList.clear();
|
|
|
|
connect(&m_timer, &QTimer::timeout, [this]()
|
|
{
|
|
foreach(auto varPortInfo, QSerialPortInfo::availablePorts())
|
|
{
|
|
QString name = varPortInfo.portName();
|
|
|
|
if (!m_portNameList.contains(name))
|
|
{
|
|
m_portNameList.append(name);
|
|
ui->m_comboBox_SerialPort->addItem(name);
|
|
}
|
|
}
|
|
});
|
|
}
|
|
|
|
void CommunicationWgt::UpdateSettings()
|
|
{
|
|
// 串口名
|
|
m_varSerialPortSettings.name = ui->m_comboBox_SerialPort->currentText();
|
|
|
|
// 波特率
|
|
m_varSerialPortSettings.baudRate = asio::serial_port::baud_rate(ui->m_comboBox_BaudRate->currentText().toInt());
|
|
m_varSerialPortSettings.strBaudRate = QString::number(m_varSerialPortSettings.baudRate.value());
|
|
|
|
// 奇偶校验
|
|
m_varSerialPortSettings.parity = asio::serial_port::parity((asio::serial_port::parity::type)ui->m_comboBox_Parity->itemData(ui->m_comboBox_Parity->currentIndex()).toInt());
|
|
m_varSerialPortSettings.strParity = ui->m_comboBox_Parity->currentText();
|
|
|
|
// 流控制
|
|
m_varSerialPortSettings.flowControl = asio::serial_port::flow_control((asio::serial_port::flow_control::type)ui->m_comboBox_FlowControl->itemData(ui->m_comboBox_FlowControl->currentIndex()).toInt());
|
|
m_varSerialPortSettings.strFlowControl = ui->m_comboBox_FlowControl->currentText();
|
|
|
|
|
|
// 数据位
|
|
m_varSerialPortSettings.dataBits = asio::serial_port::character_size(
|
|
ui->m_comboBox_DataBits->itemData(ui->m_comboBox_DataBits->currentIndex()).toInt());
|
|
m_varSerialPortSettings.strDataBits = ui->m_comboBox_DataBits->currentText();
|
|
|
|
// 停止位
|
|
m_varSerialPortSettings.stopBits = (asio::serial_port::stop_bits)(asio::serial_port::stop_bits::type)
|
|
ui->m_comboBox_StopBits->itemData(ui->m_comboBox_StopBits->currentIndex()).toInt();
|
|
m_varSerialPortSettings.strStopBits = ui->m_comboBox_StopBits->currentText();
|
|
|
|
}
|
|
|
|
void CommunicationWgt::OnButtonClicked()
|
|
{
|
|
if (sender() == ui->m_pushButton_OpenOrCloseSerialPort)
|
|
{
|
|
int index = ui->m_comboBox_SerialPort->currentIndex();
|
|
//UpdateSettings();
|
|
|
|
std::shared_ptr<SerialPort> ptrPort=nullptr;
|
|
if (SerialPortTools::SerialPortList->find(index)!= SerialPortTools::SerialPortList->end())
|
|
{
|
|
ptrPort = SerialPortTools::SerialPortList->at(index);
|
|
}
|
|
|
|
if (ptrPort == nullptr&& (m_currentBtnIsOpen==false))//如果没打开该串口则打开
|
|
{
|
|
if (m_BijectiveMap.contiansValue(ui->m_TxCombox->currentIndex())) {
|
|
QMessageBox::warning(nullptr, "警告", "串口打开失败", ui->m_TxCombox->currentText() + "已经映射了" );
|
|
return;
|
|
}
|
|
std::string portName_ = ui->m_comboBox_SerialPort->currentText().toStdString();
|
|
|
|
std::shared_ptr<SerialPort> port_ = SerialPortTools::SerialPort::create( portName_);
|
|
|
|
port_->m_sConfig.baud_rate = ui->m_comboBox_BaudRate->currentText().toInt();
|
|
port_->m_sConfig.character_size = asio::serial_port::character_size(ui->m_comboBox_DataBits->currentText().toInt());
|
|
port_->m_sConfig.flow_control = (asio::serial_port::flow_control::type)ui->m_comboBox_FlowControl->itemData(ui->m_comboBox_FlowControl->currentIndex()).toInt();
|
|
port_->m_sConfig.parity = (asio::serial_port::parity::type)ui->m_comboBox_Parity->itemData(ui->m_comboBox_Parity->currentIndex()).toInt();
|
|
port_->m_sConfig.stop_bits = (asio::serial_port::stop_bits::type)ui->m_comboBox_StopBits->itemData(ui->m_comboBox_StopBits->currentIndex()).toInt();
|
|
port_->m_sConfig.port_name = ui->m_comboBox_SerialPort->currentText().toStdString();
|
|
port_->m_sConfig.CommunicationObject = static_cast<SerialPort::通信>(ui->m_TxCombox->currentIndex());
|
|
|
|
port_->open(port_->m_sConfig);
|
|
SerialPortTools::SerialPortList->emplace(index, port_);
|
|
if (port_->is_open())
|
|
{
|
|
log_utils::info(portName_ + u8"串口打开成功");
|
|
setOpenStatus(true);
|
|
m_BijectiveMap.add(index, ui->m_TxCombox->currentIndex());
|
|
#if 0
|
|
std::vector<uint8_t> data = { 0x55,0xAA,0xBB,0xCC,0xDD };
|
|
port_->write_async(data);
|
|
Sleep(100);
|
|
port_->write_async(data);
|
|
port_->StartReceiveRun();
|
|
port_->set_receive_callback([](const std::vector<uint8_t>& data, size_t len)
|
|
{
|
|
if (len > 0)
|
|
{
|
|
std::cout << "收到数据(" << len << ")字节";
|
|
for (size_t i = 0; i < len; i++)
|
|
{
|
|
printf("%02X", data[i]);
|
|
}
|
|
}
|
|
});
|
|
|
|
port_->read_async(0, 0);
|
|
#endif
|
|
Bus::publish(Bus::事件::打开串口, portName_);
|
|
}
|
|
|
|
}
|
|
else if (ptrPort->is_open() && ui->m_pushButton_OpenOrCloseSerialPort->text().contains("关闭串口")) //如果打开,点击关闭按钮则关闭串口
|
|
{
|
|
ptrPort->close();
|
|
SerialPortTools::SerialPortList->erase(index);
|
|
m_BijectiveMap.remove(index);
|
|
if (!ptrPort->is_open())
|
|
{
|
|
setOpenStatus(false);
|
|
}
|
|
}
|
|
|
|
}
|
|
else if (sender() == ui->pushButton_ConnectMqtt)//MQTT通信
|
|
{
|
|
if (ui->pushButton_ConnectMqtt->text() == "连接服务器")
|
|
{
|
|
QString ip = ui->lineEdit_mqttServerIP->text();
|
|
int port = ui->lineEdit_mqttServerPort->text().toInt();
|
|
|
|
emit sigMqttSetOpen(ip, port);
|
|
|
|
if (MQTT_GUIDE::m_pMQTTOpenration && !MQTT_GUIDE::m_pMQTTOpenration->IsOpen())
|
|
{
|
|
QByteArray qbIP = ip.toUtf8();
|
|
|
|
if (MQTT_GUIDE::m_pMQTTOpenration->Open(qbIP.data(), port))
|
|
{
|
|
m_bMQTTisOpen = true;
|
|
ui->lineEdit_mqttServerIP->setEnabled(false);
|
|
ui->lineEdit_mqttServerPort->setEnabled(false);
|
|
ui->pushButton_ConnectMqtt->setText("断开服务器");
|
|
QString str;
|
|
str += ("background-color: rgb(85, 255, 0);");
|
|
str += ("color: rgb(0, 0, 0);");
|
|
ui->pushButton_ConnectMqtt->setStyleSheet(str);
|
|
InitMQTTtopic();//订阅
|
|
//Test
|
|
//QByteArray ba("Test hello!");
|
|
//bool rel = MQTT_GUIDE::m_pMQTTOpenration->PublishDataByMQTT("cpu_track/RPC_Request/system",ba);
|
|
}
|
|
}
|
|
}
|
|
else
|
|
{
|
|
MQTT_GUIDE::m_pMQTTOpenration->Close();
|
|
ui->pushButton_ConnectMqtt->setText("连接服务器");
|
|
QString str = "";
|
|
ui->pushButton_ConnectMqtt->setStyleSheet(str);
|
|
ui->lineEdit_mqttServerIP->setEnabled(true);
|
|
ui->lineEdit_mqttServerPort->setEnabled(true);
|
|
}
|
|
}
|
|
}
|
|
void CommunicationWgt::OnSerialPortComboxCurIndexChanged(int index)
|
|
{
|
|
|
|
if (m_BijectiveMap.contiansKey(index))
|
|
{
|
|
ShowMsg("提示", "串口已打开");
|
|
ui->m_pushButton_OpenOrCloseSerialPort->setText("关闭串口");
|
|
m_currentBtnIsOpen = true;
|
|
// 点亮图标
|
|
ui->m_label_ConnectFlag->setPixmap(QPixmap(":/image/connect.png"));
|
|
ui->m_label_ConnectFlag->setScaledContents(true);
|
|
|
|
ui->m_TxCombox->setCurrentIndex(m_BijectiveMap.getValue(index));
|
|
ui->m_TxCombox->setEnabled(false);
|
|
}
|
|
else {
|
|
ui->m_pushButton_OpenOrCloseSerialPort->setText("打开串口");
|
|
m_currentBtnIsOpen = false;
|
|
// 熄灭图标
|
|
ui->m_label_ConnectFlag->setPixmap(QPixmap(":/image/disconnect.png"));
|
|
ui->m_label_ConnectFlag->setScaledContents(true);
|
|
ui->m_TxCombox->setEnabled(true);
|
|
}
|
|
}
|