#include "mainwindow.h"
#include <QtSerialPort/QSerialPort>
#include <QtSerialPort/QSerialPortInfo>
#include <QDebug>
#include <QMessageBox>
#include <QStatusBar>
#include <QPushButton>
#include <QByteArray>
#include <QDataStream>
#include <QTextStream>
#include <QTimer>
#include <QRegExp>
#include <QRegExpValidator>
#include <QFile>
#include <QFileDialog>
#include <QDragEnterEvent>
#include <QDropEvent>
#include <QMimeData>
#include <QAction>
#include <QDataStream>
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
setFixedSize(948, 500);
ui->receiveTextBrowser->setAcceptDrops(false);//缺省情况下,QTextEdit接受来自其他应用程序拖拽来的文本,把文件名显示出来。
ui->senderTextEdit_1->setAcceptDrops(false);
ui->senderTextEdit_2->setAcceptDrops(false);
setAcceptDrops(true);//通过禁止QTextEdit控件的drop事件,允许主窗口得到drop事件
setConnections();
setWindowTitle(tr("F28377S Serial Helper"));
//当前未连接串口
ui->comLabel->setText(tr("COM:#"));
ui->baudLabel->setText(tr("BAUD:#"));
ui->chrRB_1->setChecked(true);
ui->chrRB_2->setChecked(true);
ui->chrReceive->setChecked(true);
emit checkAvailablePorts();
emit disabledSendButton();
emit disabledAutoButton();
//检测串口是否可用定时器
Timer_CP = new QTimer(this);
//刷新界面
Timer_UPDATE = new QTimer(this);
connect(Timer_UPDATE, SIGNAL(timeout()), this, SLOT(repaint()));
Timer_UPDATE->start(2000);
//自动发送定时器
Timer_AS = new QTimer(this);
}
MainWindow::~MainWindow()
{
delete ui;
}
//重置计数
void MainWindow::resetCnt()
{
BaudCnt = 0;
ParityCnt = 0;
DataBitsCnt = 0;
StopBitsCnt = 0;
FlowCtrlCnt = 0;
}
//对串口发送十六进制
void MainWindow::writeHex(QTextEdit *textEdit)
{
QString dataStr = textEdit->toPlainText();
//如果发送的数据个数为奇数的,则在前面最后落单的字符前添加一个字符0
if (dataStr.length() % 2)
dataStr.insert(dataStr.length()-1, '0');
QByteArray sendData;
StringToHex(dataStr, sendData);
mySerialPort->write(sendData);
ui->receiveTextBrowser->append(sendData + "\n");
ui->receiveTextBrowser->moveCursor(QTextCursor::End);
ui->TxLCD->display(ui->TxLCD->value() + sendData.size());
}
//对串口发送字符串
void MainWindow::writeChr(QTextEdit *textEdit)
{
QByteArray sendData = textEdit->toPlainText().toUtf8();
if (!sendData.isEmpty() && !sendData.isNull())
mySerialPort->write(sendData);
ui->receiveTextBrowser->append(sendData + "\n");
ui->receiveTextBrowser->moveCursor(QTextCursor::End);
ui->TxLCD->display(ui->TxLCD->value() + sendData.size());
}
//将字符串当作十六进制数发送
//每两个数字代表一个值
//如发送31,实际是0x31,对照ASCII得到1,也就是说你发送的是1
//如发送414243,实际发送是0x41,0x42,0x43,对照ASCII得到ABC,实际上发送的是ABC
void MainWindow::StringToHex(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].toLatin1();
if (hstr == ' ')
{
++i;
continue;
}
++i;
if (i >= len)
break;
lstr = str[i].toLatin1();
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);
}
//将十六进制转换成字符串
char MainWindow::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 ch - ch;
}
//槽函数,自动向串口发送内容
//主要是设置自动发送定时器
void MainWindow::startAutoSend(QPushButton *sendButton)
{
//连接自动发送定时器和发送按钮
connect(Timer_AS, SIGNAL(timeout()), sendButton, SIGNAL(clicked()));
QString interval;
if (sendButton->objectName() == "sendButton_1")
{
disabledSendButton();
Timer_AS->start(ui->intervalSB_1->value());
statusBar()->showMessage(tr("每 ")
+ interval.setNum(ui->intervalSB_1->value())
+ tr("ms 自动发送一次"),
2000);
}
else if (sendButton->objectName() == "sendButton_2")
{
disabledSendButton();
Timer_AS->start(ui->intervalSB_2->value());
statusBar()->showMessage(tr("每 ")
+ interval.setNum(ui->intervalSB_2->value())
+ tr("ms 自动发送一次"),
2000);
}
}
//槽函数
//各种基础信号槽链接
void MainWindow::setConnections()
{
connect(ui->autoSendCB_1, SIGNAL(stateChanged(int)), this, SLOT(checkAutoSendCB()));
connect(ui->autoSendCB_2, SIGNAL(stateChanged(int)), this, SLOT(checkAutoSendCB()));
/*
connect(ui->actionWrite_data, SIGNAL(triggered()), this, SLOT(saveAs()));
connect(ui->actionRead_data, SIGNAL(triggered()), this, SLOT(openFile()));
connect(ui->actionChoose_file, SIGNAL(triggered(bool)), this, SLOT(openFile()));
*/
connect(ui->actionWrite_data, SIGNAL(triggered()), this, SLOT(kindremind()));
connect(ui->actionRead_data, SIGNAL(triggered()), this, SLOT(kindremind()));
connect(ui->actionChoose_file, SIGNAL(triggered(bool)), this, SLOT(kindremind()));
connect(ui->actionAlways_show, SIGNAL(triggered()), this, SLOT(kindremind()));
connect(ui->exitAction, SIGNAL(triggered()), qApp, SLOT(quit()));
connect(ui->actionAbout, SIGNAL(triggered()), qApp, SLOT(aboutQt()));
connect(ui->sendButton_1, SIGNAL(clicked()), this, SLOT(writeToBuf()));
connect(ui->sendButton_2, SIGNAL(clicked()), this, SLOT(writeToBuf()));
connect(ui->BAUDCB, SIGNAL(currentIndexChanged(int)), this, SLOT(setBaudRate()));
connect(ui->ParityCB, SIGNAL(currentIndexChanged(int)), this, SLOT(setParity()));
connect(ui->DataBitsCB, SIGNAL(currentIndexChanged(int)), this, SLOT(setDataBits()));
connect(ui->StopBitsCB, SIGNAL(currentIndexChanged(int)), this, SLOT(setStopBits()));
connect(ui->FlowCtrlCB, SIGNAL(currentIndexChanged(int)), this, SLOT(setFlowCtrl()));
}
//拖事件
void MainWindow::dragEnterEvent(QDragEnterEvent *event)
{
if (event->mimeData()->hasFormat("text/uri-list"))
event->acceptProposedAction();
}
//放入事件
void MainWindow::dropEvent(QDropEvent *event)
{
QList<QUrl> urls = event->mimeData()->urls();
if (urls.isEmpty())
return;
QString fileName = urls.first().toLocalFile();
if (fileName.isEmpty()){
return;
}
loadFile(fileName);
}
//设置自动发送相关事项,并开始自动发送
void MainWindow::checkAutoSendCB()
{
QObject *signalSender = sender();
if ( signalSender->objectName() == "autoSendCB_1")
{
//如果自动发送checkbox打钩
if (ui->autoSendCB_1->isChecked())
{
//时间间隔spinbox不可选
ui->intervalSB_1->setEnabled(false);
//其余两个自动发送checkbox不
weixin_42653672
- 粉丝: 105
- 资源: 1万+
最新资源
- C++ primer 习题上半部分
- C#ASP.NET项目进度管理(甘特图表)源码 任务考核管理系统源码数据库 Access源码类型 WebForm
- 个人练习-练习版内网通?
- 支持向量机 - SVM支持向量机
- 可以识别视频语音自动生成字幕SRT文件的开源 Windows-GUI 软件工具.zip
- 基于SpringBoot框架和SaaS模式,立志为中小企业提供开源好用的ERP软件,目前专注进销存+财务+生产功能
- C#ASP.NET口腔门诊会员病历管理系统源码 门诊会员管理系统源码数据库 SQL2008源码类型 WebForm
- 微信Java开发工具包,支持包括微信支付、开放平台、公众号、企业微信、视频号、小程序等微信功能模块的后端开发
- 灰狼优化算法(Grey Wolf Optimizer,GWO)是一种群智能优化算法
- C语言课程设计项目之扫雷项目源码.zip
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
评论2