[阅读: 374] 2005-03-21 07:08:57
//读取串口,最多maxlen个字符
int CComPort::GetMaxStr(char *buf,int maxlen)
{
#ifdef WIN32
DWORD rch = maxlen;
if(!ReadFile(idComDev, buf,rch,&rch,NULL))
{
m_errstr=L"error ReadFile";
return rch;
}
else
{
//*len = rch;
return rch;
}
#else
int rch = maxlen;
rch = read(idComDev,buf,rch);
return rch;
#endif
}
//阻塞方式读取len个字节,或直到超时timeout(ms)
bool CComPort::GetStrTimeout(char *buf,int *len,int timeout)
{
int p=0,r,l=*len;
const int tms=200;
//timeout=timeout / 200;
while (l>0 && timeout>0)
{
r=GetMaxStr(buf+p,l);
p+=r;
l-=r;
if (l<=0)
{
break;
}
timeout-=tms;
SleepMS(tms);
}
*len=p;
return l==0;
}