读取串口数据的线程怎么自己结束

monshadow 2013-01-29 03:53:58
static void uart_listener(void *lpThreadParameter)
{
AllocConsole();
DWORD event_mask;
char *p, inbuf[/*256*/2 *(TMT_PACKET_PAYLOAD_MAX + 1)];
DWORD size;
int i;
uart_t *uart =(uart_s *)lpThreadParameter;

tr("uart_listener(%#x)(started)\n", lpThreadParameter);
tr("uart->recv = %#x, ->buf = ?\n", uart->recv);
//cprintf("uart_listener:\n");
while(/*1*/!stop)
{
cprintf("uart_listener\n");
// Wait the event signalling characters received.
if(!WaitCommEvent(uart->hCom, &event_mask, NULL))
{
//fprintf(stderr, "FATAL: uart_listener(): WaitCommEvent() failed with Win32 error %d\n", GetLastError());
tr("FATAL: uart_listener(): WaitCommEvent() failed with Win32 error %d\n", GetLastError());
return;
}
/*
if(WaitCommEvent(hCom[comx], &event_mask, &read_overlapped[comx]) != TRUE)
{
dwError = GetLastError();
switch(dwError) {
case ERROR_IO_PENDING:
break;
case 87:
break;
default:
main_msg(comx, "WaitCommEvent(): error %d", dwError);
return;
}
}
*/
//if((event_mask & EV_RXCHAR) == 0) {
// pctm_error_os(E_UARTRECEIVE, GetLastError());
// return;
//}

// Read all characters received in the buffer. Mysteriously, it
// does NOT work with a buffer size greater than one!
DWORD re = 0 ;
do
{
// cprintf("stop::%d!!!!\n",stop);
// re = WaitForSingleObject(ghThreadExitEvent, 10);
// cprintf("stop::%d!!!!\n",re);
// if((WaitForSingleObject(ghThreadExitEvent, 0) != WAIT_TIMEOUT))
// {
// cprintf("uart_listener exit!!!!\n");
// stop = TRUE;
// return;
// }

if(!ReadFile(uart->hCom, inbuf, 1, &size, &uart->read))
{
if(GetLastError() == ERROR_IO_PENDING)
{
if(!GetOverlappedResult(uart->hCom, &uart->read, &size, TRUE))
{
//fprintf(stderr, "FATAL: uart_listener(): GetOverlappedResult() failed with Win32 error %d\n", GetLastError());
tr("FATAL: uart_listener(): GetOverlappedResult() failed with Win32 error %d\n", GetLastError());
break;
}
}
else
{
//fprintf(stderr, "FATAL: uart_listener(): ReadFile() failed with Win32 error %d\n", GetLastError());
tr("FATAL: uart_listener(): ReadFile() failed with Win32 error %d\n", GetLastError());
uart_recv_reset(uart->hCom);
break;
}
}
if (stop)
{
cprintf("ReadFile\n");
return;
}

p = inbuf;
i = size;
//cprintf("recv_char:%s\n",inbuf);
while(i--)
recv_char(uart, *p++);
} while(size);
}
}

我的线程怎么不能自己退出,求高手指教
...全文
177 6 打赏 收藏 举报
写回复
6 条回复
切换为时间正序
当前发帖距今超过3年,不再开放新的回复
发表回复
wyx100 2013-01-30
回复于: 2013-01-29 17:04:52 因为你调用了GetOverlappedResult 退出前掉用先调用setevent
  • 打赏
  • 举报
回复
monshadow 2013-01-30
怎么让线程自己停下来
  • 打赏
  • 举报
回复
monshadow 2013-01-29
具体流程是怎样的呀?第一次做这个,能否详细解释一下,谢谢啦
  • 打赏
  • 举报
回复
飞翔的薄荷 2013-01-29
因为你调用了GetOverlappedResult 退出前掉用先调用setevent
  • 打赏
  • 举报
回复
monshadow 2013-01-29
为什么没有sleep就不能退出呢
  • 打赏
  • 举报
回复
zhangyihu321 2013-01-29
while(1) { // if(想退出) { break; } Sleep(10); }
  • 打赏
  • 举报
回复
相关推荐
发帖
硬件/系统
加入

2612

社区成员

VC/MFC 硬件/系统
社区管理员
  • 硬件/系统社区
申请成为版主
帖子事件
创建了帖子
2013-01-29 03:53
社区公告
暂无公告