#include "widget.h"
#include "ui_widget.h"
#include <QMessageBox>
Widget::Widget(QWidget *parent) :
QWidget(parent),
ui(new Ui::Widget)
{
ui->setupUi(this);
textstate_receive = false;
textstate_send = false;
setWindowTitle("个人串口助手");
serialport = new QSerialPort;
find_port(); //查找可用串口
ui->send_button->setEnabled(false); //设置控件不可用
ui->close_port->setEnabled(false);
}
Widget::~Widget()
{
delete ui,serialport;
}
//发送数据
void Widget::on_send_button_clicked()
{
if(textstate_send == true) //文版模式
{
serialport->write(ui->send_text_window->toPlainText().toLatin1());
}
if(textstate_send == false) //16进制
{
QString str = ui->send_text_window->toPlainText();
int num = str.toInt();
str = str.setNum(num,16);
ui->send_text_window->clear();
ui->send_text_window->append(str);
serialport->write(ui->send_text_window->toPlainText().toLatin1());
}
}
//打开串口
void Widget::on_open_port_clicked()
{
update();
sleep(100); //延时100ms
find_port(); //重新查找com
//初始化串口
serialport->setPortName(ui->com->currentText()); //设置串口名
if(serialport->open(QIODevice::ReadWrite)) //打开串口成功
{
serialport->setBaudRate(ui->baud->currentText().toInt()); //设置波特率
switch(ui->bit->currentIndex()) //设置数据位数
{
case 8:serialport->setDataBits(QSerialPort::Data8);break;
default: break;
}
switch(ui->jiaoyan->currentIndex()) //设置奇偶校验
{
case 0: serialport->setParity(QSerialPort::NoParity);break;
default: break;
}
switch(ui->stopbit->currentIndex()) //设置停止位
{
case 1: serialport->setStopBits(QSerialPort::OneStop);break;
case 2: serialport->setStopBits(QSerialPort::TwoStop);break;
default: break;
}
serialport->setFlowControl(QSerialPort::NoFlowControl); //设置流控制
//连接槽函数
QObject::connect(serialport, &QSerialPort::readyRead, this, &Widget::Read_Date);
// 设置控件可否使用
ui->send_button->setEnabled(true);
ui->close_port->setEnabled(true);
ui->open_port->setEnabled(false);
}
else //打开失败提示
{
sleep(100);
QMessageBox::information(this,tr("Erro"),tr("Open the failure"),QMessageBox::Ok);
}
}
//关闭串口
void Widget::on_close_port_clicked()
{
serialport->clear(); //清空缓存区
serialport->close(); //关闭串口
ui->send_button->setEnabled(false);
ui->open_port->setEnabled(true);
ui->close_port->setEnabled(false);
}
//窗口显示串口传来的数据
void Widget::Read_Date()
{
QByteArray buf;
buf = serialport->readAll();
if(!buf.isEmpty()) //将数据显示到文本串口
{
if(textstate_receive == true) //文本模式
{
QString str = ui->Receive_text_window->toPlainText();
str+=tr(buf);
str += " ";
ui->Receive_text_window->clear();
ui->Receive_text_window->append(str);
}
if(textstate_receive == false) //文本模式
{
QString str = ui->Receive_text_window->toPlainText();
// byteArray 转 16进制
QByteArray temp = buf.toHex();
str+=tr(temp);
str += " ";
ui->Receive_text_window->clear();
ui->Receive_text_window->append(str);
}
}
buf.clear(); //清空缓存区
}
//查找串口
void Widget::find_port()
{
//查找可用的串口
foreach(const QSerialPortInfo &info, QSerialPortInfo::availablePorts())
{
QSerialPort serial;
serial.setPort(info); //设置串口
if(serial.open(QIODevice::ReadWrite))
{
ui->com->addItem(serial.portName()); //显示串口name
serial.close();
}
}
}
//延时函数
void Widget::sleep( int msec)
{
QTime dieTime = QTime::currentTime().addMSecs(msec);
while( QTime::currentTime() < dieTime )
QCoreApplication::processEvents(QEventLoop::AllEvents, 100);
}
void Widget::on_clear_button_1_clicked()
{
ui->send_text_window->clear();
}
void Widget::on_clear_button2_clicked()
{
ui->Receive_text_window->clear();
}
//接收框文本模式转换
void Widget::on_receive_modl_clicked()
{
if(ui->receive_modl->text() == "文本模式")
{
textstate_receive = true;
ui->receive_modl->setText("hex模式");
}
else
{
ui->receive_modl->setText("文本模式");
textstate_receive = false;
}
}
//发送框文本转换
void Widget::on_send_modl_clicked()
{
if(ui->send_modl->text() == "文本模式")
{
textstate_send = true;
ui->send_modl->setText("hex模式");
}
else
{
ui->send_modl->setText("文本模式");
textstate_send = false;
}
}
基于单片机的上位机串口通讯界面
需积分: 18 41 浏览量
2018-07-04
10:45:23
上传
评论 2
收藏 6KB ZIP 举报
ff-jiang
- 粉丝: 34
- 资源: 15
最新资源
- apk.tw_LineLite_v8a_v.2.17.1_sign.apk
- Elasticsearch实战:构建高效搜索系统的秘诀.zip
- HTML+CSS+JS网页设计:从入门到精通.zip
- 数据库课程设计:从理论到实践的全面指南.zip
- Python闭包:深入理解与应用场景解析.zip
- Win64OpenSSL-3-3-0.exe
- 课高分程设计-基于C++实现的民航飞行与地图简易管理系统-南京航空航天大学
- 航天器遥测数据故障检测系统python源码+文档说明+数据库(课程设计)
- 北京航空航天大学操作系统课设+ppt+实验报告
- 基于Vue+Echarts实现风力发电机中传感器的数据展示监控可视化系统+源代码+文档说明(高分课程设计)
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈