18,356
社区成员
发帖
与我相关
我的任务
分享
Handle gMsg_setEvent;
MyMsg g_msg;
sendThread()
{
sendCommand();
}
sendCommand()
{
send("A");//发送A命令
resetEvent(gMsg_setEvent);
DWORD dwTimeStart = GetTickCount();
WaitForSingleObject(g_MsgSetEvent, 3100);
if (GetTickCount() - dwTimeStart > 3000)
{
//Time Out;处理
return false;////命令失败
}
else
{
//正确接收
return true;//命令成功
}
return false;//命令失败
}
ReceiveThread()
{
//初始化消息
InitMsg(g_msg);
//接收消息
ReadMsgFromRS232();
......
//然后向g_msg结构里放入接收的消息。
Setmsg(g_msg);
setevent(g_MsgSetEvent);
}
CRITICAL_SECTION g_cs;
sendCommand()
{
EnterCriticalSection(&g_cs);
bool bSucSendRecv;
send("A");//发送A命令
resetEvent(gMsg_setEvent);
DWORD dwTimeStart = GetTickCount();
WaitForSingleObject(g_MsgSetEvent, 3100);
if (GetTickCount() - dwTimeStart > 3000)
{
//Time Out;处理
bSucSendRecv = false;
}
else
{
//正确接收
//命令成功
bSucSendRecv = true;
}
LeaveCriticalSection(&g_cs);
return bSucSendRecv;
}