#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::ReadColiAndReg() { 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; }