📄 xmodemtransmit.c
字号:
/* Copyright 2001, 2002 Georges Menie (www.menie.org) This program is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with this program; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA*/#include "baselib.h"#include "xmodem.h"#include "crc16.h"#include "xmodemdefs.h"int xmodemTransmit(unsigned char *src, int srcsz){ unsigned char xbuff[1030]; /* 1024 for XModem 1k + 3 head chars + 2 crc + nul */ int bufsz, crc = -1; unsigned char packetno = 1; int i, c, len = 0; int retry; for(;;) { for( retry = 0; retry < 16; ++retry) { if ((c = _inbyte((DLY_1S)<<1)) >= 0) { switch (c) { case 'C': crc = 1; goto start_trans; case NAK: crc = 0; goto start_trans; case CAN: if ((c = _inbyte(DLY_1S)) == CAN) { _outbyte(ACK); FLUSHINPUT; return -1; /* canceled by remote */ } break; default: break; } } } _outbyte(CAN); _outbyte(CAN); _outbyte(CAN); FLUSHINPUT; return -2; /* no sync */ for(;;) { start_trans: xbuff[0] = SOH; bufsz = 128; xbuff[1] = packetno; xbuff[2] = ~packetno; c = srcsz - len; if (c > bufsz) c = bufsz; if (c >= 0) { memset (&xbuff[3], 0, bufsz); if (c == 0) { xbuff[3] = CTRLZ; } else { memcpy (&xbuff[3], &src[len], c); if (c < bufsz) xbuff[3+c] = CTRLZ; } if (crc) { unsigned short ccrc = crc16_ccitt(&xbuff[3], bufsz); xbuff[bufsz+3] = (ccrc>>8) & 0xFF; xbuff[bufsz+4] = ccrc & 0xFF; } else { unsigned char ccks = 0; for (i = 3; i < bufsz+3; ++i) { ccks += xbuff[i]; } xbuff[bufsz+3] = ccks; } for (retry = 0; retry < MAXRETRANS; ++retry) { for (i = 0; i < bufsz+4+(crc?1:0); ++i) { _outbyte(xbuff[i]); } if ((c = _inbyte(DLY_1S)) >= 0 ) { switch (c) { case ACK: ++packetno; len += bufsz; goto start_trans; case CAN: if ((c = _inbyte(DLY_1S)) == CAN) { _outbyte(ACK); FLUSHINPUT; return -1; /* canceled by remote */ } break; case NAK: default: break; } } } _outbyte(CAN); _outbyte(CAN); _outbyte(CAN); FLUSHINPUT; return -4; /* xmit error */ } else { for (retry = 0; retry < 10; ++retry) { _outbyte(EOT); if ((c = _inbyte((DLY_1S)<<1)) == ACK) break; } FLUSHINPUT; return (c == ACK)?len:-5; } } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -