#include "mymodbus.h" MyModbus::MyModbus() { } void MyModbus::Set(quint16 stationAddress, quint16 functionCode, quint16 startAdress, quint16 length) { this->stationAddress_ = stationAddress; this->functionCode_ = functionCode; this->startAdress_ = startAdress; this->length_ = length; } void MyModbus::ReadCoilAndReg() { sendCommand_.clear(); sendCommand_.append(stationAddress_%256); sendCommand_.append(functionCode_%256); sendCommand_.append(startAdress_/256); sendCommand_.append(startAdress_%256); sendCommand_.append(length_/256); sendCommand_.append(length_%256); quint16 temp = CalculateCrc(sendCommand_); sendCommand_.append(temp%256); sendCommand_.append(temp/256); } void MyModbus::WriteCoil(QVector &coils) { quint16 coilCount = coils.size(); int byteCount = (coilCount + 7) / 8; sendCommand_.clear(); sendCommand_.append(stationAddress_%256); sendCommand_.append(0x0f); sendCommand_.append(startAdress_/256); sendCommand_.append(startAdress_%256); sendCommand_.append(length_/256); sendCommand_.append(length_%256); sendCommand_.append(byteCount); for (int i = 0; i < byteCount; ++i) { quint8 byte = 0; for (int j = 0; j < 8; ++j) { int bitIndex = i * 8 + j; if (bitIndex < coils.size() && coils[bitIndex]) byte |= (1 << j); } sendCommand_.append(static_cast(byte)); } quint16 temp = CalculateCrc(sendCommand_); //计算crc sendCommand_.append(temp%256); //加入计算的crc值 sendCommand_.append(temp/256); } void MyModbus::WriteRegister(QVector &values) { sendCommand_.clear(); sendCommand_.append(stationAddress_%256); sendCommand_.append(0x10); sendCommand_.append(startAdress_/256); sendCommand_.append(startAdress_%256); sendCommand_.append(length_/256); sendCommand_.append(length_%256); sendCommand_.append(static_cast(values.size() * 2)); for (quint16 v : values) { sendCommand_.append(static_cast((v >> 8) & 0xFF)); sendCommand_.append(static_cast(v & 0xFF)); } quint16 temp = CalculateCrc(sendCommand_); //计算crc sendCommand_.append(temp%256); //加入计算的crc值 sendCommand_.append(temp/256); } QByteArray MyModbus::SendCommand() { return sendCommand_; } QByteArray MyModbus::Receive(const QByteArray &revMessage) { receive_.clear(); if (CrcCheck(revMessage)) { this->receive_ = revMessage; } return receive_; } int MyModbus::ErrorCheck() { if ((receive_.at(1) & 0x80) == 0x80) { // MODBUS异常响应结构:地址 | 功能码+0x80 | 异常码 | CRC quint8 exCode = receive_.at(2); return exCode; } else { return 0; } } QVector MyModbus::AnalReadCoil() { quint8 byteCount = static_cast(receive_[2]); QVector coil; for (int byteIndex = 0; byteIndex < byteCount; byteIndex++) { quint8 byteValue = static_cast(receive_[3 + byteIndex]); // 解析每个字节的8个位 for (int bitIndex = 0; bitIndex < 8; bitIndex++) { int coilIndex = byteIndex * 8 + bitIndex; if (coilIndex < length_) { bool state = byteValue & (1 << bitIndex); coil.append(state); } } } return coil; } QVector MyModbus::AnalReadReg() { int byteCount = receive_.at(2); QByteArray data = receive_.mid(3, byteCount); QVector registers; for (int i = 0; i < data.size(); i += 2) { quint16 value = (static_cast(data[i]) << 8) | static_cast(data[i+1]); registers.append(value); } return registers; }