串口程序中函数使用WaitCommEvent、ClearCommError、GetcommMask
来源:互联网 发布:mac下载的软件在哪里 编辑:程序博客网 时间:2024/05/16 08:31
在串口程序中的几个函数的联合使用
一、首先是利用WaitCommEvent来等待串口事件的被触发,这些事件是指在该函数被调用前,我们自己设置好的能触发串口线程的有效事件。
主要包括3类事件:
1.函数SetCommMask (ceSeries->m_hComm, EV_RXCHAR |EV_CTS | EV_DSR )设置的数据接收中断
2.用户中断线程的操作。
3.用户发送数据所产生的中断。
该函数相当于利用事件来同步,实际函数也是这么设计的。但没有有效中断触发,线程会一直在这里等待。
一旦被触发,程序才能继续往下执行。
二、函数WaitCommEvent执行完成后,他的返回值若为零,说明有错误,这时候用GetLastError来查询错误类型。
三、函数WaitCommEvent执行返回值若为1,则用ClearCommError来清除错误,并查询串口的状态,得到comstat的值。
四、检查ClearCommError所查询的串口状态comstat.cbInQue的值,若不大于0,说明错误。继续等待事件的触发
五、若omstat.cbInQue大于0,说明buffer里面有字符。在使用WaitForMultipleObjects函数确认事件,并执行对应的操作。
//WaitCommEvent函数为指定的通信资源监听一系列的Event,这些Event可以由 SetcommMask和 GetcommMask函数来设置和查询。
例子:
bResult =WaitCommEvent(port->m_hComm, &Event, &port->m_ov);
if (!bResult)
{
// If WaitCommEvent() returns FALSE,process the last error to determin
switch (dwError =GetLastError())
{
case ERROR_IO_PENDING:
{
break;
}
case 87:
{
break;
}
default:
{
port->ProcessErrorMessage("WaitCommEvent()");
break;
}
}
}
else
{
bResult =ClearCommError(port->m_hComm, &dwError, &comstat);
if (comstat.cbInQue == 0)
continue;
} // end if bResult
//m_hEventArray[0] =m_hShutdownEvent; // highest priority
//m_hEventArray[1] = m_ov.hEvent;
//m_hEventArray[2] = m_hWriteEvent;
Event = WaitForMultipleObjects(3,port->m_hEventArray, FALSE, INFINITE);
switch (Event)
{
case 0:
{
// Shutdown event. This is event zero so it will be// thehigest priority and be serviced first.
CloseHandle(port->m_hComm);
port->m_hComm=NULL;
port->m_bThreadAlive =FALSE;
// Kill this thread. break is not needed, but makes me feelbetter.
AfxEndThread(100);
break;
}
case 1: // read event
{
GetCommMask(port->m_hComm,&CommEvent);
if (CommEvent & EV_RXCHAR)
// Receive character event from port.
ReceiveChar(port, comstat);
if (CommEvent & EV_CTS)
::SendMessage(port->m_pOwner->m_hWnd,WM_COMM_CTS_DETECTED, (WPARAM) 0, (LPARAM) port->m_nPortNr);
if (CommEvent & EV_BREAK)
::SendMessage(port->m_pOwner->m_hWnd,WM_COMM_BREAK_DETECTED, (WPARAM) 0, (LPARAM) port->m_nPortNr);
if (CommEvent & EV_ERR)
::SendMessage(port->m_pOwner->m_hWnd,WM_COMM_ERR_DETECTED, (WPARAM) 0, (LPARAM) port->m_nPortNr);
if (CommEvent & EV_RING)
::SendMessage(port->m_pOwner->m_hWnd,WM_COMM_RING_DETECTED, (WPARAM) 0, (LPARAM) port->m_nPortNr);
if (CommEvent & EV_RXFLAG)
::SendMessage(port->m_pOwner->m_hWnd,WM_COMM_RXFLAG_DETECTED, (WPARAM) 0, (LPARAM) port->m_nPortNr);
break;
}
case 2: // write event
{
// Write character event from port
WriteChar(port);
break;
}
} // end switch
} // closeforever loop
- 串口程序中函数使用WaitCommEvent、ClearCommError、GetcommMask
- 串口 WaitCommEvent 、GetLastError、ClearCommError、WaitForMultipleObjects的联合使用
- 串口开发中使用WaitCommEvent
- VC++ WaitCommEvent、GetLastError、ClearCommError、WaitForMultipleObjects用法
- 串口 WaitCommEvent
- 写串口程序时waitcommevent或writefile导致死锁问题
- 写串口程序时waitcommevent或writefile导致死锁问题
- 写串口程序时waitcommevent或writefile导致死锁问题
- ClearCommError
- WaitCommEvent
- SDK串口编程 正常终止 WaitCommEvent
- 串口 SetCommMask WaitCommEvent 设置 等待串口通信事件
- BCB中使用API函数写简单串口程序(定时器接收)
- 怎样在TI stellaris lm3sxxx 程序中使用C语言printf()函数进行串口输出
- 线程中WaitCommEvent使用临时栈区变量导致Release版本崩溃
- IAR中通过串口使用printf函数
- PB程序使用串口
- 在stm32 单片机程序中使用printf()进行串口调试
- Map按照key和value进行排序
- WWDC 2017 简要总结
- RestTemplate getForObject方法不能将获取到的数据设置到对象中
- Android
- 原来wireshark还有这功能Statistics -->Conversations
- 串口程序中函数使用WaitCommEvent、ClearCommError、GetcommMask
- 如何把URL传过来的参数进行解析
- 【深度学习笔记001 深度学习入门导读】
- I/O多路转接之select
- layer弹出层显示在top顶层
- Android出现W/IInputConnectionWrapper: showStatusIcon on inactive InputConnection闪退
- dup2重定向
- 第9章 索引
- R语言基于支持向量机训练模型实现类预测