Method and circuit for parameter setting and query of single-chip microcomputer controller with MODBUS-RTU slave protocol
By defining data storage areas and status variables on the microcontroller controller and initializing serial communication with preset parameters, setting and querying of MODBUS-RTU parameters is realized, solving the problem of adding hardware structures or being easily misoperated in the prior art. , realizes simple and safe parameter setting and query functions.
Patent Information
- Application Number
- CN202411082202.5
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2024-08-08
- Publication Date
- 2024-12-10
- Estimated Expiration
- 2044-08-08
AI Technical Summary
When setting and querying MODBUS-RTU parameters, existing microcontroller controllers with MODBUS-RTU slave protocols need to add complex hardware structures or dial switches that are easily misoperated, making it difficult to do without adding original circuit components. The following realizes intuitive and safe parameter setting and query functions.
By defining a data storage area and a state variable that complies with the MODBUS-RTU protocol on the microcontroller controller, initializing serial communication with preset specified serial port parameters, and switching to the setting and query status when receiving a specific data frame, it is realized. Setting and querying MODBUS-RTU parameters without adding original circuit components.
It realizes that without adding original circuit components, the microcontroller controller has the function of setting and querying MODBUS-RTU parameters. The structure is simple, the cost is not increased, and the complexity of the hardware structure and the risk of misoperation is avoided.
Smart Images

Figure CN118605359B_ABST
Abstract
Description
Technical Field
[0001] The present invention relates to the technical field related to controllers, and in particular to a method and circuit for setting and querying parameters of a single-chip microcomputer controller with a MODBUS-RTU slave protocol. Background Art
[0002] The controller with MODBUS-RTU slave protocol is a commonly used controller. Since this controller needs to execute MODBUS-RTU slave protocol, at least the following four parameters must be set: slave address, baud rate, parity bit type, and number of stop bits (hereinafter referred to as: MODBUS-RTU parameters). Only when the MODBUS-RTU parameters are set to correspond to the corresponding MODBUS-RTU host parameters can normal communication be achieved.
[0003] Therefore, the MCU controller with MODBUS-RTU slave protocol must have the function of setting and querying MODBUS-RTU parameters.
[0004] In the prior art, there are two solutions for setting and querying MODBUS-RTU parameters, which are evaluated as follows:
[0005] The first method is to add a display device and an input device, such as a digital tube and a button or a display screen with touch input, to the original controller with the MODBUS-RTU slave protocol. This solution has a good user experience and the set values can be displayed intuitively. The disadvantage is that the cost is very high, the hardware structure is very complex, the size is also large, and it is difficult to miniaturize.
[0006] The second method: add multiple DIP switches to the original controller with MODBUS-RTU slave protocol for input and display. This solution can display the set values intuitively; the disadvantage is that it requires hardware structure coordination and may be misoperated by unauthorized personnel.
[0007] Therefore, how to provide a method and circuit for parameter setting and querying of a single-chip microcomputer controller with a MODBUS-RTU slave protocol, which can enable the single-chip microcomputer controller with a MODBUS-RTU slave protocol to have the function of setting and querying MODBUS-RTU parameters without adding original circuit elements, is a technical problem to be solved by the invention. Summary of the invention
[0008] The present invention provides a method and circuit for parameter setting and querying of a single-chip microcomputer controller with a MODBUS-RTU slave protocol, which can enable the single-chip microcomputer controller with a MODBUS-RTU slave protocol to have the function of setting and querying MODBUS-RTU parameters without adding original circuit elements.
[0009] To solve the above technical problems, according to one aspect of an embodiment of the present invention, a method for setting and querying parameters of a single-chip microcomputer controller with a MODBUS-RTU slave protocol is provided, comprising:
[0010] When the controller is powered on and initialized, the following operations are performed:
[0011] S1: Define the data storage area that complies with the MODBUS-RTU protocol, which is used to store MODBUS-RTU parameters. The data in this data storage area needs to be memorized after power failure.
[0012] S2: define a variable to indicate whether the current state is normal communication or setting and querying parameter state, and initialize the variable to normal communication state;
[0013] S3: Reading MODBUS-RTU parameters from the data storage area, initializing the first serial port of the single-chip microcomputer with the parameters, and initializing the second serial port of the single-chip microcomputer with preset specified parameters;
[0014] S4: After initialization, the first serial port of the single-chip microcomputer communicates normally according to the MODBUS-RTU parameters read from the data storage area, and the second serial port of the single-chip microcomputer monitors the data input (UART1_RXD) pin of the first serial port of the single-chip microcomputer according to the preset specified parameters;
[0015] When a specified preset data frame is monitored, the variable is set to the setting and query parameter state, the microcontroller stops the normal communication, the first serial port of the microcontroller is initialized with the preset specified parameters, and the MODBUS-RTU parameters of the data storage area are set and queried using the MODBUS-RTU protocol.
[0016] The preset specified parameters include: preset specified slave address, preset specified baud rate, preset specified check bit type, preset specified stop bit number; wherein:
[0017] The preset slave address is one of the following values: 248, 249, 250, 251, 252, 253, 254, 255;
[0018] The preset specified baud rate is one of the following values: 1200, 2400, 4800, 9600, 19200, 115200;
[0019] The default check bit type is one of the following: odd, even, or none;
[0020] The default setting is one of the following: 1, 2.
[0021] The reason for selecting the above range for the preset specified parameters is that the MODBUS-RTU slave protocol stipulates that the slave address range is 1~247, and 0 is the broadcast address. At the same time, considering that the maximum value of 1 byte of data expressed as an unsigned number is 255. The above preset specified slave address value can avoid conflicts with the MODBUS-RTU slave protocol address. The preset specified baud rate, preset specified check bit type, and preset specified stop bit number are all commonly used parameters in this field.
[0022] Preferably, the preset specified parameters are: preset specified slave address: 252, preset specified baud rate: 9600, preset specified check bit type: none, preset specified number of stop bits: 1.
[0023] Preferably, the specified preset data frame is: 0xfc,6,0,4,0,5,0x1d,0xe5.
[0024] According to another aspect of an embodiment of the present invention, a circuit for executing the above method is provided, comprising: a single chip microcomputer and an RS485 transceiver chip; wherein:
[0025] The first serial port data input (UART1_RXD) pin of the microcontroller, the second serial port data input (UART2_RXD) pin of the microcontroller, and the data output (RO) pin of the RS485 transceiver chip are connected to each other;
[0026] The first serial port data output (UART1_TXD) pin of the microcontroller and the data input (DI) pin of the RS485 transceiver chip are connected to each other;
[0027] Any switch output (GPIO) pin of the microcontroller, the receive enable (RE) pin of the RS485 transceiver chip, and the drive enable (DE) pin of the RS485 transceiver chip are connected to each other.
[0028] Beneficial effects: This technical solution enables the single-chip microcomputer controller with MODBUS-RTU slave protocol to have the function of setting and querying MODBUS-RTU parameters without adding original circuit elements, has a simple structure and does not increase costs. BRIEF DESCRIPTION OF THE DRAWINGS
[0029] Figure 1 It is a circuit diagram of the present invention. DETAILED DESCRIPTION
[0030] The present invention will be further described below in conjunction with the accompanying drawings.
[0031] according to Figure 1 After the circuits are connected, the method described in the present invention can be implemented through a single chip computer program.
[0032] The single chip microcomputer model used in this embodiment is STC8H1K28, and the control program is written in the C language environment.
[0033] in:
[0034] Step S1 is implemented by the following code, where reg4 is the data storage area that complies with the MODBUS-RTU protocol:
[0035] unsigned int xdata reg4
[20] ={
[0036] 0,0,0,0,0,0,0,00,00,00,
[0037] 0,0,0,0,0,0,0,00,00,00
[0038] };
[0039] Step S2 is implemented by the following code, where tiaoshi is the variable described in this step:
[0040] bit tiaoshi=0; / / 0: normal communication status, 1: setting and querying parameter status
[0041] Step S3 is implemented by the following code:
[0042] ReadEEPROM(); / / Read MODBUS-RTU parameters from power-off memory
[0043] Adress=reg4[0]; / / Set MODBUS-RTU parameters
[0044] tzw=reg4[1];
[0045] jyw=reg4[2];
[0046] btl=reg4[3];
[0047] R_TH0=btl_TH0[btl];
[0048] UartInit(); / / Initialize serial port 1 and serial port 2
[0049] Step S4 is implemented by the following code:
[0050] Among them, the second serial port monitors the specified preset data frame to determine:
[0051] if(stacom2==8&&js2[0]==0xfc&&js2[1]==6&&
[0052] js2[2]==0&&js2[3]==4&&js2[4]==0&&
[0053] js2[5]==5&&js2[6]==0x1d&&js2[7]==0xe5&&tiaoshi==0) / / The received data frame is 0xfc,6,0,4,0,5,0x1d,0xe5
[0054] {
[0055] tiaoshi=1; / / The variable is set to set and query parameter status
[0056] tzw=0; / / Initialize the first serial port of the microcontroller with the preset specified parameters
[0057] jyw=0;
[0058] btl=3;
[0059] R_TH0=btl_TH0[btl];
[0060] UartInit();
[0061] }
[0062] The first serial port communication code:
[0063] if(tiaoshi==0)
[0064] { / / In normal communication state, execute normal communication code
[0065] }
[0066] else
[0067] { / / In the state of setting and querying parameters, use the MODBUS-RTU protocol to set and query the MODBUS-RTU parameters of the data storage area
[0068] }.
Claims
1. A method for setting and querying parameters of a microcontroller controller with MODBUS-RTU slave protocol, include: When the controller is powered on and initialized, the following operations are performed: S1: Define the data storage area that complies with the MODBUS-RTU protocol, which is used to store MODBUS-RTU parameters. The data in this data storage area needs to be memorized after power failure. S2: define a variable to indicate whether the current state is normal communication or setting and querying parameter state; initialize the variable to normal communication state; S3: Reading MODBUS-RTU parameters from the data storage area, initializing the first serial port of the single-chip microcomputer with the parameters, and initializing the second serial port of the single-chip microcomputer with preset specified parameters; S4: After initialization, the first serial port of the single-chip microcomputer communicates normally according to the MODBUS-RTU parameters read from the data storage area, and the second serial port of the single-chip microcomputer monitors the data input (UART1_RXD) pin of the first serial port of the single-chip microcomputer according to the preset specified parameters; When a specified preset data frame is monitored, the variable is set to a setting and querying parameter state, the single-chip microcomputer stops the normal communication, the first serial port of the single-chip microcomputer is initialized with the preset specified parameters, and the MODBUS-RTU parameters of the data storage area are set and queried using the MODBUS-RTU protocol; The preset specified parameters include: preset specified slave address, preset specified baud rate, preset specified check bit type, The preset specifies the number of stop bits; where: The preset slave address is one of the following values: 248, 249, 250, 251, 252, 253, 254, 255; The preset specified baud rate is one of the following values: 1200, 2400, 4800, 9600, 19200, 115200; The default check bit type is one of the following: odd, even, or none; The preset specified number of stop bits is one of the following values: 1, 2; The preset specified parameters are: preset specified slave address: 252, preset specified baud rate: 9600, preset specified check bit type: none, preset specified number of stop bits: 1; The specified preset data frame is: 0xfc,6,0,4,0,5,0x1d,0xe5.
2. A circuit for executing the method according to claim 1, include: Single chip microcomputer and RS485 transceiver chip; its features are: The first serial port data input (UART1_RXD) pin of the microcontroller, the second serial port data input (UART2_RXD) pin of the microcontroller, and the data output (RO) pin of the RS485 transceiver chip are connected to each other; The first serial port data output (UART1_TXD) pin of the microcontroller and the data input (DI) pin of the RS485 transceiver chip are connected to each other; Any switch output (GPIO) pin of the microcontroller, the receive enable (RE) pin of the RS485 transceiver chip, and the drive enable (DE) pin of the RS485 transceiver chip are connected to each other.
