Qt-串口调试助手
做的一个项目需要上位机给下位机发送命令,然后上位机接收下位机发来的数据,发送和接收的的数据都是十六进制,于是需要一个串口调试助手需要在.pro文件中添加Qt+=serialprotmySerialProt.h文件#ifndef MYSERIALPORT_H#define MYSERIALPORT_H#include <QDi...
·
做的一个项目需要上位机给下位机发送命令,然后上位机接收下位机发来的数据,发送和接收的的数据都是十六进制,于是需要一个串口调试助手
需要在.pro文件中添加
Qt +=serialprot
mySerialProt.h文件
#ifndef MYSERIALPORT_H
#define MYSERIALPORT_H
#include <QDialog>
#include "ui_mainwindow.h"
#include <QtSerialPort/QSerialPort>
#include <QtSerialPort/QSerialPortInfo>
namespace Ui {
class MyserialPort;
}
class MyserialPort : public QDialog
{
Q_OBJECT
public:
explicit MyserialPort(QWidget *parent = 0);
~MyserialPort();
QSerialPort *serial;
QByteArray QString2Hex(QString hexStr); //转换16进制函数
char ConvertHexChar(char c);
QString formatInput(QString hexStr);
//声明plot
void ReadData();
private slots:
void on_OpenSerialButton_clicked(); //打开串口函数
void on_Send_clicked();//发送设置命令
void on_Query_clicked();//发送查询命令
void on_SendButton_clicked();//发送起始命令
void on_Stop_clicked();//发送停止命令
private:
Ui::MyserialPort *ui;
};
#endif // MYSERIALPORT_H
Myserialprot.cpp
#include "myserialport.h"
#include "ui_myserialport.h"
#include <QtSerialPort/QSerialPort>
#include <QtSerialPort/QSerialPortInfo>
#include <Plot/plot.h>
#include <Protocol/protocol.h>
#include<QDebug>
#include<queue>
MyserialPort::MyserialPort(QWidget *parent) :
QDialog(parent),
ui(new Ui::MyserialPort)
{
ui->setupUi(this);
//查找可用的串口
foreach (const QSerialPortInfo &info,QSerialPortInfo::availablePorts())
{
QSerialPort serial;
serial.setPort(info);
if(serial.open(QIODevice::ReadWrite))
{
ui->PortBox->addItem(serial.portName());
serial.close();
}
}
//设置波特率下拉菜单默认显示第0项
ui->BaudBox->setCurrentIndex(0);
draw=new Plot();
draw->initChart();
//protocol=new Protocol();
}
MyserialPort::~MyserialPort()
{
delete ui;
}
void MyserialPort::on_OpenSerialButton_clicked()
{
qDebug()<<"BBB"<<endl;
// qDebug()<< ui->OpenSerialButton->text()<<endl;
if(ui->OpenSerialButton->text() == tr("open"))
{
qDebug()<<"AAA"<<endl;
serial = new QSerialPort;
//设置串口名
serial->setPortName("COM4");
//打开串口
serial->open(QIODevice::ReadWrite);//打开串口
//设置波特率
serial->setBaudRate(QSerialPort::Baud115200);//设置波特率为115200
serial->setDataBits(QSerialPort::Data8); //设置数据位数
serial->setParity(QSerialPort::NoParity); //设置奇偶校验
serial->setStopBits(QSerialPort::OneStop); //设置停止位
serial->setFlowControl(QSerialPort::NoFlowControl); //设置流控制
//关闭设置菜单使能
ui->PortBox->setEnabled(false);
ui->BaudBox->setEnabled(false);
ui->BitBox->setEnabled(false);
ui->ParityBox->setEnabled(false);
ui->StopBox->setEnabled(false);
ui->OpenSerialButton->setText(tr("close"));
//连接信号槽
QObject::connect(serial,&QSerialPort::readyRead,this,&MyserialPort::ReadData);
}
else
{
//关闭串口
serial->clear();
serial->close();
serial->deleteLater();
//恢复设置使能
ui->PortBox->setEnabled(true);
ui->BaudBox->setEnabled(true);
ui->BitBox->setEnabled(true);
ui->ParityBox->setEnabled(true);
ui->StopBox->setEnabled(true);
ui->OpenSerialButton->setText(tr("open"));
}
}
//读取接收到的信息
void MyserialPort::ReadData()
{
protocol=new Protocol();
qDebug()<<"shu chu"<<endl;
int byteLen=serial->bytesAvailable();//串口读取数据的长度
if(byteLen<0)
return;
QByteArray buf;
buf.clear();
buf = serial->readAll();
QByteArray bufferHex=buf.toHex();
QString str_All=QString(bufferHex.toUpper());
QQueue<QString>q;
QString ss=NULL;
int yushu=str_All.count()%24;
int shang=str_All.count()/24;
while(shang>0)
{
ss=str_All.mid(0,24);
q.enqueue(ss);
shang--;
}
//使字符串的形式变成“AB 23 43 55”
// for(int i=str.count();i>0;i=i-2)
// {
// str.insert(i," ");
// }
// qDebug()<<"BBB"<<buf<<endl;
// QDataStream out(&buf,QIODevice::ReadWrite); //将字节数组读入
// //int i=0;
// //int aa[100];
// QString str=NULL;
// while(!out.atEnd())
// {
// qint8 outChar = 0;
// out>>outChar; //每字节填充一次,直到结束
// str+= QString("%1").arg(outChar&0xFF,2,16,QLatin1Char('0'));
// }
// // pRxdata=new Protocol();
// qDebug()<<"CCC"<<str<<endl;
while(q.size()!=0)
{
QString str=q.dequeue();
draw->drawLine(protocol->GetData(str));
}
// if(!buf.isEmpty())
// {
// QString str = ui->textEdit->toPlainText();
// str+=tr(buf);
// ui->textEdit->clear();
// ui->textEdit->append(str);
// }
buf.clear();
}
void MyserialPort::on_Send_clicked()
{
QString hexStr="01 12 03 01 03 e8 03 e8 03 e8 03 e8 03 e8 03 e8 27 10 45 61 00 00 07 d0 07 d0 00 a1 01 00 64 c6";
serial->write(QString2Hex(formatInput(hexStr)));
}
void MyserialPort::on_Query_clicked()
{
QString hexStr="01 14 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 15";
serial->write(QString2Hex(formatInput(hexStr)));
}
void MyserialPort::on_SendButton_clicked()
{
QString hexStr="01 11 03 01 03 e8 03 e8 03 e8 03 e8 03 e8 03 e8 27 10 45 61 00 00 07 d0 07 d0 00 a1 01 00 64 c5";
//serial->open(QIODevice::ReadWrite);
serial->write(QString2Hex(formatInput(hexStr)));
}
QByteArray MyserialPort::QString2Hex(QString hexStr)
{
QByteArray senddata;
int hexdata, lowhexdata;
int hexdatalen = 0;
int len = hexStr.length();
senddata.resize(len/2);
char lstr, hstr;
for(int i = 0; i < len; )
{
//将第一个不为' '的字符赋给hstr;
hstr = hexStr[i].toLatin1();
if(hstr == ' ')
{
i++;
continue;
}
i++;
//当i >= len时,跳出循环
if(i >= len)
break;
//当i < len时,将下一个字符赋值给lstr;
lstr = hexStr[i].toLatin1();
//将hstr和lstr转换为0-15的对应数值
hexdata = ConvertHexChar(hstr);
lowhexdata = ConvertHexChar(lstr);
//
if((hexdata == 16) || (lowhexdata == 16))
break;
else
hexdata = hexdata * 16 + lowhexdata;
i++;
senddata[hexdatalen] = (char)hexdata;
hexdatalen++;
}
senddata.resize(hexdatalen);
// qDebug()<<"AAA"<<senddata<<endl;
return senddata;
}
//将单个字符串转换为hex
//0-F -> 0-15
char MyserialPort::ConvertHexChar(char c)
{
if((c >= '0') && (c <= '9'))
return c - 0x30;
else if((c >= 'A') && (c <= 'F'))
return c - 'A' + 10;//'A' = 65;
else if((c >= 'a') && (c <= 'f'))
return c - 'a' + 10;
else
return -1;
}
QString MyserialPort::formatInput(QString hexStr)
{
int strlen = hexStr.length();
switch(strlen)
{
case 0:
hexStr = "0000";
break;
case 1:
hexStr = "000" + hexStr;
break;
case 2:
hexStr = "00" + hexStr;
break;
case 3:
hexStr = "0" + hexStr;
break;
case 4:
hexStr = hexStr;
break;
default:
break;
}
qDebug()<<"AAAA"<<hexStr<<endl;
return hexStr;
}
void MyserialPort::on_Stop_clicked()
{
QString hexStr="01 13 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 12";
serial->write(QString2Hex(formatInput(hexStr)));
}
效果示意图:

需要源码:https://download.csdn.net/download/hyyjiushiliangxing/11803403
更多推荐

所有评论(0)