大侠们,我的串口打开后,CreateThread(。。。)//读线程时,其中运行到WaitCommEvent(..)好像就死了,
if (OpenSerialport((char*)(LPCTSTR)strport,57600))
{
MessageBox(_T("串口打开失败"),_T("查询机"),MB_OK);
return FALSE;
}
if (!(m_hReadThread = CreateThread(NULL,0,ReadThread,(void*)m_hSerialport,0/*STACK_SIZE_PARAM_IS_A_RESERVATION*/,&pid)))
{
MessageBox(_T("读线程建立失败"),_T("查询机"));
}
//读线程
DWORD ReadThread(LPVOID param)
{
HANDLE hport = (HANDLE)param;
DWORD rcount;
TSBYTE buf[100];
TSBYTE rcvmsg[ARRLEN];
DWORD dwev;
int rdold = 0;
int flag = 0;
int datalen = 0;
ParseData *psdt;
DWORD pid,commerrs;
COMSTAT commstat;
PurgeComm(hport, PURGE_TXCLEAR); //Clears the output buffer
DWORD StartTime = GetTickCount(); // record the start time
if (hport == INVALID_HANDLE_VALUE ) return -1;
//
SetCommMask(hport,EV_RXCHAR);
while(hport != INVALID_HANDLE_VALUE )
{
//MessageBox(g_hwnd,_T("ReadThreadA"),_T("test"),MB_OK);
ClearCommError(hport,&commerrs,&commstat);
WaitCommEvent(hport,&dwev/*EV_RXCHAR*/,NULL);
SetCommMask(hport,EV_RXCHAR);
MessageBox(NULL,_T("ReadThreadD"),_T("test"),MB_OK);
if (dwev & EV_RXCHAR)
{
do
{
//MessageBox(g_hwnd,_T("ReadThreadB"),_T("test"),MB_OK);
ReadFile(hport,buf,100,&rcount,NULL);
//MessageBox(g_hwnd,_T("ReadThreadC"),_T("test"),MB_OK);
#ifdef _DEBUG
//printf("read before read times sof :%s\n",SOF);
for (int i = 0; i < rcount; i ++)
printf("read count :%d%02x\n",rcount,buf);
#endif
if ((buf[0] == SOF) && !flag)
{
#ifdef _DEBUG
printf("sof %d<%d>\n",rcount,buf[1]);
#endif
datalen = (buf[1] << 8) + buf[2];
break;
memcpy(rcvmsg, buf,rcount);
}
else
{
if (flag)
Byte2In1(rcvmsg, buf, rdold, rcount);
else
break;
}
flag++;
rdold += rcount;
}while(!rcount || (rdold >= datalen));
}
if (rcount > ARRLEN)
{
memset(buf,0,ARRLEN);
continue;
}
switch(rcvmsg[3])
{
case 0x0A:
//#ifdef _DEBUG
//printf("in pars01\n");
//#endif
//parse01(rd,msg);
break;
case 0x02:
#ifdef _DEBUG
printf("in pars02\n");
#endif
psdt->len = rdold;
memcpy(psdt->mem ,rcvmsg, rdold);
//PostMessage(g_hwnd..
CreateThread(NULL,0,parse02,(void*)psdt,STACK_SIZE_PARAM_IS_A_RESERVATION,&pid);//parse02(rdold,rcvmsg);
PurgeComm(hport, PURGE_TXCLEAR | PURGE_RXCLEAR); //Clears the output|input buffer
break;
case 0x01:
//#ifdef _DEBUG
//printf("pars03 inventory\n");
//#endif
//parse03(rd,msg);
break;
}
Sleep(50);
}
return 0;
}
请帮帮我吧