| 发表于:2007-09-05 20:48:352楼 得分:0 |
晕了,工作者线程是这样的: dword winapi commwatchproc(lpvoid lpparam) { cmodemdlg *pdlg=(cmodemdlg*)lpparam; handle hcomm=pdlg-> hcommdev; //清空串口,并检查串口是否打开 assert(hcomm!=invalid_handle_value); //清空串口 purgecomm(hcomm,purge_rxclear ¦purge_txclear); ::setcommmask(hcomm, ev_rxchar ¦ev_cts ¦ev_dsr);//有哪些串口事件需要监视 //重叠结构 overlapped ol={0}; ol.hevent=::createevent(null,false,false,null); overlapped readol={0}; readol.hevent=::createevent(null,false,false,null); dword dwerrorflags; char buf[1024]; dword dwmask; dword dwbytesread; bool b; while(true) //for(;;) { //等待事件发生 b=::waitcommevent(hcomm,&dwmask,&ol); if(!b) { if(::getlasterror()==error_io_pending) { b=::getoverlappedresult(hcomm,&ol,&dwbytesread,true); } } if(b) { if(dwmask&ev_rxchar) { comstat comstat; dword dwlength; ::clearcommerror(hcomm,&dwerrorflags,&comstat); dwlength=comstat.cbinque; if(dwlength <=0) { continue; } if(dwlength> 0) { // 无限等待wm_commnotify消息被处理完 waitforsingleobject(pdlg-> m_hpostmsgevent, infinite); resetevent(pdlg-> m_hpostmsgevent); b=::readfile(hcomm,buf,dwlength,&dwbytesread,&readol); if(!b) { if(::getlasterror()==error_io_pending) { b=::getoverlappedresult(hcomm,&ol,&dwbytesread,true); } } if(b) { ::postmessage(*pdlg,wm_commnotify,(wparam)hcomm,(long)buf); } } } } } return 0; } 接收到第一组数据就是connect33600之后,就停在 //使m_hpostmsgevent成为有信号,即允许发送下一个wm_commnotify消息 setevent(m_hpostmsgevent); 这个时候就接不到发送过来的消息了,但是我在串口调试工具里做是通的,可以接收到. 实在是不明白了,各位帮忙看看了 | | |
|