123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158 |
- #include "valve.h"
- #include "Modbus.h"
- Valve::Valve(SerialUi* pSerial, int id)
- {
- m_pSerial = pSerial;
- m_id = id;
- }
- bool Valve::check_crc(QByteArray data)
- {
- int size = data.size();
- unsigned short crc1 =0, crc2= 0;
- unsigned char crc_h = data.at(size-2);
- unsigned char crc_l = data.at(size-1);
- crc1 = (crc_h <<8) |crc_l;
- crc2 = CRC16_MODBUS((unsigned char *)data.data(),data.size()-2);
- if(crc1 == crc2){
- return true;
- }else{
- return false;
- }
- }
- bool Valve::Open()
- {
- QByteArray tx_buf;
- char zero = 0x00;
- tx_buf.append(RELAY_ADDRESS);
- if(ALL_VALVE == m_id){
- tx_buf.append(0x0F);
- tx_buf.append(zero);
- tx_buf.append(zero);
- tx_buf.append(zero);
- tx_buf.append(0x04);
- tx_buf.append(0x01);
- tx_buf.append(0xFF);
- }else{
- tx_buf.append(0x05);
- tx_buf.append(zero);
- tx_buf.append(static_cast<char>(m_id));
- tx_buf.append(0xFF);
- tx_buf.append(zero);
- }
- unsigned short crc;
- crc=CRC16_MODBUS((unsigned char *)tx_buf.data(),tx_buf.size());
- tx_buf.append((crc>>8)&0xff);
- tx_buf.append(crc&0xff);
- QByteArray rx_buf;
- rx_buf = m_pSerial->serialWriteReponse(tx_buf);
- if(rx_buf.size() > 0){
- if(check_crc(rx_buf)){
- //qDebug("Valve_op ok valve_id[%d], op[%d] ", id, op);
- return true;
- }else{
- qDebug("Valve Open crc failed valve_id[%d]", m_id);
- return false;
- }
- }else{
- qDebug("Valve Open no data received valve_id[%d]", m_id);
- return false;
- }
- }
- bool Valve::Close()
- {
- QByteArray tx_buf;
- char zero = 0x00;
- tx_buf.append(RELAY_ADDRESS);
- if(ALL_VALVE == m_id){
- tx_buf.append(0x0F);
- tx_buf.append(zero);
- tx_buf.append(zero);
- tx_buf.append(zero);
- tx_buf.append(0x04);
- tx_buf.append(0x01);
- tx_buf.append(zero);
- }else{
- tx_buf.append(0x05);
- tx_buf.append(zero);
- tx_buf.append(static_cast<char>(m_id));
- tx_buf.append(zero);
- tx_buf.append(zero);
- }
- unsigned short crc;
- crc=CRC16_MODBUS((unsigned char *)tx_buf.data(), tx_buf.size());
- tx_buf.append((crc>>8)&0xff);
- tx_buf.append(crc&0xff);
- QByteArray rx_buf;
- rx_buf = m_pSerial->serialWriteReponse(tx_buf);
- if(rx_buf.size() > 0){
- if(check_crc(rx_buf)){
- return true;
- }else{
- qDebug("Valve close crc failed valve_id[%d]", m_id);
- return false;
- }
- }else{
- qDebug("Valve close no data received valve_id[%d]", m_id);
- return false;
- }
- }
- bool Valve::ReadStatus()
- {
- QByteArray tx_buf;
- char zero = 0x00;
- tx_buf.append(RELAY_ADDRESS);
- tx_buf.append(0x04);
- tx_buf.append(0x03);
- tx_buf.append(0xE8);
- tx_buf.append(zero);
- tx_buf.append(0x14);
- unsigned short crc;
- crc=CRC16_MODBUS((unsigned char *)tx_buf.data(), tx_buf.size());
- tx_buf.append((crc>>8)&0xff);
- tx_buf.append(crc&0xff);
- QByteArray rx_buf;
- rx_buf = m_pSerial->serialWriteReponse(tx_buf);
- if(rx_buf.size() > 0){
- for(int i=0; i<rx_buf.size(); i++){
- qDebug("ReadStatus rx_buf[%d]: 0x%X", i, rx_buf.at(i));
- }
- if(check_crc(rx_buf)){
- return true;
- }else{
- qDebug("Valve ReadStatus crc failed valve_id[%d]", m_id);
- return false;
- }
- }else{
- qDebug("Valve ReadStatus no data received valve_id[%d]", m_id);
- return false;
- }
- }
|