Qt 与多个485设备同步通讯

由于项目需求,要求用485通讯方式与 4 个激光位移传感器进行通讯,遂写了这样一个示例:

  • 使用的传感器为博亿精科的BL系列激光位移传感器
  • 按照说明书将 4 个激光位移传感器的地址分别设置为:0x01 、0x02 、0x03 、0x04

.pro文件

QT       += core gui
QT       += serialport
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets

CONFIG += c++17

# You can make your code fail to compile if it uses deprecated APIs.
# In order to do so, uncomment the following line.
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000    # disables all the APIs deprecated before Qt 6.0.0

SOURCES += \
    crc.cpp \
    main.cpp \
    mainwindow.cpp \
    rs485com.cpp

HEADERS += \
    crc.h \
    mainwindow.h \
    rs485com.h

FORMS += \
    mainwindow.ui

# Default rules for deployment.
qnx: target.path = /tmp/$${TARGET}/bin
else: unix:!android: target.path = /opt/$${TARGET}/bin
!isEmpty(target.path): INSTALLS += target

 本文福利,莬费领取Qt开发学习资料包、技术视频,内容包括(C++语言基础,Qt编程入门,QT信号与槽机制,QT界面开发-图像绘制,QT网络,QT数据库编程,QT项目实战,QSS,OpenCV,Quick模块,面试题等等)↓↓↓↓↓↓见下面↓↓文章底部点击莬费领取↓↓ 

CRC 校验 crc.h 和 crc.cpp

/// CRC校验码计算
unsigned short CRC16(unsigned char* pBuf, unsigned short len);

/// CRC校验码计算
unsigned short CRC16(unsigned char* pBuf, unsigned short len)
{
    unsigned short uCRC = 0xFFFF;
    int i, j;
    for (i = 0; i < len; i++)
    {
        uCRC ^= (*(pBuf + i));
        for (j = 0; j < 8; j++)
        {
      
  • 1
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
Qt中,可以使用Qt的信号槽机制和QThread类来实现多线程多设备通讯。 首先,需要创建一个QObject子类来作为你的通讯类,这个类负责处理通讯的逻辑。在这个类中,需要定义信号和槽,用来在不同的线程间发送数据和接收数据。 接下来,你需要创建一个QThread子类,用来作为一个线程。在这个线程中,你需要将通讯类的对象移动到这个线程中,并且将通讯类的信号和槽连接到这个线程的信号和槽中。这样,当通讯类的对象发出信号时,这个线程就能够接收到信号并处理数据。 最后,你需要在你的主程序中创建多个QThread对象,并且将每个QThread对象分别连接到不同的设备上。然后,你需要将通讯类的对象移动到每个QThread对象中,并且将通讯类的信号和槽连接到每个QThread对象的信号和槽中。这样,你就可以在多个线程中同时通讯多个设备了。 下面是一个简单的示例代码: ```cpp //通讯类 class Communication : public QObject { Q_OBJECT public: Communication(QObject* parent = nullptr) : QObject(parent) {} signals: void dataReceived(const QByteArray& data); public slots: void sendData(const QByteArray& data) { //发送数据 } }; //线程类 class DeviceThread : public QThread { Q_OBJECT public: DeviceThread(QObject* parent = nullptr) : QThread(parent) { m_communication.moveToThread(this); connect(&m_communication, &Communication::dataReceived, this, &DeviceThread::handleData); } void run() override { //连接设备 } signals: void dataReceived(const QByteArray& data); private slots: void handleData(const QByteArray& data) { emit dataReceived(data); } private: Communication m_communication; }; //主程序 int main(int argc, char* argv[]) { QApplication app(argc, argv); DeviceThread thread1; DeviceThread thread2; //连接设备 Communication communication1; Communication communication2; communication1.moveToThread(&thread1); communication2.moveToThread(&thread2); QObject::connect(&thread1, &DeviceThread::dataReceived, &communication1, &Communication::sendData); QObject::connect(&thread2, &DeviceThread::dataReceived, &communication2, &Communication::sendData); thread1.start(); thread2.start(); return app.exec(); } ``` 在这个示例代码中,我们创建了一个Communication类来处理通讯的逻辑。这个类有一个dataReceived信号,用来在不同的线程间发送数据。我们还创建了一个DeviceThread类,用来作为一个线程。这个类中包含了一个Communication对象,用来处理与设备通讯。在DeviceThread类的构造函数中,我们将Communication对象移动到这个线程中,并且将Communication对象的信号和槽连接到这个线程的信号和槽中。在DeviceThread类的run函数中,我们连接设备,并且在收到数据时发出dataReceived信号。在主程序中,我们创建了两个DeviceThread对象,分别连接到不同的设备上。然后,我们创建了两个Communication对象,并将它们移动到不同的线程中。最后,我们将DeviceThread对象的信号和槽连接到Communication对象的信号和槽中,并启动线程。这样,我们就可以在多个线程中同时通讯多个设备了。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值