|
- #include "mymodbus.h"
-
-
- MyModbus::MyModbus()
- {
- this->stationAddress_ = 1;
- this->functionCode_ = 0x01;
- this->startAdress_ = 256;
- this->length_ = 1;
- }
-
- 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<bool> &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<char>(byte));
- }
-
- quint16 temp = CalculateCrc(sendCommand_); //计算crc
- sendCommand_.append(temp%256); //加入计算的crc值
- sendCommand_.append(temp/256);
- }
-
- void MyModbus::WriteRegister(QVector<quint16> &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<char>(values.size() * 2));
- for (quint16 v : values)
- {
- sendCommand_.append(static_cast<char>((v >> 8) & 0xFF));
- sendCommand_.append(static_cast<char>(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();
-
- // 最小Modbus响应长度检查
- if(revMessage.size() < 4)
- { // 地址1 + 功能码1 + CRC2
- return receive_;
- }
-
- // 检查站地址匹配
- if(static_cast<quint8>(revMessage[0]) != stationAddress_)
- {
- return receive_;
- }
-
- // CRC校验
- if (!CrcCheck(revMessage))
- {
- return receive_;
- }
-
- this->receive_ = revMessage;
- return receive_;
- }
-
- int MyModbus::ErrorCheck()
- {
- // 异常响应最小长度检查
- if(receive_.size() < 5)
- {
- // 地址1 + 异常功能码1 + 异常码1 + CRC2
- return -1; // 特殊错误码表示协议错误
- }
- if ((receive_.at(1) & 0x80) == 0x80)
- {
- // MODBUS异常响应结构:地址 | 功能码+0x80 | 异常码 | CRC
- quint8 exCode = receive_.at(2);
- return exCode;
- }
- else
- {
- return 0;
- }
- }
-
- QVector<bool> MyModbus::AnalReadCoil()
- {
- QVector<bool> coil;
- if(receive_.size() < 6)
- {
- // 最小长度: 地址1 + 功能码1 + 长度1 + 数据1 + CRC2
- return coil;
- }
-
- quint8 byteCount = static_cast<quint8>(receive_[2]);
-
- for (int byteIndex = 0; byteIndex < byteCount; byteIndex++)
- {
- quint8 byteValue = static_cast<quint8>(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<quint16> MyModbus::AnalReadReg()
- {
- QVector<quint16> registers;
- if(receive_.size() < 7)
- {
- // 最小长度: 地址1 + 功能码1 + 长度1 + 数据2 + CRC2
- return registers;
- }
-
- int byteCount = receive_.at(2);
- QByteArray data = receive_.mid(3, byteCount);
-
- for (int i = 0; i < data.size(); i += 2)
- {
- quint16 value = (static_cast<quint8>(data[i]) << 8) | static_cast<quint8>(data[i+1]);
- registers.append(value);
- }
-
- return registers;
- }
|