1. Basic Knowledge of RS232
Data transmission between computers or between a computer and a terminal can be achieved through two methods: serial communication and parallel communication.Serial communication is widely adopted due to its lower cost and fewer lines used, especially in remote transmission, which avoids inconsistencies in multiple line characteristics.
In serial communication, both parties must use a standard interface to facilitate communication between different devices.The RS-232-C interface (also known as EIA RS-232-C) is currently the most commonly used serial communication interface.
RS-232-C is a serial physical interface standard established by the Electronic Industry Association (EIA) in the United States.RS is an abbreviation for “Recommended Standard,” 232 is the identification number, and C indicates the revision number, representing the latest modification of RS232 (1969), with previous versions being RS232B and RS232A.It was jointly developed in 1970 by the EIA, Bell System, modem manufacturers, and computer terminal manufacturers as a standard for serial communication.Its full name is “Serial Binary Data Exchange Interface Technical Standard between Data Terminal Equipment (DTE) and Data Communication Equipment (DCE).”
1. Electrical Characteristics
The EIA-RS-232C specifies electrical characteristics, logic levels, and the functions of various signal lines.
On TxD and RxD:Logic 1 (MARK) = -3V to -15V, Logic 0 (SPACE) = +3V to +15V. On control lines such as RTS, CTS, DSR, DTR, and DCD:
Signal active (ON state, positive voltage) = +3V to +15V
Signal inactive (OFF state, negative voltage) = -3V to -15V
Depending on the power supply of the device, levels such as ±5, ±10, ±12, and ±15 are possible.
2. Mechanical Characteristics of Connectors
Since RS-232C does not define the physical characteristics of connectors, various types of connectors such as DB-25, DB-15, and DB-9 have emerged, each with different pin definitions.Recently, the 8-pin RJ-45 connector has become increasingly common, although its pin assignments vary significantly.The EIA/TIA 561 standard specifies a method for pin assignments, but the widely used Yost Serial Device Wiring Standard, invented by Dave Yost, and many other devices do not adopt any of the aforementioned wiring standards.The table below lists the most commonly used signals and pin assignments in RS-232:
The signal annotations are from the perspective of the DTE device, where TD, DTR, and RTS signals are generated by the DTE, while RD, DSR, CTS, DCD, and RI signals are generated by the DCE.
The RS-232 port of a PC is a 9-pin socket.Some devices connected to the PC’s RS-232 interface only require three lines, namely “Transmit Data TXD,” “Receive Data RXD,” and “Signal Ground GND,” as they do not use each other’s transmission control signals.
The bidirectional interface can be made with only three wires because all signals in RS-232 share a common ground.The unbalanced circuit makes RS-232 very susceptible to voltage offset between the two devices.RS-232 also has relatively poor control over signal rise and fall times, making crosstalk issues likely.RS-232 is recommended for communication over short distances (within 15m).Due to the unbalanced circuit, RS-232 interface cables are typically not made of twisted pairs.
3. Transmission Cable
The RS-232-C standard specifies data transmission rates of 50, 75, 100, 150, 300, 600, 1200, 2400, 4800, 9600, 19200 baud, with drivers allowing a capacitive load of 2500pF, which limits communication distance.
For example, with a communication cable of 150pF/m, the maximum communication distance is 15m;if the capacitance per meter of the cable is reduced, the communication distance can be increased.Another reason for the short transmission distance is that RS-232 uses single-ended signal transmission, which suffers from common ground noise and cannot suppress common-mode interference, thus generally used for communication within 20m.
According to the RS-232C standard, under the condition that the pulse distortion is less than 4%, the length of the transmission cable should be 50 feet. In practice, this 4% pulse distortion is very conservative; in actual applications, about 99% of users operate within a pulse distortion range of 10-20%, so the maximum distance in practical use can far exceed 50 feet. The American company DEC has allowed a pulse distortion of 10% and obtained the following experimental results.Cable 1 is a shielded cable, model DECP.NO.9107723, containing three pairs of twisted pairs, each pair made of 22# AWG, covered with a shielding mesh.Cable 2 is an unshielded cable, model DECP.NO.9105856-04, which is a 22#AWG four-core cable.
4. Link Layer
In the RS-232 standard, characters are transmitted one after another as a series of bits.The most commonly used encoding format is asynchronous start-stop format, which uses a start bit followed by 7 or 8 data bits, possibly a parity bit, and then two stop bits.Thus, sending a character requires 10 bits, which results in a good effect of making the overall transmission rate, the rate of sending signals, divided by 10.
Serial communication requires multiple settings in software, the most common settings include baud rate, parity, and stop bits.The baud rate refers to the rate at which bits are transmitted from one device to another, i.e., how many bits per second (bit/s).Typical baud rates are 300, 1200, 2400, 9600, 19200, etc.Generally, both devices at the communication ends must be set to the same baud rate, but some devices can also be set to automatically detect the baud rate.
Parity is used to verify the correctness of the data.Parity is generally not used; if used, it can be either odd or even parity.Parity works by modifying each transmitted byte (or limiting the bytes sent).If no parity is used, the data will not be changed.In even parity, the parity bit will be set to 1 or 0 (usually the highest or lowest bit) so that the number of “1” bits in all transmitted bits (including the bits of the character and the parity bit) is even;in odd parity, the number of “1” bits in all transmitted bits (including the bits of the character and the parity bit) is odd.Parity can be used by the receiving end to check whether there was an error in transmission—if the number of “1” bits in a byte has changed, then that byte must have encountered an error during transmission.If the parity is correct, then either no error occurred or an even number of errors occurred.
The stop bit is sent after each byte transmission to help the receiving signal hardware resynchronize.
In serial communication software settings, D/P/S is a conventional symbol representation.8/N/1 (very common) indicates 8-bit data, no parity, and 1 stop bit.Data bits can be set to 7, 8, or 9, and the parity bit can be set to none (N), odd (O), or even (E), with the parity bit using a bit from the data, so 8/E/1 indicates a total of 8 data bits, with one bit used for the parity bit.The stop bit can be 1, 1.5, or 2 bits (1.5 is used for teletypes at a baud rate of 60 wpm).
5. Transmission Control
When handshake signals or data integrity checks are needed, additional settings must be established.Common combinations include RTS/CTS, DTR/DSR, or XON/XOFF (in practice, special characters are inserted into the data stream instead of using connector pins).
The receiver sends XON/XOFF signals to the sender to control when the sender transmits data; these signals are opposite to the direction of the transmitted data.The XON signal tells the sender that the receiver is ready to accept more data, while the XOFF signal tells the sender to stop sending data until the receiver is ready again.XON/XOFF is generally not recommended; it is recommended to use RTS/CTS flow control instead.
XON/XOFF is an in-band method that works between terminals, but both ends must support this protocol, and there may be confusion during sudden starts.
XON/XOFF can work on a 3-wire interface.RTS/CTS was originally designed for half-duplex cooperative communication between teletypes and modems, where only one modem can send data at a time.The terminal must send a request to send signal and then wait for the modem to respond with a clear to send signal.Although RTS/CTS achieves handshake through hardware, it has its advantages.
6. Limitations of the RS-232 Standard
Over the years, improvements in RS-232 devices and communication technology have greatly increased the communication distance of RS-232.However, since the RS-232 interface standard was established early, it inevitably has some shortcomings, mainly as follows:
(1) The signal level values of the interface are relatively high, which can damage the interface circuit chips, and since they are not compatible with TTL levels, level conversion circuits are required to connect with TTL circuits.
(2) The transmission rate is relatively low, with a baud rate of 20Kbps during asynchronous transmission.Now, with the use of new UART chips like the 16C550, baud rates can reach 115.2Kbps.
(3) The interface uses one signal line and one signal return line to form a common ground transmission form, which is prone to common-mode interference, making it weak against noise interference.
(4) The transmission distance is limited, with a maximum standard transmission distance of 50 meters, but in practice, it can only be used for about 15 meters.
2. Basic Knowledge of RS485
In response to the limitations of the RS-232 serial port standard, the RS-422 and RS-485 interface standards were proposed.RS-485/422 uses balanced transmission and differential reception to achieve communication:The transmitter converts the TTL level signal from the serial port into differential signals A and B, which are output, and after transmission through the cable, the differential signals are restored to TTL level signals at the receiving end.Since the transmission line typically uses twisted pairs and is a differential transmission, it has strong common-mode interference resistance, and the bus transceiver sensitivity is very high, capable of detecting voltages as low as 200mV.Thus, transmission signals can be restored even from kilometers away.
1. Electrical Characteristics of RS-485
The driver can output a common-mode voltage of ±7V.
The input resistance of the receiver RIN ≥ 12kΩ.
The input capacitance ≤ 50pF.
With 32 nodes and a 120Ω termination resistor, the driver can still output a voltage of at least 1.5V (the size of the termination resistor is related to the parameters of the twisted pair used).
On the transmitting end:Logic “1” is represented by a voltage difference of + (2 to 6)V between the two wires;Logic “0” is represented by a voltage difference of – (2 to 6)V.
The input sensitivity of the receiver is 200mV (i.e., (V+) – (V-) ≥ 0.2V indicates signal “0”;(V+) – (V-) ≤ -0.2V indicates signal “1”).
2. Transmission Rate and Distance
The maximum data transmission rate of RS-485 is 10Mbps, with a maximum communication distance of about 1219M; the transmission rate is inversely proportional to the transmission distance, and only at a transmission rate of 10Kb/S can the maximum communication distance be achieved.
However, since RS-485 often needs to communicate with the RS-232 port of a PC, the actual maximum is generally 115.2Kbps.Due to the high rate reducing the transmission distance, it is often around or below 9600bps.
3. Network Topology
The RS-485 interface uses a combination of balanced drivers and differential receivers, enhancing common-mode interference resistance, thus providing good noise immunity.RS-485 operates in half-duplex mode and supports multipoint data communication.
The RS-485 bus network topology generally adopts a terminal-matched bus structure.That is, a single bus connects all nodes in series, and it does not support ring or star networks.If a star structure is needed, RS-485 repeaters or hubs must be used.The RS-485/422 bus generally supports a maximum of 32 nodes; if special 485 chips are used, it can reach 128 or 256 nodes, with a maximum of 400 nodes supported.
4. Connectors
The international standard for RS-485 does not specify a connector standard, so terminal blocks or connectors like DB-9, DB-25 can be used.
3. Basic Knowledge of RS422
The electrical performance of RS-422 is similar to that of RS-485.The main differences are:
(1) RS-485 has 2 signal lines:Both sending and receiving are A and B.Since RS-485 shares two lines for both sending and receiving, it cannot send and receive simultaneously (half-duplex).
(2) RS-422 has 4 signal lines:Two for sending (Y, Z) and two for receiving (A, B).Since RS-422 separates sending and receiving, it can send and receive simultaneously (full-duplex).
(3) RS-422 supports multi-machine communication; shorting Y-A of RS-422 as A of RS-485 and Z-B of RS-422 as B of RS-485 can easily convert it to RS-485.
Many people often mistakenly believe that the RS-422 serial interface is the full-duplex version of the RS-485 serial interface; in fact, there are significant differences in electrical characteristics, common-mode voltage range, and receiver input resistance, making these two standards suitable for different application fields.The RS-485 serial interface driver can be used in RS-422 serial interface applications because the RS-485 serial interface meets all performance parameters of the RS-422 serial interface, but the reverse is not true.For RS-485 serial interface drivers, the common-mode voltage output range is between -7V and +12V;for RS-422 serial interface drivers, this performance index is only ±7V.The minimum input resistance of RS-422 serial interface receivers is 4KΩ;while the minimum input resistance of RS-485 serial interface receivers is 12KΩ.
Basic Knowledge of Serial Ports and Handshaking
1. Basic Knowledge of Serial Ports
The serial port is a very common device communication protocol on computers (do not confuse it with Universal Serial Bus or USB).Most computers contain two RS232-based serial ports.The serial port is also a common communication protocol for instrumentation devices;many GPIB-compatible devices also have RS-232 ports.Additionally, the serial communication protocol can be used to obtain data from remote collection devices.
The concept of serial communication is very simple; the serial port sends and receives bytes bit by bit.Although it is slower than parallel communication, which sends data byte by byte, the serial port can send data on one line while receiving data on another line.It is simple and can achieve long-distance communication.For example, when IEEE488 defines parallel communication, it states that the total length of device lines must not exceed 20 meters, and the length between any two devices must not exceed 2 meters;whereas for serial ports, the length can reach 1200 meters.
Typically, the serial port is used for the transmission of ASCII characters.Communication is accomplished using three lines:(1) Ground, (2) Transmit, (3) Receive.Since serial communication is asynchronous, the port can send data on one line while receiving data on another line.Other lines are used for handshaking but are not mandatory.The most important parameters of serial communication are baud rate, data bits, stop bits, and parity.For two ports communicating, these parameters must match:
1. Baud Rate
This is a parameter that measures communication speed.It indicates the number of bits transmitted per second.For example, 300 baud means sending 300 bits per second.When we refer to clock cycles, we are referring to the baud rate; for instance, if the protocol requires a baud rate of 4800, then the clock is 4800Hz.This means that the sampling rate on the data line for serial communication is 4800Hz.Typically, the baud rates for telephone lines are 14400, 28800, and 36600.Baud rates can be much higher than these values, but baud rate is inversely proportional to distance.High baud rates are often used for communication between instruments placed very close together, a typical example being communication between GPIB devices.
2. Data Bits
This is a parameter that measures the actual data bits in communication.When a computer sends a packet of information, the actual data will not be 8 bits; standard values are 5, 7, and 8 bits.How to set this depends on the information you want to transmit.For example, standard ASCII code is 0-127 (7 bits).Extended ASCII code is 0-255 (8 bits).If the data uses simple text (standard ASCII code), then each data packet uses 7 bits of data.Each packet refers to a byte, including start/stop bits, data bits, and parity bits.Since the actual data bits depend on the selected communication protocol, the term “packet” refers to any communication situation.
3. Stop Bits
These are used to indicate the last bit of a single packet.Typical values are 1, 1.5, and 2 bits.Since data is timed on the transmission line, and each device has its own clock, there may be slight desynchronization between the two devices during communication.Therefore, stop bits not only indicate the end of transmission but also provide an opportunity for the computer to correct clock synchronization.The more stop bits that are suitable, the greater the tolerance for desynchronization between clocks, but the data transmission rate also becomes slower.
4. Parity Bit
This is a simple error-checking method in serial communication.There are four error-checking methods: even, odd, high, and low.Of course, it is also possible to have no parity bit.For even and odd parity, the serial port will set a parity bit (one bit after the data bits) to ensure that the transmitted data has an even or odd number of logical high bits.For example, if the data is 011, then for even parity, the parity bit is 0, ensuring that the number of logical high bits is even.If it is odd parity, the parity bit is 1, resulting in three logical high bits.High and low bits do not actually check the data; they simply set the logical high or low for checking.This allows the receiving device to know the state of a bit and provides an opportunity to determine whether noise has interfered with communication or whether the transmission and reception of data are out of sync.
2. Basic Knowledge of Handshaking
The RS-232 communication method allows for simple connections with three lines:Tx, Rx, and ground.However, for data transmission, both parties must use the same baud rate for timing.Although this method is sufficient for most applications, it is limited in cases of receiver overload.In such cases, the handshaking function of the serial port is required.In this section, we discuss the three most commonly used forms of RS-232 handshaking:software handshaking, hardware handshaking, and Xmodem.
1. Software Handshaking
The first handshaking method we discuss is software handshaking.This is typically used when the actual data consists of control characters, similar to how GPIB uses command strings.The necessary lines remain three:Tx, Rx, and ground, as control characters are indistinguishable from regular characters on the transmission line; the function SetXModem allows users to enable or disable the use of two control characters, XON and OXFF.These characters are sent by the receiver during communication to pause the sender.
For example:Assuming the sender is transmitting data at a high baud rate.During transmission, the receiver finds that the CPU is busy with other tasks, and the input buffer is full.To temporarily stop transmission, the receiver sends XOFF, typically the decimal value 19, which is hexadecimal 13, until the input buffer is empty.Once the receiver is ready to receive again, it sends XON, typically the decimal value 17, which is hexadecimal 11, to continue communication.When the input buffer is half full, LabWindows sends XOFF.Additionally, if the XOFF transmission is interrupted, LabWindows will send XOFF when the buffer reaches 75% and 90% full.Clearly, the sender must adhere to this protocol to ensure transmission continues.
2. Hardware Handshaking
The second method is to use hardware line handshaking.Along with Tx and Rx lines, RTS/CTS and DTR/DSR work together, one as output and the other as input.The first pair of lines is 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 readiness; if the sender is also ready, it sets the CTS line high to indicate it will send data.The second pair of lines is DTR (Data Terminal Ready) and DSR (Data Set Ready).These are mainly used for modem communication.They indicate the status of communication between the serial port and the modem.For example:When the modem is ready to receive data from the PC, it sets the DTR line high, indicating that the connection to the telephone line has been established.Reading the DSR line high indicates that the PC can start sending data.A simple rule is that DTR/DSR indicates system communication readiness, while RTS/CTS is used for the transmission of individual data packets.
In LabWindows, the function SetCTSMode enables or disables hardware handshaking.If CTS mode is enabled, LabWindows follows these 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 there is empty space in the input queue for receiving 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 sets RTS and DTR low.
3. XModem Handshaking
The last handshaking method we discuss is the XModem file transfer protocol.This protocol is very common in modem communication.Although it is typically used in modem communication, the XModem protocol can also be used directly in communication between 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 LabWindows XModem functions are used for file transfer.The functions are XModemConfig, XModemSend, and XModemReceive.
XModem uses a protocol with the following parameters:start_of_data, end_of_data, neg_ack, wait_delay, start_delay, max_tries, packet_size.These parameters need to be agreed upon by both communicating parties; the standard XModem has a standard definition;however, it can be modified through the XModemConfig function to meet specific needs.The usage of these parameters is determined by the character neg_ack sent by the receiver.This notifies the sender that it is ready to receive data.It begins attempting to send, with a timeout parameter start_delay;when the number of timeout attempts exceeds max_tries, or the sender receives start_of_data from the receiver, the sender stops attempting.If the sender receives start_of_data, the receiver will read the subsequent information data packet.The packet contains the packet number, the complement of the packet number for error checking, the actual data packet of packet_size bytes, and a checksum for 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 a response, it will resend the data packet until it receives a response or exceeds the maximum number of retries max_tries.If no response is received, the sender notifies the user that data transmission has failed.
Since data must be sent in packets of packet_size bytes, when the last data packet is sent, if there is not enough data to fill a packet, it will be padded with ASCII NULL (0) bytes.This results in the received data being larger than the original data.In the case of XModem, do not use XON/XOFF, as the number of packets sent by the XModem sender may exceed the values of the XON/OFF control characters, leading to communication failure.