想用MFC做一个串口通信程序, 用的是MSComm控件, 可以发送数据, 但是onComm()没有触发, 最后接收框内没有数据. 这个是哪里出了问题啊?
第1个回答 推荐于2016-02-16
最好用线程来接受程序
static UINT RevThreadProc2(LPVOID pParam)
{
HANDLEh_gEvent;
DWORDdwBytesRead;
OVERLAPPEDOverlapped;
BOOL bReadStatus = FALSE;
dwBytesRead = 0;
//MessageBox("abc");
memset(&Overlapped,0,sizeof(OVERLAPPED));
h_gEvent = NULL;
h_gEvent = CreateEvent(
NULL,
TRUE,//手工设置事件有无信号
FALSE,//初始化事件为无信号状态
NULL//无名事件对像
);
if(h_gEvent == NULL) return 0;
Overlapped.hEvent = h_gEvent;//指定一个I/O操作完成后触发的事件
BYTE data[COM_BUFSIZE];
ZeroMemory(data,COM_BUFSIZE);
DWORD dwEvtMask=0;
GetCommMask(g_pMainDlg->m_Io0.m_hHandle,&dwEvtMask);
dwEvtMask |=EV_RXCHAR;
SetCommMask(g_pMainDlg->m_Io0.m_hHandle,dwEvtMask);
while(g_pMainDlg->m_Io0.m_hHandle)
{
WaitCommEvent(g_pMainDlg->m_Io0.m_hHandle,&dwEvtMask,NULL);
if (g_pMainDlg->m_Io0.m_hHandle == INVALID_HANDLE_VALUE)
{
return 0;
}
if ((dwEvtMask&EV_RXCHAR) == EV_RXCHAR)
{
COMSTAT ComStat ;
DWORD dwLength,dwErrorFlags;
ClearCommError(g_pMainDlg->m_Io0.m_hHandle, &dwErrorFlags, &ComStat ) ;
dwLength = ComStat.cbInQue ; //输入缓冲区接收了多少数据?
if (dwLength > 0)
{
//AfxMessageBox("RevThreadProc2");
bReadStatus = ReadFile( g_pMainDlg->m_Io0.m_hHandle, data,dwLength, &dwBytesRead, &Overlapped);
if(!bReadStatus)
{
if(GetLastError()==ERROR_IO_PENDING)
{
while(!GetOverlappedResult(g_pMainDlg->m_Io0.m_hHandle,
&Overlapped, &dwBytesRead, TRUE ))
{
if(GetLastError() == ERROR_IO_INCOMPLETE)
continue;
}
for(DWORD i = 0;i<dwBytesRead;i++)
{
g_pMainDlg->m_RevData[rec_pos_lineless] = data[i];
rec_pos_lineless++;
}
if (rec_pos_lineless >= 5)
{
SetStudentUpDownFromCom(g_pMainDlg->m_RevData,rec_pos_lineless);
rec_pos_lineless = 0;
}
}
}
else
{
for(DWORD i = 0;i<dwBytesRead;i++)
{
g_pMainDlg->m_RevData[rec_pos_lineless] = data[i];
if (data[i] == 144 || data[i] == 80 ||data[i] == 255)
{
bvn++;
}
rec_pos_lineless++;
}
if (bvn >= 3)
{
、、、、、、、、处理接受到的数据
bvn =0;
rec_pos_lineless = 0;
}
}
}
}
}
ExitThread(0);
CloseHandle(h_gEvent);//读取时的OS
return 0;
}
这个是我写的一个线程,可以接受的,希望对你有用本回答被提问者采纳
static UINT RevThreadProc2(LPVOID pParam)
{
HANDLEh_gEvent;
DWORDdwBytesRead;
OVERLAPPEDOverlapped;
BOOL bReadStatus = FALSE;
dwBytesRead = 0;
//MessageBox("abc");
memset(&Overlapped,0,sizeof(OVERLAPPED));
h_gEvent = NULL;
h_gEvent = CreateEvent(
NULL,
TRUE,//手工设置事件有无信号
FALSE,//初始化事件为无信号状态
NULL//无名事件对像
);
if(h_gEvent == NULL) return 0;
Overlapped.hEvent = h_gEvent;//指定一个I/O操作完成后触发的事件
BYTE data[COM_BUFSIZE];
ZeroMemory(data,COM_BUFSIZE);
DWORD dwEvtMask=0;
GetCommMask(g_pMainDlg->m_Io0.m_hHandle,&dwEvtMask);
dwEvtMask |=EV_RXCHAR;
SetCommMask(g_pMainDlg->m_Io0.m_hHandle,dwEvtMask);
while(g_pMainDlg->m_Io0.m_hHandle)
{
WaitCommEvent(g_pMainDlg->m_Io0.m_hHandle,&dwEvtMask,NULL);
if (g_pMainDlg->m_Io0.m_hHandle == INVALID_HANDLE_VALUE)
{
return 0;
}
if ((dwEvtMask&EV_RXCHAR) == EV_RXCHAR)
{
COMSTAT ComStat ;
DWORD dwLength,dwErrorFlags;
ClearCommError(g_pMainDlg->m_Io0.m_hHandle, &dwErrorFlags, &ComStat ) ;
dwLength = ComStat.cbInQue ; //输入缓冲区接收了多少数据?
if (dwLength > 0)
{
//AfxMessageBox("RevThreadProc2");
bReadStatus = ReadFile( g_pMainDlg->m_Io0.m_hHandle, data,dwLength, &dwBytesRead, &Overlapped);
if(!bReadStatus)
{
if(GetLastError()==ERROR_IO_PENDING)
{
while(!GetOverlappedResult(g_pMainDlg->m_Io0.m_hHandle,
&Overlapped, &dwBytesRead, TRUE ))
{
if(GetLastError() == ERROR_IO_INCOMPLETE)
continue;
}
for(DWORD i = 0;i<dwBytesRead;i++)
{
g_pMainDlg->m_RevData[rec_pos_lineless] = data[i];
rec_pos_lineless++;
}
if (rec_pos_lineless >= 5)
{
SetStudentUpDownFromCom(g_pMainDlg->m_RevData,rec_pos_lineless);
rec_pos_lineless = 0;
}
}
}
else
{
for(DWORD i = 0;i<dwBytesRead;i++)
{
g_pMainDlg->m_RevData[rec_pos_lineless] = data[i];
if (data[i] == 144 || data[i] == 80 ||data[i] == 255)
{
bvn++;
}
rec_pos_lineless++;
}
if (bvn >= 3)
{
、、、、、、、、处理接受到的数据
bvn =0;
rec_pos_lineless = 0;
}
}
}
}
}
ExitThread(0);
CloseHandle(h_gEvent);//读取时的OS
return 0;
}
这个是我写的一个线程,可以接受的,希望对你有用本回答被提问者采纳
第2个回答 2018-09-14
MSCOM要触发消息,需要加上一个消息设置。添加MSCOM控件的onComm时会有文字在函数里面写着,提示你要在开始加入消息影射才会有反应的。