⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 xcxmdm.c

📁 支持X/YModem和cis_b+协议的串口通讯程序
💻 C
📖 第 1 页 / 共 2 页
字号:
	{	case CAN:	  show (2, "Receiver CANcelled");	  return (ERROR);	case NAK:	  show (2, "Got a NAK on sector acknowledge ");	  break;	case TIMEOUT:	  show (2, "Timeout on sector acknowledge   ");	  break;	case ACK:	  assert (g_flag == FALSE);	  firstsec = FALSE;	  return (OK);	  break;	default:	  sprintf (Msg, "Got %#x for sector acknowledge ", c);	  show (2, Msg);	  if (c == crcheck && crcheck)	    break;		/* imitate NAK */	  do	    {	      if ((c = readbyte (3)) == CAN)		{		  show (2, "Receiver CANcelled");		  return (ERROR);		}	    }	  while (c != TIMEOUT);	}      errct++;      if (!y_flag && (errct == 3) && (badline == FALSE || stx_soh == STX))	{	  show (2, "ATTEMPTING RECOVERY ");	  return (FALLBACK);	}    }  show (2, "Retry count exceeded");  return (ERROR);}/* Transmit a file using XMODEM protocol */static intwctx P_((void)){    register c ;    int sectnum, eoflg, attempts;    int tmpsize, tmpstxh, tmpbdln, terr, ee;    if (!(xfp = fopen (word, "r")) && !y_done)      {	  sprintf (Msg, "Can't open '%s'", word);	  show(1, Msg); 	  return (ERROR);      }    firstsec = TRUE;    attempts = 0;    amdone = FALSE;    tmpsize = ksecsize;		/* saves sector size for later restoration */    tmpstxh = stx_soh;		/* saves sector size for later restoration */    tmpbdln = badline;    cl_line();    show(1,"Sync...");    while ( (c = readbyte(30) ) != NAK && c != WANTCRC && c != WANTCRG && c != CAN)	if (c == TIMEOUT && ++attempts > RETRYMAX)	  {	      show(1,"Receiver not responding");	      if(!y_done)	      fclose (xfp);	      return (ERROR);	  }    if (c == CAN)      {	  show(1,"Receiver CANcelled");	  if(!y_done)	  fclose (xfp);	  return (ERROR);      }    g_flag = FALSE ;	    if ( c == WANTCRC )    	crcheck = WANTCRC;     else if ( c == WANTCRG )    	{ crcheck = WANTCRG; g_flag = TRUE ; }     else    	crcheck = FALSE ;        if( crcheck )	show (1, "CRC packet validation enabled   ");    else	show (1, "ATTENTION: CHECKSUM packet validation requested");    sectnum = y_flag ? 0 : 1;    do      {	  eoflg = getsec ();	  fprintf (tfp, "Transmitting sector #%d\r", sectnum);	  if ((terr = wcputsec (sectnum)) == ERROR)	    {		if(!y_done)		fclose (xfp);		return (ERROR);	    }	  else if (terr == FALLBACK)	    {		if (stx_soh == STX)		  {		      amdone = TRUE;		  }		else		    badline = TRUE;		sectnum--;	    }	  sectnum++;      }    while (eoflg);    ksecsize = tmpsize;    stx_soh = tmpstxh;    badline = tmpbdln;    attempts = 0;    if (!y_done){ /*  if xmodem,  or if more ymodem packets */     fclose (xfp);    do{    sendbyte (EOT);    ee=readbyte (10) ;    } while ( ee != ACK && attempts++ < RETRYMAX) ;        if (attempts >= RETRYMAX)      {	  show(1,"Receiver not responding to completion");	  return (ERROR);      }    }    if ( !y_flag || y_done )    show(1,"Transmission complete           ");    return (OK);}static intsetmode (c)     int c;{    g_flag = FALSE ;    switch (tolower (c))      {      case 'x':	  textmode = FALSE;	/* the usual case */      case 't':	  ksecsize = 128;	/* default for xmodem */	  stx_soh = SOH;	/* default for xmodem */	  if (c == 't')	      textmode = TRUE;	  break;      case 'g':	  g_flag = TRUE ;      case 'b':      case 'o':	  textmode = FALSE;	  ksecsize = 1024;	/* old ymodem sector size */	  stx_soh = STX;	/* transmit flag for 1k byte packet size */	  break;      case ' ':	  break;      default:	  return FAILURE;      }    sprintf (Msg, "%s %s file transfer mode",(y_flag ?"YMODEM":"XMODEM"),(textmode?"Text":"Binary"));    show(1,Msg);    return SUCCESS;}/* save old signal handler */static RETSIGTYPE (*oldvec) P_((int));voidxsend (c)     int c;{    badline = FALSE ;    oldvec = signal (SIGINT, xmsigint);    if (setmode (c))      {	  g_flag = FALSE ; /* only receiver can set g_flag */	  sprintf(Msg,"Sending %s",ksecsize == 128 ?"128 byte small packet   "	          : "1K byte packets XMODEM  ") ; show(1,Msg) ;	  if (!setjmp (our_env))	    {		if (wctx () == ERROR)		  {		      sprintf (Msg, "Error transmitting file %s", word);		      show(1,Msg) ;		      purge(); 		      /* return; why isn't signal restored? *??*/		  }	    }      }    signal (SIGINT, oldvec);}/* ymodem -- normalize file name, assume *filn is at least 128 chars */static char *getfname( filn, strpflg )  char *filn;   int strpflg ;{	char *wtp, *utp ;	wtp = filn ;	utp = wtp + 128 ; /* length is 128 */	while ( *wtp == 0x20 ) { 	/* strip leading spaces */		wtp++ ;		if ( wtp == utp )		    return 0 ; 		/* just spaces, no file name */	}	if ( ! filn[0] || ! wtp[0] )		return 0 ; 		/* empty string */	if ( *wtp == '.' )		while( *++wtp == '/' ) 			; /* increment pointer beyond '/' character */ 	/* normalize string:  strip away path, etc */ 	if((utp = strrchr( wtp, '/' )) != NULL) {		if ( strpflg ) {   			wtp = utp + 1 ;	/* strip path */		}		if ( utp[1] == 0 ) {			return 0 ;  /* no file name */		}	}	return wtp ;}/* parse ymodem header for file name and attributes */static intyparse P_((void)){	int i = 0 ;	char *wtp, *utp ;	phdr.fnam = NULL ;	phdr.fsiz = 0 ;	phdr.fdat = 0 ;	phdr.fprm = 0 ;	while ( i < 128 && wcbuf[i] ) i++ ;	/*!! fix the 128 thingy!*/	if ( i == 0 || i >= ksecsize ) {		y_done = TRUE ;		return OK ;	}	/* need some error checking here: */        utp = getfname( wcbuf, TRUE );		strcpy ( Name, utp );		phdr.fnam = Name ;	if ( (xfp = QueryCreate (Resume_Not_Allowed)) == NULL) {		return (ERROR);			/* can't open file */	}	while ( *utp != 0 ) utp++ ; utp++ ;	/* point to start of len*/ 	phdr.fsiz = strtol( utp, &wtp, 10 ); 	/* get file length 	*/	if ( *wtp == 0x20 ) { 			/* file date is included */		phdr.fdat = strtol( wtp, &utp, 8 );		if ( *utp == 0x20 ){			phdr.fprm = strtoul( utp, &wtp, 8 ) & 07777 ;			*wtp = 0 ; /* terminate string */		} 	}	cl_line();	sprintf(Msg,"%s %ld %s",phdr.fnam, phdr.fsiz,(phdr.fprm ? utp : " ")); 	fprintf(stderr,"\r%s\n\r",Msg);	return OK ;}/* Receive a file using XMODEM protocol */static intwcrx P_((void)){    register sectnum, sectcurr;    if ( !y_flag ){    	/* open file for xmodem reception */    	strcpy (Name, word);    	xfp = QueryCreate (Resume_Not_Allowed) ;    	if ( xfp == NULL)		return (ERROR);     }    firstsec = TRUE;    sectnum = y_flag ? -1 : 0 ;    sendchar = g_flag? WANTCRG : WANTCRC ;    sync = FALSE;    y_done = FALSE;    crcheck = TRUE;    show(2,"Sync...");    while (TRUE)      {	  if (badline)	      purge ();	  if ( g_flag == FALSE && sync ) /* g-mode doesn't ACK */	      sendbyte (sendchar);	  if ( y_done )	      { cl_line(); return OK; }	  /* 	     Here we are at this if() on the second pass through the loop.	     Here we start the ymodem transmission body after we have grabbed	     the intial header with file name. Presumably crcheck	     equals 'G' or 'C' or 0 at this juncture in time . . . 	  */	  if ( sectnum == 0 && y_flag ) {		assert(!crcheck || crcheck == WANTCRG || crcheck == WANTCRC);	        sendbyte((crcheck ? crcheck : NAK));	  }	  sectcurr = wcgetsec (Maxtime);	  if (sectcurr == ((sectnum + 1) & 0xff))	    {		if ( firstsec ){			if ( y_flag ){	/* open file for rx */				if (yparse() == ERROR ) return ERROR;			}			else{ 				fprintf(stderr,"\r%s\n\r", Name );			}			firstsec = FALSE ;		}		sectnum++;	        if (sectnum == 1)	            show (1,(ksecsize == 128 ? "Sender using 128 byte packets   ":"Sender using 1K byte packets    "));		if ( sectnum > 0 )		    putsec ();		fprintf (tfp, "Received sector #%d\r", sectnum);		sendchar = ACK;		continue;	    }	  if (sectcurr == (sectnum & 0xff))	    {		sprintf (Msg, "Received duplicate sector #%d", sectnum);		show(2,Msg);		sendchar = ACK;		continue;	    }	  fclose(xfp);	  if( y_flag ) set_dp( phdr.fnam, phdr.fdat, phdr.fprm );	  if (sectcurr == WCEOT)	    {		show( 1,"File received OK                ");		sendbyte (ACK);		return (OK);	    }	  if (sectcurr == ERROR)	      return (ERROR);	  sprintf (Msg, "Sync error ... expected %d(%d), got %d",		   (sectnum + 1) & 0xff, sectnum, sectcurr);	  show(1,Msg);	  return (ERROR);      }}/*      Put received WC sector into a UNIX file        using text translations if neccesary.*/voidxreceive (c)     int c;{    badline = FALSE ;    oldvec = signal (SIGINT, xmsigint);    if (setmode (c))      {	  if (!setjmp (our_env))	    {		if (wcrx () == ERROR)		    canit ();		/* return; why isn't signal restored first? *??*/	    }      }    signal (SIGINT, oldvec);}static intbuild_hdr P_((void)){	int retcode = 0, i = 0;	struct stat sbuf ;	char *fname, *tpr = wcbuf ;        while ( i < SECSIZ ) tpr[i] = 0, i++ ;  /* clear buffer */	getword();	fname = getfname( word, TRUE );	if ( fname )	{		if ( stat( word, &sbuf ))		{			perror( fname );			fprintf(stderr, "\rCan't stat()  %s\n\r", fname );			retcode = -1 ;		}		else		{			smallhdr = TRUE ;			strcpy( wcbuf, fname );			while( *tpr ) tpr++ ; tpr++ ; 			sprintf(tpr,"%ld %lo %o", sbuf.st_size, sbuf.st_mtime, sbuf.st_mode);			fprintf(stderr,"%s %ld %-25o\n\r", fname, sbuf.st_size, sbuf.st_mode);			while( *tpr ) tpr++ ;			if ( 128 < (tpr - wcbuf) ) smallhdr = FALSE ;		}		}	else 	{		y_done = smallhdr = TRUE ;	}	return retcode ;}voidysend P_((void)){    y_done = FALSE ;    badline = FALSE ;    oldvec = signal (SIGINT, xmsigint);    if (setmode ('b'))      {		  g_flag = FALSE ; /* only receiver can set g_flag */	  sprintf (Msg, "Sending %s", ksecsize == 128 ? "128 byte small packet YMODEM"	          : "1K byte packets YMODEM  ") ; show(1,Msg) ;	  if (!setjmp (our_env))	    {		while( !y_done )		{			if( !build_hdr() ) 			{				if (wctx () == ERROR)		  		{		      		sprintf (Msg, "Error transmitting file %s", word);		      		show(1,Msg) ;		      		purge(); 				y_done = TRUE ;		      		/* return; why isn't signal restored? *??*/		  		}			}		}	    }	    purge();       }    signal (SIGINT, oldvec);    g_flag = smallhdr = FALSE ;}voidyreceive( e ) int e ;{    y_done = FALSE ;    badline = FALSE ;    oldvec = signal (SIGINT, xmsigint);        purge();    if (setmode ('b'))    {	  if (!setjmp (our_env))	    {    		g_flag = (e == 'g' ? TRUE : FALSE ); 		while( y_done == FALSE )		{			if (wcrx () == ERROR)			{			cl_line();			sprintf (Msg, "Error receiving file %s", word);			show(1,Msg) ;			purge(); 			y_done = TRUE ;			/* return; why isn't signal restored? *??*/			}		}	    }      }    signal (SIGINT, oldvec);    g_flag = smallhdr = FALSE ;}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -