介绍X-MODEM协议(转载)

类别:编程语言 点击:0 评论:0 推荐:
XModem Protocol

XModem protocol was developed and first implemented by Ward Christensen in 1977 and placed in the public domain becoming one of the earliest and most popular protocols for file transfers between various hardware platforms.

XModem is the simplest and one of the slowest of the file transfer protocols. It uses data blocks of only 128 bytes, requires an acknowledgement of every single block transmitted and uses only a simple checksum for data validation.

XModem blocks have the following format:

<SOH> 1 byte

<BlockNo> 1 byte

<~BlockNo> 1 byte

<Data> 128 bytes

<Checksum> 1 byte SOH marks start of the block. BlockNo is a one byte block number. The block numbers have range [1, 255]. After 255, the block number cycle starts again. ~BlockNo is a complement of the block number. Data are a text long 128 characters. If the text is shorter than 128 characters, the data block is filled with padding characters (CtrlZ by default). Checksum is an arithmetic sum (modulo-256) of all data bytes.

A typical XModem protocol transfer looks like this:

Transmitter Receiver

<-

<NAK> handshake

<SOH><1><254><Data 128 bytes><CHK>

->

response

<-

<ACK> acknowledgment

<SOH><2><253><Data 128 bytes><CHK>

->

<-

<ACK>

<EOT>

->

end of transmission

<-

<ACK> Handshake: the receiver always begins communication in start-up phase by sending a handshake character (in this case a NAK character). It waits for 10 seconds for the response. If it does not receive a block within 10 seconds it sends another NAK. Receiver tries up to 10 times to establish the handshake when it gives up. Response: transmitter sends requested data. Data are divided in blocks 128 characters long. Timeout for each character to be received is 1 second. Transmitter waits after first block up to 1 minute for the receiver's acknowledgement. This gives the receiver time for disk storage preparation. Acknowledgement: receiver calculates the checksum and compares it to the checksum received. If checksums are the same, it sends positive acknowledgement ACK. If they differ, the receiver sends a NAK and the transmitter resends the current block. This continues until the all data is transmitted. End of transmission: after the all data are transmitted, the transmitter notifies that by sending a EOT character. Either side can cancel transmission at any time by sending 3 CAN (CtrlX) characters.

Protocol does not support any additional information about the file being transmitted, like file name or file size. Hence, the received file size is always a multiple of the block size. Since XModem does not escape any binary data (like XON and XOFF), a software flow control is not possible. For binary data transfer hardware flow control (called also RTS/CTS Control - Ready to Send/Clear to Send) should be selected.

XModem Extensions

XModem CRC    improves error checking by substituting 1 byte checksum with 2 bytes cyclic redundancy check (CRC16). This offers much higher level of data integrity. The receiver indicates this protocol by sending the 'C' character as a handshake. Receiver tries up to 3 times to start data transfer when it concludes that transmiter is not capable.

<CRC16> 2 bytes

CRC HiByte

CRC LoByte XModem uses a non-reversed CRC algorithm with divisor polynomial X16+X12+X5+X0.
Transmitter Receiver

<-

C

<SOH> 1 byte

<BlockNo> 1 byte

<~BlockNo> 1 byte

<Data> 128 bytes

<CRC16> 2 bytes

->

<-

<ACK>

<EOT>

->

<-

<ACK>

XModem 1k    improves data transfer speed by increasing block size up to 1024 bytes. Each block starts with an STX character rather than an SOH. A CRC16 is used for the checksum. Because of the larger block size, number of times the transmitter must wait for an acknowledgement is reduced and transmission speed is increased. With noisy lines throughput is reduced significantly, because the block must be retransmitted is bigger. If transmitter receives 5 NAK characters in a row, it should decrease block size to 128 bytes. Once the block size is reduced, it is never stepped back up to 1024 bytes.

Transmitter Receiver

<-

C

<STX> 1 byte

<BlockNo> 1 byte

<~BlockNo> 1 byte

<Data> 1024 bytes

<CRC16> 2 bytes

->

<-

<ACK>

<EOT>

->

<-

<ACK>

XModem 1kG    supports data streaming. The receiver request data by sending the 'G' character as a handshake. The transmiter continuously transmits blocks without waiting for acknowledgment. If the receiver does encounter a bad block, it aborts entire session by sending a NAK. To obtain this very high throughput, modems on both sides of the line must have enabled error correcting features.

Transmitter Receiver

<-

G

<STX> 1 byte

<BlockNo> 1 byte

<~BlockNo> 1 byte

<Data> 1024 bytes

<CRC16> 2 bytes

->

<STX> 1 byte

<BlockNo> 1 byte

<~BlockNo> 1 byte

<Data> 1024 bytes

<CRC16> 2 bytes

->

<EOT>

->

Reference:

XModem / YModem Protocol Reference by Chuck Forsberg [10-14-88]

 

本文地址:http://com.8s8s.com/it/it27966.htm