Multi-protocol compatible communication system, control method thereof, and electronic device
Through interface modules and configuration modules, the MODBUS communication protocol stack supports both MODBUS-RTU/ASCII and MODBUS-TCP communication protocol modes, solving the problem of single protocol mode in the prior art, increasing the application range and reducing communication costs.
Patent Information
- Application Number
- CN202211312802.7
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2022-10-25
- Publication Date
- 2025-06-24
- Estimated Expiration
- 2042-10-25
AI Technical Summary
The existing MODBUS communication protocol stack cannot support the MODBUS-RTU/ASCII and MODBUS-TCP communication protocol modes at the same time, resulting in redundant program code and large data space occupancy, which seriously affects reusability and scalability.
Through the interface module and configuration module, the MODBUS communication protocol stack supports both MODBUS-RTU/ASCII and MODBUS-TCP communication protocol modes, and realizes the master and slave functions of these three protocols respectively.
The application scope of the MODBUS communication protocol stack is increased, the communication cost is reduced, and the reusability and scalability of program code is improved.
Smart Images

Figure CN116055583B_ABST
Abstract
Description
Technical Field
[0001] The present invention relates to the field of communications, and in particular, to a communication system compatible with multiple protocols, a control method for a communication system compatible with multiple protocols, and an electronic device. Background Art
[0002] The MODBUS communication protocol is widely used in the industrial control industry. In order to achieve the unification of the MODBUS communication protocol stack, a FREEMODBUS protocol stack is proposed in the related art. This protocol stack only provides a slave protocol for embedded applications, and only supports the MODBUS-RTU / ASCII communication protocol mode. When the master protocol is needed, a specific password or secret order is required, and it does not support the MODBUS-TCP communication protocol mode. A Uc / MODBUS protocol stack is also proposed in the related art, which can only be used under authorization, and only supports the MODBUS-RTU / ASCII communication protocol mode for both the master and slave stations, but does not support the MODBUS-TCP communication protocol mode.
[0003] In order to solve the situation where the above three protocols cannot exist simultaneously, it is necessary to write a set of MOBUS-TCP communication protocols for both the master and slave stations, a set of MODBUS-RTU communication protocols for both the master and slave stations, and a set of MODBUS-ASCII communication protocols for both the master and slave stations to meet the above functional requirements. The program code is redundant and occupies a large amount of data space, seriously affecting the reusability and scalability of the program code. Summary of the Invention
[0004] The present invention aims to solve at least one of the technical problems in the related art to some extent. To this end, the first object of the present invention is to propose a communication system compatible with multiple protocols, which can support both the MODBUS-RTU / ASCII and MODBUS-TCP communication protocol modes through an interface module and a configuration module, and respectively implement the master and slave functions of these three protocols, increasing the application scope based on the MODBUS communication protocol stack and reducing the communication cost.
[0005] The second object of the present invention is to propose a control method for a communication system compatible with multiple protocols.
[0006] The third object of the present invention is to propose an electronic device.
[0007] To achieve the above object, an embodiment of the first aspect of the present invention provides a communication system compatible with multiple protocols, including: an interface module and a configuration module. The interface module includes: a plurality of serial communication interfaces and a plurality of network communication interfaces. Among them, the configuration module is used to configure the plurality of serial communication interfaces to implement the MODBUS-RTU / MODBUS-ASCII communication protocol; the configuration module is further used to configure the plurality of network communication interfaces to implement the MODBUS-TCP master communication protocol or the slave communication protocol; the configuration module is further used to configure the interface parameters of the plurality of serial communication interfaces and the plurality of network communication interfaces.
[0008] In the communication system compatible with multiple protocols according to the embodiment of the present invention, the configuration module configures the plurality of serial communication interfaces to implement the MODBUS-RTU / MODBUS-ASCII communication protocol, configures the plurality of network communication interfaces to implement the MODBUS-TCP master communication protocol or the slave communication protocol, and also configures the interface parameters of the communication interfaces. Thus, the communication system realizes that the MODBUS communication protocol stack supports both the MODBUS-RTU / ASCII and MODBUS-TCP communication protocol modes through the interface module and the configuration module, and realizes the master and slave functions of these three protocols, increasing the application scope based on the MODBUS communication protocol stack and reducing the communication cost.
[0009] In addition, the communication system compatible with multiple protocols according to the embodiment of the present invention may also have the following additional technical features:
[0010] According to an embodiment of the present invention, at least one of the plurality of network communication interfaces serves as a master station, at least one of the plurality of network communication interfaces serves as a slave station, at least one of the plurality of serial communication interfaces serves as a master station, and at least one of the plurality of serial communication interfaces serves as a slave station.
[0011] According to an embodiment of the present invention, the configuration module is further used to, when encapsulating and sending data packets, encapsulate the data sent by the master station network communication interface according to the first format, and encapsulate the data sent by the master station serial communication interface according to the second format.
[0012] According to an embodiment of the present invention, the configuration module is further used to, when encapsulating the response data packets, encapsulate the data responded by the slave station network communication interface according to the first format, and encapsulate the data responded by the slave station serial communication interface according to the second format.
[0013] According to an embodiment of the present invention, the configuration module is further used to configure the data reception mode of the slave station network communication interface as the network port interrupt + DMA mode, and configure the data reception mode of the slave station serial communication interface as the serial port interrupt mode.
[0014] According to an embodiment of the present invention, the configuration module is further configured to, during data processing, if the master station network communication interface and / or the master station serial communication interface send a non-broadcast command to the slave station, configure the event state machine as a sending state machine; when receiving the response data from the slave station, configure the event state machine as a receiving state machine; when the response data is correct, configure the receiving state machine as an executable state machine; when the response data is incorrect, configure the receiving state machine as an error handling state machine.
[0015] According to an embodiment of the present invention, the configuration module is further configured to, during data processing, if the master station network communication interface and / or the master station serial communication interface send a broadcast command to the slave station, configure the receiving state machine as an executable state machine.
[0016] According to an embodiment of the present invention, the configuration module is further configured to, during data processing, if the slave station network communication interface and / or the slave station serial communication interface receive a data frame from the master station, configure the ready state machine as a receiving state machine; when the data frame from the master station is an inquiry data packet or a broadcast data packet, configure the receiving state machine as an executable state machine; when the data frame from the master station is a non-broadcast data packet, configure the executable state machine as a sending state machine.
[0017] According to an embodiment of the present invention, the configuration module is further configured to, when the slave station network communication interface and / or the slave station serial communication interface receive a new data frame from the master station, configure the event state machine as a receiving state machine.
[0018] To achieve the above object, an embodiment of the second aspect of the present invention provides a control method for a communication system compatible with multiple protocols, including: configuring a plurality of serial communication interfaces to implement the MODBUS-RTU / MODBUS-ASCII communication protocol; configuring a plurality of network communication interfaces to implement the MODBUS-TCP master station communication protocol or the slave station communication protocol; configuring the interface parameters of the plurality of serial communication interfaces and the plurality of network communication interfaces.
[0019] The control method of the embodiment of the present invention, through the above-mentioned communication system compatible with multiple protocols, realizes the mode that the MODBUS communication protocol stack supports both the MODBUS-RTU / ASCII and MODBUS-TCP communication protocols at the same time, and realizes the master station and slave station functions of these three protocols, increasing the application scope based on the MODBUS communication protocol stack and reducing the communication cost.
[0020] To achieve the above object, an embodiment of the third aspect of the present invention provides an electronic device, including the above-mentioned communication system compatible with multiple protocols and a data processing module. Among them, the data processing module is configured to determine the state of the event state machine according to a command sent by the master station network communication interface and / or the master station serial communication interface to the slave station; the data processing module is further configured to determine the state of the event state machine according to a data frame received from the master station by the slave station network communication interface and / or the slave station serial communication interface; the configuration module is configured to configure the state machine according to the state of the event state machine.
[0021] The electronic device according to the embodiment of the present invention realizes the mode that the MODBUS communication protocol stack supports both the MODBUS-RTU / ASCII and MODBUS-TCP communication protocols through the above-mentioned communication system compatible with multiple protocols and the data processing module, and realizes the master and slave functions of these three protocols, increasing the application scope based on the MODBUS communication protocol stack and reducing the communication cost.
[0022] Additional aspects and advantages of the present invention will be given in part in the following description, become apparent in part from the following description, or be learned through the practice of the present invention. Description of the Drawings
[0023] Figure 1 It is a block diagram of a communication system compatible with multiple protocols according to an embodiment of the present invention;
[0024] Figure 2 It is a schematic diagram of a device including a communication system compatible with multiple protocols communicating with an external device according to an embodiment of the present invention;
[0025] Figure 3 It is a schematic diagram of function initialization of a communication system compatible with multiple protocols according to an embodiment of the present invention;
[0026] Figure 4 It is a schematic diagram of sending data of a communication system compatible with multiple protocols according to an embodiment of the present invention;
[0027] Figure 5 It is a schematic diagram of data reception of a communication system compatible with multiple protocols according to an embodiment of the present invention;
[0028] Figure 6 It is a schematic diagram of data processing of a communication system compatible with multiple protocols according to an embodiment of the present invention;
[0029] Figure 7 It is a flowchart of a control method of a communication system compatible with multiple protocols according to an embodiment of the present invention;
[0030] Figure 8 It is a block diagram of an electronic device according to an embodiment of the present invention. Detailed implementation manners
[0031] The embodiments of the present invention will be described in detail below. Examples of the embodiments are shown in the drawings, wherein the same or similar reference numerals denote the same or similar elements or elements having the same or similar functions throughout. The embodiments described below with reference to the drawings are exemplary and are intended to explain the present invention, and should not be construed as limiting the present invention.
[0032] The communication system compatible with multiple protocols, the control method of the communication system compatible with multiple protocols, and the electronic device proposed according to the embodiments of the present invention will be described below with reference to the drawings.
[0033] Figure 1 It is a block diagram of a communication system compatible with multiple protocols according to an embodiment of the present invention.
[0034] As Figure 1 shown, the communication system compatible with multiple protocols according to the embodiment of the present invention may include: an interface module 10 and a configuration module 20.
[0035] Among them, the interface module 10 includes: a plurality of serial communication interfaces 12 and a plurality of network communication interfaces 11. For example, the interface module 10 includes: a first serial communication interface, a second serial communication interface, a first network communication interface, and a second network communication interface. The configuration module 20 is used to configure the plurality of serial communication interfaces to implement the MODBUS-RTU / MODBUS-ASCII communication protocol. For example, the configuration module 20 configures the first serial communication interface and the second serial communication interface to implement the MODBUS-RTU / MODBUS-ASCII communication protocol. The configuration module 20 is also used to configure the plurality of network communication interfaces to implement the MODBUS-TCP master communication protocol or the slave communication protocol. For example, the configuration module 20 configures the first network communication interface 11 to implement the MODBUS-TCP master communication protocol, and configures the second network communication interface 11 to implement the MODBUS-TCP slave communication protocol. The configuration module 20 is also used to configure the interface parameters of the plurality of serial communication interfaces and the plurality of network communication interfaces. For example, the configuration module 20 is also used to configure the respective interface parameters of the first serial communication interface, the second serial communication interface, the first network communication interface, and the second network communication interface.
[0036] Specifically, the interface module 10 includes a network communication interface and a serial communication interface. The network communication interface is used to implement the MODBUS-TCP communication protocol, and the serial communication interface is used to implement the MODBUS-RTU / MODBUS-ASCII communication protocol. Since the MODBUS communication protocol stack adopts the master-slave communication mode, to be compatible with the three communication protocols, both the network communication interface and the serial communication interface need to support both the master protocol and the slave protocol. Among them, both the MODBUS-RTU communication protocol and the MODBUS-ASCII communication protocol modes support the communication protocol of the RS-485 bus. Therefore, the serial communication interface can be configured to implement the MODBUS-RTU / MODBUS-ASCII communication protocol; the MODBUS-TCP communication protocol mode is based on the TCP / IP link of the Ethernet. Therefore, the network communication interface can be configured to implement the MODBUS-TCP communication protocol. Initialize the underlying driver, configure the port, baud rate, parity check, etc. of the serial communication interface, and configure the communication method, timer interrupt, and SPI communication method of the network communication interface. Then initialize the functions of the protocol stack, enable the functions of the MODBUS-RTU / ASCII master and slave stations, enable the functions of the MODBUS-TCP master and slave stations, configure the MODBUS-RTU / ASCII address, configure the MODBUS local port, local IP, remote port, and remote IP, and enable the MODBUS protocol function codes to be used. In this way, the master and slave functions of the three protocols can be realized on the same control chip.
[0037] It should be noted that the first or second serial communication interface and the first or second network communication interface in the multi-protocol compatible communication system in the above embodiments should not be construed as a limitation of the present invention. In other examples, embodiments, or implementations, they can be selected according to the present invention, and no specific limitation is made here.
[0038] The components of the multi-protocol compatible communication system will be described in detail below.
[0039] According to an embodiment of the present invention, at least one of the multiple network communication interfaces serves as a master station, at least one of the multiple network communication interfaces serves as a slave station, at least one of the multiple serial communication interfaces serves as a master station, and at least one of the multiple serial communication interfaces serves as a slave station. Still taking two serial communication interfaces and two network communication interfaces as an example, the first network communication interface and the first serial communication interface can serve as the master station, and the second network communication interface and the second serial communication interface can serve as the slave station.
[0040] It should be noted that when there are three network communication interfaces, one of the network communication interfaces serves as the master station, one serves as the slave station, and the other network communication interface can serve as either the master station or the slave station. That is, regardless of the number of network communication interfaces or serial communication interfaces, there must be one serving as the master station and one serving as the slave station, and the remaining interfaces can serve as either the master station or the slave station.
[0041] For example, as Figure 2 shown, Device A with a communication system compatible with multiple protocols has two network communication interfaces and two serial communication interfaces. Among them, the first network communication interface and the first serial communication interface serve as the master station, and the second network communication interface and the second serial communication interface serve as the slave station. Devices B, C, D, and E communicate with Device A as peripheral devices.
[0042] Specifically, communication between Device A and Device B is carried out through the MODBUS-TCP communication protocol. The first network communication interface on Device A serves as the master station, and Device B serves as the slave station. Communication between Device A and Device C is carried out through the MODBUS-TCP communication protocol. The second network communication interface on Device A serves as the slave station, and Device C serves as the master station. Communication between Device A and Device D is carried out through the MODBUS-RTU / ASCII communication protocol. The first serial communication interface 13 on Device A serves as the master station, and Device D serves as the slave station. Communication between Device A and Device E is carried out through the MODBUS-RTU / ASCII communication protocol. The second serial communication interface 14 on Device A serves as the slave station, and Device E serves as the master station. Among them, Device A can communicate with peripheral devices through both the MODBUS-TCP communication protocol and the MODBUS-RTU / ASCII communication protocol, and Device A can act as both the master station and the slave station.
[0043] According to an embodiment of the present invention, the configuration module 20 is further configured to, when encapsulating and sending data packets, encapsulate the data sent by the master station network communication interface according to the first format, and encapsulate the data sent by the master station serial communication interface according to the second format. For example, the data sent by the first network communication interface can be encapsulated according to the first format, and the data sent by the first serial communication interface can be encapsulated according to the second format.
[0044] According to an embodiment of the present invention, the configuration module 20 is further configured to, when encapsulating the response data packet, encapsulate the data responded by the slave station network communication interface according to the first format, and encapsulate the data responded by the slave station serial communication interface according to the second format. For example, the data responded by the second network communication interface can be encapsulated according to the first format, and the data responded by the second serial communication interface can be encapsulated according to the second format.
[0045] Specifically, when encapsulating and sending data packets, since the data sending formats of the master station in the MODBUS-RTU / ASCII and MODBUS-TCP communication protocol modes are different, different encapsulations of the sending data are performed according to different values of the parameter MODE. For example, when MODE is equal to 0, it represents a MODBUS-RTU / ASCII data packet; when MODE is equal to 1, it represents a MODBUS-TCP data packet. When encapsulating the response data packet, since the response data formats of the slave station in the MODBUS-RTU / ASCII and MODBUS-TCP communication protocol modes are different, different encapsulations of the response data are performed according to different values of the parameter MODE. For example, when MODE is equal to 0, it represents a MODBUS-RTU / ASCII data packet; when MODE is equal to 1, it represents a MODBUS-TCP data packet.
[0046] According to an embodiment of the present invention, the configuration module 20 is further configured to configure the data receiving mode of the slave station network communication interface as the network port interrupt + DMA mode, and configure the data receiving mode of the slave station serial communication interface as the serial port interrupt mode. For example, the data receiving mode of the second network communication interface can be configured as the network port interrupt + DMA mode, and the data receiving mode of the second serial communication interface can be configured as the serial port interrupt mode.
[0047] Specifically, both the MODBUS-RTU / ASCII and MODBUS-TCP communication protocols use the interrupt method for data reception. Among them, the data reception in the MODBUS-RTU / ASCII communication protocol mode is through the serial port interrupt mode. Each time a byte is received, a serial port interrupt is entered. If the reception cycle exceeds 3.5 bytes, it is considered that the reception of a frame of data is completed. The data reception in the MODBUS-TCP communication protocol mode is through the network port interrupt + DMA mode. In the network port interrupt, the presence of a data frame is checked through the DMA reception flag bit (DMARxDescToGet).
[0048] According to an embodiment of the present invention, the configuration module 20 is further configured to, when processing data, if the master station network communication interface and / or the master station serial communication interface send a non-broadcast command to the slave station, configure the event state machine as the sending state machine; when receiving the response data from the slave station, configure the event state machine as the receiving state machine; when the response data is correct, configure the receiving state machine as the executable state machine; when the response data is incorrect, configure the receiving state machine as the error handling state machine.
[0049] Continue to refer to Figure 1, according to an embodiment of the present invention, the configuration module 20 is further configured to, when processing data, if the master station network communication interface and / or the master station serial communication interface send a broadcast command to the slave station, configure the receive state machine as an executable state machine.
[0050] Specifically, after the function enabling of the protocol stack is completed, the corresponding protocol stack function becomes the enabled state. The enabled state directly exits the event processing function without event processing and enables the successful GET event state machine. The event state machine is divided into a ready state machine, a receive state machine, an executable state machine, and a send state machine. When parsing data under the receive state machine, if the data format is correct, the event state machine becomes the executable state machine, encapsulates the response data according to the function code, and the event state machine becomes the send state machine. In addition, when the master station accesses the slave station, the event state machine also becomes the send state machine.
[0051] For example, when processing data, for the master station, after initialization, the event state machine becomes the ready event state machine. If the master station (such as the first network communication interface and / or the first serial communication interface) sends a non-broadcast command to the slave station, then the event state machine changes from the ready state to the send state machine. When receiving the response data from the slave station, the event state machine changes from the send state machine to the receive state machine. When the response data is correct, the event state machine changes from the receive state machine to the executable state machine; when the response data is incorrect, the event state machine changes from the receive state machine to the error handling state machine. Whether the event state is in the executable state machine or the error handling state machine, when a new command needs to be sent, the event state machine becomes the send state machine and continues to poll.
[0052] , according to an embodiment of the present invention, the configuration module 20 is further configured to, when processing data, if the slave station network communication interface and / or the slave station serial communication interface receive a data frame from the master station, configure the ready state machine as the receive state machine; when the data frame from the master station is an inquiry data packet or a broadcast data packet, configure the receive state machine as the executable state machine; when the data frame from the master station is a non-broadcast data packet, configure the executable state machine as the send state machine.
[0053] , according to an embodiment of the present invention, the configuration module 20 is further configured to, when the slave station network communication interface and / or the slave station serial communication interface receive a new data frame from the master station, configure the event state machine as the receive state machine. For example, the event state machine can be configured as the receive state machine when the second network communication interface and / or the second serial communication interface receive a new data frame from the master station.
[0054] Specifically, for a MODBUS slave (e.g., the second network communication interface and / or the second serial communication interface), after initialization is completed, the event state machine becomes the ready event state machine. After the slave receives a correct data frame, the event state machine changes from the ready state machine to the receiving state machine. When the master's data frame is an inquiry data packet or a broadcast data packet, the event state machine changes from the receiving state machine to the executable state machine; when the master's data frame is a non-broadcast data packet, the event state machine changes from the executable state machine to the sending state machine. Regardless of whether the receive data state machine is the executable state machine or the sending state machine, when a new data frame is received, the state machine becomes the receiving state machine.
[0055] As a specific example, as Figures 2 to 6 shown, device A communicates with device B and device C respectively via the MODBUS-TCP protocol. When device A communicates with device B, it acts as the master, and when device A communicates with device C, it acts as the slave; device A communicates with device E and device D respectively via the MODBUS-RTU / ASCII protocol. When device A communicates with device E, it acts as the master, and when device A communicates with device D, it acts as the slave.
[0056] First, the underlying driver of device A needs to be configured, including configuring the port, baud rate, data bits, and parity check of the serial port, and configuring the SPI (Serial Peripheral Interface) communication mode, timer interrupt, and network port communication mode (such as MODBUS-RTU or MODBUS-ASCII). For example, configure the serial communication interface of device A for the MODBUS-RTU / ASCII communication protocol; configure the network communication interface of device A for the MODBUS-TCP master communication protocol; configure the SPI communication interface of device A, in cooperation with the SPI communication interface to network port chip, for the MODBUS-TCP slave communication protocol.
[0057] After the underlying drivers of the serial communication interface and network communication interface of device A are completed, enable the MODBUS communication protocol stack function. As Figure 3As shown, initialize the MODBUS communication protocol stack. Initialize the communication address, configure the baud rate, data bits, stop bits, and parity bit of the serial communication. The master communication address configuration of the MODBUS-RTU / ASCII communication protocol mode is not 0 (broadcast address). If the slave communication address configuration range is 0 - 255, set the slave address. Among them, when in RTU mode, initialize the slave RTU mode; when in ASCII mode, initialize the slave ASCII mode; if neither of the two modes is correct, the parameter is incorrect, and output eStatus = MB_EINVAL. If the range of the communication address is within 0 - 255, and the slave communication address configuration is not within 0 - 255, it is determined that the parameter is incorrect, and output eStatus = MB_EINVAL. When the master communication address configuration of the MODBUS-RTU / ASCII communication protocol mode is 0 (broadcast address), enter the host mode. When in RTU mode, initialize the host RTU mode; when in ASCII mode, initialize the host ASCII mode; if neither of the two modes is correct, the parameter is incorrect, and output eStatus = MB_EINVAL.
[0058] When the master communication address configuration of the MODBUS-TCP communication protocol is not 0 (broadcast address), set the port number to determine whether it is the slave mode. If so, set the default port number of the slave, create a SOCKET socket for the slave, Blind bind the IP and port, Listen for client (host) requests, and Accept to wait for the connection of the client (host). If not, set the default port number of the host, create a SOCKET socket for the host, and send a connection request to the server (slave).
[0059] After the protocol stack function is initialized successfully, if the parameter is incorrect, create an event flag (eMBPortEventInit) for event handling. Among them, the success flag for creation is: eStatus = MB_EPORTERR; the failure flag for creation is: eMBCurrentMode = eMode, eMBState = STATE_DISABLED.
[0060] After the MODBUS communication protocol stack is initialized, it defaults to the receiving initial state. Such as Figure 4As shown, when the communication protocol is MODBUS-RTU (or MODBUS-ASCII), whenever a data is received through the serial port, the receiving state is considered to change from the STATE_RX_INIT (receiving initial) state to the STATE_RX_RCV (receiving state), and the event state machine is set to EVENT_RX_RCV (receiving state machine) and the T35 interrupt timer starts timing. When a T35 interrupt occurs, it is considered that a complete data frame is received; when the received data length is greater than the maximum byte length, it is considered that an error frame is received, and the receiving state enters the STATE_RX_ERROR (receiving error state). When a T35 interrupt occurs, the receiving state returns to the initial state again.
[0061] For the MODBUS-TCP communication protocol, when the received data is a frame of data, when the length of the received data frame meets the frame header plus the data length (the length of the received data frame = frame header + data length), the event flag bit is set to: EVENT_RX_RCV. When an error occurs in the received data frame, the data packet is discarded and the client (host) is released.
[0062] After receiving a packet of correct data, the data needs to be processed. As Figure 5 shown, when there is a data sending request, for the master station of the MODBUS-RTU / ASCII communication protocol, the sending state is: STATE_M_TX_XMIT (starting to transmit state), and the sending port is opened. When the master station determines that the data sending is completed, the sending state is: STATE_M_TX_XFWR. The master station judges whether the sent data is broadcast data. If it is, the timer conversion delay interrupt starts, and then the sending state is changed to: STATE_M_TX_IDLE (idle state). If not, the timer response timeout interrupt occurs. When it is determined that the received data is broadcast, the sending state is changed to: STATE_M_TX_IDLE (idle state), and the timer is closed at the same time; if the current data frame is not a broadcast frame, a listening error is determined, and the host sending state machine is set to the idle state and the timer is closed at the same time. For the master station of the MODBUS-RTU / ASCII communication protocol, it is necessary to verify whether the returned data meets the operation code, the starting register address, the register address length, and the data verification according to the command issued by the master station. For the slave station of the MODBUS-RTU / ASCII communication protocol, it is necessary to verify the function code, the rationality of the register address, and the rationality of the register address length, and reply with an acknowledgment frame that meets the protocol requirements, and handle the frame that does not meet the protocol requirements as an error frame.
[0063] When there is data to be sent, it is necessary to call the sending module of the protocol stack, as Figure 5As shown in the figure. For the master station of the MODBUS-TCP communication protocol, when a command is issued, it will trigger EV_FRAME_SENT (transmission event state machine). For broadcast data, there is no need to wait for the data response frame. When the timer delay conversion time arrives, it is considered that the slave station has completed receiving the data, and a round of command issuance is completed. For non-broadcast addresses, it is necessary to wait for the response from the slave station. If the slave station responds within the specified time, it is considered that a round of command issuance is completed. If the slave station does not respond within the specified time, it is considered that the data is in error, and the command issuance status of this report is released, and a new round of commands is issued. For the slave station of the MODBUS-TCP communication protocol, the sending of the data frame is processed in response to the master station. When the event state machine enters EV_EXECUTE (executable event), according to different data protocols, the data to be sent is packaged and sent, and the event state machine becomes the ready state after sending is completed.
[0064] As Figure 6 As shown in the figure, for the master station of the MODBUS communication protocol stack, after initialization is completed, the event state machine becomes the ready event state machine (EV_READY). When a command needs to be sent, the event state changes from the ready state machine to the transmission state machine (EV_FRAME_SENT). For non-broadcast commands, when the master station receives a response from the slave station to the master station's command, the event state machine changes from the transmission state machine to the reception state machine (EV_FRAME_RECEIVED). Among them, when the response packet is correct, the event state machine changes from the reception state machine to the executable state machine (EV_EXECUTE); when the response packet is incorrect, the event state machine changes from the reception state machine to the error handling state machine (EV_MASTER_ERROR_PROCESS). For broadcast commands, after sending is completed, the event state machine changes from the reception state to the executable state machine. Regardless of whether the event state is in the executable state machine or the error handling state machine, when a new command needs to be issued, the event state machine changes to the transmission state machine and continues polling.
[0065] For the slave station of the MODBUS communication protocol stack, after initialization is completed, the event state machine becomes the event ready state machine (EV_READY). When a correct data frame is received, the event state machine changes to the reception state machine (EV_FRAME_RECEIVED). Among them, when the received data is broadcast data or an inquiry command addressed to itself, the event state machine changes from the reception state machine to the executable state machine (EV_EXECUTE); when the received data is not broadcast data, the transmission state machine changes from the executable state machine to the transmission state machine (EV_FRAME_SENT). Regardless of whether the received data state machine is in the executable state machine or the transmission state machine, when a new data frame is received, the event state machine changes to the reception state machine.
[0066] It should be noted that the above embodiments are only one embodiment of the present invention. In other examples, embodiments or implementation manners, selections can be made according to the present invention, and specific limitations are not made here.
[0067] In summary, for the multi-protocol compatible communication system according to the embodiments of the present invention, the configuration module configures multiple serial communication interfaces to implement the MODBUS-RTU / MODBUS-ASCII communication protocol, and configures multiple network communication interfaces to implement the MODBUS-TCP master communication protocol or slave communication protocol. At the same time, the interface parameters of the communication interfaces are also configured. Thus, this communication system realizes that the MODBUS communication protocol stack supports both the MODBUS-RTU / ASCII and MODBUS-TCP communication protocol modes through the interface module and the configuration module, and realizes the master and slave functions of these three protocols, increasing the application scope based on the MODBUS communication protocol stack and reducing the communication cost.
[0068] Corresponding to the above embodiments, the embodiments of the present invention also propose a control method.
[0069] As Figure 7 shown, the control method of the multi-protocol compatible communication system according to the embodiments of the present invention may include the following steps:
[0070] S101, configure multiple serial communication interfaces to implement the MODBUS-RTU / MODBUS-ASCII communication protocol.
[0071] S102, configure multiple network communication interfaces to implement the MODBUS-TCP master communication protocol or slave communication protocol.
[0072] S103, configure the interface parameters of multiple serial communication interfaces and multiple network communication interfaces.
[0073] According to an embodiment of the present invention, at least one of the multiple network communication interfaces serves as a master station, at least one of the multiple network communication interfaces serves as a slave station, at least one of the multiple serial communication interfaces serves as a master station, and at least one of the multiple serial communication interfaces serves as a slave station.
[0074] According to an embodiment of the present invention, configuring the interface parameters of multiple serial communication interfaces and multiple network communication interfaces includes: when encapsulating and sending data packets, encapsulating the data sent by the master station network communication interface in a first format, and encapsulating the data sent by the master station serial communication interface in a second format.
[0075] According to an embodiment of the present invention, configuring the interface parameters of multiple serial communication interfaces and multiple network communication interfaces further includes: when encapsulating the response data packet, encapsulating the data replied by the slave network communication interface in a first format, and encapsulating the data replied by the slave serial communication interface in a second format.
[0076] According to an embodiment of the present invention, configuring the interface parameters of multiple serial communication interfaces and multiple network communication interfaces further includes: configuring the data receiving mode of the slave network communication interface as network port interrupt + DMA mode, and configuring the data receiving mode of the slave serial communication interface as serial port interrupt mode.
[0077] According to an embodiment of the present invention, configuring the interface parameters of multiple serial communication interfaces and multiple network communication interfaces further includes: during data processing, if the master network communication interface and / or the master serial communication interface send a non-broadcast command to the slave station, configuring the event state machine as a sending state machine; when receiving the response data from the slave station, configuring the event state machine as a receiving state machine; when the response data is correct, configuring the receiving state machine as an executable state machine; when the response data is incorrect, configuring the receiving state machine as an error handling state machine.
[0078] According to an embodiment of the present invention, configuring the interface parameters of multiple serial communication interfaces and multiple network communication interfaces further includes: during data processing, if the master network communication interface and / or the master serial communication interface send a broadcast command to the slave station, configuring the receiving state machine as an executable state machine.
[0079] According to an embodiment of the present invention, configuring the interface parameters of multiple serial communication interfaces and multiple network communication interfaces further includes: during data processing, if the slave network communication interface and / or the slave serial communication interface receive a data frame from the master station, configuring the ready state machine as a receiving state machine; when the data frame from the master station is an inquiry data packet or a broadcast data packet, configuring the receiving state machine as an executable state machine; when the data frame from the master station is a non-broadcast data packet, configuring the executable state machine as a sending state machine.
[0080] According to an embodiment of the present invention, configuring the interface parameters of multiple serial communication interfaces and multiple network communication interfaces further includes: when the slave network communication interface and / or the slave serial communication interface receive a new data frame from the master station, configuring the event state machine as a receiving state machine.
[0081] It should be noted that for the details not disclosed in the control method of the multi-protocol compatible communication system according to the embodiments of the present invention, please refer to the details disclosed in the multi-protocol compatible communication system according to the embodiments of the present invention, and will not be elaborated here specifically.
[0082] A control method for a communication system compatible with multiple protocols according to an embodiment of the present invention, through the above-mentioned communication system compatible with multiple protocols, increases the application scope based on the MODBUS communication protocol stack and reduces the communication cost.
[0083] Corresponding to the above embodiment, the present invention also proposes an electronic device.
[0084] As Figure 8 shown, the electronic device 200 according to an embodiment of the present invention includes the above-mentioned communication system 220 compatible with multiple protocols and a data processing module 210. The communication system 220 compatible with multiple protocols includes a configuration module 20. Among them, the data processing module 210 is used to determine the state of the event state machine according to the commands sent by the master station network communication interface and / or the master station serial communication interface to the slave station; the data processing module 210 is also used to determine the state of the event state machine according to the data frames received from the master station by the slave station network communication interface and / or the slave station serial communication interface; the configuration module 20 is used to configure the state machine according to the state of the event state machine.
[0085] It should be noted that for the details not disclosed in the communication system compatible with multiple protocols according to the embodiment of the present invention, please refer to the details disclosed in the communication system compatible with multiple protocols according to the embodiment of the present invention, and specific details will not be elaborated here.
[0086] The electronic device according to the embodiment of the present invention, through the above-mentioned communication system compatible with multiple protocols and the data processing module, realizes the mode that the MODBUS communication protocol stack supports both the MODBUS-RTU / ASCII and MODBUS-TCP communication protocols at the same time, and realizes the master and slave functions of these three protocols, increases the application scope based on the MODBUS communication protocol stack and reduces the communication cost.
[0087] Note that the logic and / or steps represented in the flowchart or otherwise described herein, for example, can be considered as a definite sequence list of executable instructions for implementing logical functions, and can be specifically implemented in any computer-readable medium for use by an instruction execution system, apparatus, or device (such as a computer-based system, a system including a processor, or other systems that can fetch and execute instructions from the instruction execution system, apparatus, or device), or used in combination with these instruction execution systems, apparatus, or devices. For the purposes of this specification, a "computer-readable medium" can be any device that can contain, store, communicate, propagate, or transport a program for use by or in connection with an instruction execution system, apparatus, or device. More specific examples (a non-exhaustive list) of the computer-readable medium include the following: an electrical connection part with one or more wirings (electronic device), a portable computer diskette (magnetic device), a random access memory (RAM), a read-only memory (ROM), an erasable programmable read-only memory (EPROM or flash memory), an optical fiber device, and a portable compact disc read-only memory (CDROM). Additionally, the computer-readable medium can even be paper or other suitable media on which the program can be printed, because the program can be obtained electronically, for example, by optically scanning the paper or other media, followed by editing, interpretation, or otherwise processing as appropriate, and then stored in a computer memory.
[0088] It should be understood that various parts of the present invention can be implemented by hardware, software, firmware, or a combination thereof. In the above-described embodiments, multiple steps or methods can be implemented by software or firmware stored in a memory and executed by a suitable instruction execution system. For example, if implemented by hardware, as in another embodiment, any one or a combination of the following techniques well known in the art can be used: discrete logic circuits having logic gate circuits for implementing logical functions on data signals, application-specific integrated circuits having appropriate combinational logic gate circuits, programmable gate arrays (PGAs), field programmable gate arrays (FPGAs), etc.
[0089] In the description of this specification, the description with reference to terms such as "one embodiment", "some embodiments", "example", "specific example", or "some examples", etc. means that the specific features, structures, materials, or characteristics described in connection with the embodiment or example are included in at least one embodiment or example of the present invention. In this specification, the schematic representations of the above terms do not necessarily refer to the same embodiment or example. Moreover, the specific features, structures, materials, or characteristics described can be combined in a suitable manner in any one or more embodiments or examples.
[0090] In addition, the terms "first", "second", etc. used in the embodiments of the present invention are for descriptive purposes only and should not be construed as indicating or implying relative importance or implicitly specifying the quantity of the technical features indicated in this embodiment. Thus, the features defined with terms such as "first" and "second" in the embodiments of the present invention may clearly or implicitly indicate that at least one such feature is included in this embodiment. In the description of the present invention, the meaning of the word "plurality" is at least two or more, such as two, three, four, etc., unless otherwise specifically defined in the embodiments.
[0091] Although the embodiments of the present invention have been shown and described above, it can be understood that the above embodiments are exemplary and should not be construed as limiting the present invention. Those of ordinary skill in the art can make changes, modifications, substitutions, and variations to the above embodiments within the scope of the present invention.
Claims
1. A communication system compatible with multiple protocols, characterized in that, Including: An interface module and a configuration module, the interface module includes: a plurality of serial communication interfaces and a plurality of network communication interfaces, wherein, The configuration module is used to configure the plurality of serial communication interfaces to implement the MODBUS-RTU / MODBUS-ASCII communication protocol; The configuration module is further used to configure the plurality of network communication interfaces to implement the MODBUS-TCP master communication protocol or slave communication protocol; The configuration module is further used to configure the interface parameters of the plurality of serial communication interfaces and the plurality of network communication interfaces; Wherein, the specific steps for implementing the master and slave functions of the three protocols on the same control chip are: first initialize the underlying driver, configure the port, baud rate, and parity check of the serial communication interface, configure the communication method, timer interrupt, and SPI communication method of the network communication interface, then initialize the functions of the protocol stack, enable the master and slave functions of MODBUS-RTU / ASCII, enable the master and slave functions of MODBUS-TCP, configure the MODBUS-RTU / ASCII address, configure the MODBUS local port, local IP, remote port, and remote IP, and enable the MODBUS protocol function codes to be used; At least one of the plurality of network communication interfaces serves as a master, at least one of the plurality of network communication interfaces serves as a slave, at least one of the plurality of serial communication interfaces serves as a master, and at least one of the plurality of serial communication interfaces serves as a slave; The configuration module is further used to configure the data reception method of the slave network communication interface as the network port interrupt + DMA method, and configure the data reception method of the slave serial communication interface as the serial port interrupt method.
2. The system according to claim 1, wherein The configuration module is further used to, When encapsulating and sending data packets, encapsulate the data sent by the master network communication interface according to the first format, and encapsulate the data sent by the master serial communication interface according to the second format.
3. The system according to claim 2, wherein The configuration module is further used to, When encapsulating the response data packet, encapsulate the data responded by the slave network communication interface according to the first format, and encapsulate the data responded by the slave serial communication interface according to the second format.
4. The system according to claim 1, wherein The configuration module is further used to, During data processing, if the master network communication interface and / or the master serial communication interface send a non-broadcast command to the slave, configure the event state machine as a send state machine; When receiving the response data from the slave, configure the event state machine as a receive state machine; When the response data is correct, configure the receive state machine as an executable state machine; When the response data is incorrect, configure the receive state machine as an error handling state machine.
5. The system according to claim 1, characterized in that, The configuration module is further used to, During data processing, if the master network communication interface and / or the master serial communication interface send a broadcast command to the slave, configure the receive state machine as an executable state machine.
6. The system according to claim 1, wherein The configuration module is further used to, During data processing, if the slave network communication interface and / or the slave serial communication interface receive a data frame from the master, configure the ready state machine as a receive state machine; When the data frame of the master station is an inquiry data packet or a broadcast data packet, configure the receiving state machine as an executable state machine; When the data frame of the master station is a non-broadcast data packet, configure the executable state machine as a sending state machine.
7. The system according to claim 6, characterized in that, The configuration module is further configured to, When a new data frame of the master station is received by the slave station network communication interface and / or the slave station serial communication interface, configure the event state machine as the receiving state machine.
8. A control method for a multi-protocol compatible communication system according to any one of claims 1-7, characterized in that, Comprising: Configure multiple serial communication interfaces to implement the MODBUS-RTU / MODBUS-ASCII communication protocol; Configure multiple network communication interfaces to implement the MODBUS-TCP master station communication protocol or the slave station communication protocol; Configure the interface parameters of the multiple serial communication interfaces and the multiple network communication interfaces.
9. An electronic device, characterized in that, Comprising a multi-protocol compatible communication system and a data processing module according to any one of claims 1-7, the multi-protocol compatible communication system comprising a configuration module, wherein, The data processing module is configured to determine the state of the event state machine according to a command sent by the master station network communication interface and / or the master station serial communication interface to the slave station; The data processing module is further configured to determine the state of the event state machine according to a data frame of the master station received by the slave station network communication interface and / or the slave station serial communication interface; The configuration module is configured to configure the state machine according to the state of the event state machine.