問題鎖定
參考網友的思路:
Qt5.9 Modbus request timeout 0x5異常解決
- 網友認為是Qt的bug, 我也認同;
- 網友認為可以更新模塊, 我也認同, 我也編譯了Qt5.15.0的code并成功安裝到Qt5.9.9中進行使用,界面拖動QModbusRTU離線問題解決!
Note: 為什么使用Qt5.15.0, 因為其他更高的版本改動較大,已經更Qt5.9.9差異變大了,移植到Qt5.9.9恐怕會有問題
編譯Qt5.15.0 QSerialbus模塊步驟
1. 下載QtSerialBus 5.15.0 模塊, 只下載模塊就好
https://mirrors.tuna.tsinghua.edu.cn/qt/official_releases/qt/5.15/5.15.0/submodules/
2. 解壓,使用Qt Creator 打開里面的qtserialbus.pro, 點擊編譯, 編譯之后報錯如3
3. 錯誤羅列如下
- Qt::hex 全局替換成 hex
- Qt::endl 全局替換成endl
- Qt:: hex 全局替換成hex
- qmodbustcpclient_p.h
setupTcpSocket() 中 &QAbstractSocket::errorOccurred 改為-》
static_cast<void(QAbstractSocket::*)(QAbstractSocket::SocketError)>(&QAbstractSocket::error)
- qmodbusserver.cpp
增加頭文件
#include <bitset>
QModbusServerPrivate::readBits 函數內
// Using byteCount * 8 so the remaining bits in the last byte are zeroQBitArray bytes(byteCount * 8);address = 0; // The data range now starts with zero.for ( ; address < count; ++address)bytes.setBit(address, unit.value(address));QByteArray payload = QByteArray::fromRawData(bytes.bits(), byteCount);payload.prepend(char(byteCount));return QModbusResponse(request.functionCode(), payload);
替換成
address = 0; // The data range now starts with zero.QVector<quint8> bytes;for (int i = 0; i < byteCount; ++i) {std::bitset<8> byte;// According to the spec: If the returned quantity is not a multiple of eight,// the remaining bits in the final data byte will be padded with zeros.for (int currentBit = 0; currentBit < 8; ++currentBit)byte[currentBit] = unit.value(address++); // The padding happens inside value().bytes.append(static_cast<quint8> (byte.to_ulong()));}return QModbusResponse(request.functionCode(), byteCount, bytes);
4. 最終編譯, 編譯通過, 在項目中添加install指令使模塊安裝到Qt5.9.9中
執行即可, 或創建新的編譯, 最后再檢查是否更新到Qt5.9.9的模塊中了!
如下代表著有新的Qt5.15.0的QSerialbus庫安裝到Qt5.9.9中了
Note: 注意編譯流程和安裝流程是否有錯, 要排錯, 否則不一定完整安裝!