25개 이상의 토픽을 선택하실 수 없습니다. Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

201 lines
5.0 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. int MyModbus::ErrorCheck(QByteArray rev)
  98. {
  99. if ((rev.at(1) & 0x80) == 0x80)
  100. {
  101. // MODBUS异常响应结构:地址 | 功能码+0x80 | 异常码 | CRC
  102. quint8 exCode = rev.at(2);
  103. return exCode;
  104. }
  105. else
  106. {
  107. return 0;
  108. }
  109. }
  110. QVector<bool> MyModbus::AnalReadCoil()
  111. {
  112. quint8 byteCount = static_cast<quint8>(receive[2]);
  113. QVector<bool> coil;
  114. for (int byteIndex = 0; byteIndex < byteCount; byteIndex++)
  115. {
  116. quint8 byteValue = static_cast<quint8>(receive[3 + byteIndex]);
  117. // 解析每个字节的8个位
  118. for (int bitIndex = 0; bitIndex < 8; bitIndex++)
  119. {
  120. int coilIndex = byteIndex * 8 + bitIndex;
  121. if (coilIndex < length)
  122. {
  123. bool state = byteValue & (1 << bitIndex);
  124. coil.append(state);
  125. }
  126. }
  127. }
  128. return coil;
  129. }
  130. QVector<quint16> MyModbus::AnalReadReg()
  131. {
  132. int byteCount = receive.at(2);
  133. QByteArray data = receive.mid(3, byteCount);
  134. QVector<quint16> registers;
  135. for (int i = 0; i < data.size(); i += 2)
  136. {
  137. quint16 value = (static_cast<quint8>(data[i]) << 8) | static_cast<quint8>(data[i+1]);
  138. registers.append(value);
  139. }
  140. return registers;
  141. }
  142. QVector<bool> MyModbus::AnalReadCoil(QByteArray rev)
  143. {
  144. quint8 byteCount = static_cast<quint8>(rev[2]);
  145. QVector<bool> coil;
  146. for (int byteIndex = 0; byteIndex < byteCount; byteIndex++)
  147. {
  148. quint8 byteValue = static_cast<quint8>(rev[3 + byteIndex]);
  149. // 解析每个字节的8个位
  150. for (int bitIndex = 0; bitIndex < 8; bitIndex++)
  151. {
  152. int coilIndex = byteIndex * 8 + bitIndex;
  153. if (coilIndex < length)
  154. {
  155. bool state = byteValue & (1 << bitIndex);
  156. coil.append(state);
  157. }
  158. }
  159. }
  160. return coil;
  161. }
  162. QVector<quint16> MyModbus::AnalReadReg(QByteArray rev)
  163. {
  164. int byteCount = rev.at(2);
  165. QByteArray data = rev.mid(3, byteCount);
  166. QVector<quint16> registers;
  167. for (int i = 0; i < data.size(); i += 2)
  168. {
  169. quint16 value = (static_cast<quint8>(data[i]) << 8) | static_cast<quint8>(data[i+1]);
  170. registers.append(value);
  171. }
  172. return registers;
  173. }