ballvalve.cpp 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. #include "ballvalve.h"
  2. #include "Modbus.h"
  3. #include <QTimer>
  4. #include <QElapsedTimer>
  5. #include "ComStatistics.h"
  6. static ComStatistics g_BallValve_Statistics("BallValve");
  7. BallValve::BallValve(SerialUi* pSerial)
  8. {
  9. m_pSerial = pSerial;
  10. }
  11. bool BallValve::check_crc(QByteArray data)
  12. {
  13. int size = data.size();
  14. unsigned short crc1 =0, crc2= 0;
  15. unsigned char crc_h = data.at(size-2);
  16. unsigned char crc_l = data.at(size-1);
  17. crc1 = (crc_h <<8) |crc_l;
  18. crc2 = CRC16_MODBUS((unsigned char *)data.data(),data.size()-2);
  19. if(crc1 == crc2){
  20. return true;
  21. }else{
  22. return false;
  23. }
  24. }
  25. //设置阀门的开度值 A (0--1000) 表示:(A/10)%
  26. bool BallValve::SetPosition(unsigned short value)
  27. {
  28. QByteArray tx_buf;
  29. char zero = 0x00;
  30. QElapsedTimer timer;
  31. timer.start();
  32. tx_buf.append(BALLVALVE_BUS_ADDRESS);
  33. tx_buf.append(0x06);
  34. tx_buf.append(zero);
  35. tx_buf.append(0x02);
  36. char c_high = (value>>8)&0xff;
  37. char c_low = (value)&0xff;
  38. tx_buf.append(c_high);
  39. tx_buf.append(c_low);
  40. unsigned short crc;
  41. crc=CRC16_MODBUS((unsigned char *)tx_buf.data(),tx_buf.size());
  42. //tx_buf.append(crc&0xff);
  43. tx_buf.append((crc>>8)&0xff);
  44. tx_buf.append(crc&0xff);
  45. g_BallValve_Statistics.ReQuestCount_increase();
  46. QByteArray rx_buf;
  47. rx_buf = m_pSerial->serialWriteReponse(tx_buf);
  48. if(rx_buf.size() > 0){
  49. if(check_crc(rx_buf)){
  50. g_BallValve_Statistics.Update_Recvtime(timer.elapsed());
  51. return true;
  52. }else{
  53. qDebug("SetPosition <<< check crc failed");
  54. g_BallValve_Statistics.CrcErrorCount_increase();
  55. return false;
  56. }
  57. }else{
  58. qDebug("SetPosition <<< no data received use time: %lld ms", timer.elapsed());
  59. g_BallValve_Statistics.TimeoutCount_increase();
  60. return false;
  61. }
  62. }
  63. bool BallValve::GetWorkStatus(unsigned char& status)
  64. {
  65. QByteArray tx_buf;
  66. QElapsedTimer timer;
  67. timer.start();
  68. char zero = 0x00;
  69. tx_buf.append(BALLVALVE_BUS_ADDRESS);
  70. tx_buf.append(0x04);
  71. tx_buf.append(zero);
  72. tx_buf.append(0x01);
  73. tx_buf.append(zero);
  74. tx_buf.append(0x01);
  75. unsigned short crc;
  76. crc=CRC16_MODBUS((unsigned char *)tx_buf.data(),tx_buf.size());
  77. //tx_buf.append(crc&0xff);
  78. tx_buf.append((crc>>8)&0xff);
  79. tx_buf.append(crc&0xff);
  80. g_BallValve_Statistics.ReQuestCount_increase();
  81. QByteArray rx_buf;
  82. rx_buf = m_pSerial->serialWriteReponse(tx_buf);
  83. if(rx_buf.size() > 0){
  84. if(check_crc(rx_buf)){
  85. status = rx_buf.at(4);
  86. g_BallValve_Statistics.Update_Recvtime(timer.elapsed());
  87. return true;
  88. }else{
  89. qDebug("GetWorkStatus <<< check crc failed");
  90. g_BallValve_Statistics.CrcErrorCount_increase();
  91. return false;
  92. }
  93. }else{
  94. qDebug("GetWorkStatus <<< no data received ");
  95. g_BallValve_Statistics.TimeoutCount_increase();
  96. return false;
  97. }
  98. }
  99. bool BallValve::GetPosition(unsigned short& value)
  100. {
  101. QByteArray tx_buf;
  102. QElapsedTimer timer;
  103. timer.start();
  104. char zero = 0x00;
  105. tx_buf.append(BALLVALVE_BUS_ADDRESS);
  106. tx_buf.append(0x04);
  107. tx_buf.append(zero);
  108. tx_buf.append(0x14);
  109. tx_buf.append(zero);
  110. tx_buf.append(0x01);
  111. unsigned short crc;
  112. crc=CRC16_MODBUS((unsigned char *)tx_buf.data(),tx_buf.size());
  113. //tx_buf.append(crc&0xff);
  114. tx_buf.append((crc>>8)&0xff);
  115. tx_buf.append(crc&0xff);
  116. g_BallValve_Statistics.ReQuestCount_increase();
  117. QByteArray rx_buf;
  118. rx_buf = m_pSerial->serialWriteReponse(tx_buf);
  119. if(rx_buf.size() > 0){
  120. if(check_crc(rx_buf)){
  121. unsigned char dath = rx_buf.at(3);
  122. unsigned char datl = rx_buf.at(4);
  123. value = (dath << 8)|datl;
  124. g_BallValve_Statistics.Update_Recvtime(timer.elapsed());
  125. return true;
  126. }else{
  127. qDebug("GetPosition <<< check crc failed");
  128. g_BallValve_Statistics.CrcErrorCount_increase();
  129. return false;
  130. }
  131. }else{
  132. qDebug("GetPosition <<< no data received ");
  133. g_BallValve_Statistics.TimeoutCount_increase();
  134. return false;
  135. }
  136. }
  137. bool BallValve::GetSP(unsigned char& status, unsigned short& value)
  138. {
  139. QByteArray tx_buf;
  140. QElapsedTimer timer;
  141. timer.start();
  142. char zero = 0x00;
  143. tx_buf.append(BALLVALVE_BUS_ADDRESS);
  144. tx_buf.append(0x04);
  145. tx_buf.append(zero);
  146. tx_buf.append(0x52);
  147. tx_buf.append(zero);
  148. tx_buf.append(0x02);
  149. unsigned short crc;
  150. crc=CRC16_MODBUS((unsigned char *)tx_buf.data(),tx_buf.size());
  151. //tx_buf.append(crc&0xff);
  152. tx_buf.append((crc>>8)&0xff);
  153. tx_buf.append(crc&0xff);
  154. g_BallValve_Statistics.ReQuestCount_increase();
  155. QByteArray rx_buf;
  156. rx_buf = m_pSerial->serialWriteReponse(tx_buf);
  157. if(rx_buf.size() > 0){
  158. if(check_crc(rx_buf)){
  159. status = rx_buf.at(4);
  160. unsigned char dath = rx_buf.at(5);
  161. unsigned char datl = rx_buf.at(6);
  162. value = (dath << 8)|datl;
  163. g_BallValve_Statistics.Update_Recvtime(timer.elapsed());
  164. return true;
  165. }else{
  166. qDebug("GetSP <<< check crc failed");
  167. g_BallValve_Statistics.CrcErrorCount_increase();
  168. return false;
  169. }
  170. }else{
  171. qDebug("GetSP <<< no data received ");
  172. g_BallValve_Statistics.TimeoutCount_increase();
  173. return false;
  174. }
  175. }
  176. void BallValve::Dump_Statistics(QString file_path)
  177. {
  178. g_BallValve_Statistics.Save2File(file_path);
  179. }