浏览代码

修改发生器接收和发送模块的逻辑!

lwk 4 天之前
父节点
当前提交
add5f0a50c
共有 1 个文件被更改,包括 92 次插入33 次删除
  1. 92 33
      Generator/PSG/CCOS.Dev.Generator.PSG_HR/CCOS.Dev.Generator.PSG_HR.cpp

+ 92 - 33
Generator/PSG/CCOS.Dev.Generator.PSG_HR/CCOS.Dev.Generator.PSG_HR.cpp

@@ -1001,38 +1001,90 @@ RET_STATUS nsGEN::PSGHRDevice::HWSend(const char* strCommand,int lengh, bool reS
 {
 	if (!m_bConnectFlag)
 	{
-		FERROR("==OUT==: not Connect,[{$}] send failed \n", strCommand);
+		FERROR("==OUT==: not Connect, send failed \n");
 		return RET_STATUS::RET_FAILED;
 	}
 
 	if (!m_SCF)  return RET_STATUS::RET_FAILED;
 
-	char strSendCommand[100] = { 0 };
-	int len = strlen(strCommand);
+	// 使用传入的lengh参数,如果为0则用strlen计算
+	int cmdLen = (lengh > 0) ? lengh : strlen(strCommand);
 
+	// 检查缓冲区大小,数据包 = 命令 + ETX(1字节) + CheckSum(1字节)
+	const int maxCmdLen = 256;  // 增大缓冲区以支持更长的命令
+	if (cmdLen > maxCmdLen - 2)
+	{
+		FERROR("Command too long: {$} bytes, max allowed: {$} bytes\n", cmdLen, maxCmdLen - 2);
+		return RET_STATUS::RET_FAILED;
+	}
+
+	char strSendCommand[maxCmdLen] = { 0 };
+
+	// 计算校验和
 	int tmpSum = 0;
-	for (int i = 0; i < len; i++)
+	for (int i = 0; i < cmdLen; i++)
 	{
-		tmpSum += (int)strCommand[i];
+		tmpSum += (unsigned char)strCommand[i];
 	}
 	char checkSum = char(tmpSum + 3);
-	memcpy(strSendCommand, strCommand, len);
-	strSendCommand[len + 0] = 0x03;
-	strSendCommand[len + 1] = checkSum;
-	FINFO("==OUT==: [{$}] \n", strSendCommand);
-	unsigned int retLength;
-	if (m_SCF->Lock(1000) == WAIT_OBJECT_0) {
-		int result = m_SCF->SendPacket(strSendCommand, strlen(strSendCommand), nTimeOut, retLength);
-		m_SCF->Unlock();
-
-		if (result != SCF_SUCCEED) {
-			return RET_STATUS::RET_FAILED;
-		}
+
+	// 构建数据包:命令 + ETX + CheckSum
+	memcpy(strSendCommand, strCommand, cmdLen);
+	strSendCommand[cmdLen] = 0x03;
+	strSendCommand[cmdLen + 1] = checkSum;
+	int totalLen = cmdLen + 2;  // 实际发送的总长度
+
+	// 打印完整数据包的十六进制(含ETX和CheckSum)便于调试
+	std::string hexStr;
+	hexStr.reserve(totalLen * 3);
+	int printLen = std::min(totalLen, 32);  // 最多打印前32字节
+	for (int i = 0; i < printLen; i++) {
+		char buf[4];
+		snprintf(buf, sizeof(buf), "%02X ", (unsigned char)strSendCommand[i]);
+		hexStr += buf;
 	}
-	else {
-		return RET_STATUS::RET_FAILED;
+	if (totalLen > 32) hexStr += "...";
+	FINFO("==OUT== Packet[{$}]: {$}\n", totalLen, hexStr.c_str());
+
+	// 发送数据包,支持重发机制
+	int maxRetry = reSend ? 2 : 1;  // 如果reSend为true,最多重试2次
+	unsigned int retLength = 0;
+
+	for (int retry = 0; retry < maxRetry; retry++)
+	{
+		if (retry > 0)
+		{
+			FWARN("Retry sending packet, attempt {$}/{$}\n", retry + 1, maxRetry);
+			Sleep(100);  // 重试前短暂延时
+		}
+
+		if (m_SCF->Lock(1000) == WAIT_OBJECT_0)
+		{
+			// 使用实际长度totalLen,而不是strlen
+			int result = m_SCF->SendPacket(strSendCommand, totalLen, nTimeOut, retLength);
+			m_SCF->Unlock();
+
+			if (result == SCF_SUCCEED)
+			{
+				if (retry > 0)
+				{
+					FINFO("Send succeeded after {$} retries\n", retry);
+				}
+				return RET_STATUS::RET_SUCCEED;
+			}
+			else
+			{
+				FERROR("SendPacket failed, result={$}, retry={$}/{$}\n", result, retry + 1, maxRetry);
+			}
+		}
+		else
+		{
+			FERROR("Lock failed, retry={$}/{$}\n", retry + 1, maxRetry);
+		}
 	}
-	return RET_STATUS::RET_SUCCEED;
+
+	FERROR("Send failed after {$} attempts\n", maxRetry);
+	return RET_STATUS::RET_FAILED;
 }
 
 void nsGEN::PSGHRDevice::FireNotify(string key, int context)
@@ -1405,7 +1457,6 @@ void nsGEN::PSGHRDevice::OnCallBack()
 		}
 		};
 
-
 	auto HWFLM = [this](const char* value, int length)
 	{
 		assert(value);
@@ -1954,23 +2005,22 @@ void nsGEN::PSGHRDevice::OnCallBack()
 		{
 		    case 1:
 		    	FDEBUG("get Gen Status_1:GENSTATE {$} -> STATUS_INIT", m_DoseUnit.m_GenState->JSGet());
-		    	///*if (m_DoseUnit.m_GenState->Update(nsGEN::AttrKey::GENERATOR_STATUS_INIT))
-		    	//	FireNotify(AttrKey::GENSTATE, m_DoseUnit.m_GenState->JSGet());*/
-		    	break;
-		    case 2:
-		    	FDEBUG("get Gen Status_2:GENSTATE {$} -> STATUS_STANDBY", m_DoseUnit.m_GenState->JSGet());
 				if (m_isFirstHWPhase) {
 					HWSend("RE", 2);
 
 					// HWSend("RR", 2);
 					HWSend("RS", 2);
 
-					HWSend("ET?", 3); 
+					HWSend("ET?", 3);
 
 					HWSend("ST?", 3);
 					m_isFirstHWPhase = false;
 				}
-				
+		    	if (m_DoseUnit.m_GenState->Update(nsGEN::AttrKey::GENERATOR_STATUS_INIT))
+		    		FireNotify(AttrKey::GENSTATE, m_DoseUnit.m_GenState->JSGet());
+		    	break;
+		    case 2:
+		    	FDEBUG("get Gen Status_2:GENSTATE {$} -> STATUS_STANDBY", m_DoseUnit.m_GenState->JSGet());					
 		    	if (m_DoseUnit.m_GenState->Update(nsGEN::AttrKey::GENERATOR_STATUS_STANDBY))
 		    		FireNotify(AttrKey::GENSTATE, m_DoseUnit.m_GenState->JSGet());
 		    	break;
@@ -1978,11 +2028,6 @@ void nsGEN::PSGHRDevice::OnCallBack()
 		    	/*FDEBUG("get Gen Status_3:RAD_OFF(PR0) -> RAD_PREPARE(PR1)");
 		    	if (m_DoseUnit.m_GenSynState->Update(nsGEN::AttrKey::GENERATOR_RAD_PREPARE))
 		    		FireNotify(m_DoseUnit.m_GenSynState->GetKey(), m_DoseUnit.m_GenSynState->JSGet());*/
-				if (m_iLoopTime != PSGHR_LoopExpHBTime) 
-				{
-					FDEBUG("get Gen Status_3:quicken loopTime[{$}]->[{$}]", m_iLoopTime.load(), PSGHR_LoopExpHBTime);
-					m_iLoopTime = PSGHR_LoopExpHBTime;
-				}
 		    	break;
 		    case 4:
 		    	FDEBUG("get Gen Status_4:PREPARE(PR1) -> RAD_READY(PR2)");
@@ -2800,6 +2845,20 @@ void nsGEN::PSGHRDriver::Dequeue(const char* Packet, DWORD Length)
 
 PACKET_RET nsGEN::PSGHRDriver::callbackPackageProcess(const char* RecData, uint32_t nLength, uint32_t& PacketLength)
 {
+	// 输出RecData的十六进制日志
+	/*if (nLength > 0)
+	{
+		std::string hexStr;
+		hexStr.reserve(nLength * 3);
+		char hexBuf[4] = { 0 };
+		for (uint32_t i = 0; i < nLength; i++)
+		{
+			sprintf(hexBuf, "%02X ", (unsigned char)RecData[i]);
+			hexStr += hexBuf;
+		}
+		FINFO("RecData[{$}]: {$}\n", nLength, hexStr.c_str());
+	}*/
+
 	if (nLength < 1)
 	{
 		FINFO("nLength < 1, nLength=={$} \n", nLength);