When using an interrupt-driven scheme, an interrupt is generated when a byte arrives through the serial port. The interrupt handler reads the byte from the port, which generally clears the interrupt source. At this point you have a choice of either processing the byte received in the ISR or putting the byte into some sort of buffer to let a background process handle the data. When you use a buffer, the size of the buffer depends on how quickly your background process can get control of the CPU to process the information. For example, if the worst case latency of your background process is 200 mS, you should plan for a buffer of at least 192 bytes if your serial port receives bytes at 9600 baud (960 bytes/sec. X 200 mS). A special type of buffer called a Ring Buffer (also called a Circular Buffer) is often used to capture data from a serial port.
To avoid allocating very large buffers, you can resort to what is called flow control. Basically, the interrupt receiving data can notify the sender that the receiver's buffer is getting full. The sender would then hold off with its transmission until the receiver empties out the buffer and notifies the sender that it can proceed. The most common flow control scheme is called XON-XOFF and it uses the ASCII characters DC1 (0x11) for XON (i.e., "send me more") and DC3 (0x13) for XOFF (i.e., "don't send me any more"). Using the XON-XOFF scheme precludes you from sending binary data because the data you are sending could happen to be one of these two characters.
Flow control can also be performed by using some of the RS-232C lines. This would allow you to send and receive binary data. Unfortunately, the RS-232C standard doesn't specify which lines to use when you are not interfacing to a modem. Nothing prevents you from using the modem control lines RTS, CTS, DSR, and DTR, but you will have to establish how flow control will work between your devices.
Input buffering using a ring buffer is shown in Figure 11.3. When bytes are received, the ISR reads the byte from the serial port (©) and places the byte into the ring buffer (©). Your application code (background) then monitors the ring buffer to see if bytes have been received (®). If the ring buffer is not empty, the "oldest" byte (least recent byte) is extracted from the ring buffer.
Was this article helpful?