博客用于自己记录学习。网上有很多教程,我看的是【北京迅为】嵌入式学习之QT学习篇_哔哩哔哩_bilibili
一.串口界面的制作
二.编程 实现逻辑功能
1.
2.关联信号和槽,进行初始化
/*波特率设置*/
if(ui->botBox->currentText() == "4800")
{
baudRate = QSerialPort::Baud4800;
}
else if(ui->botBox->currentText() == "9600")
{
baudRate = QSerialPort::Baud9600;
}
else if(ui->botBox->currentText() == "115200")
{
baudRate = QSerialPort::Baud115200;
}
/*数据位设置*/
if(ui->dataBox->currentText() == "5")
{
dataBits = QSerialPort::Data5;
}
else if(ui->dataBox->currentText() == "6")
{
dataBits = QSerialPort::Data6;
}
else if(ui->dataBox->currentText() == "7")
{
dataBits = QSerialPort::Data7;
}
else if(ui->dataBox->currentText() == "8")
{
dataBits = QSerialPort::Data8;
}
/*停止位设置*/
if(ui->stopBox->currentText() == "1")
{
stopBits = QSerialPort::OneStop;
}
else if(ui->stopBox->currentText() == "1.5")
{
stopBits = QSerialPort::OneAndHalfStop;
}
else if(ui->stopBox->currentText() == "2")
{
stopBits = QSerialPort::TwoStop;
}
/*校验位设置*/
if(ui->checkBox->currentText() == "none")
{
checkBits = QSerialPort::NoParity;
}
serialPort->setPortName(ui->serialBox->currentText());
serialPort->setBaudRate(baudRate);
serialPort->setDataBits(dataBits);
serialPort->setStopBits(stopBits);
serialPort->setParity(checkBits);
if(serialPort->open(QIODevice::ReadWrite)==true)
{
QMessageBox::information(this,"提示","成功");
}
else
{
QMessageBox::critical(this,"提示","失败");
}
}
void Widget::on_closeButton_2_clicked()
{
serialPort->close();
}
效果演示
再次打开,显示失败
点击关闭后,再次打开,提示成功。