In this tip, you will learn the basics of the Xmodem protocol.
Xmodem is a simple serial file transfer protocol that allows users to transmit files between different computers. Over time, several modified versions of this protocol have been developed.Xmodem breaks the original data into a series of packets which are sent to the receiver. Each packet includes a CRC (Cyclic Redundancy Check) which is used for validation and error detection on the receiving end.
Features:
Operates in half-duplex mode
Uses 128-byte packets,
Employs ACK/NAK responses
Support 16 Bits CRC for error checking
The following control characters are used for protocol flow control:
-SOH – start of header: 0x01-EOT – end of transmission: 0x04-ACK – acknowledge: 0x06-NAK – not acknowledge: 0x15-ETB – end of transmission block: 0x17-CAN – cancel (force receiver to start sending C’s): 0x18-C – ASCII “C” which means initiation of the start: 0x43
Format of the packet
Byte 1: must contain one of the following values: SOH, EOT, CAN or ETB. Any other value is considered an error.
Bytes 2 and 3 represent the packet number and “checksum” (should be always equal 0xff) – Byte 2 contains the packet number (starting at 1 and rolling over to 0 after 255) and byte 3 contains the complement of the packet number (i.e., 0xFF – packet number), ensuring that the sum of bytes 2 and 3 is always 0xFF.
Bytes 4 – 131: These bytes form the data packet and can be anything.
Bytes 132 and 133: form the 16-bit CRC, calculated exclusively over the data of bytes 4–131
Communication flow
Transfers are receiver-driven – the transmitter waits until it receives the character “C” from the receiver, which signals that the receiver is ready to begin the file transfer.
The receiver initiates the transfer by sending an ASCII “C” character (0x43) to the sender, indicating it is ready to receive data.
After sending the initial “C”, the receiver waits for a response. It will either wait for up to 3 seconds (time-out) or until a buffer full flag is set. If the transmitter does not answer, then another “C” is sent and the 3 seconds time-out is reset.
This process repeats until the receiver receives a complete 133-byte packet.
The receiver can cancel the transfer at any time by sending a CAN byte. In contrast, the transmitter can only cancel the transfer between blocks by sending a CAN byte.
The receiver performs the following checks on each packet:
Packet Number Validation: It verifies that the sent packet number matches the expected packet number.
1’s Complement Check: The receiver adds the 1’s complement (from the 3rd byte) and the packet number (from the 2nd byte) to ensure that the sum equals 0xFF (255).
CRC Verification: After retrieving the data, the receiver calculates the CRC over the data packet (bytes 4–131) and compares it with the received CRC (bytes 132–133) to ensure data integrity.
Once the receiver checks and validates the packet, it responds with either an ACK or NAK (in case of a non-valid packet), during which the transmitter will either send the following packet or resend the latest packet. This process continues until the receiver receives an EOT byte, which is then properly ACK-ed to the sender.
If the transmitter sends an EOT byte instead of a SOH byte, the receiver responds with a NAK byte. If the transmitter then sends another EOT immediately afterward, the receiver sends an ACK byte, signaling the completion of the transfer.
Example
Here is an example of the data flow (on the left), and the process of sending a 3-block message (on the right):