复制代码- #include "mainwindow.h"
- #include "ui_mainwindow.h"
- MainWindow::MainWindow(QWidget *parent) :
- QMainWindow(parent),
- ui(new Ui::MainWindow)
- {
- ui->setupUi(this);
- init();// 初始化相关配置
- serialToTcp = new SerialToTcp();
- wifiToTcp = new WiFiToTcp();
- ethernetToTcp = new EthernetToTcp();
- gprsToTcp = new GprsToTcp();
- }
- //构造函数
- MainWindow::~MainWindow()
- {
- delete ui;
- delete inputButtonGroup;
- delete outputButtonGroup;
- }
- // 初始化相关配置
- void MainWindow::init() {
- // 将不同通信接口按钮加入到输入输出按钮组中
- inputButtonGroup = new QButtonGroup;
- //下面调用了QButtonGroup的void addButton ( QAbstractButton * button, int id )方法
- inputButtonGroup->addButton(ui->serialInputRadioButton, 1);
- inputButtonGroup->addButton(ui->wifiInputRadioButton, 2);
- inputButtonGroup->addButton(ui->ethernetInputRadioButton, 3);
- inputButtonGroup->addButton(ui->gprsInputRadioButton, 4);
- outputButtonGroup = new QButtonGroup;
- outputButtonGroup->addButton(ui->serialOutputRadioButton, 1);
- outputButtonGroup->addButton(ui->wifiOutputRadioButton, 2);
- outputButtonGroup->addButton(ui->ethernetOutputRadioButton, 3);
- outputButtonGroup->addButton(ui->gprsOutputRadioButton, 4);
- connect( inputButtonGroup, SIGNAL(buttonClicked (int)), this, SLOT(inputButtonJudge(int)) );
- connect( outputButtonGroup, SIGNAL(buttonClicked (int)), this, SLOT(outputButtonJudge(int)) );
- // 限制网络地址和端口格式
- QRegExp portExp("[0-9]{1,5}");
- //[0-9]表示从0到9,{1,5}表示出现1到5次???也就是输入一个数字不超过5位数???
- QRegExpValidator *validator = new QRegExpValidator();
- validator->setRegExp(portExp);
- ui->portInputLineEdit->setValidator(validator);
- ui->portOutputLineEdit->setValidator(validator);
- QRegExp regExp("[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}");
- //意思大概是输入格式为***.***.***.***的ip地址
- QRegExpValidator *ipvalidator = new QRegExpValidator();//ip验证器
- ipvalidator->setRegExp(regExp);
- ui->addressInputLineEdit->setValidator(ipvalidator);
- ui->addressOutputLineEdit->setValidator(ipvalidator);
- }
- // 程序退出操作
- void MainWindow::on_exitButton_clicked()
- {
- int ret = QMessageBox::question(this, tr("退出信息"), tr("确定退出该程序?"), QMessageBox::Yes, QMessageBox::No);
- if(ret == QMessageBox::Yes) {
- close();
- }
- }
- // 使串口输入配置或者网络配置可用
- void MainWindow::inputButtonJudge(int buttonId) {
- if(buttonId == 1) {
- ui->serialInputFrame->setEnabled(true);// 设置串口输入配置可用
- ui->downloadFrame->setEnabled(false);// 设置网络配置不可用
- } else {
- ui->serialInputFrame->setEnabled(false);// 设置串口输入配置不可用
- ui->downloadFrame->setEnabled(true);// 设置网络配置可用
- }
- }
- // 使串口输出配置或者网络配置可用
- void MainWindow::outputButtonJudge(int buttonId) {
- if(buttonId == 1) {
- ui->serialOutputFrame->setEnabled(true);// 设置串口输出配置可用
- ui->uploadFrame->setEnabled(false);// 设置网络配置不可用
- } else {
- ui->serialOutputFrame->setEnabled(false);// 设置串口输出配置不可用
- ui->uploadFrame->setEnabled(true);// 设置网络配置可用
- }
- }
- // 开始连接配置
- void MainWindow::on_connectButton_clicked()
- {
- if(inputButtonGroup->checkedId() == -1) {
- QMessageBox::information(this, tr("提示信息"), tr("请选择通信接口输入方式!"), QMessageBox::Ok);
- return;
- }
- if(outputButtonGroup->checkedId() == -1) {
- QMessageBox::information(this, tr("提示信息"), tr("请选择通信接口输出方式!"), QMessageBox::Ok);
- return;
- }
- if(inputButtonGroup->checkedId() == outputButtonGroup->checkedId()) {
- QMessageBox::information(this, tr("提示信息"), tr("请选择两种不同通信接口输入输出方式!"), QMessageBox::Ok);
- return;
- }
- //上面调用了int QButtonGroup::checkedId () const方法,没有检查到button则返回-1,检查到button就返回相应的buttonId
- ui->connectButton->setEnabled(false);
- ui->disconnectButton->setEnabled(true);
- ui->mainFrame->setEnabled(false);
- QString server_ip = ui->addressInputLineEdit->text();
- QString client_ip = ui->addressOutputLineEdit->text();
- bool ok;
- int server_port = ui->portInputLineEdit->text().toInt(&ok, 10);
- int client_port = ui->portOutputLineEdit->text().toInt(&ok, 10);
- if(inputButtonGroup->checkedId() == 1) {
- qDebug()<<outputButtonGroup->checkedId();
- serialToTcp->serialToTcp(client_ip,
- client_port,
- ui->speedInputComboBox->currentIndex(),
- ui->databitInputComboBox->currentIndex(),
- ui->parityInputcomboBox->currentIndex(),
- ui->stopbitInputComboBox->currentIndex(),
- outputButtonGroup->checkedId());
- }
- else if(inputButtonGroup->checkedId() == 2){
- if(outputButtonGroup->checkedId() == 1) {
- wifiToTcp->wifiToSerial(server_ip,
- server_port,
- ui->speedOutputComboBox->currentIndex(),
- ui->databitOutputComboBox->currentIndex(),
- ui->parityOutputComboBox->currentIndex(),
- ui->stopbitOutputComboBox->currentIndex());
- }
- else {
- wifiToTcp->wifiToTcp(server_ip,
- server_port,
- client_ip,
- client_port,
- outputButtonGroup->checkedId());
- }
- }
- else if(inputButtonGroup->checkedId() == 3){
- if(outputButtonGroup->checkedId() == 1) {
- ethernetToTcp->ethernetToSerial(server_ip,
- server_port,
- ui->speedOutputComboBox->currentIndex(),
- ui->databitOutputComboBox->currentIndex(),
- ui->parityOutputComboBox->currentIndex(),
- ui->stopbitOutputComboBox->currentIndex());
- }
- else {
- ethernetToTcp->ethernetToTcp(server_ip, server_port, client_ip, client_port,
- outputButtonGroup->checkedId());
- }
- }
- else if(inputButtonGroup->checkedId() == 4){
- if(outputButtonGroup->checkedId() == 1) {
- gprsToTcp->gprsToSerial(server_ip, server_port,
- ui->speedOutputComboBox->currentIndex(),
- ui->databitOutputComboBox->currentIndex(),
- ui->parityOutputComboBox->currentIndex(),
- ui->stopbitOutputComboBox->currentIndex());
- } else {
- gprsToTcp->gprsToTcp(server_ip, server_port, client_ip, client_port,
- outputButtonGroup->checkedId());
- }
- }
- }
- // 关闭连接配置
- void MainWindow::on_disconnectButton_clicked()
- {
- ui->connectButton->setEnabled(true);
- ui->disconnectButton->setEnabled(false);
- ui->mainFrame->setEnabled(true);
- if(inputButtonGroup->checkedId() == 1) {
- serialToTcp->closeSerialToTcp();
- }
- else if(inputButtonGroup->checkedId() == 2) {
- wifiToTcp->stopTransfer();
- }
- else if(inputButtonGroup->checkedId() == 3){
- ethernetToTcp->stopTransfer();
- }
- else if(inputButtonGroup->checkedId() == 4){
- gprsToTcp->stopTransfer();
- }
- }
|