资源简介
QT串口通信助手,可实现串口间通信,打开串口(关闭串口),发送串口数据;
代码片段和文件信息
#include “frmserialport.h“
#include “ui_frmserialport.h“
#include
#include
#include
#pragma execution_character_set(“UTF-8“)
frmserialport::frmserialport(QWidget *parent) :
QWidget(parent)
ui(new Ui::frmserialport)
{
ui->setupUi(this);
inialSerialPort();
}
frmserialport::~frmserialport()
{
delete ui;
}
void frmserialport::on_btn_Open_clicked()
{
m_pSerialPort = new QSerialPort(this);
m_pSerialPort->setPortName(ui->cbox_PortName->currentText());
m_pSerialPort->setBaudRate(ui->cbox_BaudRate->currentText().toInt());
switch (ui->cbox_DataBit->currentIndex()) {
case 0:
m_pSerialPort->setDataBits(QSerialPort::Data5);
break;
case 1:
m_pSerialPort->setDataBits(QSerialPort::Data6);
break;
case 2:
m_pSerialPort->setDataBits(QSerialPort::Data7);
break;
case 3:
m_pSerialPort->setDataBits(QSerialPort::Data8);
break;
default:
break;
}
switch (ui->cbox_Parity->currentIndex()) {
case 0:
m_pSerialPort->setParity(QSerialPort::NoParity);//“无“
break;
case 1:
m_pSerialPort->setParity(QSerialPort::OddParity);//“奇“
break;
case 2:
m_pSerialPort->setParity(QSerialPort::EvenParity);//“偶“
break;
default:
break;
}
switch (ui->cbox_StopBit->currentIndex()) {
case 0:
m_pSerialPort->setStopBits(QSerialPort::OneStop);
break;
case 1:
m_pSerialPort->setStopBits(QSerialPort::OneAndHalfStop);
break;
case 2:
m_pSerialPort->setStopBits(QSerialPort::TwoStop);
break;
default:
break;
}
switch (ui->cbox_Flow->currentIndex()) {
case 0:
m_pSerialPort->setFlowControl(QSerialPort::NoFlowControl);
break;
case 1:
m_pSerialPort->setFlowControl(QSerialPort::HardwareControl);
break;
case 2:
m_pSerialPort->setFlowControl(QSerialPort::SoftwareControl);
break;
case 3:
m_pSerialPort->setFlowControl(QSerialPort::UnknownFlowControl);
break;
default:
break;
}
if (!m_pSerialPort->open(QIODevice::ReadWrite)){
QMessageBox::information(this“提示““串口连接失败“);
}
else{
QMessageBox::information(this“提示“ui->cbox_PortName->currentText() + tr(“连接成功“));
ui->btn_Open->setEnabled(false);
ui->btn_Close->setEnabled(true);
ui->btn_Send->setEnabled(true);
}
connect(m_pSerialPort&QSerialPort::readyReadthis&frmserialport::serialReadData);
}
void frmserialport::on_btn_Close_clicked()
{
m_pSerialPort->clear();
m_pSerialPort->close();
delete m_pSerialPort;
m_pSerialPort = nullptr;
m_flag = 0;
ui->btn_Open->setEnabled(true);
ui->btn_Close->setEnabled(false);
ui->btn_Send->setEnabled(false
属性 大小 日期 时间 名称
----------- --------- ---------- ----- ----
目录 0 2019-01-14 19:12 myFirstSerialPort\
文件 5020 2019-01-14 17:32 myFirstSerialPort\frmserialport.cpp
文件 786 2019-01-14 18:55 myFirstSerialPort\frmserialport.h
文件 5102 2019-01-11 16:00 myFirstSerialPort\frmserialport.ui
文件 189 2019-01-11 15:43 myFirstSerialPort\main.cpp
文件 479 2019-01-14 15:31 myFirstSerialPort\myFirstSerialPort.pro
文件 18715 2019-01-14 19:12 myFirstSerialPort\myFirstSerialPort.pro.user
文件 66062 2018-04-19 10:35 myFirstSerialPort\XW.ico
- 上一篇:opencv.txt
- 下一篇:语音识别项目案例
评论
共有 条评论