资 源 简 介
VC串口调试、发送和接收消息数据范例,这里面涉及到线程控制、串口调试、发送消息、接收消息等编写技巧,还有串口读和写操作,独占方式、延时发送消息,设置发送和接收缓冲区大小等,关于一些参数定义和接收数据函数的代码如下:
OVERLAPPED tOverLaped= {0}; //线程函数使用的OVERLAPPED结构
OVERLAPPED wOverLaped = {0}; //写操作使用的OVERLAPPED结构
OVERLAPPED rOverLaped = {0}; //读操作使用的OVERLAPPED结构
BOOL IsFun = TRUE; //线程是否运行
BOOL IsStop = FALSE; //数据是否发送完毕
//自定义消息处理函数,用于接收数据
void CSeriesPortDlg::OnRecieveData(WPARAM wParam, LPARAM lParam)
{
DWORD res,factbyte;
memset(DataBuffer,0,1024); //初始化数据缓冲区
COMSTAT rst;
ClearCommError(hCom,&res,&rst);//清空串口错误标志,记录当前通信状态
rOverLaped.hEvent = CreateEvent(NULL,TRUE,FALSE,NULL); //创建一个事件对象
if (ReadFile(hCom,DataBuffer,rst.cbInQue,&factbyte,&rOverLaped)) //读取数据到缓冲区中
{
DataBuffer[rst.cbInQue]= 0;
IsStop = FALSE;
}
else
{
res = WaitForSingleObject(rOverLaped.hEvent,5000);
}
IsStop = FALSE;
MessageBox(DataBuffer);
}