#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QMessageBox>
#include <QDebug>
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
//发送按键失能
ui->pushButton_serial_send->setEnabled(false);
//波特率默认选择下拉第三项:115200
ui->Box_baudrate->setCurrentIndex(4);
ui->checkBox_hex_display->setCheckState(Qt::Checked);
ui->checkBox_hex_display->setEnabled(false);
//连接信号和槽
QObject::connect(&serial, &QSerialPort::readyRead, this, &MainWindow::serialPort_readyRead);
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::on_Button_detect_clicked()
{
ui->Box_portnum->clear();
//通过QSerialPortInfo查找可用串口
foreach(const QSerialPortInfo &info, QSerialPortInfo::availablePorts())
{
ui->Box_portnum->addItem(info.portName());
}
}
void MainWindow::on_Button_OpenPort_clicked()
{
if(ui->Button_OpenPort->text()==QString("打开串口"))
{
//设置串口名
serial.setPortName(ui->Box_portnum->currentText());
//设置波特率
serial.setBaudRate(ui->Box_baudrate->currentText().toInt());
//设置数据位数
switch(ui->Box_dataBits->currentIndex())
{
case 8: serial.setDataBits(QSerialPort::Data8); break;
default: break;
}
//设置奇偶校验
switch(ui->Box_Parity->currentIndex())
{
case 0: serial.setParity(QSerialPort::NoParity); break;
default: break;
}
//设置停止位
switch(ui->Box_stopBits->currentIndex())
{
case 1: serial.setStopBits(QSerialPort::OneStop); break;
case 2: serial.setStopBits(QSerialPort::TwoStop); break;
default: break;
}
//设置流控制
serial.setFlowControl(QSerialPort::NoFlowControl);
//打开串口
if(!serial.open(QIODevice::ReadWrite))
{
QMessageBox::about(NULL, "提示", "无法打开串口!");
return;
}
//下拉菜单控件失能
ui->Box_portnum->setEnabled(false);
ui->Box_baudrate->setEnabled(false);
ui->Box_dataBits->setEnabled(false);
ui->Box_Parity->setEnabled(false);
ui->Box_stopBits->setEnabled(false);
ui->Button_OpenPort->setText(QString("关闭串口"));
//发送按键使能
ui->pushButton_serial_send->setEnabled(true);
}
else
{
//关闭串口
serial.close();
//下拉菜单控件使能
ui->Box_portnum->setEnabled(true);
ui->Box_baudrate->setEnabled(true);
ui->Box_dataBits->setEnabled(true);
ui->Box_Parity->setEnabled(true);
ui->Box_stopBits->setEnabled(true);
ui->Button_OpenPort->setText(QString("打开串口"));
//发送按键失能
ui->pushButton_serial_send->setEnabled(false);
}
}
void MainWindow::on_pushButton_clear_rev_clicked()
{
ui->textEdit_receive->clear();
}
void MainWindow::on_pushButton_clear_send_clicked()
{
ui->textEdit_send->clear();
}
void MainWindow::on_checkBox_hex_display_clicked(bool checked)
{
if(checked == true)
{
}
else
{
}
}
void MainWindow::on_checkBox_hex_send_clicked(bool checked)
{
if(checked == true)
{
}
else
{
}
}
void MainWindow::on_pushButton_serial_send_clicked()
{
//获取界面上的数据并转换成utf8格式的字节流
QByteArray data;
if(ui->checkBox_hex_send->isChecked() == true)
data = HexStringToByteArray(ui->textEdit_send->toPlainText());
else
data = ui->textEdit_send->toPlainText().toUtf8();
serial.write(data);
/*也可以定长发送*/
//serial.write((const char *)param_data, 16);
}
void MainWindow::serialPort_readyRead()
{
//从接收缓冲区中读取数据
QByteArray buffer = serial.readAll();
//将16进制
QString display_str = ByteArrayToHexString(buffer);
//从界面中读取以前收到的数据
QString recv = ui->textEdit_receive->toPlainText();
recv += QString(display_str);
//清空以前的显示
ui->textEdit_receive->clear();
//重新显示
ui->textEdit_receive->append(recv);
}
/*
* @breif 将16进制字符串转换为对应的字节序列
*/
QByteArray MainWindow::HexStringToByteArray(QString HexString)
{
bool ok;
QByteArray ret;
HexString = HexString.trimmed();
HexString = HexString.simplified();
QStringList sl = HexString.split(" ");
foreach (QString s, sl) {
if(!s.isEmpty())
{
char c = s.toInt(&ok,16)&0xFF;
if(ok){
ret.append(c);
}else{
qDebug()<<"非法的16进制字符:"<<s;
QMessageBox::warning(0,tr("错误:"),QString("非法的16进制字符: \"%1\"").arg(s));
}
}
}
//qDebug()<<ret;
return ret;
}
/*
* @breif 将字节序列转换为对应的16进制字符串
*/
QString MainWindow::ByteArrayToHexString(QByteArray data){
QString ret(data.toHex().toUpper());
int len = ret.length()/2;
qDebug()<<len;
for(int i=1;i<len;i++)
{
//qDebug()<<i;
ret.insert(2*i+i-1," ");
}
return ret;
}