| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503 |
- #include "serialinfo.h"
- SerialInfo::SerialInfo()
- {
- }
- void SerialInfo::on_startWork()
- {
- timer = new QTimer(this);
- connect(timer, &QTimer::timeout, this, &SerialInfo::on_Check_PDUStatus);
- timer->start(1000); // Start the timer
- }
- void SerialInfo::on_closeWork()
- {
- if(timer)timer->stop();
- deleteLater();
- }
- void SerialInfo::on_Check_PDUStatus()
- {
- //initPDUStatus();
- //轮询PDU信息
- char OrderArray[8];
- uint16_t crc=0;
- OrderArray[0]=0x01;
- OrderArray[1]=0x03;
- //查询设备类型及通道数
- OrderArray[2] = (_SWITCH_T_AC_TYPE_CHN_INFO >> 8) & 0xFF; // High byte
- OrderArray[3] = _SWITCH_T_AC_TYPE_CHN_INFO & 0xFF; // Low byte
- OrderArray[4]=0x00;
- OrderArray[5]=0x01;
- crc = crc16_ccitt(OrderArray, 6);
- OrderArray[6] = crc & 0xFF; // Low byte
- OrderArray[7] = (crc >> 8) & 0xFF; // High byte
- SendString=on_SendInfo(OrderArray,8);
- QThread::msleep(QUERYSTAMP);
- RecString=on_Rec_PDUStatus();
- on_Parse_PDUStatus(SendString,RecString,_SWITCH_T_AC_TYPE_CHN_INFO);
- //QThread::msleep(QUERYSTAMP);
- //查询L1信息
- OrderArray[2] = (_SWITCH_T_AC_IN_L1_INFO >> 8) & 0xFF; // High byte
- OrderArray[3] = _SWITCH_T_AC_IN_L1_INFO & 0xFF; // Low byte
- OrderArray[4]=0x00;
- OrderArray[5]=0x08;
- crc = crc16_ccitt(OrderArray, 6);
- OrderArray[6] = crc & 0xFF; // Low byte
- OrderArray[7] = (crc >> 8) & 0xFF; // High byte
- SendString=on_SendInfo(OrderArray,8);
- QThread::msleep(QUERYSTAMP);
- RecString=on_Rec_PDUStatus();
- on_Parse_PDUStatus(SendString,RecString,_SWITCH_T_AC_IN_L1_INFO);
- //QThread::msleep(QUERYSTAMP);
- //查询L2信息
- OrderArray[2] = (_SWITCH_T_AC_IN_L2_INFO >> 8) & 0xFF; // High byte
- OrderArray[3] = _SWITCH_T_AC_IN_L2_INFO & 0xFF; // Low byte
- OrderArray[4]=0x00;
- OrderArray[5]=0x08;
- crc = crc16_ccitt(OrderArray, 6);
- OrderArray[6] = crc & 0xFF; // Low byte
- OrderArray[7] = (crc >> 8) & 0xFF; // High byte
- SendString=on_SendInfo(OrderArray,8);
- QThread::msleep(QUERYSTAMP);
- RecString=on_Rec_PDUStatus();
- on_Parse_PDUStatus(SendString,RecString,_SWITCH_T_AC_IN_L2_INFO);
- //QThread::msleep(QUERYSTAMP);
- //查询L3信息
- OrderArray[2] = (_SWITCH_T_AC_IN_L3_INFO >> 8) & 0xFF; // High byte
- OrderArray[3] = _SWITCH_T_AC_IN_L3_INFO & 0xFF; // Low byte
- OrderArray[4]=0x00;
- OrderArray[5]=0x08;
- crc = crc16_ccitt(OrderArray, 6);
- OrderArray[6] = crc & 0xFF; // Low byte
- OrderArray[7] = (crc >> 8) & 0xFF; // High byte
- SendString=on_SendInfo(OrderArray,8);
- QThread::msleep(QUERYSTAMP);
- RecString=on_Rec_PDUStatus();
- on_Parse_PDUStatus(SendString,RecString,_SWITCH_T_AC_IN_L3_INFO);
- //QThread::msleep(QUERYSTAMP);
- //查询相信息
- for(int i=0;i<m_TotalCHTemp;i++)
- {
- OrderArray[2] = ((_SWITCH_T_AC_CH_PHASE_INFO + 30*i)>> 8) & 0xFF; // High byte
- OrderArray[3] = (_SWITCH_T_AC_CH_PHASE_INFO + 30*i) & 0xFF; // Low byte
- OrderArray[4]=0x00;
- OrderArray[5]=0x1E;
- crc = crc16_ccitt(OrderArray, 6);
- OrderArray[6] = crc & 0xFF; // Low byte
- OrderArray[7] = (crc >> 8) & 0xFF; // High byte
- SendString=on_SendInfo(OrderArray,8);
- QThread::msleep(QUERYSTAMP);
- RecString=on_Rec_PDUStatus();
- on_Parse_PDUStatus(SendString,RecString,(_SWITCH_T_AC_CH_PHASE_INFO + 30*i));
- //QThread::msleep(QUERYSTAMP);
- }
- //查询通道状态
- OrderArray[2] = (_SWITCH_T_AC_CH_STATUS >> 8) & 0xFF; // High byte
- OrderArray[3] = _SWITCH_T_AC_CH_STATUS & 0xFF; // Low byte
- OrderArray[4]=0x00;
- OrderArray[5]=0x0E;
- crc = crc16_ccitt(OrderArray, 6);
- OrderArray[6] = crc & 0xFF; // Low byte
- OrderArray[7] = (crc >> 8) & 0xFF; // High byte
- SendString=on_SendInfo(OrderArray,8);
- QThread::msleep(QUERYSTAMP);
- RecString=on_Rec_PDUStatus();
- on_Parse_PDUStatus(SendString,RecString,_SWITCH_T_AC_CH_STATUS);
- //QThread::msleep(QUERYSTAMP);
- //查询modbus传感器
- OrderArray[2] = (_SWITCH_T_AC_SENSOR_INFO >> 8) & 0xFF; // High byte
- OrderArray[3] = _SWITCH_T_AC_SENSOR_INFO & 0xFF; // Low byte
- OrderArray[4]=0x00;
- OrderArray[5]=0x10;
- crc = crc16_ccitt(OrderArray, 6);
- OrderArray[6] = crc & 0xFF; // Low byte
- OrderArray[7] = (crc >> 8) & 0xFF; // High byte
- SendString=on_SendInfo(OrderArray,8);
- QThread::msleep(QUERYSTAMP);
- RecString=on_Rec_PDUStatus();
- on_Parse_PDUStatus(SendString,RecString,_SWITCH_T_AC_SENSOR_INFO);
- //QThread::msleep(QUERYSTAMP);
- //查询干节点传感器
- OrderArray[2] =(_SWITCH_T_AC_SENSOR_INFO1 >> 8) & 0xFF; // High byte
- OrderArray[3] = _SWITCH_T_AC_SENSOR_INFO1 & 0xFF; // Low byte
- OrderArray[4]=0x00;
- OrderArray[5]=0x04;
- crc = crc16_ccitt(OrderArray, 6);
- OrderArray[6] = crc & 0xFF; // Low byte
- OrderArray[7] = (crc >> 8) & 0xFF; // High byte
- SendString=on_SendInfo(OrderArray,8);
- QThread::msleep(QUERYSTAMP);
- RecString=on_Rec_PDUStatus();
- on_Parse_PDUStatus(SendString,RecString,_SWITCH_T_AC_SENSOR_INFO1);
- //QThread::msleep(QUERYSTAMP);
- }
- void SerialInfo::on_Parse_PDUStatus(const QString &Sendstr,const QString &Recstr,uint16_t infoType)
- {
- if(Recstr=="") return;
- if(_SWITCH_T_AC_TYPE_CHN_INFO==infoType)
- {
- QVector<quint16> pRecVector=parseModbusResponse(Recstr,1);
- if(pRecVector.size()<1)
- {
- //m_TotalCHTemp =0;
- // m_PDUType =0;
- return;
- }
- m_TotalCHTemp = pRecVector[0] & 0xFF; // Low byte
- m_PDUType = (pRecVector[0] >> 8) & 0xFF; // High byte
- }
- else if(_SWITCH_T_AC_IN_L1_INFO==infoType)
- {
- QVector<quint16> pRecVector=parseModbusResponse(Recstr,8);
- if(pRecVector.size()<8)
- {
- //m_PDUAllInfo.AllVoltage_L1 = 0;
- //m_PDUAllInfo.AllCurrent_L1 = 0;
- //m_PDUAllInfo.AllPower_L1 = 0;
- //m_PDUAllInfo.AllConsumption_L1 = 0;
- return;
- }
- m_PDUAllInfo.AllVoltage_L1 = ((pRecVector[1] << 16) + pRecVector[0] )/1000.0f; // Low byte
- m_PDUAllInfo.AllCurrent_L1 = ((pRecVector[3] << 16) + pRecVector[2] )/1000.0f; // Low byte
- m_PDUAllInfo.AllPower_L1 = ((pRecVector[5] << 16) + pRecVector[4] )/1000.0f; // Low byte
- m_PDUAllInfo.AllConsumption_L1 = ((pRecVector[7] << 16)+ pRecVector[6])/1000.0f; // Low byte
- }
- else if(_SWITCH_T_AC_IN_L2_INFO==infoType)
- {
- QVector<quint16> pRecVector=parseModbusResponse(Recstr,8);
- if(pRecVector.size()<8)
- {
- //m_PDUAllInfo.AllVoltage_L2 = 0;
- //m_PDUAllInfo.AllCurrent_L2 = 0;
- //m_PDUAllInfo.AllPower_L2 = 0;
- //m_PDUAllInfo.AllConsumption_L2 = 0;
- return;
- }
- m_PDUAllInfo.AllVoltage_L2 = ((pRecVector[1] << 16) + pRecVector[0] )/1000.0f; // Low byte
- m_PDUAllInfo.AllCurrent_L2 = ((pRecVector[3] << 16) + pRecVector[2] )/1000.0f; // Low byte
- m_PDUAllInfo.AllPower_L2 = ((pRecVector[5] << 16) + pRecVector[4] )/1000.0f; // Low byte
- m_PDUAllInfo.AllConsumption_L2 = ((pRecVector[7] << 16)+ pRecVector[6])/1000.0f; // Low byte
- }
- else if(_SWITCH_T_AC_IN_L3_INFO==infoType)
- {
- QVector<quint16> pRecVector=parseModbusResponse(Recstr,8);
- if(pRecVector.size()<8)
- {
- //m_PDUAllInfo.AllVoltage_L3 = 0;
- //m_PDUAllInfo.AllCurrent_L3 = 0;
- //m_PDUAllInfo.AllPower_L3 = 0;
- //m_PDUAllInfo.AllConsumption_L3 = 0;
- return;
- }
- m_PDUAllInfo.AllVoltage_L3 = ((pRecVector[1] << 16) + pRecVector[0] )/1000.0f; // Low byte
- m_PDUAllInfo.AllCurrent_L3 = ((pRecVector[3] << 16) + pRecVector[2] )/1000.0f; // Low byte
- m_PDUAllInfo.AllPower_L3 = ((pRecVector[5] << 16) + pRecVector[4] )/1000.0f; // Low byte
- m_PDUAllInfo.AllConsumption_L3 = ((pRecVector[7] << 16)+ pRecVector[6])/1000.0f; // Low byte
- }
- else if(_SWITCH_T_AC_CH_STATUS==infoType)
- {
- QVector<quint16> pRecVector=parseModbusResponse(Recstr,14);
- if(pRecVector.size()<14)
- {
- //for(int i=0;i<14;i++)
- //{
- // m_PDUCHInfo[i].CHStatus= false; // Low byte
- //}
- return;
- }
- for(int i=0;i<14;i++)
- {
- m_PDUCHInfo[i].CHStatus=pRecVector[i] & 0xFFFF; // Low byte
- }
- }
- else if(_SWITCH_T_AC_SENSOR_INFO==infoType)
- {
- QVector<quint16> pRecVector=parseModbusResponse(Recstr,16);
- if(pRecVector.size()<16)
- {
- //m_PDUAllInfo.Sensor_Temperature = 0;
- //m_PDUAllInfo.Sensor_Humidity = 0;
- //m_PDUAllInfo.Sensor_CO2 = 0;
- //m_PDUAllInfo.Sensor_INTemperature = 0;
- //m_PDUAllInfo.Sensor_OUTTemperature = 0;
- return;
- }
- m_PDUAllInfo.Sensor_Temperature = ((pRecVector[1] << 16) + pRecVector[0] )/1000.0f;
- m_PDUAllInfo.Sensor_Humidity = ((pRecVector[3] << 16) + pRecVector[2] )/1000.0f;
- m_PDUAllInfo.Sensor_LeakFlag=((pRecVector[5] << 16) + pRecVector[4] );
- //m_PDUAllInfo.Sensor_CO2 = ((pRecVector[5] << 16) + pRecVector[4] )/1000.0f;
- m_PDUAllInfo.Sensor_INTemperature = ((pRecVector[9] << 16)+ pRecVector[8])/1000.0f;
- m_PDUAllInfo.Sensor_OUTTemperature = ((pRecVector[13] << 16)+ pRecVector[12])/1000.0f;
- }
- else if(_SWITCH_T_AC_SENSOR_INFO1==infoType)
- {
- QVector<quint16> pRecVector=parseModbusResponse(Recstr,4);
- if(pRecVector.size()<4)
- {
- //m_PDUAllInfo.Sensor_WaterImmersion=false;
- return;
- }
- bool immFlag = pRecVector[0] & 0xFFFF; // Low byte
- m_PDUAllInfo.Sensor_WaterImmersion =!immFlag;
- }
- else
- {
- for(int i=0;i<m_TotalCHTemp;i++)
- {
- if((_SWITCH_T_AC_CH_PHASE_INFO+30*i)==infoType)
- {
- QVector<quint16> pRecVector=parseModbusResponse(Recstr,30);
- if(pRecVector.size()<30)
- {
- //m_PDUCHInfo[i].CHVoltage=0;
- //m_PDUCHInfo[i].CHCurrent=0;
- //m_PDUCHInfo[i].CHPower=0;
- //m_PDUCHInfo[i].CHConsumption=0;
- return;
- }
- // bool L1flag=pRecVector[8] & 0xFFFF; // Low byte
- //bool L2flag=pRecVector[18] & 0xFFFF; // Low byte
- //bool L3flag=pRecVector[28] & 0xFFFF; // Low byte
- float VL1=((pRecVector[1] << 16) + pRecVector[0] )/1000.0f;
- float IL1=((pRecVector[3] << 16) + pRecVector[2] )/1000.0f;
- float PL1=((pRecVector[5] << 16) + pRecVector[4] )/1000.0f;
- float CL1=((pRecVector[7] << 16) + pRecVector[6] )/1000.0f;
- float VL2=((pRecVector[11] << 16) + pRecVector[10] )/1000.0f;
- float IL2=((pRecVector[13] << 16) + pRecVector[12] )/1000.0f;
- float PL2=((pRecVector[15] << 16) + pRecVector[14] )/1000.0f;
- float CL2=((pRecVector[17] << 16) + pRecVector[16] )/1000.0f;
- float VL3=((pRecVector[21] << 16) + pRecVector[20] )/1000.0f;
- float IL3=((pRecVector[23] << 16) + pRecVector[22] )/1000.0f;
- float PL3=((pRecVector[25] << 16) + pRecVector[24] )/1000.0f;
- float CL3=((pRecVector[27] << 16) + pRecVector[26] )/1000.0f;
- if(VL1>=VL2 && VL1>=VL3)
- {
- m_PDUCHInfo[i].CHVoltage=VL1;
- m_PDUCHInfo[i].CHCurrent=IL1;
- m_PDUCHInfo[i].CHPower=PL1;
- m_PDUCHInfo[i].CHConsumption=CL1;
- }
- if(VL2>=VL1 && VL2>=VL3)
- {
- m_PDUCHInfo[i].CHVoltage=VL2;
- m_PDUCHInfo[i].CHCurrent=IL2;
- m_PDUCHInfo[i].CHPower=PL2;
- m_PDUCHInfo[i].CHConsumption=CL2;
- }
- if(VL3>=VL1 && VL3>=VL2)
- {
- m_PDUCHInfo[i].CHVoltage=VL3;
- m_PDUCHInfo[i].CHCurrent=IL3;
- m_PDUCHInfo[i].CHPower=PL3;
- m_PDUCHInfo[i].CHConsumption=CL3;
- }
- break;
- }
- }
- }
- if(m_printflag) emit addPDUInfo(Sendstr, Recstr);
- }
- /*发送数据*/
- QString SerialInfo::on_SendInfo(char* OrderArray,int sendsize)
- {
- if(!m_openflag) return "";
- QString hexString="";
- // 创建一个JNI环境并调用Java静态方法
- QAndroidJniEnvironment env;
- jbyteArray byteArray = env->NewByteArray(sendsize);
- env->SetByteArrayRegion(byteArray, 0, sendsize, reinterpret_cast<const jbyte*>(OrderArray));
- QAndroidJniObject sendbacktring = QAndroidJniObject::callStaticObjectMethod(
- "usb/USBListActivity",
- "writeData",
- "([B)Ljava/lang/String;", // 方法签名:接收字节数组并返回字符串
- byteArray // 参数
- );
- QString recsendinfo=sendbacktring.toString();
- // 发送字节计数并显示
- if(recsendinfo!="")
- {
- QByteArray byteArray = recsendinfo.toLatin1(); // 将 QString 转换为 QByteArray,使用 UTF-8 编码
- for (unsigned char byte : byteArray) {
- // 使用 printf 格式化输出或 qDebug() 输出每个字节的十六进制表示
- hexString += QString("%1 ").arg(static_cast<unsigned char>(byte), 2, 16, QChar('0'));
- }
- }
- return hexString;
- }
- QString SerialInfo::on_Rec_PDUStatus()
- {
- if(!m_openflag) return "";
- /*
- // 如接收成功,会返回接收的字符串,失败返回空字符串
- QString Allrecinfo="";
- for (int i=0;i<10;i++)
- {
- QAndroidJniObject recbacktring = QAndroidJniObject::callStaticObjectMethod(
- "usb/USBListActivity",
- "readData",
- "()Ljava/lang/String;");
- QString recinfo=recbacktring.toString();
- // 发送字节计数并显示
- if(recinfo!="")
- {
- QString hexString;
- QByteArray byteArray = recinfo.toLatin1(); // 将 QString 转换为 QByteArray,使用 UTF-8 编码
- for (char byte : byteArray) {
- // 使用 printf 格式化输出或 qDebug() 输出每个字节的十六进制表示
- hexString += QString("%1 ").arg(static_cast<unsigned char>(byte), 2, 16, QChar('0'));
- }
- Allrecinfo+=hexString;
- }
- else
- {
- break;
- }
- }
- return Allrecinfo;
- */
- // 如接收成功,会返回接收的字符串,失败返回空字符串
- QString Allrecinfo="";
- QAndroidJniObject recbacktring = QAndroidJniObject::callStaticObjectMethod(
- "usb/USBListActivity",
- "readData",
- "()Ljava/lang/String;");
- QString recinfo=recbacktring.toString();
- // 发送字节计数并显示
- if(recinfo!="")
- {
- QString hexString;
- QByteArray byteArray = recinfo.toLatin1(); // 将 QString 转换为 QByteArray,使用 UTF-8 编码
- for (char byte : byteArray) {
- // 使用 printf 格式化输出或 qDebug() 输出每个字节的十六进制表示
- hexString += QString("%1 ").arg(static_cast<unsigned char>(byte), 2, 16, QChar('0'));
- }
- Allrecinfo=hexString;
- }
- else
- {
- }
- return Allrecinfo;
- }
- uint16_t SerialInfo::crc16_ccitt(char* data, size_t length) {
- uint16_t crc = 0xFFFF;
- uint16_t i, j;
- for (i = 0; i < length; i++) {
- crc ^= data[i];
- for (j = 0; j < 8; j++) {
- if (crc & 0x0001) {
- crc >>= 1;
- crc ^= 0xA001;
- } else {
- crc >>= 1;
- }
- }
- }
- // data[7] = crc & 0xFF; // Low byte
- //data[6] = (crc >> 8) & 0xFF; // High byte
- return crc; // No final XOR value for CRC16-CCITT (XMODEM)
- }
- QVector<quint16> SerialInfo::parseModbusResponse(const QString &hexString, int registerCount) {
- QVector<quint16> registers;
- QString TempHexString=hexString;
- // Remove spaces from the input string for easier parsing
- QString cleanHexString = TempHexString.remove(QRegularExpression("\\s"));
- // Verify the length of the cleaned string matches what we expect
- if (cleanHexString.length() != (registerCount * 4 + 5 * 2)) { // +5*2 for the modubus 03 code head 3 byte +tail 2 byte = 5 byte
- qWarning() << "Invalid response length.";
- return registers; // Return empty vector on error
- }
- // Skip the first byte which is the byte count in a Modbus TCP response
- QTextStream stream(&cleanHexString);
- stream.skipWhiteSpace();
- stream.seek(6); // Skip the head byte count
- for (int i = 0; i < registerCount; ++i) {
- QString byteStr = stream.read(4); // Each register is represented by 4 hex chars (2 bytes)
- if (byteStr.length() != 4) {
- qWarning() << "Unexpected end of data at register" << i;
- break;
- }
- // Convert the 4 hex chars into a quint16 value
- bool ok1, ok2;
- quint8 byte1 = byteStr.left(2).toInt(&ok1, 16); // 前两位转换为整数
- quint8 byte2 = byteStr.right(2).toInt(&ok2, 16); // 后两位转换为整数
- quint16 regValue = (byte1 << 8) | byte2;
- registers.append(regValue);
- }
- return registers;
- }
- void SerialInfo::initPDUStatus()
- {
- /*
- m_PDUAllInfo.AllVoltage_L1 =0;
- m_PDUAllInfo.AllCurrent_L1=0;
- m_PDUAllInfo.AllPower_L1=0;
- m_PDUAllInfo.AllConsumption_L1=0;
- m_PDUAllInfo.AllVoltage_L2=0;
- m_PDUAllInfo.AllCurrent_L2=0;
- m_PDUAllInfo.AllPower_L2=0;
- m_PDUAllInfo.AllConsumption_L2=0;
- m_PDUAllInfo.AllVoltage_L3=0;
- m_PDUAllInfo.AllCurrent_L3=0;
- m_PDUAllInfo.AllPower_L3=0;
- m_PDUAllInfo.AllConsumption_L3=0;
- */
- m_PDUAllInfo.Sensor_Temperature=0;
- m_PDUAllInfo.Sensor_Humidity=0;
- m_PDUAllInfo.Sensor_CO2=0;
- m_PDUAllInfo.Sensor_INTemperature=0;
- m_PDUAllInfo.Sensor_OUTTemperature=0;
- m_PDUAllInfo.Sensor_WaterImmersion=false;
- }
|