#include "mainwindow.h"
#include "ui_mainwindow.h"
#define verNum 4
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
this->setStyleSheet("QMainWindow#MainWindow{border-image:url(:/new/prefix1/deskTop.bmp)}");
comopenFlag = 0;
upgradeFileLenth = 0;
tempPosition = 0;
upgradeFlag = false;
ui->progressBar_UpgradeProgress->setValue(0);
ui->progressBar_UpgradeProgress->setVisible(false);
ui->stackedWidget->setCurrentIndex(0);
#if (verNum==1)
TitleStr = "(广西设备)";
#elif (verNum==2)
TitleStr = "(福建设备)";
#elif (verNum==3)
TitleStr = "(乐泊设备)";
#endif
this->setWindowTitle("河北高速扫码支付终端升级助手"+TitleStr);
this->setWindowIcon(QIcon("g4.ico"));
foreach( const QSerialPortInfo &Info,QSerialPortInfo::availablePorts())//读取串口信息
{
#if myDebug
qDebug() << "portName :" << Info.portName();//调试时可以看的串口信息
qDebug() << "Description :" << Info.description();
qDebug() << "Manufacturer:" << Info.manufacturer();
#endif
QSerialPort serial;
serial.setPort(Info);
if( serial.open( QIODevice::ReadWrite) )//如果串口是可以读写方式打开的
{
ui->comboBox_portName->addItem( Info.portName() );//在comboBox那添加串口号
serial.close();//然后自动关闭等待人为开启(通过那个打开串口的PushButton)
}
}
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::on_pushButton_searchCom_clicked()
{
if(ui->radioButton_comConnectStatus->isChecked()==true)
{
QMessageBox msgBox;
msgBox.setText("串口已连接,如果坚持操作,请关闭串口后继续操作!");
msgBox.exec();
return;
}
ui->comboBox_portName->clear();
foreach( const QSerialPortInfo &Info,QSerialPortInfo::availablePorts())//读取串口信息
{
#if myDebug
qDebug() << "portName :" << Info.portName();//调试时可以看的串口信息
qDebug() << "Description :" << Info.description();
qDebug() << "Manufacturer:" << Info.manufacturer();
#endif
ui->textEdit_ReceiveText->append(Info.portName()+":"+Info.description()+">"+(Info.isBusy()? "占用" : "空闲"));
QSerialPort serial;
serial.setPort(Info);
if( serial.open( QIODevice::ReadWrite) )//如果串口是可以读写方式打开的
{
ui->comboBox_portName->addItem( Info.portName() );//在comboBox那添加串口号
serial.close();//然后自动关闭等待人为开启(通过那个打开串口的PushButton)
}
}
ui->textEdit_ReceiveText->append("串口com表已刷新");
ui->pushButton_comopen->setText("打开串口");
}
void MainWindow::on_pushButton_comopen_clicked()
{
if(ui->comboBox_portName->currentIndex()==-1)
{
QMessageBox msgBox;
msgBox.setText("无选中串口,请检查连接后重试!");
msgBox.exec();
return;
}
comopenFlag = !comopenFlag;
if(comopenFlag == 1)
{
ui->pushButton_comopen->setText("关闭串口");
ui->radioButton_comConnectStatus->setChecked(true);
mySerialPort = new QSerialPort(this);
mySerialPort->setPortName( ui->comboBox_portName->currentText() );
if(true != mySerialPort->open( QIODevice::ReadWrite ))
{
comopenFlag = 0;
ui->pushButton_comopen->setText("打开串口");
ui->radioButton_comConnectStatus->setChecked(false);
mySerialPort->close();
timer->stop();
QMessageBox msgBox;
msgBox.setText("打开串口失败!");
msgBox.exec();
return;
}
else
{
#if myDebug
qDebug() << ui->comboBox_portName->currentText();
#endif
ui->textEdit_ReceiveText->append(ui->comboBox_portName->currentText()+" open succeed ^_^");
mySerialPort->setBaudRate( ui->comboBox_baudRate->currentText().toInt() );//波特率
mySerialPort->setDataBits( QSerialPort::Data8 );//数据字节,8字节
mySerialPort->setParity( QSerialPort::NoParity );//校验,无
mySerialPort->setFlowControl( QSerialPort::NoFlowControl );//数据流控制,无
mySerialPort->setStopBits( QSerialPort::OneStop );//一位停止位
timer = new QTimer(this);
connect( timer, SIGNAL( timeout() ), this, SLOT( readMyCom() ) );
timer->start(50);//每秒读一次
}
}
else
{
timer->stop();
ui->pushButton_comopen->setText("打开串口");
ui->textEdit_ReceiveText->append(ui->comboBox_portName->currentText()+" close succeed ^_^");
ui->radioButton_comConnectStatus->setChecked(false);
if(mySerialPort->isOpen())
mySerialPort->close();
}
}
void MainWindow::readMyCom()//读取缓冲的数据,每秒读一次
{
QTextCodec *codec = QTextCodec::codecForName("GBK");//指定为GBK
requestData = mySerialPort->readAll();//用requestData存储从串口那读取的数据
if(mySerialPort->errorString()!="No error")
{
ui->pushButton_comopen->setText("打开串口");
ui->radioButton_comConnectStatus->setChecked(false);
if(mySerialPort->isOpen())
mySerialPort->close();
timer->stop();
QMessageBox msgBox;
msgBox.setText("串口连接异常,已关闭本串口,请检查串口设备是否正常连接后重新开启串口!");
msgBox.exec();
return;
}
if(true != requestData.isNull())//如果读取到有数据,就显示出来。
{
unsigned char recvDataBuf[64] = {0};
memcpy(recvDataBuf,requestData,requestData.length());
QDateTime time = QDateTime::currentDateTime();//获取系统现在的时间
QString timestr = time.toString("[hh:mm:ss]收< "); //设置显示格式
QString tempstr = codec->toUnicode(requestData); //转中文显示
if(ui->stackedWidget->currentIndex()!=1)
{
ui->textEdit_ReceiveText->append(timestr+tempstr);
}
// unsigned char mystr[512]={0},cmdbuf[50]={0};//把串口接收数据缓存一遍
// memcpy(mystr,requestData,requestData.size());
// recvStrSize = requestData.size();
if(NULL != strstr(requestData,"data read succeed!") && (upgradeFlag == true))
{
m_pTimer->start(TIMER_TIMEOUT);
}
for(int i=0;i<requestData.length();i++)
{
qDebug("0x%02x ",recvDataBuf[i]);
}
if(checksum(recvDataBuf,requestData.length()) == recvDataBuf[requestData.length()-2])
{
DATA_FRAME_RECV *data_recv = (DATA_FRAME_RECV*)(&recvDataBuf[1]);
switch(data_recv->cmd)
{
case 0x01: //扫码请求命令下发后应答
qDebug() << "扫码请求命令下发后应答";
switch(data_recv->deviceAddr)
{
case 0x01:
qDebug() << "主设备扫到码";
ui->textEdit_ReceiveText->append(timestr+"主设备"+QString::fromUtf8((char*)data_recv->dat,18));
break;
case 0x02:
qDebug() << "从设备扫到码";
ui->textEdit_ReceiveText->append(timestr+"从设备"+QString::fromUtf8((char*)data_recv->dat,18));
break;
default:
break;
}
//扫码成功等待回复
- 1
- 2
前往页