免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 801 | 回复: 0
打印 上一主题 下一主题

这段代码是什么意思啊?帮帮我 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2006-08-18 11:03 |显示全部楼层 |倒序浏览
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;
  }
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP