#include "widget.h"
#include "ui_widget.h"
#include <QFile>
#include <QTextStream>
#include <QFileDialog>
#include <QMessageBox>
Widget::Widget(QWidget *parent) :
QWidget(parent),
ui(new Ui::Widget)
{
ui->setupUi(this);
this->setWindowTitle("UartAssistant");
//this->setMaximumSize(500);
this->setMinimumSize(600,500);
int WIDTH = 40; //上下Y间隔
int LENGTH = 400; //X坐标
//按钮
button_open_port = new QPushButton(this);
button_send = new QPushButton(this);
button_open_port->setText("Open Port");
button_open_port->setGeometry(QRect(260,100,100,30));
button_send->setText("Send");
button_send->setGeometry(QRect(260,330,100,30));
button_clearRec = new QPushButton(this);
button_clearSend = new QPushButton(this);
button_clearRec->setText("清空接收区");
button_clearRec->setGeometry(QRect(260,150,100,30));
button_clearSend->setText("清空发送区");
button_clearSend->setGeometry(QRect(260,380,100,30));
button_save = new QPushButton(this);
button_save->setText("保存");
button_save->setGeometry(QRect(LENGTH,345,120,50));
//文本框
label1 = new QLabel(this);
label1->setText("RecvBuffer");
label1->setGeometry(QRect(50,25,100,20));
label2 = new QLabel(this);
label2->setText("SendBuffer");
label2->setGeometry(QRect(50,260,100,20));
//多行文本
text_RecvBuffer = new QTextEdit(this);
text_SendBuffer = new QTextEdit(this);
text_RecvBuffer->setGeometry(QRect(50,45,200,200));
text_RecvBuffer->setReadOnly(true);
text_SendBuffer->setGeometry(QRect(50,280,200,200));
//列表框
//串口
label_Serial_port = new QLabel(this);
label_Serial_port->setText("串 口");
label_Serial_port->setGeometry(QRect(LENGTH,WIDTH,80,20));
label_BaudRate = new QLabel(this);
label_BaudRate->setText("波特率");
label_BaudRate->setGeometry(QRect(LENGTH,2*WIDTH,80,20));
label_Data = new QLabel(this);
label_Data->setText("数据位");
label_Data->setGeometry(QRect(LENGTH,3*WIDTH,80,20));
Parity = new QLabel(this);
Parity->setText("校验位");
Parity->setGeometry(QRect(LENGTH,4*WIDTH,80,20));
Stop = new QLabel(this);
Stop->setText("停止位");
Stop->setGeometry(QRect(LENGTH,5*WIDTH,80,20));
ComboBox_port = new QComboBox(this);
ComboBox_port->setGeometry(QRect(LENGTH+40,WIDTH,80,20));
QStringList com;
com<<"COM1"<<"COM2"<<"COM3"<<"COM4";
ComboBox_port->addItems(com);
ComboBox_port->setCurrentIndex(2);
//波特率
ComboBox_BaudRate = new QComboBox(this);
ComboBox_BaudRate->setGeometry(QRect(LENGTH+40,2*WIDTH,80,20));
QStringList baud;
baud<<"1200"<<"2400"<<"4800"<<"9600"<<"19200"<<"38400"<<"57600"<<"115200";
ComboBox_BaudRate->addItems(baud);
ComboBox_BaudRate->setCurrentIndex(3);
//数据位
ComboBox_Data = new QComboBox(this);
ComboBox_Data->setGeometry(QRect(LENGTH+40,3*WIDTH,80,20));
QStringList data;
data<<"8"<<"7"<<"6"<<"5";
ComboBox_Data->addItems(data);
//校验位
ComboBox_Parity = new QComboBox(this);
ComboBox_Parity->setGeometry(QRect(LENGTH+40,4*WIDTH,80,20));
QStringList parity;
parity<<"NONE"<<"ODD"<<"EVEN";
ComboBox_Parity->addItems(parity);
//停止位
Combobox_Stop = new QComboBox(this);
Combobox_Stop->setGeometry(QRect(LENGTH+40,5*WIDTH,80,20));
QStringList stop;
stop<<"1"<<"1.5"<<"2";
Combobox_Stop->addItems(stop);
Combobox_Stop->setCurrentIndex(0);
//十六进制复选框
Send16 = new QCheckBox(this);
Send16->setGeometry(QRect(260,430,100,30));
Send16->setText("十六进制发送");
Recv16 = new QCheckBox(this);
Recv16->setGeometry(QRect(260,200,100,30));
Recv16->setText("十六进制接收");
Recv16->setCheckState(Qt::Checked);
connect(button_open_port,SIGNAL(clicked()),this,SLOT(on_openPortBtn_clicked()));
connect(button_send,SIGNAL(clicked()),this,SLOT(on_SendBtn_clicked()));
connect(button_clearRec,SIGNAL(clicked(bool)),text_RecvBuffer,SLOT(clear()));
connect(button_clearSend,SIGNAL(clicked(bool)),text_SendBuffer,SLOT(clear()));
connect(button_save,SIGNAL(clicked(bool)),this,SLOT(file_save()));
connect(Recv16,SIGNAL(clicked(bool)),this,SLOT(checkChange()));
}
void Widget::on_openPortBtn_clicked()
{
if(ComboBox_port->isEnabled())
{
serial = new QSerialPort(ComboBox_port->currentText());
serial->setPortName(ComboBox_port->currentText());
serial->setBaudRate(ComboBox_BaudRate->currentText().toInt(),QSerialPort::AllDirections);
serial->setDataBits(static_cast<QSerialPort::DataBits>(ComboBox_Data->currentText().toInt()));
serial->setFlowControl(QSerialPort::NoFlowControl);
serial->setParity(static_cast<QSerialPort::Parity>(ComboBox_Parity->currentText().toInt()));
serial->setStopBits(static_cast<QSerialPort::StopBits>(Combobox_Stop->currentText().toInt()));
serial->close();
if(serial->open(QIODevice::ReadWrite))
{
//打开串口后无法更改设置
ComboBox_port->setDisabled(true);
ComboBox_BaudRate->setDisabled(true);
ComboBox_Data->setDisabled(true);
ComboBox_port->setDisabled(true);
ComboBox_Parity->setDisabled(true);
Combobox_Stop->setDisabled(true);
button_open_port->setText("ClosePort");
is_open_port = true;
connect(serial,&QSerialPort::readyRead,this,Widget::read_Com);
}else{
QMessageBox::critical(this, tr("打开串口失败!"), tr("无法打开串口") + ComboBox_port->currentText() + tr("\n (The serial unvaliable)"), QMessageBox::Ok);
}
}else{
button_open_port->setText("Open Port");
ComboBox_port->setEnabled(true);
ComboBox_BaudRate->setEnabled(true);
ComboBox_Data->setEnabled(true);
ComboBox_port->setEnabled(true);
ComboBox_Parity->setEnabled(true);
Combobox_Stop->setEnabled(true);
serial->close();
is_open_port = false;
}
}
void Widget::on_SendBtn_clicked()
{
if(is_open_port)
{
if(Send16->checkState() == Qt::Checked)
{
QByteArray senddata = Widget::QString2Hex(text_SendBuffer->toPlainText()); //转换为16进制
serial->write(senddata);
}else{
serial->write(text_SendBuffer->toPlainText().toLocal8Bit());
//text_SendBuffer->clear();
}
}else{
QMessageBox::critical(this, tr("发送数据失败!"), tr("未选择或打开有效串口") + tr("\n (请打开有效的串口)"), QMessageBox::Ok);
}
}
void Widget::read_Com()
{
QByteArray temp = serial->readAll();
if(!temp.isEmpty())
{
if(Recv16->checkState() == Qt::Checked)
{
text_RecvBuffer->insertPlainText("0x");
text_RecvBuffer->insertPlainText(temp.toHex().toUpper());
text_RecvBuffer->insertPlainText(" ");
}else{
text_RecvBuffer->insertPlainText(QString(temp));
}
}
}
void Widget::file_save()
{
QString path = QFileDialog::getSaveFileName(
this,
"Open File",
".",
"Text Files(*.txt)");
if(!path.isEmpty())
{
QFile file(path);
if(!file.open(QIODevice::WriteOnly | QIODevice::Text))
{
QMessageBox::warning(this,tr("Write File"),
tr("Cannot open file:\n%1").arg(path));
return;
}
QTextStream out(&file);
out << "SendBuffer:\n " << qSetFie
基于qt的usb串口通信
1星 需积分: 39 75 浏览量
2018-01-22
15:14:10
上传
评论 5
收藏 7KB ZIP 举报
业余码农AmazingJ
- 粉丝: 1087
- 资源: 1
最新资源
- 自动驾驶-状态估计和定位之Error State EKF.pdf
- STM32F103ZET6+北斗
- 程序流程图的说明及图形示例
- FDN5618P-NL-VB一款SOT23封装P-Channel场效应MOS管
- Go语言基础(变量和基本类型).zip
- 基于CYCLONE2 (EP2C8Q) FPGA 设计PLL锁相环设置时钟Verilog源码Quartus工程文件.zip
- FDN372S-NL-VB一款SOT23封装N-Channel场效应MOS管
- date0425111111111111111111111
- 包含贪心算法的定义及python代码部分实现
- 自动驾驶-状态估计和定位之扩展卡尔曼滤波.pdf
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈