16,472
社区成员
发帖
与我相关
我的任务
分享
//向串口写数据,内容在buf中,长度为bufLen
BOOL CSeries::WritePort(const char *buf, DWORD bufLen)
{
if(PostThreadMessage(m_dwWriteThreadID,CM_THREADCOMMWRITE,WPARAM(bufLen),LPARAM(psendmsgtrue)))
{
return TRUE;
}
return FALSE;
}
//消息处理
DWORD CSeries::WriteThreadFunc(LPVOID lparam)
{
CSeries *pSeries = (CSeries *)lparam;
MSG msg;
DWORD dwWritenLen=0;
char * buf=NULL;
while(TRUE)
{
//如果捕捉到线程消息
if(PeekMessage(&msg,0,0,0,PM_REMOVE))
{
if(msg.hwnd!=0)
{
TranslateMessage(&msg);
DispatchMessage(&msg);
continue;
}
if(msg.message==CM_THREADCOMMWRITE)
{
//向串口写
buf=(char *)msg.lParam;
dwWritenLen=msg.wParam;
WritePort(pSeries->m_hComm,buf,dwWritenLen);
//删除动态分配的内存
// delete[] buf;应该需要删除数据的吧?不知为何,程序里加上这句就出错
}
}
if(WaitForSingleObject(pSeries->m_hWriteCloseEvent,500) == WAIT_OBJECT_0)
break;
pSeries->m_hWriteThread=NULL;
}
return 0;
}