#include "mainwindow.h"
#include <QSerialPort>
#include <QSerialPortInfo>
#include <QDebug>
#include <QTimer>
#include <QMessageBox>
#include <QStandardItem>
#include <QGridLayout>
#include <QVBoxLayout>
#include <QLineEdit>
bool flag=false;
bool flag_2=false;
bool flag_3=false;
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
{
widget = new QWidget();
this->setCentralWidget(widget);
//布局
cmbPortName = new QComboBox();
cmbBaudRate = new QComboBox();
cmbParity = new QComboBox();
cmbDataBits = new QComboBox();
cmbStopBits = new QComboBox();
cmbFlowControl = new QComboBox();
btnOpen = new QPushButton(tr("打开串口"));
teReceiveData = new QTextEdit();
teReceiveData->setEnabled(false);
teSendData = new QTextEdit(tr("hello world!"));
lPortName = new QLabel(tr("串口号"));
lBaudRate = new QLabel(tr("波特率"));
lParity= new QLabel(tr("奇偶校验位"));
lDataBits= new QLabel(tr("数据位"));
lStopBits= new QLabel(tr("停止位"));
lFlowControl = new QLabel(tr("流 控"));
cbSend = new QCheckBox(tr("HEX发送"));
cbDisplay = new QCheckBox(tr("HEX显示"));
cbTimingSend = new QCheckBox(tr("定时发送"));
letime = new QLineEdit(tr("1000"));
lCis = new QLabel(tr("ms/次"));
lReceiveData = new QLabel(tr("数据接收区"));
lSendData = new QLabel(tr("数据发送区"));
btnSend = new QPushButton(tr("发送数据"));
QVBoxLayout *leftLayout = new QVBoxLayout(this);
leftLayout->addWidget(lPortName);
leftLayout->addWidget(lBaudRate);
leftLayout->addWidget(lDataBits);
leftLayout->addWidget(lStopBits);
leftLayout->addWidget(lParity);
leftLayout->addWidget(lFlowControl);
leftLayout->addWidget(cbSend);
leftLayout->addWidget(cbDisplay);
leftLayout->addWidget(cbTimingSend);
leftLayout->addWidget(letime);
leftLayout->addWidget(lCis);
QVBoxLayout *midLayout = new QVBoxLayout(this);
midLayout->addWidget(cmbPortName);
midLayout->addWidget(cmbBaudRate);
midLayout->addWidget(cmbDataBits);
midLayout->addWidget(cmbStopBits);
midLayout->addWidget(cmbParity);
midLayout->addWidget(cmbFlowControl);
QHBoxLayout *rightfootLayout = new QHBoxLayout(this);
rightfootLayout->addWidget(btnOpen);
rightfootLayout->addWidget(btnSend);
QVBoxLayout *rightLayout = new QVBoxLayout(this);
rightLayout->addWidget(lReceiveData);
rightLayout->addWidget(teReceiveData);
rightLayout->addWidget(lSendData);
rightLayout->addWidget(teSendData);
rightLayout->addLayout(rightfootLayout);
QHBoxLayout *mainLayout = new QHBoxLayout(this);
mainLayout->addLayout(leftLayout);
mainLayout->addLayout(midLayout);
mainLayout->addLayout(rightLayout);
widget->setLayout(mainLayout);
// setFixedHeight(sizeHint().height());
// mainLayout->addWidget(lPortName,0,0);
// mainLayout->addWidget(cmbPortName,0,1);
// mainLayout->addWidget(lBaudRate,1,0);
// mainLayout->addWidget(cmbBaudRate,1,1);
// mainLayout->addWidget(lParity,2,0);
// mainLayout->addWidget(cmbParity,2,1);
// mainLayout->addWidget(lDataBits,3,0);
// mainLayout->addWidget(cmbDataBits,3,1);
// mainLayout->addWidget(lStopBits,4,0);
// mainLayout->addWidget(cmbStopBits,4,1);
// mainLayout->addWidget(lReceiveData,0,2);
// mainLayout->addWidget(teReceiveData,1,2,3,2);
// mainLayout->addWidget(lSendData,2,2);
// mainLayout->addWidget(teSendData,3,2,3,2);
// mainLayout->addWidget(btnOpen,4,2);
// mainLayout->addWidget(btnSend,4,3);
/****************************串口初始化*****************************/
//读取串口信息
foreach (const QSerialPortInfo &info, QSerialPortInfo::availablePorts())
{
// qDebug()<<"Name:"<<info.portName();
// qDebug()<<"Description:"<<info.description();
// qDebug()<<"Manufacturer:"<<info.manufacturer();
//这里相当于自动识别串口号之后添加到了cmb,如果要手动选择可以用下面列表的方式添加进去
QSerialPort serial;
serial.setPort(info);
if(serial.open(QIODevice::ReadWrite))
{
//将串口号添加到cmb
cmbPortName->addItem(info.portName());
//关闭串口等待人为(打开串口按钮)打开
serial.close();
}
}
QStringList baudList;//波特率
QStringList parityList;//校验位
QStringList dataBitsList;//数据位
QStringList stopBitsList;//停止位
QStringList FlowControlList;//流控
baudList<<"50"<<"75"<<"100"<<"134"<<"150"<<"200"<<"300"
<<"600"<<"1200"<<"1800"<<"2400"<<"4800"<<"9600"
<<"14400"<<"19200"<<"38400"<<"56000"<<"57600"
<<"76800"<<"115200"<<"128000"<<"256000";
cmbBaudRate->addItems(baudList);
cmbBaudRate->setCurrentIndex(12);
parityList<<"无"<<"奇"<<"偶";
parityList<<"标志";
parityList<<"空格";
cmbParity->addItems(parityList);
cmbParity->setCurrentIndex(0);
dataBitsList<<"5"<<"6"<<"7"<<"8";
cmbDataBits->addItems(dataBitsList);
cmbDataBits->setCurrentIndex(3);
stopBitsList<<"1";
stopBitsList<<"1.5";
stopBitsList<<"2";
cmbStopBits->addItems(stopBitsList);
cmbStopBits->setCurrentIndex(0);
FlowControlList<<"硬控"<<"软控"<<"无"<<"custom";
cmbFlowControl->addItems(FlowControlList);
cmbFlowControl->setCurrentIndex(2);
//设置按钮可以被按下
// btnOpen->setCheckable(true);
myTime = new QTimer();
myTime->setInterval(1000);
connect(btnOpen,SIGNAL(clicked()),this,SLOT(on_btnOpen_clicked()));
connect(btnSend,SIGNAL(clicked()),this,SLOT(on_btnSend_clicked()));
connect(cbSend,SIGNAL(clicked()),this,SLOT(on_cbSend_clicked()));
connect(cbDisplay,SIGNAL(clicked()),this,SLOT(on_cbDisplay_clicked()));
connect(cmbPortName,SIGNAL(currentTextChanged(QString)),this,SLOT(on_cmbPortName_change()));
connect(myTime,SIGNAL(timeout()),this,SLOT(on_btnSend_clicked()));
connect(cbTimingSend,SIGNAL(toggled(bool)),this,SLOT(sendDataOnTime(bool)));
}
MainWindow::~MainWindow()
{
}
/****************************串口设置******************************/
void MainWindow::on_btnOpen_clicked()
{
if(btnOpen->text() == "打开串口")
{
my_serialport = new QSerialPort(this);
btnOpen->setText(tr("关闭串口"));
//设置串口号
my_serialport->setPortName(cmbPortName->currentText());
//以读写方式打开串口
if(my_serialport->open(QIODevice::ReadWrite))
{
//设置波特率
my_serialport->setBaudRate(cmbBaudRate->currentText().toInt());
//设置数据位
my_serialport->setDataBits(QSerialPort::Data8);
//设置校验位
my_serialport->setParity(QSerialPort::NoParity);
//设置流控制
my_serialport->setFlowControl(QSerialPort::NoFlowControl);
//设置停止位
my_serialport->setStopBits(QSerialPort::OneStop);
//每秒读一次
timer = new QTimer(this);
connect(timer, SIGNAL(timeout()), this, SLOT(readComDataSlot()));
timer->start(1000);
//setNonSelectable();
}
}
else
{
timer->stop();
btnOpen->setText("打开串口");
// setSelectable();
my_serialport->close();
}
}
/****************************数据传输***********************
漫慢求知路
- 粉丝: 14
- 资源: 28