ballvalve.cpp 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  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. return true;
  51. }else{
  52. qDebug("SetPosition <<< check crc failed");
  53. g_BallValve_Statistics.CrcErrorCount_increase();
  54. return false;
  55. }
  56. }else{
  57. qDebug("SetPosition <<< no data received use time: %lld ms", timer.elapsed());
  58. g_BallValve_Statistics.TimeoutCount_increase();
  59. return false;
  60. }
  61. }
  62. bool BallValve::GetWorkStatus(unsigned char& status)
  63. {
  64. QByteArray tx_buf;
  65. char zero = 0x00;
  66. tx_buf.append(BALLVALVE_BUS_ADDRESS);
  67. tx_buf.append(0x04);
  68. tx_buf.append(zero);
  69. tx_buf.append(0x01);
  70. tx_buf.append(zero);
  71. tx_buf.append(0x01);
  72. unsigned short crc;
  73. crc=CRC16_MODBUS((unsigned char *)tx_buf.data(),tx_buf.size());
  74. //tx_buf.append(crc&0xff);
  75. tx_buf.append((crc>>8)&0xff);
  76. tx_buf.append(crc&0xff);
  77. g_BallValve_Statistics.ReQuestCount_increase();
  78. QByteArray rx_buf;
  79. rx_buf = m_pSerial->serialWriteReponse(tx_buf);
  80. if(rx_buf.size() > 0){
  81. if(check_crc(rx_buf)){
  82. status = rx_buf.at(4);
  83. return true;
  84. }else{
  85. qDebug("GetWorkStatus <<< check crc failed");
  86. g_BallValve_Statistics.CrcErrorCount_increase();
  87. return false;
  88. }
  89. }else{
  90. qDebug("GetWorkStatus <<< no data received ");
  91. g_BallValve_Statistics.TimeoutCount_increase();
  92. return false;
  93. }
  94. }
  95. bool BallValve::GetPosition(unsigned short& value)
  96. {
  97. QByteArray tx_buf;
  98. char zero = 0x00;
  99. tx_buf.append(BALLVALVE_BUS_ADDRESS);
  100. tx_buf.append(0x04);
  101. tx_buf.append(zero);
  102. tx_buf.append(0x14);
  103. tx_buf.append(zero);
  104. tx_buf.append(0x01);
  105. unsigned short crc;
  106. crc=CRC16_MODBUS((unsigned char *)tx_buf.data(),tx_buf.size());
  107. //tx_buf.append(crc&0xff);
  108. tx_buf.append((crc>>8)&0xff);
  109. tx_buf.append(crc&0xff);
  110. g_BallValve_Statistics.ReQuestCount_increase();
  111. QByteArray rx_buf;
  112. rx_buf = m_pSerial->serialWriteReponse(tx_buf);
  113. if(rx_buf.size() > 0){
  114. if(check_crc(rx_buf)){
  115. unsigned char dath = rx_buf.at(3);
  116. unsigned char datl = rx_buf.at(4);
  117. value = (dath << 8)|datl;
  118. return true;
  119. }else{
  120. qDebug("GetPosition <<< check crc failed");
  121. g_BallValve_Statistics.CrcErrorCount_increase();
  122. return false;
  123. }
  124. }else{
  125. qDebug("GetPosition <<< no data received ");
  126. g_BallValve_Statistics.TimeoutCount_increase();
  127. return false;
  128. }
  129. }
  130. bool BallValve::GetSP(unsigned char& status, unsigned short& value)
  131. {
  132. QByteArray tx_buf;
  133. char zero = 0x00;
  134. tx_buf.append(BALLVALVE_BUS_ADDRESS);
  135. tx_buf.append(0x04);
  136. tx_buf.append(zero);
  137. tx_buf.append(0x52);
  138. tx_buf.append(zero);
  139. tx_buf.append(0x02);
  140. unsigned short crc;
  141. crc=CRC16_MODBUS((unsigned char *)tx_buf.data(),tx_buf.size());
  142. //tx_buf.append(crc&0xff);
  143. tx_buf.append((crc>>8)&0xff);
  144. tx_buf.append(crc&0xff);
  145. g_BallValve_Statistics.ReQuestCount_increase();
  146. QByteArray rx_buf;
  147. rx_buf = m_pSerial->serialWriteReponse(tx_buf);
  148. if(rx_buf.size() > 0){
  149. if(check_crc(rx_buf)){
  150. status = rx_buf.at(4);
  151. unsigned char dath = rx_buf.at(5);
  152. unsigned char datl = rx_buf.at(6);
  153. value = (dath << 8)|datl;
  154. return true;
  155. }else{
  156. qDebug("GetSP <<< check crc failed");
  157. g_BallValve_Statistics.CrcErrorCount_increase();
  158. return false;
  159. }
  160. }else{
  161. qDebug("GetSP <<< no data received ");
  162. g_BallValve_Statistics.TimeoutCount_increase();
  163. return false;
  164. }
  165. }
  166. void BallValve::Dump_Statistics(QString file_path)
  167. {
  168. g_BallValve_Statistics.Save2File(file_path);
  169. }