linux系统管理客户端2--串口实现测试代码编写
2014-06-12 14:56
811 查看
1、实现界面如下:
2、新建对话框窗口sDialog
3、设置ui格局如上图
4、将第三方类的文件加入项目中
5、将窗口配置文件的加入项目中
6、新建头文件如下:
#ifndef SDIALOG_H #define SDIALOG_H #include "qextSerialPort.h" #include "win_qextserialport.h" #include "comdialog.h" #include <QDialog> namespace Ui { class sDialog; } class sDialog : public QDialog { Q_OBJECT public: explicit sDialog(QWidget *parent = 0); ~sDialog(); signals: void qtWin(); void closeWin(); private: Ui::sDialog *ui; private: QextSerialPort *myCom; //使用第三方类定义对象 comDialog *comWin; //使用自定义串口配置类对象 bool isOpen; //判断串口是否被打开 bool isLogin; //判断是否登录系统 bool isClose; //判断是否关闭 private slots: void readMyCom(); //读数据槽函数 void sendData(); //发送数据槽函数 void comSetButton(); //串口配置槽函数 void disConnectMyComButton(); //断开连接槽函数 void scroll(); //textBrowser光标移动槽函数 protected: void closeEvent(QCloseEvent *e); }; #endif // SDIALOG_H
7、cpp源文件
#include "sdialog.h" #include "ui_sdialog.h" #include "qextserialport.h" #include <QMessageBox> #include <QCloseEvent> sDialog::sDialog(QWidget *parent) : QDialog(parent), ui(new Ui::sDialog) { isLogin = false; isOpen = false; isClose = true; ui->setupUi(this); comWin = new comDialog; ui->sendButton->setDefault(true); ui->disConnectButton->setEnabled(false); this->setWindowFlags(Qt::WindowCloseButtonHint); this->setWindowFlags(Qt::WindowMaximizeButtonHint); this->setWindowFlags(Qt::WindowMinMaxButtonsHint); connect(ui->openButton,SIGNAL(clicked()),comWin,SLOT(exec())); //打开串口配置对话框 connect(ui->connectButton,SIGNAL(clicked()),this,SLOT(comSetButton())); //串口连接 connect(ui->sendButton,SIGNAL(clicked()),this,SLOT(sendData())); //发送命令 connect(ui->textBrowser,SIGNAL(textChanged()),this,SLOT(scroll())); //光标滚动 connect(ui->disConnectButton,SIGNAL(clicked()),this,SLOT(disConnectMyComButton())); //断开连接 connect(this,SIGNAL(qtWin()),this,SLOT(disConnectMyComButton())); //自定义信号断开连接 connect(this,SIGNAL(closeWin()),this,SLOT(accept()),Qt::QueuedConnection); //使用排队方式连接信号emit语句后的代码将在发出信号后立即被执行 } sDialog::~sDialog() { delete ui; } //串口连接 void sDialog::comSetButton() { myCom = new QextSerialPort(comWin->comPort,QextSerialBase::EventDriven); isOpen = myCom->open(QIODevice::ReadWrite); if(isOpen == true) { QMessageBox::information(this,"infor",tr("open success!")); } else { QMessageBox::information(this,"infor","open failure!"); return; } ui->connectButton->setEnabled(false); ui->openButton->setEnabled(false); switch(comWin->comBaud) { case 0: myCom->setBaudRate(BAUD300); break; case 1: myCom->setBaudRate(BAUD600); break; case 2: myCom->setBaudRate(BAUD1200); break; case 3: myCom->setBaudRate(BAUD2400); break; case 4: myCom->setBaudRate(BAUD9600); break; case 5: myCom->setBaudRate(BAUD38400); break; case 6: myCom->setBaudRate(BAUD57600); break; case 7: myCom->setBaudRate(BAUD115200); break; case 8: myCom->setBaudRate(BAUD128000); break; case 9: myCom->setBaudRate(BAUD256000); break; } switch(comWin->comStopBit) { case 0: myCom->setStopBits(STOP_1); break; case 1: myCom->setStopBits(STOP_1_5); break; case 2: myCom->setStopBits(STOP_2); break; } switch(comWin->comDataBit) { case 0: myCom->setDataBits(DATA_5); break; case 1: myCom->setDataBits(DATA_6); break; case 2: myCom->setDataBits(DATA_7); break; case 3: myCom->setDataBits(DATA_8); break; } switch(comWin->comPariy) { case 0: myCom->setParity(PAR_NONE); break; case 1: myCom->setParity(PAR_ODD); break; case 2: myCom->setParity(PAR_EVEN); break; case 3: myCom->setParity(PAR_SPACE); break; case 4: myCom->setParity(PAR_MARK); break; } switch(comWin->comFlow) { case 0: myCom->setFlowControl(FLOW_OFF); break; case 1: myCom->setFlowControl(FLOW_HARDWARE); break; case 2: myCom->setFlowControl(FLOW_XONXOFF); break; } myCom->setTimeout(500); //等待接收数据 connect(myCom,SIGNAL(readyRead()),this,SLOT(readMyCom())); } //按行读取串口数据 void sDialog::readMyCom() { char buf[512] = {0}; if(0 <<span style=" color:#c0c0c0;"> myCom->readLine(buf,sizeof(buf))) { if(strchr(buf,'@') != NULL) //判断是否登录 { ui->disConnectButton->setEnabled(true); if(isLogin) { QString str = "exit\n"; myCom->write(str.toAscii()); isLogin = false; ui->disConnectButton->setEnabled(false); myCom->close(); ui->openButton->setEnabled(true); ui->connectButton->setEnabled(true); } } if(!isClose) { emit closeWin(); return; } ui->textBrowser->insertPlainText(buf); } } //发送数据 void sDialog::sendData() { if(isOpen) { QString str; str = ui->msgEdit->text()+"\n"; myCom->write(str.toAscii()); Sleep(50); ui->msgEdit->clear(); } else { QMessageBox::information(this,"infor","can not open"); } } void sDialog::scroll() { ui->textBrowser->moveCursor(QTextCursor::End); } void sDialog::disConnectMyComButton() { if(!isClose && !isOpen) { emit closeWin(); return; } if(isOpen) { isLogin = true; isOpen = false; QString str = "\n"; myCom->write(str.toAscii()); } else { QMessageBox::information(this,"infor","Has not open"); } } //重写closeEvent拦截关闭按钮 void sDialog::closeEvent(QCloseEvent *e) { if(QMessageBox::Yes == QMessageBox::information(this,"infor","Are you sure to close the window!",QMessageBox::Yes,QMessageBox::Cancel)) { e->ignore(); isClose = false; emit(qtWin()); //发送自定义信号清理打开的系统 } else { e->ignore(); } }
相关文章推荐
- linux系统管理客户端1--串口配置代码编写
- linux环境下C语言实现非阻塞方式读取字符串数据的串口测试程序,即串口工具的编写
- (一个代码学会c语言操作数据库)linux上通过c语言操作数据库实现基本的学生信息管理系统(增、删、查、改‘显示)
- Linux系统新加物理盘实现LVM管理
- 一个资源管理系统的设计--解析linux的cgroup实现
- 使用 ipmitool 实现 Linux 系统下对服务器的 ipmi 管理
- Linux下串口编程实例,实现0~255数据自环测试
- 学生信息管理系统(在LINUX下用GCC编译需要更改conio.h为curses.h并修改相关代码)
- (C语言) 怎么用代码实现一个学生成绩管理系统
- Linux 系统批量管理工具介绍,如何实现对一万台服务器的同时批量管理?
- 不写代码开发信息管理系统-配置实现 下载地址:http://121.18.78.216
- 基于 Linux 系统的Nagios网络管理模块的实现
- 权限管理系统(用户信息管理模块业务组件实现代码,带注解)
- 配置实现通过串口(com 口、RS232 接口)终端控制 Linux 系统
- linux系统调用实现代码分析
- 在嵌入式Linux 系统中实现基于Web 的配置管理
- 用泛型技术对业务管理Bean抽象_实现_测试(代码示例)
- 使用 ipmi实现Linux系统下对服务器的管理
- 使用subversion实现虚拟机中linux和windows宿主机代码共享管理
- 学生成绩管理系统C语言代码实现