|
- #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<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();
- 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;
- }
- }
-
- int MyModbus::ErrorCheck(QByteArray rev)
- {
- if ((rev.at(1) & 0x80) == 0x80)
- {
- // MODBUS异常响应结构:地址 | 功能码+0x80 | 异常码 | CRC
- quint8 exCode = rev.at(2);
- return exCode;
- }
- else
- {
- return 0;
- }
- }
-
- QVector<bool> MyModbus::AnalReadCoil()
- {
- quint8 byteCount = static_cast<quint8>(receive[2]);
- QVector<bool> coil;
-
- 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()
- {
- int byteCount = receive.at(2);
- QByteArray data = receive.mid(3, byteCount);
-
- QVector<quint16> registers;
- 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;
- }
-
- QVector<bool> MyModbus::AnalReadCoil(QByteArray rev)
- {
- quint8 byteCount = static_cast<quint8>(rev[2]);
- QVector<bool> coil;
-
- for (int byteIndex = 0; byteIndex < byteCount; byteIndex++)
- {
- quint8 byteValue = static_cast<quint8>(rev[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(QByteArray rev)
- {
- int byteCount = rev.at(2);
- QByteArray data = rev.mid(3, byteCount);
-
- QVector<quint16> registers;
- 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;
- }
-
|