具体代码:
int ComRd(int portNo, unsigned char buf[], int maxCnt, int Timeout)
{
fd_set rfds;
int actualRead = 0;
struct timeval tv;
int retval;
if (!ports[portNo].busy)
{
printf("Com%d is not valid\n",portNo);
return -1;
//assert(0);
}
FD_ZERO(&rfds);
FD_SET(ports[portNo].handle, &rfds);
tv.tv_sec = Timeout/1000;
tv.tv_usec = (Timeout%1000)*1000;
retval = select(8, &rfds, NULL, NULL, &tv);
if (retval)
{
actualRead = read(ports[portNo].handle, buf, maxCnt);
}
else printf("ComRd Warning!!!!:Timeout.\n");
return actualRead;
}