Current location - Quotes Website - Collection of slogans - Serial communication of single chip microcomputer
Serial communication of single chip microcomputer
Basic knowledge of serial communication

Basic concepts of serial communication

1, what is a serial port?

2. What is RS-232?

3. What is RS-422?

4. What is RS-485?

5. What is a handshake?

1, what is a serial port?

Serial port is a very common communication protocol for devices on computers (not to be confused with universal serial bus or USB). Most computers contain two serial ports based on RS232. Serial port is also a common communication protocol for instruments. Many GPIB-compatible devices also have RS- 232 ports. At the same time, serial communication protocol can also be used to obtain the data of remote acquisition equipment.

The concept of serial communication is very simple. The serial port sends and receives bytes bit by bit. Although it is slower than byte-by-byte parallel communication, serial port can use one line to send data and another line to receive data. Simple and can realize long-distance communication. For example, when IEEE488 defines the parallel traffic state, it stipulates that the total length of equipment lines should not exceed 20 meters, and the length between any two devices should not exceed 2 meters; For serial port, the length can reach 1200m.

Usually, serial ports are used to transfer ASCII characters. Communication is completed with three wires: (1) ground wire, (2) transmission and (3) reception. Because serial communication is asynchronous, ports can send data on one line and receive data on another line. Other lines are used to shake hands, but they are not necessary. The most important parameters of serial communication are baud rate, data bits, stop bits and parity. For two pass-through ports, these parameters must match:

A. Baud rate: This is a parameter to measure the communication speed. It represents the number of bits transmitted per second. For example, 300 baud means sending 300 bits per second. When we talk about clock cycle, we mean baud rate. For example, if the protocol requires 4800 baud rate, the clock is 4800Hz. This means that the sampling rate of serial communication on the data line is 4800Hz. Usually the baud rates of telephone lines are 14400, 28800 and 36600. The baud rate can be much higher than these values, but the baud rate is inversely proportional to the distance. High baud rate is usually used for communication between instruments placed nearby, and a typical example is communication between GPIB devices.

B. Data bit: This is a parameter to measure the actual data bit in communication. When a computer sends a package, the actual data will not be 8 digits, and the standard values are 5, 7 and 8 digits. How to set it depends on the information you want to send. For example, the standard ASCII code is 0 ~ 127 (7 bits). The extended ASCII code is 0 ~ 255 (8 bits). If the data uses simple text (standard ASCII code), each packet uses 7 bits of data. Each packet refers to one byte, including start/stop bits, data bits and parity bits. Since the actual data bits depend on the choice of communication protocol, the term "packet" refers to any communication situation.

C stop bit: used to indicate the last bit of a single packet. Typical values are 1, 1.5 and 2 digits. Because data is clocked on the transmission line and each device has its own clock, two devices may be a little out of sync when communicating. Therefore, the stop bit not only indicates the end of transmission, but also provides the computer with an opportunity to correct clock synchronization. The more suitable stop bits, the greater the tolerance of different clock synchronization, but the slower the data transmission rate.

D parity bit: a simple error detection method in serial communication. There are four error detection methods: even, odd, high and low. Of course, there is no parity bit. In the case of parity, the serial port will set a parity bit (one bit after the data bit) to ensure that the transmitted data has even or odd logical high bits with values. For example, if the data is 0 1 1, then for even parity, the parity bit is 0, and it is ensured that the number of logical high bits is even. In case of odd parity, the parity bit is 1, so there are three logical high bits. The high and low bits don't really check the data, just set the logic high or logic low check. This enables the receiving device to know the status of the bit, and has the opportunity to judge whether noise interferes with communication or whether the transmission and reception of data are out of synchronization.

2. What is RS-232?

RS-232 (ANSI/EIA-232 standard) is a serial connection standard on IBM-PC and its compatible computers. It can be used for many purposes, such as connecting a mouse, printer or modem, and it can also be connected to industrial instruments. In order to improve the driving and wiring, the transmission length or speed of RS-232 often exceeds the standard value in practical application. RS-232 is limited to point-to-point communication between PC serial port and equipment. The longest distance of RS- 232 serial communication is 50 feet.

DB-9 pin connector

-

\ 1 2 3 4 5 /

\ 6 7 8 9 /

-

Cross section of the line connected from the computer.

Function of RS-232 pin:

Data:

TXD (pin 3): serial data output.

RXD (pin 2): serial data input

Shake hands:

RTS (Pin 7): Send data request.

CTS (Pin 8): Clear Send

DSR (pin 6): Data transmission is ready.

DCD (pin 1): data carrier detection.

DTR (Pin 4): The data terminal is ready.

Ground wire:

GND (5th pin): ground wire

other

RI (Pin 9): Ring indication

3. What is RS-422?

RS -422(EIA RS-422-A standard) is the serial port connection standard of Apple Macintosh computer. RS-422 uses differential signals, and RS-232 uses signals from unbalanced reference ground. Differential transmission uses two wires to send and receive signals. Compared with RS-232, it has better noise resistance and longer transmission distance. Better noise immunity and longer transmission distance are a big advantage in industrial environment.

4. What is RS-485?

RS -485(EIA-485 standard) is an improvement of RS-422, because it increases the number of devices from 10 to 32, and defines the electrical characteristics under the condition of the maximum number of devices to ensure sufficient signal voltage. With the ability of multiple devices, a device network can be established with one RS-422 port. Excellent anti-noise and multi-device ability. When establishing a distributed device network connected to PC, other data acquisition controllers, HMI or other operations in industrial applications, RS-485 will be selected for serial connection. RS-485 is a superset of RS-422, so all RS-422 devices can be controlled by RS-485. RS-485 can be used for serial communication with cable length exceeding 4000 feet.

DB-9 pin connection

-

\ 1 2 3 4 5 /

\ 6 7 8 9 /

-

Cross section of the line connected from the computer.

Functions of RS-485 and RS-422 pins

Data: TXD+ (pin 8), TXD- (pin 9), RXD+ (pin 4), RXD- (pin 5).

Handshake: RTS+ (pin 3), RTS- (pin 7), CTS+ (pin 2), CTS- (pin 6).

Ground wire: GND (pin 1)

5. What is a handshake?

The RS -232 access mode 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 when the receiver is overloaded. At this time, the handshake function of serial port is needed. In this section, we will discuss the three most commonly used forms of RS-232 handshake: software handshake, hardware handshake and Xmodem.

A. Software handshake: The first handshake we discussed was software handshake. It is usually used when the actual data is control characters, similar to the way GPIB uses command strings. There are still three necessary lines: Tx, Rx and ground. Because control characters are no different from ordinary characters on the transmission line, the SetXModem function allows users to enable or disable two control characters, XON and OXFF. These characters are sent by the receiver in communication, making the sender 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 work. In order to temporarily stop transmission, the receiver sends XOFF, with a typical value of decimal 19, that is, hexadecimal 13, until the input buffer is empty. Once the receiver is ready to receive, it sends XON, with a typical value of 17 in decimal, that is, 1 1 in hexadecimal, to continue the communication. When the input buffer is half full, LabWindows sends XOFF. In addition, if the 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 lines to shake hands. Like Tx and Rx lines, RTS/CTS and DTR/DSR work together, with one as the output and the other as the input. The first set of lines are RTS (Request to Send) and CTS (Allow to Send). When the receiver is ready to receive data, it will set the RTS line high to indicate that it is ready. If the sender is ready, it will set CTS to high level, indicating that it is about to send data. The other set of wires are DTR (data terminal ready) and DSR (data set ready). These are now mainly used for modem communication. Let the serial port and modem communicate their status. For example, when the modem is ready to receive data from a PC, it sets the DTR line to a high level, indicating that the connection with the telephone line has been established. Read the DSR line and set it to high level, and the PC starts to send data. A simple rule is that DTR/DSR is used to indicate the communication readiness of the system, while RTS/CTS is used to transmit a single packet.

In LabWindows, the function SetCTSMode enables or disables hardware handshaking. If CTS mode is enabled, LabWindows will use the following rules:

When the computer sends data:

RS-232 library must detect the height of CTS line before sending data.

When the PC receives the data:

If the port is open, the input queue can receive data freely, and the library function sets RTS and DTR to high level.

If the input queue is 90% full, the library function sets RTS to low, but keeps DTR high.

If the port queue is almost empty, the crying number sets RTS high, but keeps DRT high.

If the port is closed, the library function sets RTS and DTR to low.

C, XModem handshake: the handshake discussed at last is called XModem file transfer protocol. This protocol is very common in modern communication. Although XModem protocol is usually used for modem communication, it can also be directly used for other devices that follow this protocol. In LabWindows, the actual XModem application is hidden from users. As long as devices such as PC use XModem protocol, file transfer uses XModem function of LabWindows. These functions are XModemConfig, XModemSend and XModemReceive.

XModem uses protocols with the following parameters: start_of_data, end_of_data, neg_ack, wait_delay, start_delay, max_tries and packet_size. These parameters need to be confirmed by both communication parties, and the standard XModem has a standard definition: however, it can be modified by XModemConfig function to meet specific requirements. 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, and the timeout parameter is start _ delay;; When the number of timeout attempts exceeds max_ties, or when the start_of_data sent by the receiver is received, the sender stops trying. If start_of_data is received from the sender, the receiver will read the subsequent packets. 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 of further error checking. After reading the data, the receiver will call wait_delay and then send a response to the sender. If the sender does not receive the response, it will resend the packet until it receives the 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.

Because data must be sent in packets in the unit of pack_size bytes, when the last packet is sent, if the data is not enough to fill a packet, ASCII empty (0) bytes will be filled later. This will result in receiving more data than the original data. Never use XON/XOFF in the case of XModem, because the number of packets sent by XModem sender is likely to increase to the value of XON/OFF control character, which will lead to communication failure.