# Handshaking

A common error condition in RS-232 communications is that the receiver appears to lose data the sender transmits. This condition typically results because the receiver cannot empty its input queue quickly enough.

Handshaking prevents overflow of the input queue that occurs when the receiver cannot empty its input queue as quickly as the sender is able to fill it. The RS-232 Library has two types of handshaking: software handshaking and hardware handshaking. Enable one or the other if you want to ensure that your application program synchronizes its data transfers with other serial devices that perform handshaking.

## Software Handshaking

SetXMode enables software handshaking. You can use software handshaking to transfer ASCII data or text to or from a serial device that also uses software handshaking. The RS-232 Library performs software handshaking by sending and monitoring incoming data for special data bytes (XON and XOFF, or decimal 17 and 19). These bytes indicate whether the receiver is ready to receive data.

Do not enable software handshaking when you transmit binary data because the special XON/XOFF characters can occur as part of the data stream and the receiver can mistake them as control codes. However, you can enable hardware handshaking regardless of the type of data you transfer.

Software handshaking does not require a special cable configuration.

## Hardware Handshaking

SetCTSMode enables hardware handshaking. For hardware handshaking to work, two conditions must exist. First, the serial devices must follow the same or similar hardware handshake protocols; they must use the same lines for the handshake and assign the same meanings to those lines. Second, the serial cable that connects the two devices must include the lines required to support the protocol. Because no single well-defined hardware handshake protocol exists, you must resolve any differences between the LabWindows/CVI hardware handshake protocol and the protocol your device uses.

Most serial devices primarily rely on the CTS and RTS lines to perform hardware handshaking and use the DTR line to signal the device's online presence to the other device. Some serial devices also use the DTR line for hardware handshaking, similar to the CTS line. SetCTSMode has two different modes to handle either case.

SetCTSMode employs the following line behaviors for each mode:

LWRS_HWHANDSHAKE_OFF

The library raises the RTS and DTR lines when opening the port and lowers them when closing the port. The library sends data from the port regardless of the status of CTS.

| Note You can use ComSetEscape to change the state of the RTS and DTR lines.

LWRS_HWHANDSHAKE_CTS_RTS

- When the PC is the receiver:
  - If the port is opened, the library raises RTS and DTR.
  - If the input queue of the port is nearly full, the library lowers RTS.
  - If the input queue of the port is nearly empty, the library raises RTS.
  - If the port is closed, the library lowers RTS and DTR.
- When the PC is the sender, the RS-232 Library must detect that its CTS line is high before it sends data from the port.

LWRS_HWHANDSHAKE_CTS_RTS_DTR

- When the PC is the receiver:
  - If the port is opened, the library raises RTS and DTR.
  - If the input queue of the port is nearly full, the library lowers RTS and DTR.
  - If the input queue of the port is nearly empty, the library raises RTS and DTR.
  - If the port is closed, the library lowers RTS and DTR.
- When the PC is the sender, the RS-232 Library must detect that its CTS line is high before it sends data from the port.

| Note The only difference between LWRS_HWHANDSHAKE_CTS_RTS and LWRS_HWHANDSHAKE_CTS_RTS_DTR is the behavior of the DTR line.

If the handshaking mechanism of your device uses the CTS and RTS lines, refer to Table 2 and Table 3 in the RS-232 Cable Information section for information about how to configure your cable. Your cable can omit the connection between pins 6 and 20 if your device does not monitor DSR when it sends data. Notice that the RTS pin of the receiver translates to the CTS pin of the sender and that the DSR pin of the receiver translates to the DTR pin of the sender.

If you want to use hardware handshaking but your device uses a different hardware handshake protocol than the ones described here, you can build a cable that resolves the differences. Refer to the pin description in Table 1 in the RS-232 Cable Information section for information about constructing a cable to serve your special needs.