You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

147 regels
3.8 KiB

  1. #include "mymodbus.h"
  2. MyModbus::MyModbus()
  3. {
  4. }
  5. void MyModbus::Set(quint16 stationAddress, quint16 functionCode, quint16 startAdress, quint16 length)
  6. {
  7. this->stationAddress_ = stationAddress;
  8. this->functionCode_ = functionCode;
  9. this->startAdress_ = startAdress;
  10. this->length_ = length;
  11. }
  12. void MyModbus::ReadCoilAndReg()
  13. {
  14. sendCommand_.clear();
  15. sendCommand_.append(stationAddress_%256);
  16. sendCommand_.append(functionCode_%256);
  17. sendCommand_.append(startAdress_/256);
  18. sendCommand_.append(startAdress_%256);
  19. sendCommand_.append(length_/256);
  20. sendCommand_.append(length_%256);
  21. quint16 temp = CalculateCrc(sendCommand_);
  22. sendCommand_.append(temp%256);
  23. sendCommand_.append(temp/256);
  24. }
  25. void MyModbus::WriteCoil(QVector<bool> &coils)
  26. {
  27. quint16 coilCount = coils.size();
  28. int byteCount = (coilCount + 7) / 8;
  29. sendCommand_.clear();
  30. sendCommand_.append(stationAddress_%256);
  31. sendCommand_.append(0x0f);
  32. sendCommand_.append(startAdress_/256);
  33. sendCommand_.append(startAdress_%256);
  34. sendCommand_.append(length_/256);
  35. sendCommand_.append(length_%256);
  36. sendCommand_.append(byteCount);
  37. for (int i = 0; i < byteCount; ++i)
  38. {
  39. quint8 byte = 0;
  40. for (int j = 0; j < 8; ++j)
  41. {
  42. int bitIndex = i * 8 + j;
  43. if (bitIndex < coils.size() && coils[bitIndex])
  44. byte |= (1 << j);
  45. }
  46. sendCommand_.append(static_cast<char>(byte));
  47. }
  48. quint16 temp = CalculateCrc(sendCommand_); //计算crc
  49. sendCommand_.append(temp%256); //加入计算的crc值
  50. sendCommand_.append(temp/256);
  51. }
  52. void MyModbus::WriteRegister(QVector<quint16> &values)
  53. {
  54. sendCommand_.clear();
  55. sendCommand_.append(stationAddress_%256);
  56. sendCommand_.append(0x10);
  57. sendCommand_.append(startAdress_/256);
  58. sendCommand_.append(startAdress_%256);
  59. sendCommand_.append(length_/256);
  60. sendCommand_.append(length_%256);
  61. sendCommand_.append(static_cast<char>(values.size() * 2));
  62. for (quint16 v : values)
  63. {
  64. sendCommand_.append(static_cast<char>((v >> 8) & 0xFF));
  65. sendCommand_.append(static_cast<char>(v & 0xFF));
  66. }
  67. quint16 temp = CalculateCrc(sendCommand_); //计算crc
  68. sendCommand_.append(temp%256); //加入计算的crc值
  69. sendCommand_.append(temp/256);
  70. }
  71. QByteArray MyModbus::SendCommand()
  72. {
  73. return sendCommand_;
  74. }
  75. QByteArray MyModbus::Receive(const QByteArray &revMessage)
  76. {
  77. receive_.clear();
  78. if (CrcCheck(revMessage))
  79. {
  80. this->receive_ = revMessage;
  81. }
  82. return receive_;
  83. }
  84. int MyModbus::ErrorCheck()
  85. {
  86. if ((receive_.at(1) & 0x80) == 0x80)
  87. {
  88. // MODBUS异常响应结构:地址 | 功能码+0x80 | 异常码 | CRC
  89. quint8 exCode = receive_.at(2);
  90. return exCode;
  91. }
  92. else
  93. {
  94. return 0;
  95. }
  96. }
  97. QVector<bool> MyModbus::AnalReadCoil()
  98. {
  99. quint8 byteCount = static_cast<quint8>(receive_[2]);
  100. QVector<bool> coil;
  101. for (int byteIndex = 0; byteIndex < byteCount; byteIndex++)
  102. {
  103. quint8 byteValue = static_cast<quint8>(receive_[3 + byteIndex]);
  104. // 解析每个字节的8个位
  105. for (int bitIndex = 0; bitIndex < 8; bitIndex++)
  106. {
  107. int coilIndex = byteIndex * 8 + bitIndex;
  108. if (coilIndex < length_)
  109. {
  110. bool state = byteValue & (1 << bitIndex);
  111. coil.append(state);
  112. }
  113. }
  114. }
  115. return coil;
  116. }
  117. QVector<quint16> MyModbus::AnalReadReg()
  118. {
  119. int byteCount = receive_.at(2);
  120. QByteArray data = receive_.mid(3, byteCount);
  121. QVector<quint16> registers;
  122. for (int i = 0; i < data.size(); i += 2)
  123. {
  124. quint16 value = (static_cast<quint8>(data[i]) << 8) | static_cast<quint8>(data[i+1]);
  125. registers.append(value);
  126. }
  127. return registers;
  128. }