What are Serial Ports?
Serial communication refers to the serial port sending and receiving bytes in bits. Although the serial communication of the byte is slow, the serial port can use one line to send data while receiving data on another line. The serial communication protocol refers to the content of the data packet, which includes the start bit, the main data, the check bit, and the stop bit. Both parties need to agree on a consistent packet format in order to receive and send data normally. In serial communication, commonly used protocols include RS-232, RS-422, and RS-485.
- RS-232 communication allows simple connection of three wires: Tx, Rx and ground. But for data transmission, both parties must use the same baud rate for data timing. Although this method is sufficient for most applications, its use is limited for receiver overload situations. At this time, the handshake function of the serial port is required. In this section, we discuss the three most commonly used RS-232 handshake forms: software handshake, hardware handshake, and Xmodem.
- a. Software handshake: The first handshake we discuss is software handshake. Usually used when the actual data is a control character, similar to the way GPIB uses command strings. The necessary lines are still three: Tx, Rx, and ground, because the control characters are not different from ordinary characters on the transmission line. The function SetXModem allows or disables the use of two control characters, XON and XOFF. These characters are sent by the receiver during communication, causing the sender to pause.
- For example: Suppose the sender sends data at a high baud rate. During transmission, the receiver found that the input buffer was full because the CPU was busy with other tasks. To temporarily stop the transmission, the receiver sends XOFF. The typical value is 19 decimal, that is, 13 hexadecimal, until the input buffer is empty. Once the receiver is ready to receive, it sends XON, the typical value is 17 in decimal, 11 in hexadecimal, to continue communication. When the input buffer is half full, LabWindows sends XOFF. In addition, if XOFF transmission is interrupted, LabWindows will send XOFF when the buffer reaches 75% and 90%. Obviously, the sender must follow this code to ensure that the transmission continues.
- b. Hardware handshake: The second is to use hardware wire handshake. As with the Tx and Rx lines, RTS / CTS and DTR / DSR work together, one as an output and the other as an input. The first set of lines are RTS (Request to Send) and CTS (Clear to Send). When the receiver is ready to receive data, it sets the RTS line high to indicate that it is ready. If the sender is also ready, it sets CTS high to indicate that it is about to send data. The other set of lines are DTR (DataTerminal Ready) and DSR (Data SetReady). These lines are mainly used for modem communication. Make the serial port and the modem communicate their status. For example: When the Modem is ready to receive data from the PC, it raises the DTR line to indicate that the connection to the telephone line has been established. Read the DSR line and set it high, and the PC will start sending data. A simple rule is that DTR / DSR is used to indicate that the system is ready for communication, while RTS / CTS is used for the transmission of a single data packet.
- In LabWindows, the function SetCTSMode enables or disables the use of hardware handshake. If CTS mode is enabled, LabWindows uses the following rules:
- When the PC sends data:
- The RS-232 library must detect the CTS line high before sending data.
- When the PC receives data:
- If the port is open and the input queue is free to receive data, the library function sets RTS and DTR high.
- If the input queue is 90% full, the library function sets RTS low, but keeps DTR high.
- If the port queue is nearly empty, the library function sets RTS high, but keeps DTR high.
- If the port is closed, the library function deasserts RTS and DTR.
- c, XModem handshake: The handshake discussed at last is called XModem file transfer protocol. This protocol is very common in Modem communication. Although it is commonly used in modem communication, the XModem protocol can be used directly in communication with other devices that follow this protocol. In LabWindows, the actual XModem application is hidden from the user. As long as the PC and other devices use the XModem protocol, the XModem function of LabWindows is used in the file transfer. The functions are XModemConfig, XModemSend and XModemReceive.
- XModem uses a protocol between the following parameters: start_of_data, end_of_data, neg_ack, wait_delay, start_delay, max_tries, packet_size. These parameters need to be determined by the communicating parties. The standard XModem has a standard definition: however, it can be modified through the XModemConfig function to meet specific needs. The use of these parameters is determined by the character neg_ack sent by the receiver. This informs the sender that it is ready to receive data. It starts to try to send with a timeout parameter start_delay; when the timeout attempts exceed max_ties times, or it receives start_of_data from the receiver, the sender stops trying. If start_of_data is received from the sender, the receiver will read the subsequent information packet. The packet contains the number of packets, the complement of the number of packets as error checking, the actual packet size of packet_size bytes, and the sum check value for further error checking. After reading the data, the receiver calls wait_delay and sends a response to the sender. If the sender does not receive a response, it will resend the packet until it receives a response or exceeds the maximum number of retransmissions max_tries. If no response is received, the sender informs the user that the data transmission failed.
- Since the data must be sent in packets of pack_size bytes, when the last data packet is sent, if the data is not full enough to fill a data packet, the ASCII code NULL (0) bytes will be filled later. This results in more data being received than the original data. Do not use XON / XOFF in the case of XModem, because the number of packets sent by the XModem sender is likely to increase to the value of the XON / OFF control character, resulting in communication failure.