Qt 談一談串口serialport

前言:

最近一段時間在做有關串口通訊的Qt項目,在ubuntu 和 window兩個平臺上都有接觸。在這中秋佳節即將來臨之際,也得空寫點關於這方面的心得,算是一個記錄筆記吧。

Qt官方有關串口類的介紹,說的很明白,Since: Qt5.1,從5.1版本才引進的QSerialPort 類。

那麼要是qt5版本之前的平臺要怎麼寫串口功能呢?

qt5之前

需要引入第三方qextserialport類,而這對於linux和window平臺又有所不同。

官方地址:https://qextserialport.github.io/

下載地址:https://github.com/qextserialport/qextserialport

window平臺:

在我們的項目中加入:

C文件qextserialbase.cpp         頭文件qextserialbase.h

C文件qextserialport.cpp          頭文件qextserialport.h,

C文件win_qextserialport.cpp   頭文件win_qextserialport.h

使用時:

頭文件聲明類對象
QextSerialPort *myCom;

 打開並配置串口,舉例:

    //選擇串口號
    QString portName = ui->PortNamecomboBox->currentText();
    myCom = new QextSerialPort(portName);

    //串口數據讀取連接
    connect(myCom, SIGNAL(readyRead()), this, SLOT(ReadMyCom()));

    //設置波特率
    myCom->setBaudRate((BaudRateType)ui->BaudRatecomboBox->currentText().toInt());

    //設置數據位
    myCom->setDataBits((DataBitsType)ui->DataBitscomboBox->currentText().toInt());

    //設置校驗位
    switch(ui->ParitycomboBox->currentIndex())
    {
        case 0:
            myCom->setParity(PAR_NONE);
        break;
        case 1:
            myCom->setParity(PAR_ODD);
        break;
        case 2:
            myCom->setParity(PAR_EVEN);
        break;
        default:
            myCom->setParity(PAR_NONE);
            qDebug("set to default : PAR_NONE");
        break;
    }

    //設置停止位
    switch(ui->StopBitscomboBox->currentIndex())
    {
        case 0:
            myCom->setStopBits(STOP_1);
        break;
        case 1:
            myCom->setStopBits(STOP_1_5);
        break;
        case 2:
            myCom->setStopBits(STOP_2);
        break;
        default:
            myCom->setStopBits(STOP_1);
            qDebug("set to default : STOP_1");
        break;
    }

    //設置數據流控制
    myCom->setFlowControl(FLOW_OFF);

    //設置延時
    myCom->setTimeout(TIME_OUT);

    //以可讀寫方式打開串口
    bool flag = myCom->open(QIODevice::ReadWrite);

Linux(ubuntu)平臺:

稍有不同,需將win_qextserialport.cpp和win_qextserialport.h 換爲 posix_qextserialport.cpp和posix_qextserialport.h。

用法類似。比如:

頭文件聲明:

Posix_QextSerialPort *myComA;

打開和配置串口:

#ifdef ARM
    myComA = new Posix_QextSerialPort("/dev/ttymxc4", QextSerialBase::Polling);
#endif
#ifndef ARM
    myComA = new Posix_QextSerialPort("/dev/ttyUSB1", QextSerialBase::Polling);
#endif
    if(!myComA->open(QIODevice::ReadWrite))
    {
        qDebug()<<"Warning : serialPortA open failed.";
    }
    else
        qDebug()<<"Open serialPortA success!";

    myComA->setBaudRate(BAUD57600);
//    myComA->setBaudRate(BAUD115200);
    myComA->setDataBits(DATA_8);
    myComA->setParity(PAR_NONE);
    myComA->setStopBits(STOP_1);
    myComA->setFlowControl(FLOW_OFF);
    myComA->setTimeout(10);

這裏的條件編譯是方便我在ubuntu桌面平臺和ARM嵌入式板端調試,請忽略。

探討兩個平臺不同之處:

1.兩種輪詢模式:

window平臺下支持:QextSerialPort::Polling //異步讀寫   和   QextSerialPort::EventDriven //同步讀寫(接受到數據即讀取)

linux平臺下只支持: QextSerialPort::Polling //異步讀寫(需要開定時器讀取)

2. 支持的波特率不同:

更多詳情請查看:https://qextserialport.github.io/1.2/qextserialport.html#details

qt5之後

Qt官方引入了QSerialPort類,然後使用serialport時就方便多了,一般包括以下幾個步驟:

1. 項目文件.pro,加入serialport模塊:

QT += serialport

2. 自定義類頭文件,按自身需求加入官方庫,如:

#include <QSerialPort>
#include <QSerialPortInfo>

3. 使用時:

先頭文件申明類對象:

QSerialPort *m_serialPort;

後打開和配置串口:

    m_serialPort = new QSerialPort("COM3");

    if(!m_serialPort->open(QIODevice::ReadWrite))//ReadWrite 模式
    {
	qDebug()<<"打開失敗!";
	return;
    }
    else
	qDebug()<<"打開串口成功!";
    m_serialPort->setBaudRate(QSerialPort::Baud115200,QSerialPort::AllDirections);
    m_serialPort->setDataBits(QSerialPort::Data8);             //數據位爲8
    m_serialPort->setFlowControl(QSerialPort::NoFlowControl);  //無流控制
    m_serialPort->setParity(QSerialPort::NoParity);	       //無校驗位
    m_serialPort->setStopBits(QSerialPort::OneStop);           //停止位1

以上僅是個人對qt中串口使用的大概瞭解,各位有補充的歡迎留言指導及斧正,謝謝大家,預祝大家中秋佳節快樂。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章