- 论坛徽章:
- 0
|
int GetMessage(int sd, unsigned char *ptr, int max)
{
int nread, npacklen;
int nbyte, i;
unsigned char lrc;
int ret = -1;
/* 接收 STX */
nread = readn(sd, ptr, 1, 1);/*STX=2 这STX表示什么东西?*/
if (nread != 1)
return (nread);
tgLastReceiveTime = time(NULL);
if (ptr[0] != STX) return (0);
/* 接收数据长度 length */
nread = readn(sd, ptr+1, 2, 1);
if (nread != 2)
{
if (nread == 1) return (0);
/* 接收数据长度 length */
nread = readn(sd, ptr+1, 2, 1);
if (nread != 2)
{
if (nread == 1) return (0);
return (nread);
}
npacklen = (ptr[2] >> 4) * 10 + (ptr[2] & 0x0f) +
(ptr[1] >> 4) * 1000 + (ptr[1] & 0x0f) * 100 + 2;
/*npacklen 代表什么?ptr[2]是BCD码包长度,移位的时候0X有没有参加移位的,如果*/
ptr[1]=0X12,ptr[2]=0X34,npacklen 是多少?*/
if (npacklen > max - 3)
nbyte = max -3;
else
nbyte = npacklen;
/* 读取剩下的字节 */
nread = readn(sd, ptr+3, nbyte, 2);
if (nread != nbyte)/*这里nread怎么可能等于nbyte呢?*/
{
if (nread >= 0) return (0);
return -1;
}
ret = nread + 3;
nread = npacklen - (max - 3);
while (nread > 0)
{
if (nread >= MMAX)
nbyte = MMAX;
else
nbyte = nread;
nbyte = readn(sd, dirty_buf, nbyte, 2);
if (nbyte > 0)
nread -= nbyte;
else
break;
} |
|