2,新增“Apps”; 3,新增“Common”; 4,新增“FileList”; 5,新增“MediaX”; 6,新增“OpenSource”; 7,新增“Samples”; 8,新增“SoftwareBusinessLines”.
190 lines
7.2 KiB
C++
190 lines
7.2 KiB
C++
#include "DeviceStatusPanel.h"
|
|
#include "ui_DeviceStatusPanel.h"
|
|
|
|
#include <QMessageBox>
|
|
|
|
static const char blankString[] = QT_TRANSLATE_NOOP("DeviceStatusPanel", "N/A");
|
|
|
|
DeviceStatusPanel::DeviceStatusPanel(QWidget *parent) :
|
|
QWidget(parent),
|
|
ui(new Ui::DeviceStatusPanel)
|
|
{
|
|
ui->setupUi(this);
|
|
|
|
// Com Port
|
|
m_pSerialPort = new QSerialPort(this);
|
|
|
|
FillPortsParameters();
|
|
FillPortsInfo();
|
|
|
|
UpdateSettings();
|
|
|
|
// 串口
|
|
connect(ui->m_pButtonComConnDisConn, &QPushButton::clicked, this, &DeviceStatusPanel::OnClicked_OpenCloseSerialPort);
|
|
}
|
|
|
|
DeviceStatusPanel::~DeviceStatusPanel()
|
|
{
|
|
delete ui;
|
|
}
|
|
|
|
void DeviceStatusPanel::FillPortsParameters()
|
|
{
|
|
ui->m_pListBoxBaudRate->addItem(QStringLiteral("1200"), QSerialPort::Baud1200);
|
|
ui->m_pListBoxBaudRate->addItem(QStringLiteral("2400"), QSerialPort::Baud2400);
|
|
ui->m_pListBoxBaudRate->addItem(QStringLiteral("4800"), QSerialPort::Baud4800);
|
|
ui->m_pListBoxBaudRate->addItem(QStringLiteral("9600"), QSerialPort::Baud9600);
|
|
ui->m_pListBoxBaudRate->addItem(QStringLiteral("19200"), QSerialPort::Baud19200);
|
|
ui->m_pListBoxBaudRate->addItem(QStringLiteral("38400"), QSerialPort::Baud38400);
|
|
ui->m_pListBoxBaudRate->addItem(QStringLiteral("57600"), QSerialPort::Baud57600);
|
|
ui->m_pListBoxBaudRate->addItem(QStringLiteral("115200"), QSerialPort::Baud115200);
|
|
ui->m_pListBoxBaudRate->setCurrentIndex(4);
|
|
|
|
ui->m_pListBoxDataBits->addItem(QStringLiteral("5"), QSerialPort::Data5);
|
|
ui->m_pListBoxDataBits->addItem(QStringLiteral("6"), QSerialPort::Data6);
|
|
ui->m_pListBoxDataBits->addItem(QStringLiteral("7"), QSerialPort::Data7);
|
|
ui->m_pListBoxDataBits->addItem(QStringLiteral("8"), QSerialPort::Data8);
|
|
ui->m_pListBoxDataBits->setCurrentIndex(3);
|
|
|
|
ui->m_pListBoxParity->addItem(tr("None"), QSerialPort::NoParity);
|
|
ui->m_pListBoxParity->addItem(tr("Even"), QSerialPort::EvenParity);
|
|
ui->m_pListBoxParity->addItem(tr("Odd"), QSerialPort::OddParity);
|
|
ui->m_pListBoxParity->addItem(tr("Mark"), QSerialPort::MarkParity);
|
|
ui->m_pListBoxParity->addItem(tr("Space"), QSerialPort::SpaceParity);
|
|
ui->m_pListBoxParity->setCurrentIndex(0);
|
|
|
|
ui->m_pListBoxStopBits->addItem(QStringLiteral("1"), QSerialPort::OneStop);
|
|
#ifdef Q_OS_WIN
|
|
ui->m_pListBoxStopBits->addItem(tr("1.5"), QSerialPort::OneAndHalfStop);
|
|
#endif
|
|
ui->m_pListBoxStopBits->addItem(QStringLiteral("2"), QSerialPort::TwoStop);
|
|
ui->m_pListBoxStopBits->setCurrentIndex(0);
|
|
|
|
ui->m_pListBoxFlowControl->addItem(tr("None"), QSerialPort::NoFlowControl);
|
|
ui->m_pListBoxFlowControl->addItem(tr("RTS/CTS"), QSerialPort::HardwareControl);
|
|
ui->m_pListBoxFlowControl->addItem(tr("XON/XOFF"), QSerialPort::SoftwareControl);
|
|
ui->m_pListBoxFlowControl->setCurrentIndex(0);
|
|
}
|
|
|
|
void DeviceStatusPanel::FillPortsInfo()
|
|
{
|
|
ui->m_pListBoxSerialPort->clear();
|
|
QString description;
|
|
QString manufacturer;
|
|
QString serialNumber;
|
|
|
|
foreach(const QSerialPortInfo &info, QSerialPortInfo::availablePorts()) {
|
|
QStringList list;
|
|
description = info.description();
|
|
manufacturer = info.manufacturer();
|
|
serialNumber = info.serialNumber();
|
|
list << info.portName()
|
|
<< (!description.isEmpty() ? description : blankString)
|
|
<< (!manufacturer.isEmpty() ? manufacturer : blankString)
|
|
<< (!serialNumber.isEmpty() ? serialNumber : blankString)
|
|
<< info.systemLocation()
|
|
<< (info.vendorIdentifier() ? QString::number(info.vendorIdentifier(), 16) : blankString)
|
|
<< (info.productIdentifier() ? QString::number(info.productIdentifier(), 16) : blankString);
|
|
|
|
ui->m_pListBoxSerialPort->addItem(list.first(), list);
|
|
}
|
|
}
|
|
|
|
void DeviceStatusPanel::UpdateSettings()
|
|
{
|
|
m_varSerialPortSettings.name = ui->m_pListBoxSerialPort->currentText();
|
|
|
|
if (ui->m_pListBoxBaudRate->currentIndex() == 4)
|
|
{
|
|
m_varSerialPortSettings.baudRate = ui->m_pListBoxBaudRate->currentText().toInt();
|
|
}
|
|
else
|
|
{
|
|
m_varSerialPortSettings.baudRate = static_cast<QSerialPort::BaudRate>(
|
|
ui->m_pListBoxBaudRate->itemData(ui->m_pListBoxBaudRate->currentIndex()).toInt());
|
|
}
|
|
m_varSerialPortSettings.stringBaudRate = QString::number(m_varSerialPortSettings.baudRate);
|
|
|
|
m_varSerialPortSettings.dataBits = static_cast<QSerialPort::DataBits>(
|
|
ui->m_pListBoxDataBits->itemData(ui->m_pListBoxDataBits->currentIndex()).toInt());
|
|
m_varSerialPortSettings.stringDataBits = ui->m_pListBoxDataBits->currentText();
|
|
|
|
m_varSerialPortSettings.parity = static_cast<QSerialPort::Parity>(
|
|
ui->m_pListBoxParity->itemData(ui->m_pListBoxParity->currentIndex()).toInt());
|
|
m_varSerialPortSettings.stringParity = ui->m_pListBoxParity->currentText();
|
|
|
|
m_varSerialPortSettings.stopBits = static_cast<QSerialPort::StopBits>(
|
|
ui->m_pListBoxStopBits->itemData(ui->m_pListBoxStopBits->currentIndex()).toInt());
|
|
m_varSerialPortSettings.stringStopBits = ui->m_pListBoxStopBits->currentText();
|
|
|
|
m_varSerialPortSettings.flowControl = static_cast<QSerialPort::FlowControl>(
|
|
ui->m_pListBoxFlowControl->itemData(ui->m_pListBoxFlowControl->currentIndex()).toInt());
|
|
m_varSerialPortSettings.stringFlowControl = ui->m_pListBoxFlowControl->currentText();
|
|
}
|
|
|
|
|
|
//! [4]
|
|
void DeviceStatusPanel::OpenSerialPort()
|
|
{
|
|
UpdateSettings();
|
|
|
|
m_pSerialPort->setPortName(m_varSerialPortSettings.name);
|
|
m_pSerialPort->setBaudRate(m_varSerialPortSettings.baudRate);
|
|
m_pSerialPort->setDataBits(m_varSerialPortSettings.dataBits);
|
|
m_pSerialPort->setParity(m_varSerialPortSettings.parity);
|
|
m_pSerialPort->setStopBits(m_varSerialPortSettings.stopBits);
|
|
m_pSerialPort->setFlowControl(m_varSerialPortSettings.flowControl);
|
|
|
|
if (m_pSerialPort->open(QIODevice::ReadWrite))
|
|
{
|
|
//ui->actionConnect->setEnabled(false);
|
|
//ui->actionDisconnect->setEnabled(true);
|
|
//ui->actionConfigure->setEnabled(false);
|
|
// showStatusMessage(tr("Connected to %1 : %2, %3, %4, %5, %6")
|
|
// .arg(p.name).arg(p.stringBaudRate).arg(p.stringDataBits)
|
|
// .arg(p.stringParity).arg(p.stringStopBits).arg(p.stringFlowControl));
|
|
}
|
|
else
|
|
{
|
|
QMessageBox::critical(this, tr("Error"), m_pSerialPort->errorString());
|
|
}
|
|
}
|
|
|
|
void DeviceStatusPanel::CloseSerialPort()
|
|
{
|
|
if (m_pSerialPort->isOpen())
|
|
{
|
|
m_pSerialPort->close();
|
|
}
|
|
}
|
|
|
|
void DeviceStatusPanel::OnClicked_OpenCloseSerialPort()
|
|
{
|
|
if (!m_pSerialPort->isOpen())
|
|
{
|
|
OpenSerialPort();
|
|
|
|
ui->m_pButtonComConnDisConn->setText(QObject::tr("断开连接"));
|
|
|
|
ui->m_pListBoxSerialPort->setEnabled(false);
|
|
ui->m_pListBoxBaudRate->setEnabled(false);
|
|
ui->m_pListBoxDataBits->setEnabled(false);
|
|
ui->m_pListBoxParity->setEnabled(false);
|
|
ui->m_pListBoxStopBits->setEnabled(false);
|
|
ui->m_pListBoxFlowControl->setEnabled(false);
|
|
}
|
|
else
|
|
{
|
|
CloseSerialPort();
|
|
|
|
ui->m_pButtonComConnDisConn->setText(QObject::tr("连接串口"));
|
|
|
|
ui->m_pListBoxSerialPort->setEnabled(true);
|
|
ui->m_pListBoxBaudRate->setEnabled(true);
|
|
ui->m_pListBoxDataBits->setEnabled(true);
|
|
ui->m_pListBoxParity->setEnabled(true);
|
|
ui->m_pListBoxStopBits->setEnabled(true);
|
|
ui->m_pListBoxFlowControl->setEnabled(true);
|
|
}
|
|
}
|