switch、case语句的问题
switch、case语句:
点击查看代码
int state = 1;
switch(state)
{
case 1:
{
//状态1执行的程序
}
case 2:
{
//状态2执行的程序
}
defalt:
{
//默认执行的程序
}
}
在工作过程当中的一个switch case语句为:
点击查看代码
UINT CCS2X90_HHU_DirectUpgradeDlg::UpgradeThread(LPVOID pParam)
{
std::string logFilePath = "C:\\Users\\Administrator\\Desktop\\项目文件\\BootLoader 错误注入测试项目\\上位机源码\\CS2X90_HHU_DirectUpgrade\\Fault_Injection_Test_Log.txt";
std::ofstream logFile(logFilePath);
if (!logFile.is_open())
{
std::cerr << "无法打开日志文件: " << logFilePath.c_str() << std::endl;
}
TeeStream teeStream(std::cout, logFile);
BOOL m_IsUpgrading = TRUE;
UpgradeState_For_HHU_Device state = HHU_COM_TEST_STAGE_GET_BIOS;
BYTE Cmd[10] = { 0 };
BYTE rbuf[50] = { 0 };
BYTE UpgradeStep = 0;
UINT16 rcr_result = 0;
UINT rLen = 0, tryCnt = 0;
UINT preTime = 0;
CString VersionStr = "";
UINT32 frameSending = 0;
UINT8 Data[258] = { 0 };
BOOL CS3290_rflag = FALSE;
CCS2X90_HHU_DirectUpgradeDlg* pInstance = (CCS2X90_HHU_DirectUpgradeDlg*)pParam;
while (m_IsUpgrading)
{
//std::cout << "开始外循环" << std::endl;
//用于检测串口设备是否开启,只会在程序开始前执行一次,之后程序就会卡在while (m_ThreadRunFlag)当中了
if (m_ThreadRunFlag)
{
std::cout << "正在检测串口设备是否开启" << std::endl;
if (!pInstance->m_ComDevice.OpenDevice(pInstance->m_PortName, 115200))
{
std::cout << "串口设备开启失败" << std::endl;
pInstance->AddToInfOut(pInstance->m_Info.OpenDeviceErrorInfo());
pInstance->GetDlgItem(IDC_COMBO_PORTNAME)->EnableWindow(TRUE);
pInstance->GetDlgItem(IDC_BUTTON_UPGRADE)->EnableWindow(TRUE);
return 0;
}
std::cout << "串口设备开启成功" << std::endl;
}
//开始进行升级循环
while (m_ThreadRunFlag)
{
//std::cout << "开始内循环" << std::endl;
//只要任意条件满足,则进行读取设备上发数据的操作
//在此处不断的读取数据到rbuf当中,用于后续的判断
if ((m_IsCS3290 == FALSE) || (CS3290_rflag == TRUE))
{
memset(rbuf, 0, sizeof(rbuf));
do
{
std::cout << "读取设备返回数据" << std::endl;
rLen = pInstance->m_ComDevice.ReadDevice(rbuf, 1);
if ((UpgradeStep == 6) && (((GetTickCount() - preTime) > 30000) || (rLen != 0)))
break;
} while (UpgradeStep == 6);
}
//如果任意条件都不满足
else
{
if (UpgradeStep == 0)
{
std::cout << "正在判断上位机控件选择的升级模式" << std::endl;
if (((CButton*)pInstance->GetDlgItem(IDC_RADIO_LOADER))->GetCheck())
{
Cmd[0] = RS232_UPGRADE_HHUINT_LDR;
std::cout << "选择了Lodaer升级模式" << std::endl;
}
else if (((CButton*)pInstance->GetDlgItem(IDC_RADIO_APP))->GetCheck())
{
Cmd[0] = RS232_UPGRADE_HHUINT_APP;
std::cout << "选择了APP升级模式" << std::endl;
}
std::cout << "发送升级模式指令" << std::endl;
pInstance->m_ComDevice.WriteDevice(Cmd, 1);
UpgradeStep = 1;
CS3290_rflag = TRUE;
continue;
}
}
//程序状态进行回滚,将升级流程恢复到初始状态
if (m_IsCS3290 == FALSE && pInstance->m_AutoUpgrade == FALSE && flag == TRUE && UpgradeStep == 0)
{
std::cout << "程序状态进行回滚,将升级流程恢复到初始状态" << std::endl;
flag = FALSE;
pInstance->GetDlgItem(IDC_COMBO_PORTNAME)->EnableWindow(TRUE);
pInstance->GetDlgItem(IDC_BUTTON_UPGRADE)->EnableWindow(TRUE);
pInstance->m_File.CloseFile();
pInstance->m_ComDevice.SetBaudRate(115200);
pInstance->m_ComDevice.CloseDevice();
return 0;
}
switch (state)
{
//获取BIOS信息
case HHU_COM_TEST_STAGE_GET_BIOS:
{
std::cout << "开始进行获取BIOS的阶段" << std::endl;
switch (rbuf[0])
{
case RS232_UPGRADE_RQ: // 处理升级请求, //最开始的升级步骤为0
{
if (UpgradeStep == 0)
{
std::cout << "收到了“0x05”且升级流程状态为0" << std::endl;
std::cout << "发送“0x06”,用以回应设备" << std::endl;
Cmd[0] = RS232_UPGRADE_CF;
pInstance->m_ComDevice.WriteDevice(Cmd, 1);
Sleep(500);
std::cout << "开始根据上位机选择的升级模式发送对应的指令" << std::endl;
if (((CButton*)pInstance->GetDlgItem(IDC_RADIO_LOADER))->GetCheck())
{
Cmd[0] = RS232_UPGRADE_HHUINT_LDR;
std::cout << "选择Loader模式,发送“0xAA”" << std::endl;
}
else if (((CButton*)pInstance->GetDlgItem(IDC_RADIO_APP))->GetCheck())
{
Cmd[0] = RS232_UPGRADE_HHUINT_APP;
std::cout << "选择APP模式,发送“0xBB”" << std::endl;
}
pInstance->m_ComDevice.WriteDevice(Cmd, 1);
if ((pInstance->m_AutoUpgrade) && (m_IsCS3290 == FALSE))
{
pInstance->SetDlgItemText(IDC_EDIT_DISPLAY, "");
pInstance->m_UpgradeProgress.SetPos(0);
pInstance->m_UpgradeProgress.SetBarColor(RGB(0, 0, 255));
}
//设置升级步骤1
UpgradeStep = 1;
std::cout << "设置升级步骤1" << std::endl;
}
break;
}
case RS232_UPGRADE_ACK: // 处理升级应答
{
switch (UpgradeStep)
{
case 1:
{
if (((CButton*)pInstance->GetDlgItem(IDC_RADIO_APP))->GetCheck())
{
Sleep(500);
}
std::cout << "发送BIOS版本信息查询指令“0x30”" << std::endl;
Cmd[0] = RS232_REQ_VER;
pInstance->m_ComDevice.WriteDevice(Cmd, 1);
rLen = pInstance->m_ComDevice.ReadDevice(rbuf, 30);
VersionStr += pInstance->m_Info.BiosVersionInfo();
std::cout << "开始接受BIOS信息" << std::endl;
for (UINT i = 0; i < rLen; i++)
{
VersionStr += rbuf[i];
}
std::cout << "BIOS" << (LPCSTR)VersionStr << std::endl;
std::cout << "-----------------------------------------------------------------------------------------------------------------------------------------------------------" << std::endl;
std::cout << "" << std::endl;
teeStream << "【Bootloader Upgrade(Fault Injection Test)测试】" << std::endl;
teeStream << "无线串口RS232快速升级" << std::endl;
teeStream << "BIOS" << (LPCSTR)VersionStr << std::endl;
teeStream << "-----------------------------------------------------------------------------------------------------------------------------------------------------------" << std::endl;
teeStream << "" << std::endl;
pInstance->AddToInfOut(VersionStr + "\r\n");
UpgradeStep = 0;
state = HHU_COM_NORMAL_STAGE_1;
std::cout << "state状态变化为HHU_COM_NORMAL_STAGE_1" << std::endl;
Sleep(20000);
break;
}
}
break;
}
default: // 当 rbuf[0] 不匹配任何 case 时执行
{
std::cout << "未收到预期的返回数据,保持状态为 HHU_COM_TEST_STAGE_GET_BIOS" << std::endl;
state = HHU_COM_TEST_STAGE_GET_BIOS;
break;
}
} //end of "switch (rbuf[0])"
break;
}//end of "case HHU_COM_TEST_STAGE_GET_BIOS:"
//明明没有满足这个条件,但是还是会自动执行到HHU_COM_TEST_STAGE_1这个状态
//测试阶段1,此处属于握手阶段的异常测试项,发送“0xFF”,等待2分钟,只要设备没有任何响应就是符合预期的现象。此时通过上位机窗口提升用户重启设备进行之后的测试
case HHU_COM_TEST_STAGE_1:
{
std::cout << "测试阶段1:握手阶段异常测试,发送“0xFF”,等待2分钟,只要设备没有任何响应就是符合预期的现象。此时通过上位机窗口提升用户重启设备进行之后的测试" << std::endl;
}
//正常阶段1,一直顺序执行,直到测试项来到设置设备的波特率
case HHU_COM_NORMAL_STAGE_1:
{
std::cout << "正常阶段1" << std::endl;
}
} //end of switch
} //end of "while (m_ThreadRunFlag)" 内循环
}//end of "while (m_IsUpgrading)" 外循环
return 0;
}
其中case HHU_COM_TEST_STAGE_GET_BIOS:
中的switch在实际编写的过程当中出了问题,如果需要跳出这个case语句,其中的break需要放在case语句最外层括号的内部,具体如下:
点击查看代码
//获取BIOS信息
case HHU_COM_TEST_STAGE_GET_BIOS:
{
std::cout << "开始进行获取BIOS的阶段" << std::endl;
switch (rbuf[0])
{
case RS232_UPGRADE_RQ: // 处理升级请求, //最开始的升级步骤为0
{
break;
}
case RS232_UPGRADE_ACK: // 处理升级应答
{
switch (UpgradeStep)
{
case 1:
{
break;
}
}
break;
}
default: // 当 rbuf[0] 不匹配任何 case 时执行
{
std::cout << "未收到预期的返回数据,保持状态为 HHU_COM_TEST_STAGE_GET_BIOS" << std::endl;
state = HHU_COM_TEST_STAGE_GET_BIOS;
break;
}
} //end of "switch (rbuf[0])"
break;
}//end of "case HHU_COM_TEST_STAGE_GET_BIOS:"
这样才是正确的,即break放在case语句右括号之前