| |

VerySource

 Forgot password?
 Register
Search
View: 981|Reply: 5

How to implement serial file transfer through XMODEM protocol

[Copy link]

1

Threads

1

Posts

2.00

Credits

Newbie

Rank: 1

Credits
2.00

 China

Post time: 2020-2-23 22:00:01
| Show all posts |Read mode
Two computers use xmodem protocol to transfer files through the serial port. Who has the vc ++ example code in this regard?
There are classes. What is needed is the xmodem file transfer protocol class. How to transfer files, mainly sending
Reply

Use magic Report

0

Threads

1

Posts

2.00

Credits

Newbie

Rank: 1

Credits
2.00

 China

Post time: 2020-8-24 08:30:01
| Show all posts
I am also doing this, but found a class (megepipenet), but I can't see the specific implementation.
There are many programs written in C. How can there be an explanation of its specific process?
Reply

Use magic Report

0

Threads

1

Posts

2.00

Credits

Newbie

Rank: 1

Credits
2.00

 China

Post time: 2020-8-24 09:00:01
| Show all posts
There are several types of XMODEM, which one do you mean? XMODEM-CRC? XMODEM-1K?
Reply

Use magic Report

0

Threads

2

Posts

2.00

Credits

Newbie

Rank: 1

Credits
2.00

 China

Post time: 2020-8-24 09:15:01
| Show all posts
Xmodem communication protocol
Xmodem protocol is an asynchronous file transport protocol widely used in personal computer communication using dial-up modems. This protocol transmits data in 128/1k byte blocks, and each block uses a checksum process for error detection. If the receiver's checksum for a block is the same as the sender's checksum, the receiver sends an approval byte to the sender. In order to facilitate readers to read the program, the main features of the protocol are briefly described below. For the complete protocol of Xmoden, please refer to other related materials.

Xmodem control characters: <soh> 01H, <stx> 02H <eot> 04H, <ack> 06H, <nak> 15H, <can> 18H, <eof> 1AH.

Xmodem transmission data block format: "<soh> <packNO> <255-packNO> <...128-byte data block...> <cksum>". Among them, <soh> is the starting byte; <packNO> is the data block number byte, incremented by one each time; <255-packNO> is the complement of the previous byte; the next is a data block with a length of 128 bytes ; The last <cksum> is the check code of 128 bytes of data, the length is 2 bytes.

The receiving end dominates the entire transmission. Start the transmission by sending <nak> or <stx> or'C'. The control character "C" has a special function. When the sender receives the "C" control character, it restarts to send the data block with CRC check (packNO = 1). The CRC check method uses 16 bits (X^16 + X^12 + X^5 + 1). When the sender receives the first <nak>, it starts sending in checksum mode. When the sender receives the first <stx>, it starts sending in 1k-crc mode. When the receiving end receives a data block and the verification is correct, it sends back <ack>; receiving error sends back <nak>; and sends back <can> to indicate that the sender should stop sending. After receiving <ack>, the sender can continue to send the next data block (packNO+1). Each time a new data block <packNO> is sent, 1 is added, and the <packNO> of the next data block after adding 0xFF is zero. When receiving <nak>, the last data block can be resent again. The sender sends <eot> to indicate that all data has been sent. If the last data to be sent is less than 128 bytes, fill a data block with <eof>.
Reply

Use magic Report

0

Threads

2

Posts

2.00

Credits

Newbie

Rank: 1

Credits
2.00

 China

Post time: 2020-8-24 09:30:01
| Show all posts
Written in C, the same principle

#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;
To
}
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;
To
}
To
To
file_len-=128;
}
    write_char(XYMODEM_EOT);

xmodem_flush();
     
return 0;

} /* end function Xmodem_SendData */
Reply

Use magic Report

0

Threads

1

Posts

2.00

Credits

Newbie

Rank: 1

Credits
2.00

 China

Post time: 2020-9-4 17:00:01
| Show all posts
Learn, thank you for sharing!
Reply

Use magic Report

You have to log in before you can reply Login | Register

Points Rules

Contact us|Archive|Mobile|CopyRight © 2008-2023|verysource.com ( 京ICP备17048824号-1 )

Quick Reply To Top Return to the list