How do I send an Xon/Xoff character to control data flow?



Software Flow Control: Xon / Xoff

Xon-Xoff is a flow control method used in communication between devices. It is in distinction to hardware flow control, which is done by specific out-of-band signals, such as DTR/DSR or RTS/CTS on the RS232 standard protocol.

For example, you have two devices, A and B, and A is quicker at sending data than device B is able to receive and process the data. Device B will very quickly reach a point where it can no longer process any more data and will be overwhelmed by the amount of data being sent by device A. At this point, device B would need to send an Xoff character to device A, so that it stops sending data. It will not send anymore data to device B until device B has sent an Xon character to device A, thus allowing device A to know that device B is ready to receive more data.

Xon/Xoff flow control diagram

Xon-Xoff uses special codes agreed on by both the transmitter and receiver.

The table below is an example of some of the ways of expressing Xon-Xoff codes sent in the data flow, although these can be set to other values.

Code Meaning ASCII DEC HEX Keyboard
XOFF Pause transmission DC3 19 13 Ctrl + S
XON Resume transmission DC1 17 11 Ctrl + Q

Advantages of Xon/Xoff

  • The main advantage of Xon-Xoff flow control is that it only requires the minimum number of signal wiring, three. This is because Xon-Xoff flow control only requires two signals (send, receive), and of course a single common ground wire. Hardware flow control requires at least two additional wires between the two devices, such as in RTS/CTS and DTR/DSR. The cost of this was significant in the early days of computing.

Disadvantages of Xon/Xoff

  • Sending Xoff/Xon characters take up bandwidth because they are in-band signals. Due to this, they cannot be mistaken for data but must be recognised as flow control commands. This means that encoding is needed for the transmission of the Xon and Xoff characters so that the receiver does not mistake the commands as device control commands. This is usually done by some form of escape characters, but this means that more bandwidth is taken up by these commands. On the other hand, hardware signals such as RTS/CTS may be asserted very quickly as out-of-band-signals, taking up less bandwidth.