TR
UE))
UE))
{ dwError=GetLastError();
if(dwError == ERROR_IO_INCOMPLETE)
continue;
}
}
else
{ dwLength=0;
ClearCommError(COMDEV(npGPSInfo),&dwErrorFlags,&ComStat);
}
}
}
return ( dwLength ) ;
}
BOOL NEAR WriteCommBlock(LPSTR lpByte,DWORD dwBytesToWrite)
{ BOOL fWriteStat;
DWORD dwBytesWritten;
DWORD dwErrorFlags;
DWORD dwError;
COMSTAT ComStat;
if (!npGPSInfo) return(FALSE);
fWriteStat=WriteFile(COMDEV(npGPSInfo),lpByte,dwBytesToWrite,
&dwBytesWritten,&WRITE_OS(npGPSInfo));
if (!fWriteStat)
{ if(GetLastError()==ERROR_IO_PENDING)
{ while(!GetOverlappedResult(COMDEV(npGPSInfo),
&WRITE_OS(npGPSInfo),&dwBytesWritten,TRUE))
{ dwError=GetLastError();
if(dwError == ERROR_IO_INCOMPLETE)
continue;
else
{
ClearCommError(COMDEV(npGPSInfo),&dwErrorFlags,&ComStat);
break;
}
}
}
else
{
{
ClearCommError(COMDEV(npGPSInfo),&dwErrorFlags,&ComStat);
return ( FALSE );
}
}
return ( TRUE ) ;
}
DWORD FAR PASCAL CommWatchProc(LPSTR)
{ DWORD dwEvtMask;
OVERLAPPED os;
int nLength;
BYTE abIn[1024];
memset(&os,0,sizeof(OVERLAPPED));
// create I/O event used for overlapped read
os.hEvent=CreateEvent(NULL,TRUE,FALSE,NULL);
if (os.hEvent==NULL)
{ MessageBox(NULL,"Failed to create event for thread!","GPS
Error!",MB_ICONEXCLAMATION|MB_OK);
return ( FALSE ) ;
}
if (!SetCommMask(COMDEV(npGPSInfo),EV_RXCHAR)) return (FALSE);
while (CONNECTED( npGPSInfo))
{ dwEvtMask=0 ;
WaitCommEvent(COMDEV(npGPSInfo),&dwEvtMask,NULL);
if ((dwEvtMask&EV_RXCHAR)==EV_RXCHAR)
{ do
{ if
(nLength=ReadCommBlock((LPSTR)abIn,1024))
{
//WriteCommBlock((LPSTR)abIn,nLength );
*(abIn+nLength)=0;
::SendMessage(hGPSWnd,CN_SEND,nLength,(LONG)(LPSTR)abIn);
}
}
while ((nLength>0)&&(CONNECTED( npGPSInfo)));
}
}
}
CloseHandle(os.hEvent);
THREADID(npGPSInfo)=0;
HTHREAD(npGPSInfo)=NULL;
return(TRUE);
}
就这些了,希望能对问这些
问题的朋友有所帮助!
一般使用的顺序是:
CreateGPSInfo(被通知的窗口句柄,串口端口号1或2);
OpenConnection();//建立联结它会调用SetupConnection
DestroyGPSInfo();//解除联结它会调用CloseConnection
可以用ReadCommBlock/WriteCommBlock来读/写串口
CommWatchProc是监视串口的线程,由OpenConnection建立
当串口有数据来的时侯,它会通知''被通知的窗口句柄''的窗口数据传到的消息(自
定义的)
SendMessage(hGPSWnd,CN_SEND,nLength,(LONG)(LPSTR)abIn);
好了,文章结束了!希望能帮助你!