Serial communication with MODBUS protocol

Nov 22, 2024 Leave a message

1. Basic communication methods

 

There are two basic communication methods: parallel communication and serial communication.

Parallel communication: This is a communication method in which each bit of data is transmitted at the same time.

Serial communication: This is a communication method in which data is transmitted one by one in a sequential manner.

 

2. Two types of serial communication

 

Two basic forms of serial communication: synchronous communication and asynchronous communication.

Asynchronous communication transmits a data frame format consisting of a start bit, 1 to 9 data bits, and 1 to 2 stop bits, relying on the start and stop bits to keep synchronized;

Synchronous communication data frame format is composed of multiple bytes of a frame, each frame has two (or one) synchronization characters as the start bit to trigger the synchronization clock to start sending or receiving data.

 

3. Classification according to transmission direction

 

Serial communication can be further categorized into simplex, half-duplex and full-duplex according to the direction of information transmission. If in any moment of the communication process, the information can only be transmitted from one party A to the other party B, it is called simplex. If at any moment, information can be transmitted both from A to B and from B to A, but only by the presence of transmission in one direction, it is called half-duplex transmission. If, at any instant in time, bidirectional signaling exists on the line from A to B and from B to A, it is called full duplex.

 

4. Serial communication interface

 

Serial communication physical interface according to electrical standards and protocols to include RS-232, RS-422, RS485, etc., here mainly about some of the most commonly used RS485 interface.

 

5. RS485 interfacecircuit

info-1-1

As a demonstration circuit the SP485R chip is often used and can be directly embedded in an actual RS-485 application circuit. The standard serial port of the microprocessor is directly connected to the RO pin of the SP485R chip via RXD and to the DI pin of the SP485R chip via TXD.

 

R/D signal output from the microprocessor directly control the SP485R chip transmitter/receiver enable: R/D signal for the "1", the SP485R chip transmitter is valid, the receiver is prohibited, at this time the microprocessor can be to the RS-485 bus to send data bytes; R / D signal for the R / D signal is "0", then the SP485R chip transmitter is disabled, the receiver is valid, at this time the microprocessor can receive data bytes from the RS-485 bus. In this circuit, any moment in the SP485R chip "receiver" and "transmitter" can only have a working state.

 

Pull-up resistor R7 connected to pin A and pull-down resistor R8 connected to pin B are used to ensure that the unconnected SP485R chip is in an idle state, providing network failure protection to improve the reliability of the RS-485 node and the network.

 

If the SP485R is connected to the UART serial port of the microprocessor 80C51 chip, the RO pin of the SP485R chip does not need to be pulled up; otherwise, it is necessary to consider whether or not to add a pull-up resistor of about 10K to the RO pin according to the actual situation.

6. Serial MODBUS communication

 

Modbus as a rule of data exchange in industrial communication circuits has been widely used in various fields, making it convenient to communicate with devices with Modbus communication protocol, such as PLCs, inverters, flow meters, temperature and humidity, host computer group configuration software, and a variety of sensors and instruments.

 

Modbus communication protocol is divided into two serial communication modes, ASCII and RTU communication modes. In use, you need to set the communication mode and serial port RS232, RS485 communication parameters (baud rate, parity, slave address, data bits, stop bits, byte order), all devices on the Modbus bus should have the same communication mode and serial communication parameters.

 

Modbus message frame structure

 

address Function Code Data address Data 1 Data... Data n

CRC16

 

7. MODBUS command message
Read data
Master sends

address Function Code Data start address high bit Data start address low Number of data high Number of data low CRC16 high CRC16 low bit

 

Return:

address Function Code Byte length Data 1 High Data 1 low Data 2 high Data 2 low

…

CRC16 High CRC16 Low

 

8. MODBUS key concepts

 

(1) The upper computer reads the data each time the interval is generally not less than 100ms

(2) Device address: MODBUS slave communication address, there can not be two identical addresses in a network. (3) Function code: Function code specified by MODBUS protocol.

(4) Register address and number of registers

The parameter in the master command is the register from the register address, read register length of N registers. (5) Slave response data

Slave response data is: number of bytes and N numeric section data.

 

9. MODBUS master common operations

 

(1) Read Optimization: For the data reading of discontinuous address, whether it takes packaged read or not, then the system uses the packaged read command to complete the reading of multiple addresses at one time.

(2) Read Optimization Interval: When reading optimization, if the interval between addresses is less than this interval, they will be packed together and read at one time using multiple read command.

(3) Maximum packet length: the maximum data length allowed for one communication.

(4) Byte Order: Adjust the decoding order of 32-bit double words.

Take 32-bit integer as an example to illustrate the meaning of byte order:.

1234: indicates that the double-word components are not processed directly decoded,.

For example: the data read from the serial port is 0000 00 01 means 1.

2143: indicates that the double-word components of the high and low words are not reversed, but the word within the high and low bytes reversed.

For example, the data read from the serial port is 0000 00 01 means 0x00000100 (i.e. 256).

3412: indicates that the high and low words of the double-word component are reversed, but the high and low bytes of the word are not reversed.

For example, the data read from the serial port is 0000 00 01 which means 0x00010000 (i.e. 65536).

4321: Indicates that all 4 bytes in the double word element are reversed.

For example: the data read from the serial port is 00 00 00 01 means 0x0100 0000 (i.e. 1677 721).

 

10. MODBUS Commissioning Software MODBUS PULL

Take the slave address as 1, baud rate 9600, read all the real-time data as an example, the setting method is as follows.
According to Table 2: Start register address 4113 Number of registers is 22
1. The setup acquisition commands include device address (1), MODBUS function code (04), register address (4113), register length (2), and acquisition interval (1000).
info-1-1

2. Set the serial port data
According to the slave serial port format (1 bit start bit, 8 bits data bit, 1 bit stop bit, no parity), set the following figure:
info-1-1

3. Setting the data display format
info-1-1

4. Communication success screen
info-1-1

Send Inquiry

whatsapp

Phone

E-mail

Inquiry