Communication method based on MODBUS protocol and electronic equipment

By configured the MODBUS protocol message format and the creation of the Ethernet communication port, the problem that the Omron NX1P2 PLC device cannot communicate with the MODBUS protocol device, and realizes the high applicability and efficient data transmission of the device.

CN119363803AInactive Publication Date: 2025-01-24FUJIAN SHIWEI INTELLIGENT EQUIP CO LTD
View PDF 5 Cites 0 Cited by

Patent Information

Application Number
CN202411500562.2
Authority / Receiving Office
CN · China
Patent Type
Applications(China)
Current Assignee / Owner
Filing Date
2024-10-25
Publication Date
2025-01-24
Estimated Expiration
Not applicable · inactive patent

AI Technical Summary

Technical Problem

Omron NX1P2 PLC devices cannot communicate with host computers or other Modbus-enabled devices based on the Modbus protocol, resulting in low device applicability.

Method used

By configuring the message format of the Modbus protocol and creating an Ethernet communication port based on the Ethernet industrial protocol, the communication connection between the device and the client that supports the Modbus protocol is realized, and the sending and receiving of Modbus messages is completed.

Benefits of technology

There is no need to agree on the development and maintenance of the device. The device can communicate with other devices based on the MODBUS protocol, which improves the applicability of the equipment and solves the problem of low transmission efficiency of the Ethernet industrial agreement.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN119363803A_ABST
    Figure CN119363803A_ABST
Patent Text Reader

Abstract

The invention discloses a communication method based on an MODBUS protocol and electronic equipment, and the method comprises the steps: configuring a message format of an Ethernet industrial protocol according to the MODBUS protocol, and creating an Ethernet communication port based on the Ethernet industrial protocol; establishing a communication connection with a client supporting a MODBUS protocol through the Ethernet communication port, so as to receive request data sent by the client based on the communication connection; analyzing the request data according to the message format to obtain a target parameter, and generating response data according to the message format and the target parameter; and sending the response data to the client through the communication connection. According to the invention, communication between the Omron device and the upper computer can be realized based on the MODBUS protocol, and the applicability of the device is improved.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The present invention relates to the field of communication technology, and in particular to a communication method and electronic equipment based on the MODBUS protocol. Background Art

[0002] In the field of industrial automation, PLC (Programmable Logic Controller) is widely used to control and monitor the operating status of equipment. Omron NX1P2 PLC is a powerful PLC device, mainly used in various industrial automation control systems.

[0003] At present, the communication between Omron NX1P2 PLC devices and host computers mostly uses FINS protocol or CIP protocol. However, the implementation of these protocols is complex and requires special library support. For some developers, this additionally increases the development difficulty and maintenance cost of the device. The communication between Omron NX1P2 PLC devices and host computers can also use Ethernet / IP protocol, that is, Ethernet industrial protocol, but this protocol needs to be converted into 8-byte data when sending data, and it is also necessary to distinguish between high bytes and low bytes. When the amount of converted data is large, it will seriously affect the data transmission efficiency. In addition, with the widespread application of Modbus protocol in the field of industrial control, more and more systems need to communicate with devices that support Modbus protocol. However, the built-in instruction library and communication configuration of Omron NX1P2 PLC devices do not support ModbusTcp protocol. Therefore, the current Omron NX1P2 devices cannot communicate with host computers or other devices that support Modbus based on Modbus protocol, resulting in low applicability of the device. Summary of the invention

[0004] The technical problem to be solved by the present invention is to provide a communication method and electronic equipment based on the MODBUS protocol, which can realize communication between the Omron device and the host computer based on the MODBUS protocol and improve the applicability of the device.

[0005] In order to solve the above technical problems, a technical solution adopted by the present invention is: A communication method based on the MODBUS protocol, comprising: Configuring a message format of an Ethernet industrial protocol according to the MODBUS protocol, and creating an Ethernet communication port based on the Ethernet industrial protocol; Establishing a communication connection with a client supporting the MODBUS protocol through the Ethernet communication port, so as to receive request data sent by the client based on the communication connection; Parsing the request data according to the message format to obtain target parameters, and generating response data according to the message format and the target parameters; The response data is sent to the client via the communication connection.

[0006] In order to solve the above technical problems, another technical solution adopted by the present invention is: An electronic device comprises a memory, a processor and a computer program stored in the memory and running on the processor. When the processor executes the computer program, each step of the above-mentioned communication method based on the MODBUS protocol is implemented.

[0007] The beneficial effect of the present invention is that: since the device itself does not have the function of communicating based on the MODBUS protocol, the message format of the Ethernet industrial protocol is configured according to the MODBUS protocol, an Ethernet communication port is created based on the Ethernet industrial protocol, and a communication connection is made with a client supporting the MODBUS protocol through the Ethernet communication port, so that the device can complete the sending and receiving of MODBUS messages with other devices based on the communication function of the Ethernet industrial protocol, without the need to develop and maintain the protocol for the device itself. When sending and receiving data, although the device communicates based on the Ethernet industrial protocol, the message format based on the MODBUS protocol is parsed, so that the client and the device can directly use the MODBUS protocol to complete data transmission, thereby improving the applicability of the device, and without the need for data conversion, solving the problem of low transmission efficiency of the Ethernet industrial protocol. BRIEF DESCRIPTION OF THE DRAWINGS

[0008] Figure 1 A flow chart of a communication method based on the MODBUS protocol provided by an embodiment of the present invention; Figure 2 Another flow chart of a communication method based on the MODBUS protocol provided by an embodiment of the present invention; Figure 3 A schematic diagram of the structure of a communication terminal based on the MODBUS protocol provided by an embodiment of the present invention; Description of labels: 100. A communication terminal based on the MODBUS protocol; 101. A memory; 102. A processor. DETAILED DESCRIPTION

[0009] In order to explain the technical content, achieved objectives and effects of the present invention in detail, the following is an explanation in combination with the implementation modes and the accompanying drawings.

[0010] An embodiment of the present invention provides a communication method based on the MODBUS protocol, comprising: Configuring a message format of an Ethernet industrial protocol according to the MODBUS protocol, and creating an Ethernet communication port based on the Ethernet industrial protocol; Establishing a communication connection with a client supporting the MODBUS protocol through the Ethernet communication port, so as to receive request data sent by the client based on the communication connection; Parsing the request data according to the message format to obtain target parameters, and generating response data according to the message format and the target parameters; The response data is sent to the client via the communication connection.

[0011] From the above description, it can be seen that the beneficial effect of the present invention is that: since the device itself does not have the function of communicating based on the MODBUS protocol, the message format of the Ethernet industrial protocol is configured according to the MODBUS protocol, an Ethernet communication port is created based on the Ethernet industrial protocol, and a communication connection is made with a client supporting the MODBUS protocol through the Ethernet communication port, so that the device can complete the sending and receiving of MODBUS messages with other devices based on the communication function of the Ethernet industrial protocol, without the need to develop and maintain the protocol for the device itself. When sending and receiving data, although the device communicates based on the Ethernet industrial protocol, the message format based on the MODBUS protocol is parsed, so that the client and the device can directly use the MODBUS protocol to complete data transmission, which improves the applicability of the device, and does not require data conversion, solving the problem of low transmission efficiency of the Ethernet industrial protocol.

[0012] Furthermore, before establishing a communication connection with a client supporting the MODBUS protocol through the Ethernet communication port, the method further includes: Detecting whether the Ethernet communication port is successfully created, and if so, monitoring the connection status between the Ethernet communication port and the client, and configuring communication variables according to the connection status; The step of establishing a communication connection with a client supporting the MODBUS protocol through the Ethernet communication port to receive request data sent by the client based on the communication connection includes: Initialize a communication instruction, and open the Ethernet communication port through the communication instruction to wait for the client to actively establish a communication connection with the Ethernet communication port; After the Ethernet communication port is successfully opened, it is detected according to the communication variable whether a communication connection has been established between the Ethernet communication port and the client; if so, the request data sent by the client is received based on the communication connection.

[0013] From the above description, it can be seen that since the Ethernet communication port can only passively receive requests sent by the client during communication connection, the corresponding communication variables are configured after the Ethernet communication port is successfully created to determine whether the Ethernet communication port is connected to the client. After the device opens the Ethernet communication port, since the device cannot directly know whether the connection with the client is successful, it is necessary to monitor the communication variables in real time to determine the connection status of the two. After the connection is successful, data is received and sent based on the current communication port, so that the device that originally does not support MODBUS TCP communication can act as a MODBUS TCP server to interact with the client for data.

[0014] Further, after establishing a communication connection with a client supporting the MODBUS protocol through the Ethernet communication port, it also includes: If the request data sent by the client is not received within the preset time, the Ethernet communication port is closed and the process returns to the step of executing the initialization communication instruction.

[0015] From the above description, it can be seen that if the request data sent by the client is not received within the preset time, it indicates that the current data reception has timed out and there may be an abnormality in the communication connection. Therefore, the Ethernet communication port is closed and the initialization communication instruction is returned to allow the device to reconnect with the client and improve the fault tolerance of the communication connection.

[0016] Furthermore, before parsing the request data according to the message format to obtain the target parameters, the method further includes: Detecting whether the request data conforms to the message format; If yes, executing the step of parsing the request data according to the message format to obtain the target parameters; Otherwise, an error code is sent to the client via the communication connection, and the Ethernet communication port is closed.

[0017] From the above description, we can know that since the current device parses messages based on the MODBUS protocol, when receiving the request data, it first determines whether the data conforms to the message format. If it conforms, it parses the data. If it does not conform, it returns an error code to the client to prompt the user that there is an error in the current communication message. In this way, the reliability of data transmission and parsing is guaranteed.

[0018] Further, the detecting whether the request data conforms to the message format includes: Detecting whether the number of bytes of the requested data is greater than a preset standard number of bytes; If yes, the request data conforms to the message format; Otherwise, the request data does not conform to the message format.

[0019] From the above description, it can be seen that due to the characteristics of the message format of the MODBUS protocol, that is, it must contain 7 bytes of header information and 1 byte of function code, so whether the request data conforms to the message format is determined by the number of bytes, which ensures data reliability while improving detection efficiency.

[0020] Furthermore, the message format of configuring the Ethernet industrial protocol according to the MODBUS protocol includes: The original message element of each byte bit in the message format of the Ethernet industrial protocol is configured as a target message element corresponding to each byte bit in the message format of the MODBUS protocol in byte bit order.

[0021] From the above description, it can be seen that each byte bit in the MODBUS protocol has a different definition. Therefore, when configuring the message format of the Ethernet industrial protocol, its original message elements are configured as target message elements corresponding to the MODBUS protocol according to the byte bits, so that the Ethernet industrial protocol sends and receives data based on the message format of the MODBUS protocol.

[0022] Further, parsing the request data according to the message format to obtain the target parameter includes: The request data is decomposed one by one in byte order to obtain the value of each byte, and the value is assigned to the target message element at the same byte to obtain the target parameter.

[0023] From the above description, it can be seen that after decomposing the request data one by one according to the byte position to obtain the value of each byte position, the target message element with the same byte position is matched with the value to obtain the exact target parameter, so that the device can know the specific content requested by the client through the MODBUS protocol and respond.

[0024] Further, generating response data according to the message format and the target parameter includes: Calculate response parameters to be sent to the client according to the target parameters; The response parameters are combined according to the message format to obtain response data.

[0025] From the above description, it can be seen that after the target parameters requested by the client are known, the response parameters that the device needs to return can be directly determined by the target parameters, and the response parameters can be combined according to the message format to generate response data, so that the client can accurately obtain the specified information, thereby realizing communication between the client and the device based on the MODBUS protocol.

[0026] Furthermore, before generating the response data according to the message format and the target parameter, the method further includes: Detecting whether the data volume of the target parameter is within a preset standard data volume range; If yes, then executing the step of generating response data according to the message format and the target parameters; Otherwise, it is determined that there is an error in the request data, the Ethernet communication port is closed, and the process returns to the step of executing the initialization communication instruction.

[0027] From the above description, it can be seen that since the received target parameter may be accidentally cleared or incorrectly parsed, resulting in the data volume of the target parameter being in an abnormal state, the integrity and correctness of the requested data are determined by detecting whether the data volume of the target parameter is within the standard data volume range.

[0028] Another embodiment of the present invention provides a communication terminal based on the MODBUS protocol, including a memory, a processor, and a computer program stored in the memory and running on the processor, and when the processor executes the computer program, each step in the above-mentioned communication method based on the MODBUS protocol is implemented.

[0029] From the above description, it can be seen that the beneficial effect of the present invention is that: since the device itself does not have the function of communicating based on the MODBUS protocol, the message format of the Ethernet industrial protocol is configured according to the MODBUS protocol, an Ethernet communication port is created based on the Ethernet industrial protocol, and a communication connection is made with a client supporting the MODBUS protocol through the Ethernet communication port, so that the device can complete the sending and receiving of MODBUS messages with other devices based on the communication function of the Ethernet industrial protocol, without the need to develop and maintain the protocol for the device itself. When sending and receiving data, although the device communicates based on the Ethernet industrial protocol, the message format based on the MODBUS protocol is parsed, so that the client and the device can directly use the MODBUS protocol to complete data transmission, which improves the applicability of the device, and does not require data conversion, solving the problem of low transmission efficiency of the Ethernet industrial protocol.

[0030] The communication method and terminal based on MODBUS protocol of the present invention can be applied to Omron NX1P2 PLC device, which communicates with the host computer through MODBUS protocol, effectively improving the applicability of device scenarios. The following is an explanation through specific implementation methods. Please refer to Figure 1 to Figure 2 , Embodiment 1 of the present invention is: A communication method based on the MODBUS protocol, comprising: S10. Configure a message format of the Ethernet industrial protocol according to the MODBUS protocol, and create an Ethernet communication port based on the Ethernet industrial protocol.

[0031] Specifically, the step S10 includes: S101. According to the byte order, the original message element of each byte in the message format of the Ethernet industrial protocol is configured as a target message element corresponding to each byte in the message format of the MODBUS protocol.

[0032] In some embodiments, the message elements of the MODBUS protocol include a transaction identifier, a protocol identifier, a message length, a device address, a function code, a data field, and an exception code. In the MODBUS protocol, the transaction identifier, the protocol identifier, the message length, and the device address are collectively referred to as a message header. Among them, the transaction identifier occupies 2 bytes, the protocol identifier occupies 2 bytes, the message length occupies 2 bytes, and the device address occupies 1 byte, so the message header occupies a total of 7 bytes. The function code, the data field, and the exception code are collectively referred to as a protocol data unit. Among them, the function code occupies 1 byte, and the number of bytes occupied by the data field and the exception code varies depending on the function code. Therefore, the message format of the Ethernet industrial protocol (later represented by Ethernet / IP) configured based on the MODBUS protocol is specifically expressed as: message header (7 bytes) + function code (1 byte) + other data. Specifically, the correspondence between each byte in the Ethernet / IP protocol and each byte in the MODBUS protocol is saved, so that when a device receives a message of the Ethernet / IP protocol, the message content is parsed by default according to this correspondence, rather than parsing the message content according to the original message format of the Ethernet / IP protocol. For example, the first seven bytes in the original Ethernet / IP protocol represent the preamble, which is used to provide a synchronization signal for the receiving device, and the first seven bytes of the MODBUS protocol represent the message header, which is used to provide identification information for communication transmission. The first seven bytes of the Ethernet / IP protocol configured based on the MODBUS protocol also represent the message header. At this time, when the device receives a message of the Ethernet / IP protocol, parsing the first seven bytes obtains the message header information instead of the preamble.

[0033] In some embodiments, the request data packet received through the Ethernet / IP protocol usually contains 12 bytes, and the response data packet sent through the Ethernet / IP protocol varies according to the function code. For example, the request data packet received by the device through the Ethernet / IP protocol is: message header (7 bytes) + function code (1 byte) + request address (2 bytes) + data corresponding to the function code (2 bytes), and the response data packet sent by the device through the Ethernet / IP protocol is: message header (7 bytes) + function code (1 byte) + response data length (1 byte) + data of the response function code (varies according to the function code). In this way, the message format of the Ethernet / IP protocol is fixed to the message format of the MODBUS protocol. When the device interacts with other devices through the Ethernet / IP protocol, there is no need to perform data conversion, which improves the transmission efficiency while realizing MODBUS protocol communication.

[0034] It should be noted that the Omron NX1P2 device itself can support Ethernet / IP protocol, Fins protocol and CIP protocol, among which Fins protocol and CIP protocol are already packaged protocols, and their messages have specific formats, so the message format cannot be configured independently. The data of the Ethernet / IP protocol will be encapsulated into data packets before transmission, and each data packet has a corresponding address label to ensure that the data packet can be accurately delivered. This condition enables the Ethernet / IP protocol to realize the configuration of a specific message format based on the MODBUS protocol. Therefore, the present invention selects the Ethernet / IP protocol supported by the device itself as the basic protocol to realize MODBUS TCP communication.

[0035] S20. Establish a communication connection with a client supporting the MODBUS protocol through the Ethernet communication port, so as to receive request data sent by the client based on the communication connection.

[0036] Specifically, the step S20 includes: S201, initializing a communication instruction, opening the Ethernet communication port through the communication instruction, and waiting for the client to actively establish a communication connection with the Ethernet communication port.

[0037] In some embodiments, the Omron NX1P2 device needs to initialize all instructions and values ​​of related variables (such as communication errors, error identifiers, client ports, and client IP addresses, etc.) required for the communication process. The initialized communication instructions include fbCmdModGetStatus (an instruction for obtaining communication status), fbCmdModOpenUpPort (an instruction for opening a server port), fbCmdModRcvData (an instruction for receiving client data), fbCmdModSendData (an instruction for sending data), and fbCmdModTCPClose (an instruction for closing a server port).

[0038] In some embodiments, after initializing the communication instruction, the Omron NX1P2 device starts the server mode through the communication instruction bModTcpStartup, that is, the Omron NX1P2 device will receive, process and respond to relevant requests from the client in the server mode.

[0039] In some embodiments, the Omron NX1P2 device opens the Ethernet communication port through the inherent instruction fbCmdModOpenUpPort. When the variable fbCmdModOpenUpPort.Done is true, it indicates that the Ethernet communication port is opened successfully. When the variable fbCmdModOpenUpPort.Error is true, it indicates that the Ethernet communication port fails to be opened.

[0040] S202: After the Ethernet communication port is successfully opened, detect whether a communication connection has been established between the Ethernet communication port and the client according to the communication variable; if so, receive request data sent by the client based on the communication connection.

[0041] In some embodiments, when the communication variable bModTcpConnected is true, the Omron NX1P2 device starts waiting to receive request data sent by the client.

[0042] In some embodiments, if the Ethernet communication port fails to open, the device data receiving and data sending functions are turned off, and it is detected whether the current device is in server mode, that is, it is detected whether the bModTcpStartup variable is in server mode.

[0043] Specifically, before step S20, the method further includes: S12: Detect whether the Ethernet communication port is successfully created. If so, monitor the connection status between the Ethernet communication port and the client, and configure communication variables according to the connection status.

[0044] In some embodiments, stTcpSocket.Handle is an inherent variable used to characterize the status of the Ethernet communication port in the Omron NX1P2 device. When stTcpSocket.Handle>0 is detected, it indicates that an Ethernet communication port that can be used has been successfully created in the Omron NX1P2 device. When the Ethernet communication port is successfully created, the connection status between the Ethernet communication port and the client is obtained through the inherent instruction fbCmdModGetStatus of the Omron NX1P2 device. When the inherent instruction fbCmdModGetStatus is executed successfully, that is, the fbCmdModGetStatus.Done variable will become True, which means that the connection status between the Ethernet communication port and the client has been obtained, and then the connection status is configured in the user-defined communication variable bModTcpConnected.

[0045] Specifically, after step S20, the method further includes: S22: If the request data sent by the client is not received within the preset time, the Ethernet communication port is closed and the process returns to the step of executing the initialization communication instruction.

[0046] In some embodiments, the Omron NX1P2 device detects whether the request data sent by the client is received within a preset time through the inherent instruction tModTcpWaitClientLink. If the request data sent by the client is not received, the Ethernet communication port is closed through the inherent instruction fbCmdModTCPClose, and returns to the step of executing the initialization communication instruction.

[0047] In some embodiments, if the request data sent by the client is not received within a preset time, the state of the variable stModStatus can be detected, where stModStatus is used to store the communication status of the client. When stModStatus=_CLOSED is detected, it means that the client is disconnected, and the device returns to execute the initialization communication instruction to connect other clients; when stModStatus=_CLOSED_WAIT is detected, it means that the client connection has timed out, and the device closes the Ethernet communication port through the inherent instruction fbCmdModTCPClose, and returns to the step of executing the initialization communication instruction.

[0048] In some embodiments, if the Omron NX1P2 device detects that the variable fbCmdModGetStatus.DatRcvFlag is true, it indicates that the request data sent by the client is received. If the Omron NX1P2 device detects that the variable fbCmdModRcvData.Done is true, it indicates that all request data sent by the client has been successfully received.

[0049] S30. Parse the request data according to the message format to obtain target parameters, and generate response data according to the message format and the target parameters.

[0050] Specifically, in the step S30, parsing the request data according to the message format to obtain the target parameters includes: S301, decomposing the request data one by one in byte order to obtain the value of each byte, and assigning the value to the target message element on the same byte to obtain the target parameter.

[0051] In some embodiments, in the Omron NX1P2 device, the request data is stored in the array aModRcvData according to the received byte order. The target parameters obtained by parsing include the function code, the starting address and the data amount. Then step S301 is specifically as follows: the request data is parsed by the function NumToEnum(In:=BYTE TO DINT(aModRcvData[7]), InOut:=nModFunctionCode) to obtain the function code, the request data is parsed by the function AryByteTo(aModRcvData[8], UINT#2, HIGH LOW, nModRegisterAddress) to obtain the starting address, and the request data is parsed by the function AryByteTo(aModRcvData

[10] , UINT#2, HIGH LOW, nModSendDataQuantity_Word) to obtain the data amount. Among them, NumToEnum means converting a number to an enumeration type; In:=BYTE TODINT(aModRcvData[7]) means converting the 8th byte of the array aModRcvData to a double integer DINT, and then passing the double integer value as the input In to the NumToEnum function; InOut:=nModFunctionCode means assigning the enumeration value converted by the NumToEnum function to the variable nModFunctionCode. AryByteTo means parsing data in a specific format from an array; aModRcvData[8],UINT#2, HIGH LOW,nModRegisterAddress means parsing an unsigned integer (UINT) value from the 9th and 10th bytes of the array aModRcvData according to the specified byte order (HIGH LOW), and assigning the parsed value to the variable nModRegisterAddress. It can be seen that the variables nModFunctionCode, nModRegisterAddress, and nModSendDataQuantity_Word are all target message elements, and the target message elements that have been assigned are the target parameters.

[0052] Specifically, in step S30, generating response data according to the message format and the target parameter includes: S302: Calculate response parameters that need to be sent to the client according to the target parameters.

[0053] In some embodiments, if the function code of the request data sent by the client is 01, it means that multiple coils need to be read. If the function code of the request data sent by the client is 02, it means that multiple discrete input quantities need to be read. At this time, the response parameters that need to be sent to the client include the number of bytes of coils or discrete inputs, the total number of bytes of response data, and the position of the sending sequence. Specifically. In the Omron NX1P2 device, the number of bytes of coils and discrete inputs that need to be sent to the client is calculated by nQuantityMod8, the total number of bytes that need to be sent to the client is calculated by nQuantityByte, and the position of the client's request data in the sending sequence is determined by nModByteDataIndex.

[0054] S303: Combine the response parameters according to the message format to obtain response data.

[0055] In some embodiments, the above response parameters are integrated into byte data according to the message format and then assigned to a designated sending register so that the response data can be sent to the client.

[0056] Before generating response data according to the message format and the target parameters, the method further includes: S32. Detect whether the data volume of the target parameter is within the preset standard data volume range. If so, execute the step of generating response data according to the message format and the target parameter; otherwise, determine that there is an error in the request data, close the Ethernet communication port, and return to execute step S20.

[0057] In some embodiments, it is detected whether the data quantity in the request data is between the standard data quantity range (0, 1024), that is, when nModSendDataQuantity_Word=0 or nModSendDataQuantity_Word>1024 is detected, it indicates that the received request data has errors.

[0058] Specifically, before step S30, the method further includes: S24, detecting whether the request data conforms to the message format, if so, executing step S30; otherwise, sending an error code to the client through the communication connection, and closing the Ethernet communication port.

[0059] Wherein, the step S24 comprises: S241. Detect whether the number of bytes of the request data is greater than a preset standard number of bytes. If so, the request data conforms to the message format; otherwise, the request data does not conform to the message format.

[0060] In some embodiments, the Omron NX1P2 device stores the number of bytes of the requested data through the variable fbCmdModRcvData.RcvSize. When it is detected that the variable fbCmdModRcvData.RcvSize>8, it indicates that the amount of data received by the current device is normal, that is, it complies with the message format.

[0061] S40: Send the response data to the client through the communication connection.

[0062] In some embodiments, the Omron NX1P2 device sends response data to the client through the inherent instruction fbCmdModSendData. If the variable fbCmdModSendData.Done is true, it indicates that the response data is sent successfully. If the variable fbCmdModSendData.Error is true, it indicates that the response data is sent abnormally.

[0063] In some embodiments, after the response data is successfully sent to the client, the data receiving and data sending functions are turned off, and the process returns to step S20.

[0064] Please refer to Figure 3 , Embodiment 2 of the present invention is: A communication terminal 100 based on the MODBUS protocol includes a memory 101, a processor 102, and a computer program stored in the memory 101 and running on the processor 102. When the processor 102 executes the computer program, each step of a communication method based on the MODBUS protocol in embodiment 1 is implemented.

[0065] In summary, the present invention provides a communication method and terminal based on the MODBUS protocol, configures the message format of the Ethernet industrial protocol according to the MODBUS protocol, creates an Ethernet communication port based on the Ethernet industrial protocol, and communicates and connects with a client supporting the MODBUS protocol through the Ethernet communication port, so that the device can complete the sending and receiving of MODBUS messages with other devices based on the communication function of the Ethernet industrial protocol, without the need to develop and maintain the protocol for the device itself, which is convenient for developers to debug and manage the device. At the same time, due to the high openness of the MODBUS protocol, the interactivity between devices of different manufacturers is increased, thereby improving the application flexibility and scalability of the device. When sending and receiving data, the device communicates based on Ethernet, without the need to use dedicated communication hardware, effectively reducing the production cost of the device. In addition, since data sending and receiving is parsed based on the message format of the MODBUS protocol, data conversion is not required between the client and the device, solving the problem of low transmission efficiency of the Ethernet industrial protocol.

[0066] The above descriptions are merely embodiments of the present invention and are not intended to limit the patent scope of the present invention. Any equivalent transformations made using the contents of the present invention's specification and drawings, or directly or indirectly applied in related technical fields, are also included in the patent protection scope of the present invention.

Claims

1. A communication method based on MODBUS protocol, characterized in that: include: Configuring a message format of an Ethernet industrial protocol according to the MODBUS protocol, and creating an Ethernet communication port based on the Ethernet industrial protocol; Establishing a communication connection with a client supporting the MODBUS protocol through the Ethernet communication port, so as to receive request data sent by the client based on the communication connection; Parsing the request data according to the message format to obtain target parameters, and generating response data according to the message format and the target parameters; The response data is sent to the client via the communication connection.

2. A communication method based on MODBUS protocol according to claim 1, characterized in that: Before establishing a communication connection with a client supporting the MODBUS protocol through the Ethernet communication port, the method further includes: Detecting whether the Ethernet communication port is successfully created, and if so, monitoring the connection status between the Ethernet communication port and the client, and configuring communication variables according to the connection status; The step of establishing a communication connection with a client supporting the MODBUS protocol through the Ethernet communication port to receive request data sent by the client based on the communication connection includes: Initialize a communication instruction, and open the Ethernet communication port through the communication instruction to wait for the client to actively establish a communication connection with the Ethernet communication port; After the Ethernet communication port is successfully opened, it is detected according to the communication variable whether a communication connection has been established between the Ethernet communication port and the client; if so, the request data sent by the client is received based on the communication connection.

3. A communication method based on MODBUS protocol according to claim 2, characterized in that: After establishing a communication connection with a client supporting the MODBUS protocol through the Ethernet communication port, the method further includes: If the request data sent by the client is not received within the preset time, the Ethernet communication port is closed and the process returns to the step of executing the initialization communication instruction.

4. A communication method based on MODBUS protocol according to claim 1, characterized in that: Before parsing the request data according to the message format to obtain the target parameter, the method further includes: Detecting whether the request data conforms to the message format; If yes, executing the step of parsing the request data according to the message format to obtain the target parameters; Otherwise, an error code is sent to the client via the communication connection, and the Ethernet communication port is closed.

5. A communication method based on MODBUS protocol according to claim 4, characterized in that: The detecting whether the request data conforms to the message format comprises: Detecting whether the number of bytes of the requested data is greater than a preset standard number of bytes; If yes, the request data conforms to the message format; Otherwise, the request data does not conform to the message format.

6. A communication method based on MODBUS protocol according to claim 1, characterized in that: The message format of configuring the Ethernet industrial protocol according to the MODBUS protocol includes: The original message element of each byte bit in the message format of the Ethernet industrial protocol is configured as a target message element corresponding to each byte bit in the message format of the MODBUS protocol in byte bit order.

7. A communication method based on MODBUS protocol according to claim 6, characterized in that: The step of parsing the request data according to the message format to obtain the target parameter comprises: The request data is decomposed one by one in byte order to obtain the value of each byte, and the value is assigned to the target message element at the same byte to obtain the target parameter.

8. A communication method based on MODBUS protocol according to claim 1, characterized in that: The generating response data according to the message format and the target parameter comprises: Calculate response parameters to be sent to the client according to the target parameters; The response parameters are combined according to the message format to obtain response data.

9. A communication method based on MODBUS protocol according to claim 7, characterized in that: Before generating response data according to the message format and the target parameters, the method further includes: Detecting whether the data volume of the target parameter is within a preset standard data volume range; If yes, then executing the step of generating response data according to the message format and the target parameters; Otherwise, it is determined that there is an error in the request data, the Ethernet communication port is closed, and the process returns to the step of establishing a communication connection with a client supporting the MODBUS protocol through the Ethernet communication port.

10. An electronic device comprising a memory, a processor, and a computer program stored in the memory and running on the processor, characterized in that: When the processor executes the computer program, each step of the communication method based on the MODBUS protocol as described in any one of claims 1 to 9 is implemented.

Citation Information

Patent Citations

  • Ethernet / IP communication gateway and implementation method thereof

    CN101741770A

  • Method and system for realizing online remote control based on raspberry pie technology

    CN109600420A

  • Computer control system based on Modbus-TCP

    CN115051841A

  • Unified network interface for industrial networks

    US20210157758A1

  • Interface apparatus and method of converting communication protocol

    WO2019132095A1