接收显示控件
添加串口模块
添加头文件
在头文件中声明一个函数
使用声明的函数
在mainwindow中进行调用InitUI
void MainWindow::InitUI()
{
//串口名称
for(int i=1;i<=10;i++)
{
ui->portName->addItem(QString("/dev/ttyS%1").arg(i));
}
//波特率
ui->buadRate->addItem("1200",QSerialPort::Baud1200);
ui->buadRate->addItem("2400",QSerialPort::Baud2400);
ui->buadRate->addItem("4800",QSerialPort::Baud4800);
ui->buadRate->addItem("9600",QSerialPort::Baud9600);
ui->buadRate->addItem("19200",QSerialPort::Baud19200);
//数据位
ui->dataBits->addItem("8",QSerialPort::Data8);
ui->dataBits->addItem("7",QSerialPort::Data7);
ui->dataBits->addItem("6",QSerialPort::Data6);
ui->dataBits->addItem("5",QSerialPort::Data5);
//奇偶校验
ui->parity->addItem("None",QSerialPort::NoParity);
ui->parity->addItem("Even",QSerialPort::EvenParity);
ui->parity->addItem("odd",QSerialPort::OddParity);
//停止位
ui->stopBits->addItem("1",QSerialPort::OneStop);
ui->stopBits->addItem("1.5",QSerialPort::OneAndHalfStop);
ui->stopBits->addItem("2",QSerialPort::TwoStop);
//流控制
ui->flowControl->addItem("None",QSerialPort::NoFlowControl);
ui->flowControl->addItem("RTS/CTS",QSerialPort::HardwareControl);
ui->flowControl->addItem("XON/XOFF",QSerialPort::SoftwareControl);
}
打开按钮代码
void MainWindow::on_openButton_clicked()
{
QString text=ui->openButton->text();
if(text==QStringLiteral("打开串口"))
{
//设置串口的各种参数
m_serial.setPortName(ui->portName->currentText());
m_serial.setBaudRate(ui->buadRate->currentData().toInt());
m_serial.setParity((QSerialPort::Parity)ui->parity->currentData().toInt());
m_serial.setDataBits((QSerialPort::DataBits)ui->dataBits->currentData().toInt());
m_serial.setStopBits((QSerialPort::StopBits)ui->stopBits->currentData().toInt());
m_serial.setFlowControl((QSerialPort::FlowControl)ui->flowControl->currentData().toInt());
//打开串口
bool ret=m_serial.open(QIODevice::ReadWrite); //打开并设置为读和写
if(ret)
{
ui->groupBox->setEnabled(false); //禁用参数更改
ui->openButton->setText(QStringLiteral("关闭串口"));
}
else
{
ui->statusbar->showMessage(m_serial.errorString()+QString::number(m_serial.error()),5000); //打开失败,显示错误信息
}
ui->openButton->setText(QStringLiteral("关闭串口"));
}
else
{
//关闭串口
m_serial.close();
ui->groupBox->setEnabled(true); //开启参数更改
ui->openButton->setText(QStringLiteral("打开串口"));
}
}
//串口发送信号槽函数
void MainWindow::bytesWriteData(qint64 bytes)
{
ui->statusbar->showMessage(QStringLiteral("发送了%1字节!").arg(bytes),5000);
}
//串口接收函数
void MainWindow::serialReadData()
{
QByteArray arr= m_serial.readAll();
QString strtext=QString(arr);//字节数组转为字符串
//加上时间
QDateTime current_date_time=QDateTime::currentDateTime();
QString t=current_date_time.toString("yyyy-MM-dd hh:mm:ss.zzz:");
//追加到末尾
ui->recvTextEdit->appendPlainText(t+strtext+"\n");
}
//发送数据
void MainWindow::on_sendButton_clicked()
{
QString strSend=ui->sendTextEdit->toPlainText();
QByteArray arr=strSend.toUtf8();
//发送数据
m_serial.write(arr);
}
//连接有数据到来信号与槽
connect(&m_serial,&QSerialPort::readyRead,this,&MainWindow::serialReadData);
//连接数据发送后的信号槽
connect(&m_serial,&QSerialPort::bytesWritten,this,&MainWindow::bytesWriteData);
定时器的使用
标签:addItem,QT,串口,QSerialPort,ui,LINUX,serial,MainWindow From: https://www.cnblogs.com/yzdz2/p/17058709.html