日期:2014-05-16  浏览次数:20966 次

嵌入式linux下串口采用xmodem协议 接受不成功
接受时总是无法接受完整的包而超出最大次数,最后退出程序, 请问是什么原因? 怎么改?

int xmodemReceive(int fd, char *dest, int destsz)
{
  unsigned char xbuff[38]; /* 1024 for XModem 1k + 3 head chars + 2 crc + nul */
  //unsigned char *p;
  int bufsz, crc = 0;
  unsigned char c, trychar = 'C';
  unsigned char packetno = 1;
  int len = 0;
  int retry, retrans = MAXRETRANS;
  for(;;)
  {
  for( retry = 0; retry < 4; ++retry)
  {
  if (trychar)
  port_outbyte(fd, trychar);
  }
  while(c != SOH){
  c = port_inbyte(fd);
  if(c == EOT || c == CAN)
  break;
  }//(DLY_1S)<<1);
  if (last_error == 0)
  {
  switch (c)
  {
  case SOH:
  bufsz = 32;
  goto start_recv;
  case EOT:
  flushinput();
  port_outbyte(fd, ACK);
  return len; /* normal end */
  case CAN:
  c = port_inbyte(fd);//DLY_1S);
  if (c == CAN)
  {
  flushinput();
  port_outbyte(fd, ACK);
  return -1; /* canceled by remote */
  }
  break;
  default:
  break;
  }
  }

  if (trychar == 'C')
  {
  trychar = NAK;
  continue;
  }
  flushinput();
  port_outbyte(fd, CAN);
  port_outbyte(fd, CAN);
  port_outbyte(fd, CAN);
  return -2; /* sync error */
  start_recv:
  if (trychar == 'C') crc = 1;
  trychar = 0;
  usleep(10000);
  retry = read(fd, xbuff, 38);
  if (retry < 37)
  {
  printf("last_error2=%d\n", retry);
  goto reject;
  }
  if (xbuff[1] == (unsigned char)(~xbuff[2]) &&
  (xbuff[1] == packetno || xbuff[1] == (unsigned char)packetno-1) &&
  check(crc, &xbuff[3], bufsz))
  {
  if (xbuff[1] == packetno)
  {
  int coun