https://blog.csdn.net/u014252478/article/details/80389016
Qt中添加了QSerialPort類,非常好用,可是由於各種原因,很多人還是要用Qt4,比如我,後來發現官方文檔說QSerialPort支持Qt4,就研究了一下,比較笨,搞了好久才弄好。
第一步:下載QSerialPort模塊
官方網站我經常打不開,所以上傳到網盤了。
http://download.qt-project.org/這是官方的,各種版本都有,大家可以自己找找。
http://pan.baidu.com/s/1gdnanOv這是我自己下載的,共享給大家。
第二步:打開QSerialPort工程
首先確認您電腦上有可以正常使用的Qt4版本,我這裏用的是Qt4.8.6.
解壓下載的qtserialport-opensource-src-5.3.2.rar,會發現一個qtserialport.pro,打開它。
選擇項目,在構建步驟中選擇“添加構建步驟->Make”,如下圖:
在Make參數這一欄裏輸入“install”
搞定,現在點擊項目中的“構建”,就會編譯,然後把QSerialPort庫安裝到你的Qt4裏面。
這是Debug版本,然後Release版本也是一樣做的
第三步:編譯錯誤undefined reference to ...
如果是下載的官方的,可能會有下面的錯誤:(建議用最新的5.4.2版本,已經修復改錯誤,後面有鏈接)
跟蹤到錯誤的地方:
修改爲:
再次編譯,還有錯誤:
跟蹤錯誤地方:
修改爲:注意四個地方都要修改
第四步:串口測試程序
新建一個工程,在pro文件中加入 CONFIG += serialport
然後在.h文件中包含 #include <QSerialPort/QSerialPort> #include <QSerialPort/QSerialPortInfo>
搞定了,QSerialPortInfo是一個很不錯的東西,可以查找電腦上的可用串口,用法如下:
有時候串口比較多的時候,會發現排序混亂,向下面這個樣子:
作爲一個重度強迫症程序員,這是絕對不允許的,3怎麼能排在8後面呢,絕對不能!!!
這樣解決:
這回看起來就舒服多了:
差不多就這些了。
虛擬串口工具
推薦一個虛擬串口工具,很好用,裏面有教程
http://download.csdn.net/detail/yuyu414/5094469
上面這個需要兩個積分,找了半天不知道怎麼刪掉積分要求,重新上傳了一份在百度網盤,好東西,當然要大家來分享
http://pan.baidu.com/s/1o69M78I
——————————————————我是分割線——————————————————
Qt串口模塊更新:qtserialport-opensource-src-5.4.0
修復了上面的錯誤,官方更新日誌:
- [QTBUG-41190] Fixed build on Qt4 related to QT_DEPRECATED_SINCE(5, 2) macro.
- QSerialPortInfo:
* Added support of a serial number of the USB device from the Sysfs backend.
* Fixed crash on OSX related to wrong use of QCFString class.
* Added enumeration of on-board types of serial ports (like SOC and OCP)
at using of the Sysfs backend on Linux.
* [QTBUG-40113] Fixed crash related to dynamic udev loading.
- QSerialPort:
* Fixed filtering of custom baud rate on Linux.
* [QTBUG-41295] Now the reading are not stalled on Windows when used the
read buffer with the limited size.
* Now the reading are not stalled on Windows after calling of the clear()
method.
* Now the bytesToWrite() on Windows returns the zero after WriteFile()
was called.
——————————————————我還是分割線——————————————————
重要更新:Qt串口模塊5.5開始不再支持Qt4,所以建議用最後一個版本,qtserialport-opensource-src-5.4.2
下載地址:
http://pan.baidu.com/s/1o6UVlk2百度網盤
http://download.qt.io/official_releases/qt/5.4/5.4.2/submodules/官方
轉載自: https://blog.csdn.net/yuyu414/article/details/42400721