Qt 在windows下的串口读写

127 阅读1分钟

在windows下的串口读写

添加serialport模块支持

QT       += core gui serialport

添加头文件

 

#include <QtSerialPort>

创建串口对象

 

private:
    QSerialPort *serial;

创建必要的槽函数

private slots:
    void getData();        // 串口数据接受的槽函数
    void writeData(char *data, qint64 len);    // 数据发送
    void error(QSerialPort::SerialPortError serialPortError); // 操作错误槽函数

端口号获取


    QList<QString> portList;
    portList.clear();
    QList<QSerialPortInfo> serialPortInfo = QSerialPortInfo::availablePorts();
    //搜索串口
    for (int i=0;i<serialPortInfo.length();i++) {
        qDebug() << "Name        : " << serialPortInfo[i].portName();
        qDebug() << "Description : " << serialPortInfo[i].descri