目前使用最普遍的串口爲DB9接口,適用於較近距離的通訊。通常小於10米。DB9接口有9個針腳。編程
串口通訊的主要參數以下:ide
A、波特率:衡量通訊速度的參數,表示每秒鐘傳送的bit的個數。例如9600波特表示每秒鐘發送9600個bit。函數
B、數據位:衡量通訊中實際數據位的參數,當計算機發送一個信息包,實際包含的有效數據位個數。ui
C、中止位:用於表示單個包的最後一位。典型的值爲1和2位。編碼
D、奇偶校驗位:串口通訊中一種檢錯方式。經常使用的檢錯方式有:偶、奇校驗。spa
QtSerialPort模塊是QT5中附加模塊的一個模塊,爲硬件和虛擬的串口提供統一的接口。
串口因爲其簡單和可靠,目前在像嵌入式系統、機器人等工業中依舊用得不少。使用QtSerialPort模塊,開發者能夠大大縮短開發串口相關的應用程的週期。線程
Qt SerialPort提供了基本的功能,包括配置、I/O操做、獲取和設置RS-232引腳的信號。對象
Qt SerialPort模塊暫不支持如下特性:
A、終端的特性,例如回顯,控制CR/LF等等
B、文本模式
C、讀或寫操做的超時和延時配置
D、當RS-232引腳信號變化通知
要在應用程序中使用QtSerialPort,須要包括以下的聲明:
#include <QtSerialPort/QtSerialPort>
要連接QtSerialPort模塊,須要在.pro文件中添加以下內容:
QT += serialport接口
QSerialPort提供了訪問串口的接口函數。使用輔助類QSerialPortInfo能夠獲取可用的串口信息。將QSerialPortInfo輔助類對象作爲參數,使用setPort()或setPortName()函數能夠設置要訪問的串口設備。ip
設置好端口後,可使用open()函數以只讀、只寫或讀寫的模式打開使用。
注意,串口使用獨佔方式打開。
使用close()函數關閉串口而且取消IO操做。
串口成功打開後,QSerialPort會嘗試肯定串口的當前配置並初始化。可使用setBaudRate()、setDataBits()、setParity()、setStopBits()和setFlowControl()函數從新配置端口設置。
有一對名爲QSerialPort::dataTerminalReady、QSerialPort::requestToSend的屬性
QSerialPort提供了停止正在調用線程直到信號觸發的一系列函數。這些函數用於阻塞串口。
waitForReadyRead():阻塞調用,直到有新的數據可讀
waitForBytesWritten():阻塞調用,直到數據以及寫入串口
阻塞串口編程與非阻塞串口編程徹底不一樣。阻塞串口不會要求時間循環而且一般會簡化代碼。然而,在GUI程序中,爲了不凍結用戶界面,阻塞串口編程只能用於非GUI線程。
QSerialPort也能使用QTextStream和QDataStream的流操做符。在試圖使用流操做符>>讀時,須要確保有足夠可用的數據。
QSerialPort::QSerialPort(QObject *parent = Q_NULLPTR)
QSerialPort::QSerialPort(const QString &name, QObject *parent = Q_NULLPTR)
QSerialPort::QSerialPort(const QSerialPortInfo &serialPortInfo, QObject *parent = Q_NULLPTR)
構造函數
[virtual] bool QSerialPort::atEnd() const
若是當前沒有數據可讀,返回true
[signal] void QSerialPort::baudRateChanged(qint32 baudRate, QSerialPort::Directions directions)
波特率改變後,信號觸發
[virtual] qint64 QSerialPort::bytesAvailable() const
返回可讀數據的字節數
[virtual] qint64 QSerialPort::bytesToWrite() const
返回可寫數據的字節數
[virtual] void QSerialPort::close()
關閉串口
void QSerialPort::setPort(const QSerialPortInfo &serialPortInfo)
設置串口端口信息爲serialPortInfo
void QSerialPort::setPortName(const QString &name)
設置串口名爲name
QSerialPortInfo類提供已有串口設備的信息。使用QSerialPortInfo類的靜態成員函數生成QSerialPortInfo對象的鏈表。鏈表中的每一個QSerialPortInfo對象表明一個串口,每一個串口可使用端口名、系統定位、描述、製造商查詢。QSerialPortInfo類對象也能夠用作QSerialPort類的setPort()成員函數的參數。
QSerialPortInfo::QSerialPortInfo(const QSerialPort &port)
QSerialPortInfo::QSerialPortInfo(const QString &name)
QSerialPortInfo::QSerialPortInfo(const QSerialPortInfo &other)
構造函數
[static] QList<QSerialPortInfo> QSerialPortInfo::availablePorts()
返回當前系統可用串口的鏈表
QString QSerialPortInfo::description() const
若是串口可用,返回串口的描述信息
bool QSerialPortInfo::hasProductIdentifier() const
若是有一個合法的16位生產碼,返回true
bool QSerialPortInfo::hasVendorIdentifier() const
若是有一個合法的16位製造商編碼,返回true
bool QSerialPortInfo::isBusy() const
若是串口當前正忙,返回true
QString QSerialPortInfo::manufacturer() const
若是串口可用,返回串口的製造商的名字
QString QSerialPortInfo::portName() const
返回串口的名字
quint16 QSerialPortInfo::productIdentifier() const
若是串口可用,返回串口的16位的生產編碼
QString QSerialPortInfo::serialNumber() const
若是串口可用,返回串口的序列號
[static] QList<qint32> QSerialPortInfo::standardBaudRates()
返回目標平臺支持的可用的標準波特率的鏈表
void QSerialPortInfo::swap(QSerialPortInfo &other)
使用other交換QSerialPortInfo對象
QString QSerialPortInfo::systemLocation() const
返回串口的系統位置
quint16 QSerialPortInfo::vendorIdentifier() const
若是串口可用,返回16位的製造商編碼
#include <QCoreApplication> #include <QtSerialPort/QtSerialPort> #include <QList> #include <QDebug> int main(int argc, char *argv[]) { QCoreApplication a(argc, argv); QList<QSerialPortInfo> list = QSerialPortInfo::availablePorts(); qDebug() << "Total number of availiable ports:" << list.count(); foreach(const QSerialPortInfo &serialportinfo, list) { qDebug() << "Port: " << serialportinfo.portName(); qDebug() << "Location: " << serialportinfo.systemLocation(); qDebug() << "Description: " << serialportinfo.description(); qDebug() << "Manufactutor: " << serialportinfo.manufacturer(); qDebug() << "Vendor Indentifier: " << serialportinfo.vendorIdentifier(); qDebug() << "Busy: " << serialportinfo.isBusy(); } return a.exec(); }