132 lines
4.0 KiB
C++
132 lines
4.0 KiB
C++
#include "mainwindow.h"
|
||
#include "./ui_mainwindow.h"
|
||
#include <QSerialPort>
|
||
#include <QSerialPortInfo>
|
||
#include <QMessageBox>
|
||
#include <QDebug>
|
||
#include <QStandardItemModel>
|
||
#include <QStandardItem>
|
||
|
||
|
||
|
||
MainWindow::MainWindow(QWidget *parent)
|
||
: QMainWindow(parent)
|
||
, ui(new Ui::MainWindow)
|
||
, serial(new QSerialPort(this)) // 在构造函数中初始化 serial
|
||
{
|
||
ui->setupUi(this);
|
||
// 连接串口的 readyRead 信号到槽函数
|
||
connect(serial, &QSerialPort::readyRead, this, &MainWindow::onReadyRead);
|
||
#ifdef Q_OS_WIN
|
||
qDebug() << "当前是 Windows 平台";
|
||
#elif defined(Q_OS_MAC)
|
||
qDebug() << "当前是 macOS 平台";
|
||
#elif defined(Q_OS_LINUX)
|
||
qDebug() << "当前是 Linux 平台";
|
||
#else
|
||
qDebug() << "未知平台";
|
||
#endif
|
||
QStringList portList;
|
||
const auto serialPortList = QSerialPortInfo::availablePorts();
|
||
for (const QSerialPortInfo &info : serialPortList) {
|
||
qDebug() << "Port Name:" << info.portName();
|
||
qDebug() << "Description:" << info.description();
|
||
qDebug() << "Manufacturer:" << info.manufacturer();
|
||
qDebug() << "Serial Number:" << info.serialNumber();
|
||
qDebug() << "System Location:" << info.systemLocation();
|
||
qDebug() << "-----------------------------";
|
||
if (info.manufacturer().compare("GenesysLogic") == 0 ||info.manufacturer().compare("Espressif") == 0) {
|
||
// 完全匹配
|
||
if(info.systemLocation().startsWith("/dev/tty."))
|
||
{
|
||
portList << info.systemLocation();
|
||
}
|
||
}
|
||
|
||
}
|
||
|
||
ui->comboBox->addItems(portList);
|
||
|
||
// 创建 QStandardItemModel(表格数据模型)
|
||
QStandardItemModel *model = new QStandardItemModel(3, 2);
|
||
|
||
// 设置表头标签
|
||
model->setHorizontalHeaderLabels({"Name", "value"});
|
||
|
||
// 插入数据
|
||
model->setItem(0, 0, new QStandardItem("SN"));
|
||
model->setItem(0, 1, new QStandardItem("123456789"));
|
||
model->setItem(1, 0, new QStandardItem("BLE_NAME"));
|
||
model->setItem(1, 1, new QStandardItem("G01-06"));
|
||
model->setItem(2, 0, new QStandardItem(""));
|
||
model->setItem(2, 1, new QStandardItem(""));
|
||
|
||
// 将模型与视图关联
|
||
ui->tableView->setModel(model);
|
||
|
||
//设置列宽
|
||
ui->tableView->setColumnWidth(0,100);
|
||
ui->tableView->setColumnWidth(1,160);
|
||
|
||
}
|
||
|
||
MainWindow::~MainWindow()
|
||
{
|
||
delete ui;
|
||
}
|
||
|
||
void MainWindow::on_pushButton_clicked()
|
||
{
|
||
// 获取用户在 comboBox 中选择的串口设备
|
||
QString selectedPort = ui->comboBox->currentText();
|
||
|
||
if (selectedPort.isEmpty()) {
|
||
QMessageBox::warning(this, "警告", "请选择一个串口设备");
|
||
return;
|
||
}
|
||
|
||
// 创建串口对象,如果已创建则先关闭
|
||
if (!serial) {
|
||
serial = new QSerialPort(this);
|
||
} else {
|
||
// 确保串口已打开才调用 close
|
||
if (serial->isOpen()) {
|
||
serial->close();
|
||
}
|
||
}
|
||
|
||
// 设置串口信息
|
||
serial->setPortName(selectedPort);
|
||
serial->setBaudRate(QSerialPort::Baud9600); // 设置波特率
|
||
serial->setDataBits(QSerialPort::Data8); // 数据位
|
||
serial->setParity(QSerialPort::NoParity); // 校验位
|
||
serial->setStopBits(QSerialPort::OneStop); // 停止位
|
||
serial->setFlowControl(QSerialPort::NoFlowControl); // 无流控
|
||
|
||
// 打开串口
|
||
if (serial->open(QIODevice::ReadWrite)) {
|
||
QMessageBox::information(this, "成功", "串口已成功打开");
|
||
|
||
// 这里可以执行更多的操作,比如发送数据、读取数据等
|
||
// 连接 readyRead 信号到槽函数
|
||
// connect(serial, &QSerialPort::readyRead, this, &MainWindow::onReadyRead);
|
||
// 示例:发送简单数据
|
||
QByteArray dataToSend = "Hello, Serial Port!\r\n";
|
||
serial->write(dataToSend);
|
||
} else {
|
||
QMessageBox::critical(this, "错误", "无法打开串口 " + selectedPort);
|
||
}
|
||
|
||
}
|
||
|
||
void MainWindow::onReadyRead()
|
||
{
|
||
while (serial->canReadLine()) {
|
||
QByteArray line = serial->readLine(); // 读取一行数据
|
||
qDebug() << "接收到一行数据: " << line;
|
||
|
||
// 处理数据
|
||
|
||
}
|
||
}
|