|
发表于 2020-8-24 09:30:01
|
显示全部楼层
C写的,原理一样
#define XYMODEM_SOH 0x01
#define XYMODEM_STX 0x02
#define XYMODEM_EOT 0x04
#define XYMODEM_ACK 0x06
#define XYMODEM_BSP 0x08
#define XYMODEM_NAK 0x15
#define XYMODEM_CAN 0x18
#define XYMODEM_EOF 0x1A // ^Z for DOS officionados
int Xmodem_SendData(unsigned int addr, unsigned int size)
{
com_addr=xmodem_init();
int res;
int file_len;
file_len=size;
unsigned char *p=(unsigned char *)addr;
unsigned char pack_num;
pack_num=1;
int i;
unsigned short check_sum;
check_sum=0;
char c;
int count;
count=129;
while(file_len>0)
{
res = CYGACC_COMM_IF_GETC_TIMEOUT(com_addr, &c);
if(file_len<128)
{
count=file_len;
}
if(!res)
{
continue;
}
if(c==XYMODEM_CAN)
{
return -1;
}
if((pack_num!=1)&&(c==XYMODEM_NAK))
{
write_char(XYMODEM_SOH);
pack_num-=1;
write_char(pack_num);
write_char(~pack_num);
pack_num++;
p=p-128;
for(i=0;i<128;i++)
{
if(i<count)
{
write_char(*p);
check_sum+=*p;
p++;
}
else
{
write_char(XYMODEM_EOF);
check_sum+=XYMODEM_EOF;
}
}
write_char(check_sum);
check_sum=0;
}
else
{
write_char(XYMODEM_SOH);
write_char(pack_num);
write_char(~pack_num);
pack_num++;
for(i=0;i<128;i++)
{
if(i<count)
{
write_char(*p);
check_sum+=*p;
p++;
}
else
{
write_char(XYMODEM_EOF);
check_sum+=XYMODEM_EOF;
}
}
write_char(check_sum);
check_sum=0;
}
file_len-=128;
}
write_char(XYMODEM_EOT);
xmodem_flush();
return 0;
} /* end function Xmodem_SendData */
|
|