valve.cpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. #include "valve.h"
  2. #include "Modbus.h"
  3. Valve::Valve(SerialUi* pSerial, int id)
  4. {
  5. m_pSerial = pSerial;
  6. m_id = id;
  7. }
  8. bool Valve::check_crc(QByteArray data)
  9. {
  10. int size = data.size();
  11. unsigned short crc1 =0, crc2= 0;
  12. unsigned char crc_h = data.at(size-2);
  13. unsigned char crc_l = data.at(size-1);
  14. crc1 = (crc_h <<8) |crc_l;
  15. crc2 = CRC16_MODBUS((unsigned char *)data.data(),data.size()-2);
  16. if(crc1 == crc2){
  17. return true;
  18. }else{
  19. return false;
  20. }
  21. }
  22. bool Valve::Open()
  23. {
  24. QByteArray tx_buf;
  25. char zero = 0x00;
  26. tx_buf.append(RELAY_ADDRESS);
  27. if(ALL_VALVE == m_id){
  28. tx_buf.append(0x0F);
  29. tx_buf.append(zero);
  30. tx_buf.append(zero);
  31. tx_buf.append(zero);
  32. tx_buf.append(0x04);
  33. tx_buf.append(0x01);
  34. tx_buf.append(0xFF);
  35. }else{
  36. tx_buf.append(0x05);
  37. tx_buf.append(zero);
  38. tx_buf.append(static_cast<char>(m_id));
  39. tx_buf.append(0xFF);
  40. tx_buf.append(zero);
  41. }
  42. unsigned short crc;
  43. crc=CRC16_MODBUS((unsigned char *)tx_buf.data(),tx_buf.size());
  44. tx_buf.append((crc>>8)&0xff);
  45. tx_buf.append(crc&0xff);
  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. //qDebug("Valve_op ok valve_id[%d], op[%d] ", id, op);
  51. return true;
  52. }else{
  53. qDebug("Valve Open crc failed valve_id[%d]", m_id);
  54. return false;
  55. }
  56. }else{
  57. qDebug("Valve Open no data received valve_id[%d]", m_id);
  58. return false;
  59. }
  60. }
  61. bool Valve::Close()
  62. {
  63. QByteArray tx_buf;
  64. char zero = 0x00;
  65. tx_buf.append(RELAY_ADDRESS);
  66. if(ALL_VALVE == m_id){
  67. tx_buf.append(0x0F);
  68. tx_buf.append(zero);
  69. tx_buf.append(zero);
  70. tx_buf.append(zero);
  71. tx_buf.append(0x04);
  72. tx_buf.append(0x01);
  73. tx_buf.append(zero);
  74. }else{
  75. tx_buf.append(0x05);
  76. tx_buf.append(zero);
  77. tx_buf.append(static_cast<char>(m_id));
  78. tx_buf.append(zero);
  79. tx_buf.append(zero);
  80. }
  81. unsigned short crc;
  82. crc=CRC16_MODBUS((unsigned char *)tx_buf.data(), tx_buf.size());
  83. tx_buf.append((crc>>8)&0xff);
  84. tx_buf.append(crc&0xff);
  85. QByteArray rx_buf;
  86. rx_buf = m_pSerial->serialWriteReponse(tx_buf);
  87. if(rx_buf.size() > 0){
  88. if(check_crc(rx_buf)){
  89. return true;
  90. }else{
  91. qDebug("Valve close crc failed valve_id[%d]", m_id);
  92. return false;
  93. }
  94. }else{
  95. qDebug("Valve close no data received valve_id[%d]", m_id);
  96. return false;
  97. }
  98. }
  99. bool Valve::ReadStatus()
  100. {
  101. QByteArray tx_buf;
  102. char zero = 0x00;
  103. tx_buf.append(RELAY_ADDRESS);
  104. tx_buf.append(0x04);
  105. tx_buf.append(0x03);
  106. tx_buf.append(0xE8);
  107. tx_buf.append(zero);
  108. tx_buf.append(0x14);
  109. unsigned short crc;
  110. crc=CRC16_MODBUS((unsigned char *)tx_buf.data(), tx_buf.size());
  111. tx_buf.append((crc>>8)&0xff);
  112. tx_buf.append(crc&0xff);
  113. QByteArray rx_buf;
  114. rx_buf = m_pSerial->serialWriteReponse(tx_buf);
  115. if(rx_buf.size() > 0){
  116. for(int i=0; i<rx_buf.size(); i++){
  117. qDebug("ReadStatus rx_buf[%d]: 0x%X", i, rx_buf.at(i));
  118. }
  119. if(check_crc(rx_buf)){
  120. return true;
  121. }else{
  122. qDebug("Valve ReadStatus crc failed valve_id[%d]", m_id);
  123. return false;
  124. }
  125. }else{
  126. qDebug("Valve ReadStatus no data received valve_id[%d]", m_id);
  127. return false;
  128. }
  129. }