代码语言
.
CSharp
.
JS
Java
Asp.Net
C
MSSQL
PHP
Css
PLSQL
Python
Shell
EBS
ASP
Perl
ObjC
VB.Net
VBS
MYSQL
GO
Delphi
AS
DB2
Domino
Rails
ActionScript
Scala
代码分类
文件
系统
字符串
数据库
网络相关
图形/GUI
多媒体
算法
游戏
Jquery
Extjs
Android
HTML5
菜单
网页交互
WinForm
控件
企业应用
安全与加密
脚本/批处理
开放平台
其它
【
C
】
linux下的图形化串口调试工具
作者:
飞扬青云
/ 发布于
2013/3/18
/
532
在linux的图形化串口调试工具少之又少,特意开发了这个,可以与开发板连接调试。
#include "frmmain.h" #include "ui_frmmain.h" #include "myStaticClass.h" frmMain::frmMain(QWidget *parent) : QWidget(parent), ui(new Ui::frmMain) { ui->setupUi(this); FormHelper::FormInCenter(this); FormHelper::FormNotResize(this); this->setWindowIcon(QIcon(":/2.ico")); this->setWindowTitle(tr("串口调试工具Windows版V1.2(QQ:517216493)")); this->InitForm(); //安装监听器 ui->txtSend->installEventFilter(this); } frmMain::~frmMain() { delete ui; } //监听鼠标双击则清空接收数据 void frmMain::mouseDoubleClickEvent(QMouseEvent *e) { if (e->button()==Qt::LeftButton) { ui->txtDataHex->clear(); } } //监听回车键 bool frmMain::eventFilter(QObject *obj, QEvent *event) { if (obj==ui->txtSend) { if (event->type()==QEvent::KeyPress) { QKeyEvent *keyEvent=static_cast<QKeyEvent *>(event); if (keyEvent->key()==Qt::Key_Return || keyEvent->key()==Qt::Key_Enter) { this->WriteMyCom(); return true; } } } return QObject::eventFilter(obj,event); } void frmMain::InitForm() { ReceiveCount=0; SendCount=0; this->IsShow=true; this->IsAutoClear=false; this->IsAutoSave=false; this->IsAutoSend=false; this->IsInit=false; QStringList comList;//串口号 QStringList baudList;//波特率 QStringList parityList;//校验位 QStringList dataBitsList;//数据位 QStringList stopBitsList;//停止位 comList.clear(); comList<<"COM1"<<"COM2"<<"COM3"<<"COM4"<<"COM5"<<"COM6" <<"COM7"<<"COM8"<<"COM9"<<"COM10"<<"COM11"<<"COM12" <<"COM13"<<"COM14"<<"COM15"<<"COM16"<<"COM17"<<"COM18" <<"COM19"<<"COM20"<<"COM21"<<"COM22"<<"COM23"<<"COM24" <<"COM25"<<"COM26"<<"COM27"<<"COM28"<<"COM28"<<"COM29"<<"COM30"; ui->cboxPortName->clear(); ui->cboxPortName->addItems(comList); ui->cboxPortName->setCurrentIndex(0); baudList.clear(); baudList<<"50"<<"75"<<"100"<<"134"<<"150"<<"200"<<"300" <<"600"<<"1200"<<"1800"<<"2400"<<"4800"<<"9600" <<"14400"<<"19200"<<"38400"<<"56000"<<"57600" <<"76800"<<"115200"<<"128000"<<"256000"; ui->cboxBaudRate->clear(); ui->cboxBaudRate->addItems(baudList); ui->cboxBaudRate->setCurrentIndex(12); parityList.clear(); parityList<<"无"<<"奇"<<"偶"<<"标志"<<"空格"; ui->cboxParity->clear(); ui->cboxParity->addItems(parityList); ui->cboxParity->setCurrentIndex(0); dataBitsList.clear(); dataBitsList<<"5"<<"6"<<"7"<<"8"; ui->cboxDataBit->clear(); ui->cboxDataBit->addItems(dataBitsList); ui->cboxDataBit->setCurrentIndex(3); stopBitsList.clear(); stopBitsList<<"1"<<"1.5"<<"2"; ui->cboxStopBit->clear(); ui->cboxStopBit->addItems(stopBitsList); ui->cboxStopBit->setCurrentIndex(0); ui->cboxSend->clear(); ui->cboxSave->clear(); for (int i=1;i<61;i++) { ui->cboxSend->addItem(QString::number(i)+"秒"); ui->cboxSave->addItem(QString::number(i)+"秒"); } ui->cboxSave->setCurrentIndex(4); ui->cboxSend->setCurrentIndex(4); ui->cboxSend->setEnabled(false); ui->cboxSave->setEnabled(false); //读取数据 myReadTimer=new QTimer(this); myReadTimer->setInterval(200); connect(myReadTimer,SIGNAL(timeout()),this,SLOT(ReadMyCom())); //发送数据 mySendTimer=new QTimer(this); mySendTimer->setInterval(5000); connect(mySendTimer,SIGNAL(timeout()),this,SLOT(SendData())); //保存数据 mySaveTimer=new QTimer(this); mySaveTimer->setInterval(5000); connect(mySaveTimer,SIGNAL(timeout()),this,SLOT(SaveData())); //显示日期时间 myTimer=new QTimer(this); myTimer->start(1000); connect(myTimer,SIGNAL(timeout()),this,SLOT(GetDateTime())); this->ChangeEnable(false); this->ReadTxtData(); } void frmMain::ChangeEnable(bool b) { ui->cboxBaudRate->setEnabled(!b); ui->cboxDataBit->setEnabled(!b); ui->cboxParity->setEnabled(!b); ui->cboxPortName->setEnabled(!b); ui->cboxStopBit->setEnabled(!b); ui->gbox2->setEnabled(b); ui->gbox3->setEnabled(b); ui->gbox5->setEnabled(b); ui->gbox6->setEnabled(b); ui->btnOpen->setEnabled(true); if (b) { this->myReadTimer->start(); this->mySaveTimer->start(); this->mySendTimer->start(); } else { this->myReadTimer->stop(); this->mySaveTimer->stop(); this->mySendTimer->stop(); } } void frmMain::GetDateTime() { QTime timeNow=QTime::currentTime(); ui->labTime->setText(tr("时间:%1").arg(timeNow.toString())); ui->labDate->setText(tr("日期:%1").arg(QDate::currentDate().toString(tr("yyyy年MM月dd日 dddd")))); } QByteArray frmMain::GetHexValue(QString str) { QByteArray senddata; int hexdata,lowhexdata; int hexdatalen = 0; int len = str.length(); senddata.resize(len/2); char lstr,hstr; for(int i=0; i<len; ) { hstr=str[i].toAscii(); if(hstr == ' ') { i++; continue; } i++; if(i >= len) break; lstr = str[i].toAscii(); 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); return senddata; } char frmMain::ConvertHexChar(char ch) { if((ch >= '0') && (ch <= '9')) return ch-0x30; else if((ch >= 'A') && (ch <= 'F')) return ch-'A'+10; else if((ch >= 'a') && (ch <= 'f')) return ch-'a'+10; else return (-1); } void frmMain::ReadMyCom() { MyHelper::DelayTime(100);//延时100毫秒 QString strHex;//16进制数据 QString strNomal;//正常数据 QByteArray temp=myCom->readAll(); if (!temp.isEmpty() && IsShow) { QDataStream out(&temp,QIODevice::ReadWrite); while (!out.atEnd()) { qint8 outChar=0; out>>outChar; QString str=QString("%1").arg(outChar&0xFF,2,16,QLatin1Char('0')); if (str.length()>1) { strHex+=str+" "; } else { strHex+="0"+str+" "; } } strNomal=temp; if (ui->ckHexReceive->isChecked()) { ui->txtDataHex->append(strHex.toUpper()); } else { ui->txtDataHex->append(strNomal); } ReceiveCount=ReceiveCount+temp.size(); ui->labReceive->setText(tr("接收:%1 字节").arg(ReceiveCount)); } } void frmMain::WriteMyCom() { QString str=ui->txtSend->currentText(); QByteArray outData=str.toAscii(); int size=outData.size(); //如果串口没有打开 if (!myCom->isOpen()) { return; } //如果发送数据为空 if (outData.isEmpty()) { ui->txtSend->setFocus(); return; } //QT中的回车只有一个字符\n,而windows下需要解释为\r\n if (outData[size-1]!='\n') { outData.resize(size+1); outData[size]='\r'; outData[size+1]='\n'; } //默认16进制发送的数据没有加入回车符 if (ui->ckHexSend->isChecked())//转化为16进制发送 { outData=this->GetHexValue(str); size=outData.size(); myCom->write(outData); } else { size=outData.size(); myCom->write(outData); } SendCount=SendCount+size; ui->labSend->setText(tr("发送:%1 字节").arg(SendCount)); if (this->IsAutoClear) { ui->txtSend->setCurrentIndex(-1); ui->txtSend->setFocus(); } } void frmMain::SendData() { if (this->IsAutoSend) { this->WriteMyCom(); } } void frmMain::SaveMyCom() { QDateTime now=QDateTime::currentDateTime(); QString name=now.toString("yyyyMMddHHmmss"); QString fileName=QCoreApplication::applicationDirPath()+"/"+name+".txt"; //如果没有内容则不保存 if (ui->txtDataHex->document()->toPlainText().length()==0) { return; } QFile file(fileName); file.open(QFile::WriteOnly | QIODevice::Text); QTextStream out(&file); out<<ui->txtDataHex->document()->toPlainText(); file.close(); } void frmMain::SaveData() { if (this->IsAutoSave) { this->SaveMyCom(); } } void frmMain::on_btnOpen_clicked() { if (ui->btnOpen->text()=="打开串口") { QString portName=ui->cboxPortName->currentText(); QString myPortName; if (ui->cboxPortName->currentIndex()>8)//COM9以上的串口要重新处理 { myPortName.append("\\\\.\\"); myPortName.append(portName); } else { myPortName=portName; } myCom = new Win_QextSerialPort(myPortName, QextSerialBase::Polling); if (myCom->open(QIODevice::ReadWrite)) { myCom->flush(); myCom->setBaudRate((BaudRateType)ui->cboxBaudRate->currentIndex()); myCom->setDataBits((DataBitsType)ui->cboxDataBit->currentIndex()); myCom->setParity((ParityType)ui->cboxParity->currentIndex()); myCom->setStopBits((StopBitsType)ui->cboxStopBit->currentIndex()); myCom->setFlowControl(FLOW_OFF); myCom->setTimeout(10); this->ChangeEnable(true); ui->btnOpen->setText("关闭串口"); ui->labIsOpen->setText(tr("串口状态:打开")); MyMessageBox::ChMessageOnlyOk_Information(tr("串口打开成功!(*^__^*) ")); } else { MyMessageBox::ChMessageOnlyOk_Error(tr("串口打开失败!( ⊙ o ⊙ )")); return; } } else { this->ChangeEnable(false); ui->btnOpen->setText("打开串口"); ui->labIsOpen->setText(tr("串口状态:关闭")); myCom->close(); } } void frmMain::on_ckIsAutoSend_stateChanged(int ) { this->IsAutoSend=ui->ckIsAutoSend->isChecked(); ui->cboxSend->setEnabled(this->IsAutoSend); } void frmMain::on_ckIsAutoSave_stateChanged(int ) { this->IsAutoSave=ui->ckIsAutoSave->isChecked(); ui->cboxSave->setEnabled(this->IsAutoSave); } void frmMain::on_ckIsAutoClear_stateChanged(int ) { this->IsAutoClear=ui->ckIsAutoClear->isChecked(); } void frmMain::on_btnClearSend_clicked() { SendCount=0; ui->labSend->setText(tr("发送:0 字节")); } void frmMain::on_btnClearReceive_clicked() { ReceiveCount=0; ui->labReceive->setText(tr("接收:0 字节")); } void frmMain::on_btnStopShow_clicked() { if (ui->btnStopShow->text()=="停止显示") { IsShow=false; ui->btnStopShow->setText("开始显示"); } else { IsShow=true; ui->btnStopShow->setText("停止显示"); } } void frmMain::on_btnClearAll_clicked() { ui->txtDataHex->document()->clear(); } void frmMain::on_btnSave_clicked() { this->SaveMyCom(); } void frmMain::on_btnSend_clicked() { this->WriteMyCom(); } void frmMain::ReadTxtData() { ui->txtSend->clear(); QString fileName=QCoreApplication::applicationDirPath()+"/data.txt"; QFile file(fileName); file.open(QFile::ReadOnly | QIODevice::Text); QTextStream in(&file); QString line; do { line=in.readLine(); if (line!="") { ui->txtSend->addItem(line); } } while(!line.isNull()); file.close(); } void frmMain::on_btnData_clicked() { QString fileName=QCoreApplication::applicationDirPath()+"/data.txt"; QFile file(fileName); if (!file.exists()) { MyMessageBox::ChMessageOnlyOk_Error(tr("数据文件不存在!")); return; } if (ui->btnData->text()==tr("管理数据")) { ui->txtDataHex->setReadOnly(false); ui->gbox2->setTitle(tr("管理数据")); ui->txtDataHex->clear(); file.open(QFile::ReadOnly | QIODevice::Text); QTextStream in(&file); ui->txtDataHex->setText(in.readAll()); file.close(); ui->btnData->setText(tr("保存数据")); } else { ui->txtDataHex->setReadOnly(true); ui->gbox2->setTitle(tr("接收数据")); file.open(QFile::WriteOnly | QIODevice::Text); QTextStream out(&file); out<<ui->txtDataHex->document()->toPlainText(); file.close(); ui->txtDataHex->clear(); ui->btnData->setText(tr("管理数据")); this->ReadTxtData(); } } void frmMain::on_cboxSend_currentIndexChanged(int index) { if (this->IsInit) { mySendTimer->setInterval((index+1)*1000); } } void frmMain::on_cboxSave_currentIndexChanged(int index) { if (this->IsInit) { mySaveTimer->setInterval((index+1)*1000); } }
试试其它关键字
图形化
同语言下
.
获取手机通讯录 iOS去除数字以外的所有字符
.
异步加载音乐等资源
.
交通罚单管理系统
.
freemark实现,简单的替换
.
计算斐波那契数列
.
base64解码 包括解码长度
.
图像显示
.
冒泡排序
.
输入十进制数,输出指定进制
.
链式栈
可能有用的
.
C#实现的html内容截取
.
List 切割成几份 工具类
.
SQL查询 多列合并成一行用逗号隔开
.
一行一行读取txt的内容
.
C#动态修改文件夹名称(FSO实现,不移动文件)
.
c# 移动文件或文件夹
.
c#图片添加水印
.
Java PDF转换成图片并输出给前台展示
.
网站后台修改图片尺寸代码
.
处理大图片在缩略图时的展示
飞扬青云
贡献的其它代码
(
1
)
.
linux下的图形化串口调试工具
Copyright © 2004 - 2024 dezai.cn. All Rights Reserved
站长博客
粤ICP备13059550号-3