Serial communication protocol RS232

1. Introduction to serial communication protocol

​The concept of serial communication (Serial Communications) is very simple, the communication method of sending and receiving bytes by the serial port bit by bit. Although serial communication is slower than parallel communication that transmits bytes, serial ports can transmit data using only two wires.

Because serial communication is asynchronous, the port can send data on one wire while receiving data on the other. The simple explanation is: two people talk, one speaks, and one listens.

The most important parameters of serial communication are port (com), baud rate, data bits, stop bits and parity check. For two ports that need serial communication, these parameters must match and be followed together, which is also the prerequisite for serial communication.

1. Baud rate

The baud rate refers to the modulation rate of the data signal to the carrier, which is expressed by the number of times the carrier modulation state changes per unit time.

For example, the baud rate is 9600bps; it represents the transmission of 9600 bits per second, which is equivalent to dividing each second into 9600 equal parts.

Therefore, the time per 1 bit is 1/9600 second = 104.1666...us. About 0.1ms. Since it is 9600 equal parts, that is, every 1 bit is followed by the next bit, there is no additional interval. If two devices want to realize serial communication, the baud rate set by the transceiver must be the same, otherwise there is no way to realize the communication.

The same baud rate for sending and receiving can realize communication:

The sending and receiving baud rates are inconsistent, causing the RX end to fail to receive normally: 

2. Data structure of serial communication

Start bit: The start bit must be a logic 0 level that lasts for one bit time, marking the beginning of a character transmission, and the receiver can use the start bit to synchronize its receiving clock with the sender's data.

Data bit: The data bit follows the start bit and is the real effective information in the communication. The number of data bits can be agreed upon by both communication parties. When transmitting data, the low bit of the character is transmitted first, and then the high bit of the character is transmitted.

Parity check bit: The parity check bit only occupies one bit, which is used for odd check or even check, and the parity check bit is not necessary. If it is odd parity, it needs to ensure that the transmitted data has an odd number of logic high bits in total; if it is even parity, it needs to ensure that the transmitted data has a total of even number of logic high bits.

Stop bit: The stop bit can be 1, 1.5 or 2 bits, which can be set by software. It must be a logic 1 level, marking the end of the transmission of a character.

Idle bit: Idle bit means from the end of the stop bit of a character to the start bit of the next character, indicating that the line is in an idle state and must be filled with a high level.

2. RS232 serial communication protocol

Initially, the data was a simple process quantity of analog signal output. Later, the RS232 interface appeared on the instrument interface. This interface can realize point-to-point communication, but this method cannot realize the networking function, which led to the birth of RS485.

We know that the data transmission of serial communication is both 0 and 1. In single bus, I2C, and UART, the logic 1 or logic 0 is judged by the high and low level of a line, but the GND of this signal line is not connected with other devices. Communication in the common ground mode is formed. This common ground mode transmission is prone to interference, and the anti-interference performance is relatively weak. Therefore, RS485 with differential communication, support for multi-machine communication, and strong anti-interference is widely used.

The biggest feature of RS485 communication is that the transmission speed can reach more than 10Mb/s, and the transmission distance can reach about 3000 meters. Everyone needs to pay attention to that although the maximum speed and maximum transmission distance of 485 are very large, the transmission speed will slow down with the increase of distance, so the two cannot have both.

RS232, RS485 communication protocol is actually a standard, we can directly use this standard for communication without any problem at all. We can also customize the communication protocol. The custom communication protocol is written based on the requirements, and the protocol that meets the standards such as RS232 can communicate.

There are many standards for the physical layer of serial communication, such as those mentioned above. Here we mainly receive the RS232 standard, which mainly stipulates the purpose of the signal, the communication interface and the level standard of the signal.

In the above communication mode, the "DB9 interface" of the two communication devices is connected through the serial port signal line, and the "RS232 standard" is used to transmit the data signal in the serial port signal line. Since the signals of the RS232 level standard cannot be directly recognized by the controller, these signals will be converted into "TTL calibration" level signals recognized by the controller through a "level conversion chip" to realize communication. 

The chip is connected to the PC, usually the PC uses the RS232 interface (9 pins). If you want the chip to communicate directly with the RS232 interface of the PC, you need to convert the input and output ports of the chip to the RS232 type, and then cross-connect. The level standards of the two are different:

  • MCU comment standard (TTL level): +5V means 1, 0V means 0;
  • RS232 level standard: +15/+13V means 0, -15/-13 means 1;

Therefore, the serial port communication between the single-chip microcomputer and the PC should follow: between the serial port of the single-chip microcomputer and the RS232 port given by the host computer, the conversion between TTL level and RS232 level is realized through the level conversion circuit.

The figure below shows the DB9 standard serial communication interface:

DB9 pin description: 

The above table is the standard connection method of the DB9 male head on the computer side. Since the sending and receiving signals (RXD and TXD) between the two communication devices should be cross-connected, the connection method of the sending and receiving signal of the DB9 female head on the modem side is generally the same as that of the male head. On the contrary, when connecting two devices, just use a "straight-through" serial cable to connect them. 

The RTS, CTS, DSR, DTR and DCD signals in the serial port line use logic 1 to indicate that the signal is valid, and logic 0 to indicate that the signal is invalid. For example, when the computer side controls the DTR signal line to represent a logic 1, it is to inform the remote modem that the machine is ready to receive data, and 0 means it is not ready yet. 

If you use USB to serial port, you can also realize serial port communication. The circuit diagram of USB to serial port is as follows:

3. Remote serial port use

If you want to connect the serial devices on the production line to the computer in the office, such as smart modules, instruments, PLCs, etc., it is definitely not possible to directly pull a long-distance serial cable, because the distance in the middle is too long. Distance, we can realize it by means of Ethernet, that is, the local area network of the factory. The solution is shown in the figure below (taking PLC connection as an example).

Implementation:

  • Add a serial port server on the PLC side to realize the serial port to network port function. The serial server has a network port and a serial port.
  • The remote computer host and the serial port server are respectively connected to the local area network through a network cable to obtain an IP address and establish a network connection.
  • The purpose of running the virtual serial port software on the computer is to virtualize the serial port on the remote serial port server to the local serial port, and the effect is to use the remote serial port as a local serial port. The method is to search the serial port server through the network first, and then assign an idle serial port number as the local serial port after finding it, as shown in the figure below:

  • At this time, the PLC programming software on the computer can use the serial port number just specified to connect to the remote PLC, which is as convenient as a local connection. The same is true for connecting other serial devices. 

Notice:

  • It is best to set the IP address of the serial port server as a fixed IP, which can reduce communication failures caused by dynamic IP changes.
  • When the serial port server is applied across network segments, the gateway address must be set.

4. C# develops host computer serial communication

For the upper computer, we only need to get a custom communication protocol provided by a third party, and program according to the content. The specific function realization is realized by the hardware engineer.

Most of the serial port communication has corresponding library functions. When using and configuring the serial port, you can directly call the library functions and configure them, as in the following case.

First, we implement an interface, as follows:

Configure serial port parameters - open the serial port:

send data:

Receive data:

Receive data asynchronously.

If you need to read directly after writing.

Refer to the following methods: 

Renderings: 

For communication based on a custom protocol (both sending and receiving are performed in hexadecimal), first customize a communication protocol.

Use RS232 for communication, set as follows:

  • Baud rate: 9600;
  • data bits: 8;
  • stop_bit: 1;
  • parity: none;

Communication protocol content:

  • Set register 1 to execute function 1 address 0b;
  • Set register 2 to 1 to execute function 2 address 1b;
  • CRC verification: A value generated by data + address etc. through AND or operations (generally, self-defined ones will be verified);
  • start bit: 01;
  • End bit: 05;
  • Address bit: 0a (decided according to different registers);
  • Result bit: 0e (success 0e, failure 00);

Send example: Execute function 1.

01 0b 01 00 00 00 00 00 00 00 06 0e 05

Parse:

01 is the start bit, 0b is the address corresponding to register 1, the data length is 8, if there is no data, set 00, 06 is the generated value of crc check, 0e is the result bit, and 05 is the end bit.

returns success:

01 0b 01 00 00 00 00 00 00 00 06 0e 05

returns failure:

01 0b 01 00 00 00 00 00 00 00 07 00 05

One of the simplest custom protocols, the communication process is as follows:

  1. Host computer sends data
  2. The lower computer receives the data
  3. The lower computer performs CRC verification, if it fails, it does not respond.
  4. The CRC verification of the lower computer is successful, and the function is executed. and feedback the results.
  5. The upper computer receives the data and performs CRC check. If it succeeds, the data is considered correct. Otherwise, the data is considered to be wrong and the execution fails.
  6. Completion communication

The analog registers process the instructions sent by the host computer:

The command returns correctly (corresponding to the protocol):

Instruction error return (corresponding to the protocol): 

Code download: studydome: 1221211232023032032030320 

Guess you like

Origin blog.csdn.net/qq_35029061/article/details/130717375