#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
setWindowFlags(windowFlags()& ~Qt::WindowMaximizeButtonHint);
setFixedSize(this->width(), this->height());
//find the available serial ports
foreach (const QSerialPortInfo &info, QSerialPortInfo::availablePorts())
{
qDebug() << "Name : " << info.portName();
qDebug() << "Description : " << info.description();
qDebug() << "Manufacturer: " << info.manufacturer();
QSerialPort serial;
serial.setPort(info);
ui->comboBoxCOM->addItem(info.portName());
}
myCom=new QSerialPort;
IsComOpen=false;
BaudRete[0] = QSerialPort::Baud9600,
BaudRete[1] = QSerialPort::Baud115200;
QString str;
for(qint8 i = 0; i < 2; i++)
{
str.setNum(BaudRete[i], 10);
ui->comboBoxBaudRate->addItem(str);
}
ui->comboBoxBaudRate->setCurrentIndex(1); //default is 115200
ui->comboBoxProtocolType->addItem("Ymodem");
ui->comboBoxProtocolType->setCurrentIndex(0);//default Ymodem
// FilePath="Z:/share/app_test/Project.bin";
FilePath="Z:/share/app_test/STM32L1XX_MDP.bin";
ui->lineEditOpenFile->setText(FilePath);
//set the mainwindow status bar
ComStateLabel = new QLabel;
FileStateLabel = new QLabel;
PackStateLabel = new QLabel;
UseTimeStateLabel = new QLabel;
statusBar()->addWidget(ComStateLabel);
statusBar()->addWidget(FileStateLabel);
statusBar()->addWidget(PackStateLabel);
statusBar()->addWidget(UseTimeStateLabel);
//connect to slot
connect(ui->comboBoxProtocolType, SIGNAL(currentIndexChanged(int)), this, SLOT(StateUpdate()));
connect(ui->comboBoxCOM, SIGNAL(currentIndexChanged(int)), this, SLOT(StateUpdate()));
connect(ui->comboBoxBaudRate, SIGNAL(currentIndexChanged(int)), this, SLOT(StateUpdate()));
connect(ui->lineEditOpenFile, SIGNAL(textChanged(QString)), this, SLOT(FilePathChanged(QString)));
connect(myCom,SIGNAL(readyRead()),this,SLOT(readMyCom()));
ui->pushButtonSendFile->setEnabled(false);
//signal from the child thread
connect(&this->thread_send, SIGNAL(SendPack()), this, SLOT(StateUpdate()));
connect(this, SIGNAL(sendRecvData(QByteArray)), &this->thread_send, SLOT(getSerialData(QByteArray)));
connect(&this->thread_send, SIGNAL(WriteToSerial(QByteArray,qint16)), this, SLOT(WriteMyCom(QByteArray,qint16)));
//start the thread
thread_send.start();
StateUpdate();
}
MainWindow::~MainWindow()
{
thread_send.Stop();
thread_send.quit();
if(thread_send.wait(100))
{
qDebug("thread_send exit successful");
}
else
{
qDebug("thread_send terminate");
thread_send.terminate();
}
delete ui;
}
//update the status bar
void MainWindow::StateUpdate()
{
qDebug("State Update");
qint32 size=0;
QString str;
float tmp;
//update the packgesize
// if(ui->comboBoxProtocolType->currentIndex())
// SerialThread.PackSize = 1024;
// else
// SerialThread.PackSize = 1024;
thread_send.PackSize = 1024;//fixed the ymodem protocol packgesize
//update the serial status
str += ui->comboBoxCOM->currentText();
str += tr("(");
str += ui->comboBoxBaudRate->currentText();
str += tr(" 8, N, 1)");
if(myCom->isOpen())
str += tr(" is opened. ");
else
str += tr(" is closed. ");
ComStateLabel->setText(str);
//update the file size
str = tr("File size:");
size = FileData.size();
QString strTmp;
if(size >= 1024)
{
tmp = size;
tmp /= 1024.00;
strTmp.setNum(tmp, 'g', 4);
str += strTmp;
str += tr(" KBytes");
}
else
{
strTmp.setNum(size, 10);
str += strTmp;
str += tr(" Byte");
}
FileStateLabel->setText(str);
//update the packge number
str = tr("Send pack:");
strTmp.setNum(thread_send.PackNumber);
str += strTmp;
str += tr("/");
strTmp.setNum(thread_send.PackCnt);
str += strTmp;
PackStateLabel->setText(str);
//update the used time
str = tr("Times:");
tmp = (thread_send.UseTimes * 5) / 1000.00;
strTmp.setNum(tmp, 'g', 4);
str += strTmp;
str += tr(" S");
UseTimeStateLabel->setText(str);
if(thread_send.bIsSend)
{
ui->pushButtonSendFile->setText("发送");
thread_send.bIsSend = false;
thread_send.Recv_able=false;
ui->sendbyserial->setEnabled(true);
ui->pushButton_2->setEnabled(true);
thread_send.PackCnt = 0;
QMessageBox::about(NULL, "发送进度", "文件发送成功!");
}
//set the progressBar
ui->progressBar->setValue(thread_send.PackCnt);
}
void MainWindow::FilePathChanged(QString path)
{
FilePath = path;
}
void MainWindow::on_pushButton_clicked()
{
ui->textBrowser->clear();
}
void MainWindow::on_pushButtonOpenCom_clicked()
{
qDebug("on pushButton OpenCom clicked");
qint8 Index = ui->comboBoxBaudRate->currentIndex();
myCom->setPortName(ui->comboBoxCOM->currentText());
if(false == IsComOpen)
{
IsComOpen=myCom->open(QIODevice::ReadWrite);
if(true == IsComOpen)
{
myCom->setBaudRate(BaudRete[Index]);
myCom->setDataBits(QSerialPort::Data8);
myCom->setParity(QSerialPort::NoParity);
myCom->setStopBits(QSerialPort::OneStop);
myCom->setFlowControl(QSerialPort::NoFlowControl);
IsComOpen = true;
ui->pushButtonOpenCom->setText("关闭串口");
ui->pushButtonSendFile->setEnabled(true);
ui->comboBoxBaudRate->setEnabled(false);
ui->comboBoxCOM->setEnabled(false);
ui->comboBoxProtocolType->setEnabled(false);
ui->pushButton_2->setEnabled(true);
}
else
QMessageBox::about(NULL, tr("串口打开错误"), tr("无法打开串口或是串口被占用!"));
}
else
{
myCom->close();
ui->pushButtonOpenCom->setText("打开串口");
ui->pushButtonSendFile->setEnabled(false);
ui->comboBoxBaudRate->setEnabled(true);
ui->comboBoxCOM->setEnabled(true);
ui->comboBoxProtocolType->setEnabled(true);
IsComOpen = false;
ui->pushButton_2->setEnabled(false);
}
StateUpdate();
}
void MainWindow::on_pushButtonOpenFile_clicked()
{
qDebug("On PushButton OpenFile Clicked");
FileData.clear();//file buf clear
//the file path
FilePath = QFileDialog::getOpenFileName(this,tr("Open Binary File"),
".",
tr("Binary File(*.bin *.hex)"));
//the path is ok
if(FilePath.length())
{
ui->lineEditOpenFile->setText(FilePath);
QFile file(FilePath);
if(file.open(QIODevice::ReadOnly))
{
FileData = file.readAll();//read file
thread_send.TxData = FileData;
thread_send.PackNumber = FileData.size() / thread_send.PackSize;
if(FileData.size() % thread_send.PackSize)
{
thread_send.PackNumber += 1;
}
file.close();
}
QFileInfo file_info(file);
thread_send.FileName=file_info.fileName();
thread_send.FileSize=file_info.size();
}
StateUpdate();
}
void MainWindow::on_pushButtonSendFile_clicked()
{
qDebug("On PushButton Send File");
if(false == thread_send.bSendFile)
{ //no file open
if(FilePath.isEmpty())
{