Du kannst nicht mehr als 25 Themen auswählen Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.
 
 
 

147 Zeilen
3.7 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::ReadColiAndReg()
  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. }