#include "widget.h"
#include "ui_widget.h"
#include <QPixmap>
#include <QMessageBox>
#include <QFileDialog>
#include <QTextCodec>
#include <QDebug>
#include <QTimer>
Widget::Widget(QWidget *parent)
: QWidget(parent)
, ui(new Ui::Widget)
{
ui->setupUi(this);
this->setFixedSize(800,600);
// this->setWindowIcon(QPixmap(":/res/dayulogo.png"));
this->setWindowTitle("科技成就梦想,创新引领未来");
ui->sendDataBtn->setEnabled(false);
ui->baudrateBox->setCurrentIndex(3);
timer = new QTimer(this);
connect(timer,&QTimer::timeout,this,[=](){
this->timerTransDate();
});
QObject::connect(&serial,&QSerialPort::readyRead,this,[=](){
this->serialPort_readyRead();
});
/*点击扫描串口执行*/
connect(ui->searchComBtn,&QPushButton::clicked,this,[=](){
this->searchCom();
});
/*点击打开串口执行*/
connect(ui->openComBtn,&QPushButton::clicked,this,[=](){
this->openCom();
});
/*点击发送数据*/
connect(ui->sendDataBtn,&QPushButton::clicked,this,[=](){
this->sendData();
});
/*清空接收*/
connect(ui->clearDataBtn,&QPushButton::clicked,this,[=](){
this->clearData();
});
connect(ui->openFileBtn,&QPushButton::clicked,this,[=](){
fileName = QFileDialog::getOpenFileName(this,QString::fromLocal8Bit("bin file"),"G:\\360MoveData\\Users\\123\\Desktop");
ui->lineEdit->setText(fileName);
if(fileName.isEmpty())
{
QMessageBox::information(this,"Error Message","please Select a Text File");
return;
}
QFileInfo * pcsfileInfo = new QFileInfo(fileName);
binSize = pcsfileInfo->size();
QFile * file = new QFile;
file->setFileName(fileName);
// QTextCodec * codec = QTextCodec::codecForName("gbk");
bool ok = file->open(QIODevice::ReadOnly);//打开方式
if(ok)
{
}
else{
QMessageBox::information(this,"Error Message","File Open Error" + file->errorString());
return;
}
QDataStream in(file);
char * binByte = new char[binSize];
in.setVersion(QDataStream::Qt_5_12);
in.readRawData(binByte,binSize);//读取数据到缓存
tempByte = new QByteArray(binByte,binSize);//格式转换
QString * tempStr = new QString(tempByte->toHex().toUpper());
qint8 cnt = 1;
for(qint64 i =2;i<tempStr->size();)
{
tempStr->insert(i,' ');//每个字节之间空一格
i += 3;
cnt++;
if(cnt % 8 == 0)
{
tempStr->insert(i,' ');
i += 1;
}
}
ui->sendtextEdit->insertPlainText(*tempStr);
delete tempByte;
delete[] binByte;
delete tempStr;
file->close();
delete file;
});
}
void Widget::timerTransDate()
{
QString buffer = ui->sendtextEdit->toPlainText();
QByteArray senddata = buffer.toLocal8Bit();
if(ulNum<senddata.length())
{
QByteArray data = senddata.mid(ulNum,1);
serial.write(data);
qDebug() << data<< " "<<ulNum<<"/"<<senddata.length();
ulNum +=1;
}
else
{
QByteArray data = senddata.mid(ulNum,1);
serial.write(data);
timer->stop();
}
}
Widget::~Widget()
{
delete ui;
}
void Widget::serialPort_readyRead()
{
/*从接收缓冲区读取数据*/
QByteArray buffer = serial.readAll();
//从界面读取中读取以前收到的数据
QString recv = ui->recvTextEdit->toPlainText();
QString temp = " ";
recv += QString(buffer);
ui->recvTextEdit ->clear();
recv += temp;
ui->recvTextEdit ->append(recv);
}
void Widget::searchCom()
{
ui->comBox->clear();
//查找可用串口
foreach(const QSerialPortInfo &info,QSerialPortInfo::availablePorts())
{
ui->comBox->addItem(info.portName());
}
}
void Widget::openCom()
{
if(ui->openComBtn->text() == QString("打开串口"))
{
serial.setPortName(ui->comBox->currentText());
serial.setBaudRate(QSerialPort::Baud9600);
serial.setDataBits(QSerialPort::Data8);
serial.setParity(QSerialPort::NoParity);
serial.setStopBits(QSerialPort::OneStop);
serial.setFlowControl(QSerialPort::NoFlowControl);
if(!serial.open(QIODevice::ReadWrite))
{
QMessageBox::about(NULL,"提示","无法打开串口");
return;
}
ui->comBox->setEnabled(false);
ui->baudrateBox->setEnabled(false);
ui->dataBitsBox->setEnabled(false);
ui->parityBox->setEnabled(false);
ui->stopBitsBox->setEnabled(false);
ui->openComBtn->setText(QString("关闭串口"));
//发送使能
ui->sendDataBtn->setEnabled(true);
}
else
{
serial.close();
ui->comBox->setEnabled(true);
ui->baudrateBox->setEnabled(true);
ui->dataBitsBox->setEnabled(true);
ui->parityBox->setEnabled(true);
ui->stopBitsBox->setEnabled(true);
ui->openComBtn->setText(QString("打开串口"));
//发送使能
ui->sendDataBtn->setEnabled(false);
}
}
void Widget::sendData()
{
ulNum=0;
timer->start(1);
// QString buffer = ui->sendtextEdit->toPlainText();
// QByteArray senddata = buffer.toLocal8Bit();
}
void Widget::clearData()
{
ui->recvTextEdit->clear();
}
评论5