Qt中添加了QSerialPort類,非常好用,可是由于各種原因,很多人還是要用Qt4,比如我,后來發現官方文檔說QSerialPort支持Qt4,就研究了一下,比較笨,搞了好久才弄好。
官方網站我經常打不開,所以上傳到網盤了。
http://download.qt-PRoject.org/這是官方的,各種版本都有,大家可以自己找找。
http://pan.baidu.com/s/1gdnanOv這是我自己下載的,共享給大家。
首先確認您電腦上有可以正常使用的Qt4版本,我這里用的是Qt4.8.6.
解壓下載的qtserialport-opensource-src-5.3.2.rar,會發現一個qtserialport.pro,打開它。
選擇項目,在構建步驟中選擇“添加構建步驟->Make”,如下圖:

在Make參數這一欄里輸入“install”

搞定,現在點擊項目中的“構建”,就會編譯,然后把QSerialPort庫安裝到你的Qt4里面。

這是Debug版本,然后Release版本也是一樣做的

如果是下載的官方的,可能會有下面的錯誤:(建議用最新的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
——————————————————我是分割線——————————————————
修復了上面的錯誤,官方更新日志:
- [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/官方
|
新聞熱點
疑難解答