Bootstrap

LCUS型 USB继电器模块 电脑串口控制开关 PC智能控制器在pc端使用Qt程序进行串口控制

#include "my_serial.h"  // 或者包含你的主窗口头文件

/*
    * 通讯协议说明:
    * LC USB 开关默认通讯波特率为:9600BPS
    * 打开第一路USB开关:A0 01 01 A2
    * 关闭第一路USB开关:A0 01 00 A1
    * 打开第二路USB开关:A0 02 01 A3
    * 关闭第二路USB开关:A0 02 00 A2
    *
    * LC USB 开关通讯协议
    * 数据(1) --- 启始标识(默认为0xA0)
    * 数据(2) --- 开关地址码(0x01和0x02分别代表第一和第二路开关)
    * 数据(3) --- 操作数据(0x00为“关”,0x01为“开”)
    * 数据(4) --- 校验码
    *
 */

    SerialPortHandler::SerialPortHandler(QObject *parent) : QObject(parent) {

    }

    SerialPortHandler::~SerialPortHandler() {
        if (serial->isOpen()) {
            serial->close();
        }
    }

    QSerialPort * SerialPortHandler::SerialPortInit(){
        serial = new QSerialPort(this);

        connect(serial, &QSerialPort::readyRead, this, &SerialPortHandler::readData);

        // 配置串口参数
        serial->setPortName("COM3");  // 根据实际情况修改串口名称
        serial->setBaudRate(QSerialPort::Baud9600);
        serial->setDataBits(QSerialPort::Data8);
        serial->setParity(QSerialPort::NoParity);
        serial->setStopBits(QSerialPort::OneStop);
        serial->setFlowControl(QSerialPort::NoFlowControl);

        if (!serial->open(QIODevice::ReadWrite)) {
            qDebug() << "Failed to open port" << serial->portName() << ", error:" << serial->errorString();
        } else {
            qDebug() << "Port" << serial->portName() << "opened successfully.";
        }
        return serial;
    }

    void SerialPortHandler::readData() {
        QByteArray data = serial->readAll();
        qDebug() << "Read data:" << data;

        // 示例:回显接收到的数据
        serial->write(data);
    }

    void SerialPortHandler::sendData(const QByteArray &data) {
        if (serial->isOpen()) {
            serial->write(data);
        } else {
            qDebug() << "Port is not open!";
        }
    }

    void SerialPortHandler::sendOpen1() {
        // QByteArray hexString = "A00101A2";
        QByteArray hexData = QByteArray::fromHex("A00101A2");
        if (serial->isOpen()) {
            // serial->write(hexData);
            qint64 bytesWritten = serial->write(hexData);
            if (bytesWritten == -1) {
                qDebug() << "Failed to write data to serial port";
            } else {
                qDebug() << "Data written to serial port successfully";
            }
        } else {
            qDebug() << "Port is not open!";
        }
    }

    void SerialPortHandler::sendClose1() {
        // QByteArray hexString = ;
        QByteArray hexData = QByteArray::fromHex("A00100A1");
        if (serial->isOpen()) {
            qint64 bytesWritten = serial->write(hexData);
            if (bytesWritten == -1) {
                qDebug() << "Failed to write data to serial port";
            } else {
                qDebug() << "Data written to serial port successfully";
            }
        } else {
            qDebug() << "Port is not open!";
        }
    }

    void SerialPortHandler::sendOpen2() {
        QByteArray hexString = "A00201A3";
        QByteArray hexData = QByteArray::fromHex(hexString);
        if (serial->isOpen()) {
            serial->write(hexData);
        } else {
            qDebug() << "Port is not open!";
        }
    }

    void SerialPortHandler::sendClose2() {
        QByteArray hexString = "A00200A2";
        QByteArray hexData = QByteArray::fromHex(hexString);
        if (serial->isOpen()) {
            serial->write(hexData);
        } else {
            qDebug() << "Port is not open!";
        }
    }

my_serial.cpp

#ifndef MY_SERIAL_H
#define MY_SERIAL_H

#include <QCoreApplication>
#include <QSerialPort>
#include <QSerialPortInfo>
#include <QObject>
#include <QDebug>
#include <QByteArray>


class SerialPortHandler : public QObject {
    Q_OBJECT

public:
    // explicit ReceiveThread(QObject *parent = nullptr);
    explicit SerialPortHandler(QObject *parent = nullptr);

    ~SerialPortHandler();

    QSerialPort * SerialPortInit();

    void sendData(const QByteArray &data);

    void sendOpen1();

    void sendClose1();

    void sendOpen2();

    void sendClose2();

public slots:
    void readData();



private:
    QSerialPort *serial;
};
#endif // MY_SERIAL_H

my_serial.h

上面是串口封装代码

同时需要在.pro文件的中添加串口模块:确保在项目文件(.pro)中添加了串口通信相关的运行库。QT += serialport

接下来就是安装驱动程序,这个我推荐看b站up的视频,下面是链接。

CH340串口驱动安装教程-不废话_哔哩哔哩_bilibili

按照链接安装好程序驱动后启动你写的qt程序之后调用对应的代码就可以控制继电器了

下面是我写的控制代码

void MainWindow::on_pushButton_2_clicked()
{
    if(false == power_flag){
        power_on();
        power_flag = true;
    }else if(true == power_flag){
        power_off();
        power_flag = false;
    }
}

void MainWindow::power_on(){
    m_serialSend->sendOpen1();
    m_serialSend->sendOpen2();
    ui->pushButton_2->setStyleSheet("QPushButton { border-radius: 20px; background-color: rgb(144,238,144); }");
}

void MainWindow::power_off(){
    m_serialSend->sendClose2();
    m_serialSend->sendClose1();
    ui->pushButton_2->setStyleSheet("QPushButton { border-radius: 20px; background-color: rgb(255,157,154); }");
}

通过点击电源按钮即可控制电源的通断,同时也可在程序中调用代码进行逻辑上的控制电源通断。

;