Qt串口类库QExtSerialPort的安装与简单使用
2010-05-22 17:28
323 查看
做毕业设计要用到串口通信,由于该项目是基于Qt开发的,网上查了下Qt常用的第三方类库是QExtSerialPort。于是下载下来安装了下,以下是安装的过程。
一安装
QExtSerialPort的项目网网址是:http://qextserialport.sourceforge.net/,上面有关于它的详细介绍。下载地址是:http://sourceforge.net/projects/qextserialport/files/。到现在为止,QExtSerialPort有四个版本:1.2win-alpha, 1.1, 0.9.0, 0.8.0,由于我是在windows下开发的,所以我选用的是1.2win-alpha版。
注:1.2win-alpha最好是windows平台下使用,若在linux下使用的话,将无法使用Event driven mode(Posix_QextSerialPort类的初始化选项为QextSerialBase::EventDriven),即无法采用信号和槽机制将Posix_QextSerialPort类的readyRead信号与自定义槽相连。网上似乎有牛人实现了posix平台下的Event driven mode,这是原文的链接:http://code.google.com/p/qextserialport/issues/detail?id=7,不过我没实验过。
1. 下载1.2win-alpha版的代码包:qextserialport-1.2win-alpha.zip,解压到任意目录,例如:E:/Temp/qextserialport-1.2win-alpha
2. 进入E:/Temp/qextserialport-1.2win-alpha目录,执行qmake make all
3. 编译完成后,在E:/Temp/qextserialport-1.2win-alpha/build下会生成libqextserialportd.a,qextserialportd.dll,libqextserialport.a,qextserialport.dll等文件。将qextserialportd.dll和qextserialport.dll拷贝到Qt的bin目录(在我的电脑上是D:/Qt/2010.02.1/qt/bin),将libqextserialportd.a和libqextserialport.a拷贝到Qt的lib目录(在我的电脑上是D:/Qt/2010.02.1/qt/lib)。
4. 在Qt的include目录下新建QExtSerialPort文件夹,将qextserialport-1.2win-alpha里的所有头文件复制过去,修改qextserialport.h文件:
将
改为
这样,QExtSerialPort库的安装就完成了。
最后,在使用时,须在Qt工程的pro文件中加入
LIBS += -lqextserialport
INCLUDEPATH += D:/Qt/2010.02.1/qt/include/QExtSerialPort
二简单使用
关于QExtSerialPort的使用教程,网上有很多,这里我主要想说一下必须先建立串口链接再进行串口的相关设置。我刚开始时没有注意到这一点,浪费了很多时间。下面的是我用QExtSerialPort写的一个简单的串口类:
头文件
cpp实现
一安装
QExtSerialPort的项目网网址是:http://qextserialport.sourceforge.net/,上面有关于它的详细介绍。下载地址是:http://sourceforge.net/projects/qextserialport/files/。到现在为止,QExtSerialPort有四个版本:1.2win-alpha, 1.1, 0.9.0, 0.8.0,由于我是在windows下开发的,所以我选用的是1.2win-alpha版。
注:1.2win-alpha最好是windows平台下使用,若在linux下使用的话,将无法使用Event driven mode(Posix_QextSerialPort类的初始化选项为QextSerialBase::EventDriven),即无法采用信号和槽机制将Posix_QextSerialPort类的readyRead信号与自定义槽相连。网上似乎有牛人实现了posix平台下的Event driven mode,这是原文的链接:http://code.google.com/p/qextserialport/issues/detail?id=7,不过我没实验过。
1. 下载1.2win-alpha版的代码包:qextserialport-1.2win-alpha.zip,解压到任意目录,例如:E:/Temp/qextserialport-1.2win-alpha
2. 进入E:/Temp/qextserialport-1.2win-alpha目录,执行qmake make all
3. 编译完成后,在E:/Temp/qextserialport-1.2win-alpha/build下会生成libqextserialportd.a,qextserialportd.dll,libqextserialport.a,qextserialport.dll等文件。将qextserialportd.dll和qextserialport.dll拷贝到Qt的bin目录(在我的电脑上是D:/Qt/2010.02.1/qt/bin),将libqextserialportd.a和libqextserialport.a拷贝到Qt的lib目录(在我的电脑上是D:/Qt/2010.02.1/qt/lib)。
4. 在Qt的include目录下新建QExtSerialPort文件夹,将qextserialport-1.2win-alpha里的所有头文件复制过去,修改qextserialport.h文件:
将
/*POSIX CODE*/ #ifdef _TTY_POSIX_ #include "posix_qextserialport.h" #define QextBaseType Posix_QextSerialPort /*MS WINDOWS CODE*/ #else #include "win_qextserialport.h" #define QextBaseType Win_QextSerialPort #endif
改为
#include "win_qextserialport.h" #define QextBaseType Win_QextSerialPort
这样,QExtSerialPort库的安装就完成了。
最后,在使用时,须在Qt工程的pro文件中加入
LIBS += -lqextserialport
INCLUDEPATH += D:/Qt/2010.02.1/qt/include/QExtSerialPort
二简单使用
关于QExtSerialPort的使用教程,网上有很多,这里我主要想说一下必须先建立串口链接再进行串口的相关设置。我刚开始时没有注意到这一点,浪费了很多时间。下面的是我用QExtSerialPort写的一个简单的串口类:
头文件
/* * SerialPort.h * * Created on: 2010-5-19 * Author: Administrator */ #ifndef SERIALPORT_H_ #define SERIALPORT_H_ #include <QString> #include <QStringList> #include <QtExtSerialPort/QtExtSerialPort> class SerialPort: public QObject { Q_OBJECT public: SerialPort(); SerialPort(QString); virtual ~SerialPort(); void setSerial(QString); signals: void comOutput(QString); public slots: void writeSerial(QString); private: Win_QextSerialPort *com; private slots: void outStrChanged(); }; #endif /* SERIALPORT_H_ */
cpp实现
/* * SerialPort.cpp * * Created on: 2010-5-19 * Author: Administrator */ #include <iostream> #include "SerialPort.h" SerialPort::SerialPort() { } SerialPort::SerialPort(QString str) { com = new Win_QextSerialPort(str, QextSerialBase::EventDriven); connect(com, SIGNAL(readyRead()), this, SLOT(outStrChanged())); com->open(QIODevice::ReadWrite); } SerialPort::~SerialPort() { delete com; } void SerialPort::setSerial(QString str) { QStringList lst = str.split(","); if (lst[0] == tr("4800")) com->setBaudRate(BAUD4800); else if (lst[0] == tr("2400")) com->setBaudRate(BAUD2400); else if (lst[0] == tr("1200")) com->setBaudRate(BAUD1200); else if (lst[0] == tr("600")) com->setBaudRate(BAUD600); else if (lst[0] == tr("300")) com->setBaudRate(BAUD300); else if (lst[0] == tr("115200")) com->setBaudRate(BAUD115200); else com->setBaudRate(BAUD9600); if (lst[1] == tr("8")) com->setDataBits(DATA_8); else com->setDataBits(DATA_7); if (lst[2] == tr("odd")) com->setParity(PAR_ODD); else if (lst[2] == tr("low")) com->setParity(PAR_SPACE); else if (lst[2] == tr("high")) com->setParity(PAR_MARK); else if (lst[2] == tr("none")) com->setParity(PAR_NONE); else com->setParity(PAR_EVEN); if (lst[3] == tr("2")) com->setStopBits(STOP_2); else com->setStopBits(STOP_1); if (lst[4] == tr("XON/XOFF")) com->setFlowControl(FLOW_XONXOFF); else if (lst[4] == tr("None")) com->setFlowControl(FLOW_OFF); else com->setFlowControl(FLOW_HARDWARE); com->setTimeout(lst[5].toLong()); } void SerialPort::outStrChanged() { QString comOutputStr = com->readAll(); emit comOutput(comOutputStr); } void SerialPort::writeSerial(QString str) { com->write(str.toAscii()); }
相关文章推荐
- [Python]Pip的安装以及简单的使用
- VisualStudio 2010 Beta1 开始简单用一下(2008共存)-从安装到使用发几张图片 (*^__^*)
- nc在centos7上的安装和简单使用
- svn的安装和简单使用
- JUnit简单安装使用步骤总结
- 安装Visual Studio 2013以及简单使用
- memcached 的安装以及简单使用
- mac 环境下svn客户端安装和简单使用
- Liferay Portal 简单介绍及安装配置使用 (二)
- windows10上安装单机版hbase到使用eclipse连接简单使用hbase
- PostgreSQL在RHEL5上的安装和简单使用
- mongodb的安装和简单使用
- mysql的安装以及简单使用
- Flume的安装与简单使用
- Ansible 安装与简单使用
- nosql之redis简单安装与使用
- ubuntu中snap包的安装、更新删除与简单使用
- pycharm linux版本的安装以及简单使用
- MongoDB的安装和简单使用
- centos 6.6 x86_64 安装docker和简单使用