串口异步读写问题

ameliawuxi 2008-07-21 05:05:50
DWORD WINAPI SerialPort1ThreadProcess( HWND hWnd) //主窗口句柄
{
DWORD dwEvtMask = 0;
OVERLAPPED osRead = {0};
CSerialPortAPIDlg *pdlg = (CSerialPortAPIDlg*)CWnd::FromHandle(hWnd);
WaitCommEvent(pdlg->hCom, &dwEvtMask, &osRead);
SetCommMask(pdlg->hCom, EV_RXCHAR| EV_TXEMPTY);
if((dwEvtMask&EV_RXCHAR)==EV_RXCHAR)
{
COMSTAT comstat;
DWORD dwlength;
DWORD dwbytesread; //读取的字节数
DWORD dwerror;
DWORD *lpRead=NULL;
DWORD dwerrorflags;
ClearCommError(pdlg->hCom, &dwerrorflags, &comstat);
dwlength = comstat.cbInQue;
if(dwlength>0)
{
BOOL freadstat;
freadstat = ReadFile(pdlg->hCom, lpRead, dwlength, &dwbytesread, &osRead);
if(! freadstat)
{
if(GetLastError()==ERROR_IO_PENDING)
{
while(!GetOverlappedResult(pdlg->hCom, &osRead, &dwbytesread, true))
{
dwerror=GetLastError();
if(dwerror==ERROR_IO_INCOMPLETE)
continue;
::PostMessage((HWND)hWnd, COM_RECVDATA, (unsigned int)lpRead, dwbytesread);
}
}
}
}
}
return true;
}


初学,现在程序编译通过,但是读写还是不成功,郁闷,因为比较急就请大家帮下忙,谢谢!
...全文
113 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
ameliawuxi 2008-08-02
  • 打赏
  • 举报
回复
我现在没有用线程来写,读数据是没有问题,但是发数据通不过,明明缓存中已经有要发送数据了,就是写不出去,不知道是什么原因。大家帮忙看一下!
ameliawuxi 2008-08-02
  • 打赏
  • 举报
回复
哪位好心人,赐教一下啦,急
ameliawuxi 2008-07-31
  • 打赏
  • 举报
回复
请问异步通信能不能不用线程做的啊,我初学,感觉有点复杂!
mybabyanddear 2008-07-23
  • 打赏
  • 举报
回复
没时间细讲,我监测程序串口通信的主驱部分,自己看
mybabyanddear 2008-07-23
  • 打赏
  • 举报
回复
// 线程主执行体
DWORD __thread_serialport_workproc_main(LPVOID lParam)
{
if( lParam == NULL )
{
return -1;
}
CSerialPort::CtlParam& ctp = *( (CSerialPort::CtlParam*)lParam );
CSerialPort::IoParam iop;

BOOL bThreadExit = FALSE;
DWORD dwEvtMask;

iop.pIoOwner = ctp.pOwner;
iop.hIo = ctp.hComm;
iop.rbuf.cbBuf = 0;
iop.rbuf.len = 0;
iop.rbuf.pbuf = NULL;
iop.wbuf.cbBuf = 0;
iop.wbuf.len = 0;
iop.wbuf.pbuf = NULL;

iop.eol.hEvent = CreateEvent( NULL, TRUE, FALSE, NULL );
iop.eol.Internal = 0;
iop.eol.InternalHigh = 0;
iop.eol.Offset = 0;
iop.eol.OffsetHigh = 0;

iop.rol.hEvent = CreateEvent( NULL, TRUE, FALSE, NULL );
iop.rol.Internal = 0;
iop.rol.InternalHigh = 0;
iop.rol.Offset = 0;
iop.rol.OffsetHigh = 0;

HANDLE evts_driver[] =
{
ctp.hEvtExitThread, // 线程退出
ctp.hEvtDrivThread // 驱动运行
};

HANDLE evts_transp[] =
{
ctp.hEvtExitThread, // 线程退出
iop.eol.hEvent, // 串口重叠
iop.rol.hEvent // 读重叠
};

while( !bThreadExit )
{
{// 线程主驱动控制
DWORD dwRes = WaitForMultipleObjects( 2, evts_driver, FALSE, INFINITE );
ResetEvent( evts_transp[ dwRes - WAIT_OBJECT_0 ] );
if( dwRes == WAIT_OBJECT_0 )
{
bThreadExit = FALSE;
continue; // break;
}

}// 线程主驱动控制

{// 业务处理
ResetEvent(iop.eol.hEvent);
WaitCommEvent( iop.hIo, &dwEvtMask, &iop.eol );

DWORD dwRes = WaitForMultipleObjects( 3, evts_transp, FALSE, 10000 );
if( dwRes != WAIT_TIMEOUT )
{
if( WaitForSingleObject( ctp.hEvtExitThread, 0 ) == WAIT_OBJECT_0 )
{// 退出线程
ResetEvent( ctp.hEvtExitThread );
bThreadExit = TRUE;
}
if( WaitForSingleObject( iop.eol.hEvent, 0 ) == WAIT_OBJECT_0 )
{// 通信口事件
ResetEvent( iop.eol.hEvent );
__thread_serialport_workproc_subproc_comevent( dwEvtMask, LPARAM(&iop) );
}
if( WaitForSingleObject( iop.rol.hEvent, 0 ) == WAIT_OBJECT_0 )
{// 重叠读完成
ResetEvent( iop.rol.hEvent );
__thread_serialport_workproc_subproc_RolComplete( 0, LPARAM(&iop));
}
}
}// 业务处理
}

// 关闭重事件量
CloseHandle( iop.eol.hEvent );
CloseHandle( iop.rol.hEvent );

// 释放缓冲区
if( iop.rbuf.pbuf )
{
delete []iop.rbuf.pbuf;
}
if( iop.wbuf.pbuf )
{
delete []iop.wbuf.pbuf;
}
return 0;
}
Lstyk 2008-07-22
  • 打赏
  • 举报
回复
你的线程中没有等待函数啊,也没有一直在执行!
开了线程立即就返回了,根本等不到都串口吧。
建议你从网上下一个CSerialPort类看看,别人封装的串口操作。
ameliawuxi 2008-07-22
  • 打赏
  • 举报
回复
怎么没有人理我,还是太简单了大家都不屑了。

64,646

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧