#include "serial_port.h"
#include<qtimer.h>
serial_port::serial_port(QWidget* parent)
: QMainWindow(parent)
{
ui.setupUi(this);
rst();//初始化
/****************************************************************************************************/
//定时器设置
QTimer* pTimer = new QTimer(this);
pTimer->start(100); // 启动定时器
// 一但定时器启用后,会间隔一段时间就发送一个信号timeout
connect(pTimer, &QTimer::timeout, this, [=]() {
serialList = QSerialPortInfo::availablePorts();
if (serialList.size() != num_of_port)
{
num_of_port = serialList.size();
get_portinfo();
}
});
//pTimer->stop(); // 关闭定时器
/*****************************************************************************************************/
//连接comboBox信号与槽区域
connect(ui.comboBox, &QComboBox::currentTextChanged, this, [=]() {
serial.setPortName(ui.comboBox->currentText());
//ui.textEdit->setPlainText(ui.comboBox->currentText());
});
connect(ui.comboBox_2, &QComboBox::currentTextChanged, this, [=]() {
serial.setBaudRate(ui.comboBox_2->currentText().toInt());
});
connect(ui.comboBox_3, &QComboBox::currentTextChanged, this, [=]() {
switch (ui.comboBox_3->currentIndex())
{
case 0:serial.setStopBits(QSerialPort::OneStop); break;
case 1:serial.setStopBits(QSerialPort::OneAndHalfStop); break;
case 2:serial.setStopBits(QSerialPort::TwoStop); break;
default:
break;
}
});
connect(ui.comboBox_4, &QComboBox::currentTextChanged, this, [=]() {
switch (ui.comboBox_4->currentIndex())
{
case 0:serial.setDataBits(QSerialPort::Data8); break;
case 1:serial.setDataBits(QSerialPort::Data7); break;
case 2:serial.setDataBits(QSerialPort::Data6); break;
case 3:serial.setDataBits(QSerialPort::Data5); break;
default:
break;
}
// ui.textEdit->setPlainText(ui.comboBox_4->currentText());
});
connect(ui.comboBox_5, &QComboBox::currentTextChanged, this, [=]() {
switch (ui.comboBox_5->currentIndex())
{
case 0:serial.setParity(QSerialPort::EvenParity); break;
case 1:serial.setParity(QSerialPort::OddParity); break;
case 2:serial.setParity(QSerialPort::NoParity); break;
default:
break;
}
//ui.textEdit->setPlainText(ui.comboBox_5->currentText());
});
/*******************************************************************/
//按钮槽设置
//打开串口
connect(ui.pushButton, &QPushButton::clicked, this, [=] {
if (ui.pushButton->text() == "打开串口")
{
ui.pushButton->setText("关闭串口");
serial.open(QSerialPort::ReadWrite);
}
else
{
ui.pushButton->setText("打开串口");
serial.close();
}
connect(&serial, SIGNAL(readyRead()), this, SLOT(read_port()));
connect(ui.pushButton_2, &QPushButton::clicked, this, [=] {
QStringList text = ui.textEdit_2->toPlainText().split("\n");
for (int i = 0; i < text.size(); i++) {
serial.write(text.at(i).toLocal8Bit() + "\r\n");
}
});
});
}
/***************************************************************************************************/
/***************************************读取当前串口数据********************************************/
/***************************************************************************************************/
void serial_port::read_port()
{
QByteArray buf;
buf = serial.readAll();
if (!buf.isEmpty())
{
str.append(QString::fromLocal8Bit(buf));
ui.textEdit->setPlainText(str);
}
buf.clear();
};
/***************************************************************************************************/
/***********************获取当前可用串口信息,并更新串口号显示**************************************/
/***************************************************************************************************/
void serial_port::get_portinfo()
{
//serialList = QSerialPortInfo::availablePorts();//给链表赋值为获取到的当前设备的所有串口信息的链表
ui.comboBox->clear();//清除显示
if (serialList.size() == 0)
{
ui.comboBox->addItem("none");//无可用端口显示none
}
else
{
ui.comboBox->addItem(current_portname);
for (int i = 0; i < serialList.size(); i++)
{
if (serialList[i].portName() != current_portname)
{
ui.comboBox->addItem(serialList[i].portName());//将可用的串口端口号传入串口号对应的comboBox
}
}
}
};
/*****************************************************************************************/
/**************************************端口初始化*****************************************/
/*****************************************************************************************/
void serial_port::rst()
{
// 端口初始化
serialList = QSerialPortInfo::availablePorts();
if (serialList.size() == 0)
{
current_portname = "none";
}
else
{
current_portname = serialList[0].portName();
}
serial.setBaudRate(QSerialPort::Baud9600);
ui.comboBox_2->setCurrentText("9600");
serial.setDataBits(QSerialPort::Data8);
ui.comboBox_4->setCurrentText("8");
serial.setParity(QSerialPort::EvenParity);
ui.comboBox_5->setCurrentText("even");
serial.setStopBits(QSerialPort::OneStop);
ui.comboBox_3->setCurrentText("1");
serial.setFlowControl(QSerialPort::NoFlowControl);
};
评论0
最新资源