本文會(huì)先介紹 Qt 自帶的串口模塊冷尉,然后引出在 PyQt5 中使用這些模塊的方法但荤。
為了演示類(lèi)和函數(shù)的具體使用方法,我會(huì)在串口上連接一臺(tái) Arduino Uno扬蕊。當(dāng)然也可以使用虛擬串口程序進(jìn)行演示谊囚,但是被虛擬出來(lái)的串口總會(huì)缺少一些信息怕享,所以我還是采用了外接 Arduino 的方法。
系統(tǒng)環(huán)境:Win10 64位
轉(zhuǎn)載請(qǐng)注明出處:http://www.reibang.com/u/5e6f798c903a
1.簡(jiǎn)介
Qt 串口模塊提供的基本功能包括:串口配置镰踏、I/O 操作函筋、獲取和設(shè)置 RS-232 引腳的控制信號(hào)等。該模塊的 C++ 類(lèi) 包含 QSerialPort(用于提供訪問(wèn)串行端口的函數(shù))和 QSerialPortInfo(提供本機(jī)現(xiàn)存串行端口的相關(guān)信息)奠伪,官方同時(shí)也提供了一些應(yīng)用程序示例 跌帐。
本模塊不支持以下項(xiàng)目:
- 終端功能,如 echo 和 CR/LF 控制等
- 文本模式
- 在對(duì)串口進(jìn)行讀取或?qū)懭霑r(shí)绊率,配置超時(shí)和延遲
- 引腳信號(hào)變化通知
在 Qt 應(yīng)用程序中使用串行端口時(shí)谨敛,請(qǐng)?zhí)砑?#include < QSerialPort >
,
對(duì) PyQt5 而言滤否,則是:from PyQt5.QtSerialPort import QSerialPort
脸狸。
在 Qt 應(yīng)用程序中查看本機(jī)現(xiàn)存串口信息時(shí),請(qǐng)?zhí)砑?#include < QSerialPortInfo >
藐俺,
對(duì) PyQt5 而言炊甲,則是:from PyQt5.QtSerialPort import QSerialPortInfo
。
與模塊連接時(shí)欲芹,請(qǐng)將 QT += serialport
添加到 qmake .pro 文件中卿啡,PyQt5 中沒(méi)有類(lèi)似操作。
2.QSerialPortInfo 類(lèi)
官方文檔
Header: #include <QSerialPortInfo>
qmake: QT += serialport
Since: Qt 5.1
該類(lèi)用于提供本機(jī)現(xiàn)有串口的相關(guān)信息菱父。
靜態(tài)公共成員:
QList<QSerialPortInfo> availablePorts()
QList<qint32>standardBaudRates()
2.1 創(chuàng)建 QSerialPortInfo 對(duì)象
- QSerialPortInfo()
構(gòu)造一個(gè)空 QSerialPortInfo 對(duì)象颈娜。如果 QSerialPortInfo 對(duì)象為空,則表示不持有串口定義滞伟,若對(duì)其調(diào)用 isNull() 函數(shù)揭鳞,會(huì)返回True
炕贵。 - QSerialPortInfo(const QSerialPort &port)
使用指定的 QSerialPort 對(duì)象梆奈,構(gòu)建相應(yīng)的 QSerialPortInfo 對(duì)象。 - QSerialPortInfo(const QString &name)
根據(jù)給定的串口名 &name称开,構(gòu)造 QSerialPortInfo 對(duì)象亩钟。
該構(gòu)造器會(huì)在本機(jī)當(dāng)前可用的串口中查找具有相應(yīng)名稱的串口乓梨,并為該端口構(gòu)建 QSerialPortInfo 實(shí)例。 - QSerialPortInfo(const QSerialPortInfo &other)
創(chuàng)建另一個(gè) QSerialPortInfo 對(duì)象的副本清酥。
在 PyQt5 調(diào)用上述語(yǔ)句便可創(chuàng)建相應(yīng)實(shí)例扶镀。
Tips:QSerialPortInfo 實(shí)例對(duì)象可用作 QSerialPort 類(lèi)中 setPort() 方法的輸入?yún)?shù),以此創(chuàng)建 QSerialPort 的實(shí)例焰轻。
class QSerialPortInfo(): # skipped bases: <class 'sip.simplewrapper'>
"""
QSerialPortInfo()
QSerialPortInfo(QSerialPort)
QSerialPortInfo(str)
QSerialPortInfo(QSerialPortInfo)
"""
2.2 交換 QSerialPortInfo 對(duì)象
- void QSerialPortInfo::swap(QSerialPortInfo &other)
如果存在兩個(gè)分別指向不同的 QSerialPortInfo 實(shí)例的變量名臭觉,對(duì)其中一個(gè)實(shí)例調(diào)用swap()
方法,并將另一作為參數(shù)辱志,便可將兩個(gè)變量名所指向的實(shí)例對(duì)象進(jìn)行互換蝠筑。此操作會(huì)非常迅速的執(zhí)行,并且永遠(yuǎn)不會(huì)失敗揩懒。
示例代碼:
from PyQt5.QtSerialPort import QSerialPortInfo
port_Arduino = QSerialPortInfo('COM4')
port_virtual = QSerialPortInfo('COM1')
print(port_Arduino.portName())
print(port_virtual.portName())
port_Arduino.swap(port_virtual)
print(port_Arduino.portName())
print(port_virtual.portName())
輸出:
COM4
COM1
COM1
COM4
2.3 銷(xiāo)毀 QSerialPortInfo 對(duì)象
- ~QSerialPortInfo()
銷(xiāo)毀 QSerialPortInfo 對(duì)象什乙。當(dāng)對(duì)象被銷(xiāo)毀后,便無(wú)法再引用該對(duì)象的值已球。
PyQt5 中沒(méi)有此函數(shù)臣镣,可使用 del
替代。
2.4 查詢可用串口列表
- QList<QSerialPortInfo> QSerialPortInfo::availablePorts() [static]
靜態(tài)函數(shù) availablePorts()
可生成由 QSerialPortInfo 對(duì)象組成的列表智亮。列表中的每個(gè) QSerialPortInfo 對(duì)象都表示一個(gè)獨(dú)立的串行端口忆某,并且可以通過(guò)這些 QSerialPortInfo 對(duì)象查詢相應(yīng)端口的名稱、系統(tǒng)位置鸽素、描述信息及制造商等信息褒繁。
示例代碼:
# python 代碼:
from PyQt5.QtSerialPort import QSerialPortInfo
port_list = QSerialPortInfo.availablePorts()
print(port_list[0].portName())
print(port_list[0].description())
輸出:
COM4
Genuino Uno
2.5 查詢串口信息
描述信息
- QString QSerialPortInfo::description() const
如果存在用于描述串口相關(guān)信息的字符串,則返回該字符串馍忽。否則棒坏,返回空字符串。
示例代碼:
# Python 代碼:
from PyQt5.QtSerialPort import QSerialPortInfo
port_Arduino = QSerialPortInfo('COM3')
print(port_Arduino.description())
輸出:
Genuino Uno
產(chǎn)品識(shí)別碼
-
quint16 QSerialPortInfo::productIdentifier() const
如果被查詢的 QSerialPortInfo 串口實(shí)例存在16-bit
產(chǎn)品編碼遭笋,則返回該編碼坝冕;否則,返回 0瓦呼。
-
bool QSerialPortInfo::hasProductIdentifier() const
如果被查詢的 QSerialPortInfo 串口實(shí)例存在有效的
16-bit
產(chǎn)品編碼喂窟,便會(huì)返回true
;否則央串,返回false
磨澡。
示例代碼:
# Python 代碼:
port_Arduino = QSerialPortInfo('COM3')
print(port_Arduino.hasProductIdentifier())
print(port_Arduino.productIdentifier())
輸出:
True
579
供應(yīng)商識(shí)別碼
-
quint16 QSerialPortInfo::vendorIdentifier() const
如果被查詢的 QSerialPortInfo 串口實(shí)例存在16-bit
的供應(yīng)商識(shí)別碼,則返回該編碼质和;否則稳摄,返回 0。 - bool QSerialPortInfo::hasVendorIdentifier() const
如果被查詢的 QSerialPortInfo 串口實(shí)例存在有效的16-bit
供應(yīng)商編碼饲宿,則返回true
厦酬;否則胆描,返回false
。
示例代碼:
# python 代碼:
port_Arduino = QSerialPortInfo('COM4')
print(port_Arduino.hasVendorIdentifier())
print(port_Arduino.vendorIdentifier())
輸出:
True
9025
制造商
-
QString QSerialPortInfo::manufacturer() const
如果被查詢的 QSerialPortInfo 串口實(shí)例含有制造商字符串仗阅,則返回該字符串昌讲;否則,返回一個(gè)空字符串减噪。
示例代碼:
# python 代碼:
port_Arduino = QSerialPortInfo('COM4')
print(port_Arduino.manufacturer())
輸出:
Arduino LLC (www.arduino.cc)
端口名稱
-
QString QSerialPortInfo::portName() const
返回被查詢的 QSerialPortInfo 串口實(shí)例的名稱短绸。
示例代碼:
# python 代碼:
port_Arduino = QSerialPortInfo('COM4')
print(port_Arduino.portName())
輸出:
COM4
序列號(hào)
-
QString QSerialPortInfo::serialNumber() const
如果被查詢的 QSerialPortInfo 串口實(shí)例存在序列號(hào)字符串,則返回該字符串筹裕;否則鸠按,返回一個(gè)空字符串。
注意:序列號(hào)可能會(huì)包含字母饶碘,該函數(shù)在 Qt 5.3 被引入目尖。
示例代碼:
# python 代碼:
port_Arduino = QSerialPortInfo('COM4')
print(port_Arduino.serialNumber())
輸出:
55438303539351200210
系統(tǒng)位置
-
QString QSerialPortInfo::systemLocation() const
返回被查詢的 QSerialPortInfo 串口實(shí)例的系統(tǒng)位置。
示例代碼:
# python 代碼:
port_Arduino = QSerialPortInfo('COM4')
print(port_Arduino.systemLocation())
輸出:
\\.\COM4
2.6 測(cè)試串口狀態(tài)
- bool QSerialPortInfo::isBusy() const
如果被查詢的 QSerialPortInfo 串口實(shí)例忙扎运,則返回true
瑟曲;否則,返回false
豪治。 - bool QSerialPortInfo::isNull() const
如果被查詢的 QSerialPortInfo 串口實(shí)例持有串口定義洞拨,則返回false
;否則负拟。返回true
烦衣。 - bool QSerialPortInfo::isValid() const
如果被查詢的 QSerialPortInfo 串口實(shí)例存在于本機(jī)上,則返回true
掩浙。否則返回false
花吟。注意,該函數(shù)已經(jīng)被廢棄3ΑP瞥骸!
示例代碼:
# python 代碼:
port_Arduino = QSerialPortInfo('COM4')
print(port_Arduino.isBusy())
print(port_Arduino.isNull())
print(port_Arduino.isValid(),end='\n\n')
port_null = QSerialPortInfo()
print(port_null.isBusy())
print(port_null.isNull())
print(port_null.isValid())
輸出:
False
False
True
False
True
False
2.7 查詢標(biāo)準(zhǔn)波特率列表
- QSerialPortInfo::standardBaudRates() [static]
返回在當(dāng)前系統(tǒng)平臺(tái)上可用的標(biāo)準(zhǔn)波特率列表谬墙。
示例代碼:
from PyQt5.QtSerialPort import QSerialPortInfo
print(QSerialPortInfo.standardBaudRates())
輸出:
[110, 300, 600, 1200, 2400, 4800, 9600, 14400, 19200, 38400, 56000, 57600, 115200, 128000, 256000]
?