Qt 串口相关配置
2024-10-22 04:52:54
QSerialPort M_serialPort;
QSerialPortInfo M_SerialPortInfo;
void MainWindow::on_M_portNumComboBox_currentIndexChanged(const QString &arg1)
{
#ifdef MAIN_DEBUG
qDebug()<<"M_serialPort : " <<arg1;
#endif
if(M_SerialPortInfo.portName() == arg1)
{
M_serialPort.setPortName(M_SerialPortInfo.portName());
}
else
{
qDebug()<<"M没有此串口";
}
}
//获取并设置M串口信息
void MainWindow::on_M_stopBitComboBox_currentIndexChanged(int index)
{
#ifdef MAIN_DEBUG
qDebug()<<"M_serialPort.stopBit : " <<index;
#endif
switch (index)
{
case 0:
M_serialPort.setStopBits(QSerialPort::OneStop);
break;
case 1:
M_serialPort.setStopBits(QSerialPort::TwoStop);
break;
default:
M_serialPort.setStopBits(QSerialPort::UnknownStopBits);
break;
}
}
void MainWindow::on_M_baudRateComboBox_currentIndexChanged(int index)
{
#ifdef MAIN_DEBUG
qDebug()<<"M_serialPort.baudRate : " <<index;
#endif
switch (index)
{
case 0:
M_serialPort.setBaudRate(QSerialPort::Baud1200,QSerialPort::AllDirections);
break;
case 1:
M_serialPort.setBaudRate(QSerialPort::Baud2400,QSerialPort::AllDirections);
break;
case 2:
M_serialPort.setBaudRate(QSerialPort::Baud4800,QSerialPort::AllDirections);;
break;
case 3:
M_serialPort.setBaudRate(QSerialPort::Baud9600,QSerialPort::AllDirections);;
break;
case 4:
M_serialPort.setBaudRate(QSerialPort::Baud19200,QSerialPort::AllDirections);;
break;
case 5:
M_serialPort.setBaudRate(QSerialPort::Baud38400,QSerialPort::AllDirections);;
break;
case 6:
M_serialPort.setBaudRate(QSerialPort::Baud57600,QSerialPort::AllDirections);;
break;
case 7:
M_serialPort.setBaudRate(QSerialPort::Baud115200,QSerialPort::AllDirections);;
break;
default:
M_serialPort.setBaudRate(QSerialPort::UnknownBaud,QSerialPort::AllDirections);;
break;
}
}
void MainWindow::on_M_parityBitComboBox_currentIndexChanged(int index)
{
#ifdef MAIN_DEBUG
qDebug()<<"M_serialPort.parityBit : " <<index;
#endif
switch (index)
{
case 0:
M_serialPort.setParity(QSerialPort::NoParity);
break;
case 1:
M_serialPort.setParity(QSerialPort::OddParity);
break;
case 2:
M_serialPort.setParity(QSerialPort::EvenParity);
default:
M_serialPort.setParity(QSerialPort::UnknownParity);
break;
}
}
void MainWindow::on_M_digitBitComboBox_currentIndexChanged(int index)
{
#ifdef MAIN_DEBUG
qDebug()<<"M_serialPort.dataBit : " <<index;
#endif
switch (index)
{
case 0:
M_serialPort.setDataBits(QSerialPort::Data5);
break;
case 1:
M_serialPort.setDataBits(QSerialPort::Data6);
break;
case 2:
M_serialPort.setDataBits(QSerialPort::Data7);
break;
case 3:
M_serialPort.setDataBits(QSerialPort::Data8);
break;
default:
M_serialPort.setDataBits(QSerialPort::UnknownDataBits);
break;
}
}
void MainWindow::on_M_dataFlowComboBox_currentIndexChanged(int index)
{
#ifdef MAIN_DEBUG
qDebug()<<"M_serialPort.dataFlow : " <<index;
#endif
switch (index)
{
case 0:
M_serialPort.setFlowControl(QSerialPort::NoFlowControl);
break;
case 1:
M_serialPort.setFlowControl(QSerialPort::HardwareControl);
break;
case 2:
M_serialPort.setFlowControl(QSerialPort::SoftwareControl);
default:
M_serialPort.setFlowControl(QSerialPort::UnknownFlowControl);
break;
}
}
void MainWindow::on_M_openSerialPortButton_clicked()
{
bool comState = M_serialPort.open(QIODevice::ReadWrite);//打开串口并选择读写模式
if(comState)
{
ui->statusBar->showMessage("M串口打开成功");
}
else
{
ui->statusBar->showMessage("M串口打开失败");
}
}
void MainWindow::on_M_closeSerialPortButton_clicked()
最新文章
- 以self-contained方式在Linux上部署ASP.NET Core站点
- storm中DAU实时计算方案
- [原创]纯JS实现网页中多选复选框checkbox和单选radio的美化效果
- MVVM架构~Knockoutjs系列之js接收C#数据集合的方式
- Android保持屏幕常亮的方法
- JavaScript parseInt函数
- 关于DOM树的常见增删操作
- extjs4与ckeditor、ckfinder整合
- linux内核值shmmax问题
- 白帽子讲Web安全2.pdf
- 原 IOS之NSValue整理
- 前端--关于CSS文本
- bzoj 3261最大异或和
- Python django解决跨域请求的问题
- java实现文章敏感词过滤检测
- Linux C 编程
- JSAP101
- Jenkins自动化构建(一)执行selenium+python脚本
- Angular2 组件与模板 -- 输入和输出属性
- 如何判断SOCKET还是连接着的