网站 制作公司,国内个人网站,高端网站制作怎么样,wordpress 4.7 暴路径CSerialPort教程4.3.x (4) - CSerialPort在QT中的使用
环境#xff1a;
QT: 5.6.3前言
CSerialPort项目是一个基于C/C的轻量级开源跨平台串口类库#xff0c;可以轻松实现跨平台多操作系统的串口读写#xff0c;同时还支持C#, Java, Python, Node.js等。
CSerialPort项目…CSerialPort教程4.3.x (4) - CSerialPort在QT中的使用
环境
QT: 5.6.3前言
CSerialPort项目是一个基于C/C的轻量级开源跨平台串口类库可以轻松实现跨平台多操作系统的串口读写同时还支持C#, Java, Python, Node.js等。
CSerialPort项目的开源协议自 V3.0.0.171216 版本后采用GNU Lesser General Public License v3.0
为了让开发者更好的使用CSerialPort进行开发特编写基于4.3.x版本的CSerialPort教程系列。
CSerialPort项目地址
https://github.com/itas109/CSerialPorthttps://gitee.com/itas109/CSerialPort
QT完整示例程序地址
https://github.com/itas109/CSerialPort/tree/master/examples/CommQThttps://gitee.com/itas109/CSerialPort/tree/master/examples/CommQT
1. 新建QT项目
新建一个QT项目目,解决方案名称为CommQT
【文件】-【新建文件或项目】-【Application(Qt)】-【Qt Widgets Application】-【choose…】-【名称: CommQT】
在CommQT解决方案目录下载CSerialPort源码
$ cd CommQT
$ git clone https://github.com/itas109/CSerialPort目录结构如下
D:/CommQT $ tree
.
--- CommQT.pro
--- CSerialPort
| --- include
| | --- CSerialPort
| | | --- SerialPort.h
| | | --- SerialPortInfo.h
| --- src
| | --- SerialPort.cpp
| | --- SerialPortBase.cpp
| | --- SerialPortInfo.cpp
| | --- SerialPortInfoBase.cpp
| | --- SerialPortInfoUnixBase.cpp
| | --- SerialPortInfoWinBase.cpp
| | --- SerialPortUnixBase.cpp
| | --- SerialPortWinBase.cpp
--- main.cpp
--- mainwindow.cpp
--- mainwindow.h
--- mainwindow.ui2. CommQT.pro中增加必要的CSerialPort依赖
CommQT.pro中增加CSerialPort依赖
#-------------------------------------------------
#
# Project created by QtCreator
#
#-------------------------------------------------QT core guigreaterThan(QT_MAJOR_VERSION, 4): QT widgetsTARGET CommQT
TEMPLATE appSOURCES main.cpp\mainwindow.cppHEADERS mainwindow.hFORMS mainwindow.ui# add by itas109
# 1. headers
INCLUDEPATH $$PWD/CSerialPort/include# 2. sources
SOURCES $$PWD/CSerialPort/src/SerialPortBase.cpp
SOURCES $$PWD/CSerialPort/src/SerialPort.cpp
SOURCES $$PWD/CSerialPort/src/SerialPortInfoBase.cpp
SOURCES $$PWD/CSerialPort/src/SerialPortInfo.cppwin32 {SOURCES $$PWD/CSerialPort/src/SerialPortWinBase.cppSOURCES $$PWD/CSerialPort/src/SerialPortInfoWinBase.cpp
}unix {SOURCES $$PWD/CSerialPort/src/SerialPortUnixBase.cppSOURCES $$PWD/CSerialPort/src/SerialPortInfoUnixBase.cpp
}# 3. add system libs
win32-msvc*:LIBS advapi32.lib
win32-msvc*:LIBS setupapi.lib
win32-g:LIBS libsetupapi# 4. define UNICODE
DEFINES _UNICODE
# end by itas1093. 在QT中添加CSerialPort代码
3.1 增加CSerialPort的头文件、继承类、接收函数及CSerialPort实例对象
在mainwindow.h文件中
增加CSerialPort的头文件MainWindow类继承CSerialPortListener增加接收函数onReadEvent(const char *portName, unsigned int readBufferLen)增加CSerialPort的实例对象增加QPlainTextEdit控件接收数据
mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H#include QMainWindow// add by itas109
#include QPlainTextEdit#include CSerialPort/SerialPort.h
#include CSerialPort/SerialPortInfo.h
using namespace itas109;
// end by itas109namespace Ui {
class MainWindow;
}class MainWindow : public QMainWindow,public CSerialPortListener // add by itas109
{Q_OBJECTpublic:explicit MainWindow(QWidget *parent 0);~MainWindow();private:Ui::MainWindow *ui;// add by itas109
private:void onReadEvent(const char *portName, unsigned int readBufferLen);signals:void emitUpdateReceive(QString str);private slots:void OnUpdateReceive(QString str);private:QPlainTextEdit * p_plainTextEditReceive;CSerialPort m_serialPort;// end by itas109
};#endif // MAINWINDOW_H3.2 增加串口的相关实现代码
在mainwindow.cpp文件增加
MainWindow::MainWindow中增加CSerialPort的测试代码增加OnReceive函数的实现
#include mainwindow.h
#include ui_mainwindow.hMainWindow::MainWindow(QWidget *parent) :QMainWindow(parent),ui(new Ui::MainWindow)
{ui-setupUi(this);// add by itas109p_plainTextEditReceive NULL;p_plainTextEditReceive new QPlainTextEdit();this-setCentralWidget(p_plainTextEditReceive);std::vectorSerialPortInfo portNameList CSerialPortInfo::availablePortInfos();if (portNameList.size() 0){p_plainTextEditReceive-moveCursor (QTextCursor::End);p_plainTextEditReceive-insertPlainText(QString(First avaiable Port: %1\n).arg(portNameList[0].portName));}else{p_plainTextEditReceive-moveCursor (QTextCursor::End);p_plainTextEditReceive-insertPlainText(No avaiable Port);return;}connect(this,MainWindow::emitUpdateReceive,this,MainWindow::OnUpdateReceive,Qt::QueuedConnection);m_serialPort.connectReadEvent(this);m_serialPort.init(portNameList[0].portName);m_serialPort.open();if (m_serialPort.isOpen()){p_plainTextEditReceive-moveCursor (QTextCursor::End);p_plainTextEditReceive-insertPlainText(QString(open %1 success\n).arg(portNameList[0].portName));m_serialPort.writeData(itas109, 7);}else{p_plainTextEditReceive-moveCursor (QTextCursor::End);p_plainTextEditReceive-insertPlainText(QString(open %1 failed\n).arg(portNameList[0].portName));}// end by itas109
}MainWindow::~MainWindow()
{delete ui;// add by itas109m_serialPort.disconnectReadEvent();// end by itas109
}// add by itas109
void MainWindow::onReadEvent(const char *portName, unsigned int readBufferLen)
{if(readBufferLen 0){char data[1024];int recLen m_serialPort.readData(data,readBufferLen 1023 ? 1023 : readBufferLen);if (recLen 0){data[recLen] \0;emitUpdateReceive(QString::fromLocal8Bit(data,recLen));}}
}void MainWindow::OnUpdateReceive(QString str)
{p_plainTextEditReceive-moveCursor (QTextCursor::End);p_plainTextEditReceive-insertPlainText(str);
}
// end by itas1094. 结果
代码中的COM2对应的串口为RS232环回测试硬件因此对应的结果为 程序启动后初始化并打开串口COM2接收窗打印open xxx success发送数据itas09,随后接收窗打印itas09
windows下结果
First avaiable Port: COM1
open COM1 success
itas109linux下结果
First avaiable Port: /dev/ttyUSB0
open /dev/ttyUSB0 success
itas109linux下可能遇到权限问题
open port error: Unable to open /dev/ttyUSB0: 权限不够临时解决
$ sudo chmod 777 /dev/ttyUSB0永久解决(dev为当前用户名)
$ sudo usermod -aG dialout dev
$ newgrp dialout # 立即生效License
License under CC BY-NC-ND 4.0: 署名-非商业使用-禁止演绎 Reference:
https://github.com/itas109/CSerialPorthttps://gitee.com/itas109/CSerialPorthttps://blog.csdn.net/itas109 文章转载自: http://www.morning.kgqpx.cn.gov.cn.kgqpx.cn http://www.morning.xkjrs.cn.gov.cn.xkjrs.cn http://www.morning.wyzby.cn.gov.cn.wyzby.cn http://www.morning.mdwb.cn.gov.cn.mdwb.cn http://www.morning.zqbrd.cn.gov.cn.zqbrd.cn http://www.morning.qfnrx.cn.gov.cn.qfnrx.cn http://www.morning.kzrbd.cn.gov.cn.kzrbd.cn http://www.morning.nsyzm.cn.gov.cn.nsyzm.cn http://www.morning.xllrf.cn.gov.cn.xllrf.cn http://www.morning.fwgnq.cn.gov.cn.fwgnq.cn http://www.morning.tnzwm.cn.gov.cn.tnzwm.cn http://www.morning.ddjp.cn.gov.cn.ddjp.cn http://www.morning.hjjhjhj.com.gov.cn.hjjhjhj.com http://www.morning.hrypl.cn.gov.cn.hrypl.cn http://www.morning.rdnjc.cn.gov.cn.rdnjc.cn http://www.morning.fdmtr.cn.gov.cn.fdmtr.cn http://www.morning.cczrw.cn.gov.cn.cczrw.cn http://www.morning.qdlnw.cn.gov.cn.qdlnw.cn http://www.morning.mmjyk.cn.gov.cn.mmjyk.cn http://www.morning.mhfbp.cn.gov.cn.mhfbp.cn http://www.morning.crrmg.cn.gov.cn.crrmg.cn http://www.morning.rhzzf.cn.gov.cn.rhzzf.cn http://www.morning.mkfhx.cn.gov.cn.mkfhx.cn http://www.morning.rsqpc.cn.gov.cn.rsqpc.cn http://www.morning.jfmjq.cn.gov.cn.jfmjq.cn http://www.morning.ywtbk.cn.gov.cn.ywtbk.cn http://www.morning.rwyd.cn.gov.cn.rwyd.cn http://www.morning.pzlhq.cn.gov.cn.pzlhq.cn http://www.morning.kysport1102.cn.gov.cn.kysport1102.cn http://www.morning.rjynd.cn.gov.cn.rjynd.cn http://www.morning.nrpp.cn.gov.cn.nrpp.cn http://www.morning.bnmfq.cn.gov.cn.bnmfq.cn http://www.morning.nrddx.com.gov.cn.nrddx.com http://www.morning.qxgmp.cn.gov.cn.qxgmp.cn http://www.morning.ndcf.cn.gov.cn.ndcf.cn http://www.morning.pxrfm.cn.gov.cn.pxrfm.cn http://www.morning.nqyfm.cn.gov.cn.nqyfm.cn http://www.morning.srbl.cn.gov.cn.srbl.cn http://www.morning.fstesen.com.gov.cn.fstesen.com http://www.morning.hsklc.cn.gov.cn.hsklc.cn http://www.morning.fpxsd.cn.gov.cn.fpxsd.cn http://www.morning.srgyj.cn.gov.cn.srgyj.cn http://www.morning.sbkb.cn.gov.cn.sbkb.cn http://www.morning.xxfxxf.cn.gov.cn.xxfxxf.cn http://www.morning.tqfnf.cn.gov.cn.tqfnf.cn http://www.morning.qzsmz.cn.gov.cn.qzsmz.cn http://www.morning.pqcrz.cn.gov.cn.pqcrz.cn http://www.morning.gcbhh.cn.gov.cn.gcbhh.cn http://www.morning.hsdhr.cn.gov.cn.hsdhr.cn http://www.morning.cknrs.cn.gov.cn.cknrs.cn http://www.morning.nrfrd.cn.gov.cn.nrfrd.cn http://www.morning.yzygj.cn.gov.cn.yzygj.cn http://www.morning.rckdq.cn.gov.cn.rckdq.cn http://www.morning.c7630.cn.gov.cn.c7630.cn http://www.morning.lsjtq.cn.gov.cn.lsjtq.cn http://www.morning.dpplr.cn.gov.cn.dpplr.cn http://www.morning.wmrgp.cn.gov.cn.wmrgp.cn http://www.morning.djxnw.cn.gov.cn.djxnw.cn http://www.morning.hnkkm.cn.gov.cn.hnkkm.cn http://www.morning.mwhqd.cn.gov.cn.mwhqd.cn http://www.morning.qqnjr.cn.gov.cn.qqnjr.cn http://www.morning.dxrbp.cn.gov.cn.dxrbp.cn http://www.morning.gnwse.com.gov.cn.gnwse.com http://www.morning.bpmdh.cn.gov.cn.bpmdh.cn http://www.morning.pjrql.cn.gov.cn.pjrql.cn http://www.morning.znqxt.cn.gov.cn.znqxt.cn http://www.morning.yqgny.cn.gov.cn.yqgny.cn http://www.morning.fbmjl.cn.gov.cn.fbmjl.cn http://www.morning.wiitw.com.gov.cn.wiitw.com http://www.morning.ccyjt.cn.gov.cn.ccyjt.cn http://www.morning.xesrd.com.gov.cn.xesrd.com http://www.morning.gfqjf.cn.gov.cn.gfqjf.cn http://www.morning.zlsmx.cn.gov.cn.zlsmx.cn http://www.morning.trsfm.cn.gov.cn.trsfm.cn http://www.morning.sdhmn.cn.gov.cn.sdhmn.cn http://www.morning.fnmtc.cn.gov.cn.fnmtc.cn http://www.morning.ykwgl.cn.gov.cn.ykwgl.cn http://www.morning.ljjmr.cn.gov.cn.ljjmr.cn http://www.morning.kcxtz.cn.gov.cn.kcxtz.cn http://www.morning.gthwr.cn.gov.cn.gthwr.cn