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

cpp 复制代码
#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

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程序之后调用对应的代码就可以控制继电器了

下面是我写的控制代码

cpp 复制代码
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); }");
}

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

相关推荐
用户805533698032 天前
不止三件套:QObject 属性系统全关键字与运行时反射!
c++·qt
xcyxiner2 天前
DicomViewer (vcpkg Windows和ubuntu编译)7
qt
Quz7 天前
QML Hello World 入门示例
qt
xcyxiner10 天前
DicomViewer (dcmtk读取dcm文件)5
qt
xcyxiner10 天前
DicomViewer (后台线程处理文件)4
qt
xcyxiner11 天前
DicomViewer (添加模型类)3
qt
xcyxiner11 天前
DicomViewer (目录调整) 2
qt
xcyxiner12 天前
dcmtk vtk vtk-dicom(gdcm) 编译(debug) v2
qt
LDR00613 天前
Type-C 快充全面升级!LDR6601 赋能个人护理便携电机,重塑剃须刀 / 理发器新体验
c语言·开发语言
雪碧聊技术13 天前
Tree.js是什么?一文讲透
开发语言·javascript·ecmascript