Communication compatibility control method, device, system and gateway
Through the interrupt mechanism, the communication electrical signals of the main device are sampled and protocol analysis, which solves the time-consuming and labor-intensive parameter configuration problem when the main device communicates with the gateway in the prior art, and realizes the plug-and-play and efficient automatic configuration of the device.
Patent Information
- Application Number
- CN202510483454.7
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2025-04-17
- Publication Date
- 2025-08-15
AI Technical Summary
In the prior art, when the main device is replaced or added when communicating with the gateway, professional and technical personnel need to configure the parameters, which is time-consuming and labor-intensive and error-prone.
Through the interrupt mechanism, the communication electrical signal of the master device is sampled, the pulse width of the level signal is analyzed, the baud rate is determined, and the communication protocol is analyzed through the set of preset protocols to realize the automatic configuration between the master device and the slave device.
It realizes plug-and-play between the master and slave devices, without manual intervention, efficient and reliable operation, and simplifies the process of equipment replacement and addition.
Smart Images

Figure CN120498983A_ABST
Abstract
Description
Technical Field
[0001] The present application relates to the field of communication technology, and in particular to a communication compatibility control method, device, system and gateway. Background Art
[0002] In the existing technology, when the main device that communicates with the gateway needs to be replaced due to damage, or when some main devices that realize other functions need to be added to communicate with the gateway, professional technicians are often required to be present or remotely guided through social software to configure parameters. The entire operation process is time-consuming, labor-intensive and prone to errors. Summary of the Invention
[0003] The main purpose of this application is to propose a communication compatibility control method, device, system and gateway, which can realize automatic communication parameter configuration between the master device and the slave device through the gateway bridging method.
[0004] To achieve the above objectives, one aspect of the present application provides a communication compatibility control method, which is applied to a gateway. The method includes:
[0005] When the main serial port provided by the gateway cannot recognize the communication electrical signal sent by the main device, the communication electrical signal transmitted on the receiving signal line of the main serial port is sampled through the interrupt mechanism to obtain multiple level signals;
[0006] Determining the baud rate of the master device according to the multiple pulse widths corresponding to the multiple level signals, and converting the multiple level signals to obtain a target bit string;
[0007] Parsing the target bit string to obtain the main serial port communication protocol and all byte data;
[0008] All the byte data are decoded to obtain a character string and sent to the slave device, and the master serial port is configured according to the baud rate of the master device and the master serial port communication protocol.
[0009] Further, determining the baud rate of the master device according to the multiple pulse widths corresponding to the multiple level signals, and converting the multiple level signals to obtain the target bit string includes:
[0010] Screening multiple pulse widths corresponding to the multiple level signals to obtain a minimum pulse width;
[0011] Determining the baud rate of the master device by looking up a table according to the minimum pulse width;
[0012] For each level signal, determining a ratio of a pulse width of the level signal to the minimum pulse width, and then converting the level signal according to the ratio and a polarity parameter of the level signal to obtain a corresponding sub-bit string;
[0013] The multiple sub-bit strings corresponding to the multiple level signals are concatenated to obtain the target bit string.
[0014] Furthermore, the target bit string is parsed to obtain the main serial port communication protocol and all byte data, including:
[0015] According to a preset protocol set, the target bit string is matched and parsed to obtain the main serial port communication protocol;
[0016] According to the main serial port communication protocol, the target bit string is segmented and extracted to obtain all the byte data.
[0017] Furthermore, the preset protocol set includes multiple serial port communication protocols with different data frame formats; the matching and parsing of the target bit string according to the preset protocol set to obtain the main serial port communication protocol includes:
[0018] Remove a serial port communication protocol from the preset protocol set and use it as the current serial port communication protocol;
[0019] According to the data frame format recorded in the current serial communication protocol, a window movement step size, a first window length, and a judgment threshold are set; wherein the window movement step size is the data frame length, and the first window length is the total length of the start bit and the stop bit included in the data frame format;
[0020] Sampling the target bit string according to the window moving step size and the first window length;
[0021] If the first bit combination obtained by the sampling is different from the judgment threshold, returning to the step of removing a serial port communication protocol from the preset protocol set and setting it as the current serial port communication protocol;
[0022] If the first bit combination obtained by continuous multiple samplings is the same as the judgment threshold, then a check bit analysis is performed on the target bit string according to the data frame format to determine the primary serial port communication protocol.
[0023] Furthermore, performing check bit analysis on the target bit string according to the data frame format to determine the primary serial port communication protocol includes:
[0024] If the check bit length included in the data frame format is zero, the current serial port communication protocol is used as the main serial port communication protocol;
[0025] If the check bit length included in the data frame format is not zero, determining that the current serial port communication protocol includes a first serial port communication protocol and a second serial port communication protocol that differ only in the check bit type;
[0026] Setting the total length of the data bits and the check bits included in the data frame format to a second window length;
[0027] Sampling the target bit string according to the window moving step size and the second window length;
[0028] The first serial port communication protocol, the second serial port communication protocol, or the third serial port communication protocol is selected as the main serial port communication protocol based on the numerical distribution of the second bit combination obtained through multiple consecutive samplings; wherein the third serial port communication protocol is updated based on the current serial port communication protocol, and the data frame formats recorded in the third serial port communication protocol and the current serial port communication protocol have different data bit lengths and check bit lengths, but the data frame lengths are the same.
[0029] Furthermore, the method further comprises:
[0030] According to a preset serial port communication parameter set, a slave serial port provided on the gateway and connected to the slave device is configured in a polling manner.
[0031] To achieve the above objectives, another aspect of the present application provides a communication compatibility control device, which is applied to a gateway. The device includes:
[0032] The first module is configured to sample the communication electrical signal transmitted on the receiving signal line of the main serial port through an interrupt mechanism to obtain multiple level signals when the main serial port provided by the gateway cannot recognize the communication electrical signal sent by the main device;
[0033] A second module is configured to determine the baud rate of the master device according to the multiple pulse widths corresponding to the multiple level signals, and convert the multiple level signals to obtain a target bit string;
[0034] The third module is used to parse the target bit string to obtain the main serial port communication protocol and all byte data;
[0035] The fourth module is used to decode all the byte data to obtain a character string and send it to the slave device, and configure the master serial port according to the baud rate of the master device and the master serial port communication protocol.
[0036] To achieve the above-mentioned object, another aspect of the present application provides a communication-compatible control system, comprising a master device, a gateway, and a slave device;
[0037] The main device is used to send communication electrical signals;
[0038] The gateway is configured to, when the master serial port provided with the gateway cannot recognize the communication electrical signal, sample the communication electrical signal transmitted on the receiving signal line of the master serial port through an interrupt mechanism to obtain a plurality of level signals; determine the baud rate of the master device according to a plurality of pulse widths corresponding to the plurality of level signals, and convert the plurality of level signals to obtain a target bit string; parse the target bit string to obtain a master serial port communication protocol and all byte data; decode all the byte data to obtain a character string and send it to the slave device, and configure the master serial port according to the baud rate of the master device and the master serial port communication protocol;
[0039] The slave device is used to receive the character string.
[0040] Furthermore, the master device is further configured to configure and periodically calibrate operating parameters of the slave device according to system memory parameters when a communication link is formed between the master device and the slave device via the gateway.
[0041] To achieve the above-mentioned purpose, another aspect of the present application provides a gateway, which includes a memory and a processor, wherein the memory stores a computer program, and the processor implements the above-mentioned method when executing the computer program.
[0042] The present application includes at least the following beneficial effects: the communication electrical signal sent by the master device is sampled through the interrupt mechanism, and then the pulse width analysis and bit conversion analysis are performed on the multiple level signals obtained by the sampling, so that the communication parameter configuration between the master device and the slave device can be automated through the gateway bridging method, thereby realizing plug-and-play of the equipment in the industrial control site without the need for human intervention, and the operation process is efficient and reliable. BRIEF DESCRIPTION OF THE DRAWINGS
[0043] Figure 1 This is a flow chart of a communication compatibility control method provided in an embodiment of the present application;
[0044] Figure 2 This is a schematic diagram of the structure of a communication compatibility control device provided in an embodiment of the present application;
[0045] Figure 3 This is a schematic diagram of the structure of a communication-compatible control system provided by an embodiment of the present application;
[0046] Figure 4 It is a schematic diagram of the structure of the gateway provided in the embodiment of the present application. DETAILED DESCRIPTION
[0047] In order to make the purpose, technical solutions and advantages of the present application clearer, the present application is further described in detail below with reference to the accompanying drawings and examples. It should be understood that the specific embodiments described herein are only used to explain the present application and are not intended to limit the present application. When the following description refers to the drawings, unless otherwise indicated, the same numbers in different drawings represent the same or similar elements. The embodiments described in the following exemplary embodiments do not represent all embodiments consistent with the embodiments of the present application. They are merely examples of systems and methods consistent with some aspects of the embodiments of the present application as detailed in the appended claims.
[0048] It will be understood that the terms "first", "second", etc. used in this application may be used herein to describe various concepts, but unless otherwise specified, these concepts are not limited by these terms. These terms are only used to distinguish one concept from another. For example, without departing from the scope of the embodiments of the present application, the first information may also be referred to as the second information, and similarly, the second information may also be referred to as the first information. Depending on the context, the words "if" and "if" as used herein may be interpreted as "at the time of" or "when" or "in response to determining".
[0049] The terms "at least one", "plurality", "each", "any", etc. used in this application include "at least one", "two" or more, "plurality" or "each", "any" or "any one", "each" or "any one" as used herein.
[0050] Unless otherwise defined, all technical and scientific terms used herein have the same meaning as commonly understood by those skilled in the art to which this application pertains. The terms used herein are for the purpose of describing the embodiments of this application only and are not intended to limit this application.
[0051] In the current context of industrial development, smart manufacturing and the Internet of Things (IoT), as key nationally promoted industries, aim to achieve information exchange, management, and scheduling through the networking of widely deployed smart devices, automated equipment, and sensors, thereby ensuring efficient and coordinated operation between devices. Device-layer network communication forms the fundamental architecture for the entire system. Asynchronous serial communication is the underlying protocol for device-layer communication connections. Most industrial field communication protocols, such as Modbus in industrial control, are application-layer protocols operating on asynchronous serial communication. Because asynchronous serial communication protocols are based on electrical level characteristics and transmit bit-by-bit in bytes with start, parity, and stop bits, to ensure the receiver can correctly decode the data transmitted by the sender, both parties must agree on the same parameter configuration for baud rate, data bits, parity, and stop bits. Furthermore, the basic unit devices in smart manufacturing and IoT systems are often designed with universal modes to adapt to different scenarios. However, in actual applications, parameter configuration is required to ensure that the device operates as required.
[0052] In the existing technology, when the main device that communicates with the gateway needs to be replaced due to damage, or when some main devices that realize other functions need to be added to communicate with the gateway as the complexity of the system increases, professional technicians are often required to be present or remotely guided through social software to configure parameters. The entire operation process is time-consuming, labor-intensive and prone to errors.
[0053] In view of this, the embodiments of the present application provide a communication compatibility control method, device, system and gateway. The scheme proposes to sample the communication electrical signal sent by the master device through an interrupt mechanism, and then perform pulse width analysis and bit conversion analysis on the multiple level signals obtained by the sample, so that the communication parameter configuration between the master device and the slave device can be automated through the gateway bridging method, thereby realizing plug-and-play of equipment in the industrial control site without the need for human intervention, and the operation process is efficient and reliable.
[0054] See also Figure 1 , Figure 1 This is an optional flow chart of a communication compatibility control method provided in an embodiment of the present application. The method is applied to a gateway. The method may include, but is not limited to, the following steps S101 to S104:
[0055] Step S101: When the main serial port provided by the gateway cannot recognize the communication electrical signal sent by the main device, the communication electrical signal transmitted on the receiving signal line of the main serial port is sampled through the interrupt mechanism to obtain multiple level signals;
[0056] Step S102: determining the baud rate of the master device according to the multiple pulse widths corresponding to the multiple level signals, and converting the multiple level signals to obtain a target bit string;
[0057] Step S103: Parse the target bit string to obtain the main serial port communication protocol and all byte data;
[0058] Step S104: decode all byte data to obtain a character string and send it to the slave device, and configure the master serial port according to the baud rate of the master device and the master serial port communication protocol.
[0059] Steps S101 to S104 shown in the embodiment of the present application introduce an interrupt mechanism to sample the communication electrical signals sent by the master device and perform a series of subsequent analyses, thereby enabling the master device and the slave device to automatically configure communication parameters through a gateway bridge.
[0060] In step S101 of some embodiments, the gateway establishes a communication connection with the main device through its main serial port. The interrupt pin of the ATmega2560 chip provided in the gateway is connected to the receiving signal line of the main serial port. When the main serial port cannot recognize the communication electrical signal sent by the main device, the interrupt pin of the ATmega2560 chip is activated to start the sampling operation of the communication electrical signal.
[0061] Specifically, the interrupt pin of the ATmega2560 chip uses an interrupt mechanism triggered by the signal level polarity conversion edge (i.e., rising and falling edges), combined with the built-in hardware clock pulse counter of the ATmega2560 chip to sample the communication electrical signal. The entire sampling process is counted at the eighth frequency of the system 16MHz clock pulse, that is, the sampling clock frequency is set to 2MHz. At the same time, the clock pulse count value experienced by each level signal obtained by sampling is recorded to facilitate the subsequent determination of the pulse width of each level signal.
[0062] It should be noted that in this application, the polarity parameters of each level signal are calculated based on the asynchronous serial communication characteristic logic. Since the asynchronous serial communication characteristic logic limits the signal transmission to start with a low level, the multiple level signals obtained in the order of sampling time from first to last are actually arranged in a pattern in which low-level signals and high-level signals alternate. That is, the first level signal obtained by the first sample is a low-level signal, the second level signal obtained by the second sample is a high-level signal, and the third level signal obtained by the next sample is a low-level signal, and so on.
[0063] In the present application, the interrupt pin and hardware clock pulse counter of the ATmega2560 chip are used to sample the communication electrical signal sent by the main device when it is detected that the gateway cannot communicate normally with the main device through its main serial port, which can occupy less or even no chip computing resources; in addition, the above steps S101 to S104 can all be executed by the ATmega2560 chip. The use of the ATmega2560 chip, which has been verified by extensive engineering applications, can immediately form a product and ensure stability.
[0064] In step S102 of some embodiments, the multiple pulse widths corresponding to the multiple level signals may be calculated, but not limited to, by the following method:
[0065] (1) obtaining a sampling clock frequency recorded during the sampling of a communication electrical signal sent by the master device and a plurality of clock pulse count values corresponding to a plurality of level signals;
[0066] (2) Calculating based on the sampling clock frequency and the multiple clock pulse count values corresponding to the multiple level signals to obtain multiple pulse widths corresponding to the multiple level signals.
[0067] Specifically, the reciprocal of the sampling clock frequency is calculated to obtain the sampling clock pulse period; for each level signal, the clock pulse count value experienced by the level signal is multiplied by the sampling clock pulse period to obtain the pulse width of the level signal.
[0068] In the present application, by using the sampling clock frequency and the clock pulse count value experienced by the level signal to calculate the pulse width of the level signal, the hardware design can be simplified, and the duration of the level signal can be measured more accurately with the help of the clock pulse count value experienced by the level signal, thereby improving the calculation reliability.
[0069] In some embodiments, step S102 may include, but is not limited to, the following steps S201 to S204:
[0070] Step S201 : screening multiple pulse widths corresponding to multiple level signals to obtain a minimum pulse width. It can be understood that the level signal having the minimum pulse width has experienced the smallest clock pulse count value.
[0071] Step S202: Determine the baud rate of the master device by looking up the table according to the minimum pulse width.
[0072] In this step, a preset communication parameter table is first called, which records at least a plurality of different baud rates and their corresponding valid pulse width ranges; then, the valid pulse width range in which the minimum pulse width is located is filtered out from the preset communication parameter table, and the baud rate corresponding to the filtered valid pulse width range is directly used as the baud rate of the master device.
[0073] It should be noted that in the preset communication parameter table, the effective pulse width range corresponding to each baud rate is limited by the minimum effective pulse width and the maximum effective pulse width corresponding to the baud rate, as shown in Table 1. The bit width (us / bit) represents the bit width in units of us, and the standard bit width (clocks / bit) represents the standard bit width in units of clock pulse counts. The effective pulse width range corresponding to each baud rate can be understood as being determined based on the ±3.00% error of the standard bit width corresponding to the baud rate, that is, the minimum effective pulse width corresponding to the baud rate is obtained by directly multiplying the standard bit width corresponding to the baud rate by the difference (1-3.00%) and taking the integer part. The maximum effective pulse width corresponding to the baud rate is obtained by directly multiplying the standard bit width corresponding to the baud rate by the sum (1+3.00%) and taking the integer part.
[0074] Table 1 Preset communication parameters
[0075]
[0076] Step S203 : For each level signal, determine the ratio of the pulse width of the level signal to the minimum pulse width, and then convert the level signal according to the ratio and the polarity parameter of the level signal to obtain a corresponding sub-bit string.
[0077] In this step, if the ratio is an integer, the ratio is directly used as the number of bits. In special cases, if the ratio is a decimal, the ratio is rounded off and used as the number of bits. The number of bits is recorded as N, where N is a positive integer. When the polarity parameter of the level signal is a high level, the level signal is converted into a sub-bit string consisting of N binary values 1, and when the polarity parameter of the level signal is a low level, the level signal is converted into a sub-bit string consisting of N binary values 0.
[0078] For example, assuming that the minimum pulse width is 208 clocks, a certain level signal is a high level signal with a pulse width of 624 clocks, the number of bits is calculated to be N=624 clocks÷208 clocks=3, and then the level signal is converted into a sub-bit string of 111.
[0079] Step S204 : splicing the multiple sub-bit strings corresponding to the multiple level signals to obtain a target bit string. It should be noted that the splicing is performed in the order of the sampling times of the multiple level signals from earliest to latest.
[0080] In this application, by selecting the minimum pulse width as a benchmark to determine the baud rate of the master device, the actual communication capability of the master device can be better matched, and the communication interference problem caused by setting the baud rate too high or too low can be reduced; by selecting the minimum pulse width as a benchmark to perform bit conversion on each level signal, the signal interpretation error problem caused by inconsistent pulse width can be reduced, and the bit conversion accuracy can be improved.
[0081] In some embodiments, step S103 may include, but is not limited to, the following steps S301 to S302:
[0082] Step S301: According to a preset protocol set, the target bit string is matched and parsed to obtain a primary serial port communication protocol.
[0083] Among them, the preset protocol set includes multiple serial port communication protocols with different data frame formats. The data frame format recorded in each serial port communication protocol includes the start bit length, data bit length, check bit length, check bit type and stop bit length. The length refers to the number of bits, and the check bit type refers to whether it is an even check bit or an odd check bit.
[0084] Optionally, if the present application is mainly used in the field of industrial control, the preset protocol set is set to include 7N1 protocol, 7E / O1 protocol, 7N2 protocol, 7E / O2 protocol, 8N1 protocol, 8E / O1 protocol, 8N2 protocol and 8E / O2 protocol, and the following description is made for each serial port communication protocol:
[0085] The data frame format recorded in the 7N1 protocol includes 1 start bit, 7 data bits, 0 parity bit and 1 stop bit; the data frame format recorded in the 7E / O1 protocol includes 1 start bit, 7 data bits, 1 even / odd parity bit and 1 stop bit; the data frame format recorded in the 7N2 protocol includes 1 start bit, 7 data bits, 0 parity bit and 2 stop bits; the data frame format recorded in the 7E / O2 protocol includes 1 start bit, 7 data bits, 1 even / odd parity bit and 2 stop bits; the data frame format recorded in the 8N1 protocol includes 1 start bit, 7 data bits, 1 even / odd parity bit and 2 stop bits. The data frame format includes 1 start bit, 8 data bits, 0 parity bit and 1 stop bit; the data frame format recorded in the 8E / O1 protocol includes 1 start bit, 8 data bits, 1 even / odd parity bit and 1 stop bit; the data frame format recorded in the 8N2 protocol includes 1 start bit, 8 data bits, 0 parity bit and 2 stop bits; the data frame format recorded in the 8E / O2 protocol includes 1 start bit, 8 data bits, 1 even / odd parity bit and 2 stop bits; among them, 0-bit parity bit means there is no parity bit.
[0086] Step S302: Segment and extract the target bit string according to the primary serial port communication protocol to obtain all byte data.
[0087] When two devices perform asynchronous serial communication, the data generated by one device is usually divided into several byte frames and then sent out. In each byte frame, bytes are used as units and additional information bits (such as start bits, check bits and stop bits) are added. In order to improve timeliness, the next byte frame is sent immediately after sending one byte frame. Therefore, in this step, the target bit string can be divided according to the data frame format recorded in the main serial port communication protocol to obtain all byte frame data, and then all additional information bit data are removed from all byte frame data to obtain all byte data.
[0088] In this application, the target bit string is matched and parsed by a preset protocol set to determine the associated main serial port communication protocol, which can improve the data parsing efficiency. Subsequently, the target bit string is segmented and extracted using the main serial port communication protocol, which can effectively reduce the misrecognition rate of byte data.
[0089] In some embodiments, step S301 may include, but is not limited to, the following steps S401 to S403:
[0090] Step S401: remove a serial communication protocol from the preset protocol set and use it as the current serial communication protocol; it should be noted that the removal operation can avoid re-obtaining the same serial communication protocol for meaningless repeated verification when a subsequent verification error occurs.
[0091] Step S402: Set the window movement step, the first window length, and the judgment threshold according to the data frame format recorded in the current serial communication protocol; wherein the window movement step refers to the data frame length (i.e., the total length of the start bit, data bits, check bits, and stop bits contained in the data frame format), and the first window length refers to the total length of the start bit and stop bits contained in the data frame format.
[0092] In this step, the judgment threshold is set based on the first window length and a preset value constraint condition, and the preset value constraint condition is used to constrain the binary value of the 1-bit start bit and the binary value of the 1-bit stop bit. In this application, it is preferred to set the binary value of the 1-bit start bit to 0 and the binary value of the 1-bit stop bit to 1. In addition, since when two byte frames are sent successively, there must be a situation where the stop bit appears first (which is the stop bit of the byte frame sent first) and the start bit appears later (which is the start bit of the byte frame sent later), the judgment threshold is also required to be assigned in the order of the stop bit appearing first and the start bit appearing later.
[0093] Exemplarily, according to the specific content of the preset protocol set provided in the above step S301, if the current serial port communication protocol is the 7N1 protocol or the 7E / O1 protocol or the 8N1 protocol or the 8E / O1 protocol, that is, the total length of the start bit and the stop bit is determined to be 2 bits, then the judgment threshold is set to 10; if the current serial port communication protocol is the 7N2 protocol or the 7E / O2 protocol or the 8N2 protocol or the 8E / O2 protocol, that is, the total length of the start bit and the stop bit is 3 bits, then the judgment threshold is set to 110.
[0094] Step S403: Sample the target bit string according to the window moving step size and the first window length, and compare the first bit combination obtained by each sampling with the judgment threshold in real time to make the next action. At this time, it is clear that the length of the first bit combination obtained by each sampling and the length of the judgment threshold are both the first window length. The specific description is as follows:
[0095] If the first bit combination obtained by the current sampling is different from the judgment threshold, the process returns to step S401; if the first bit combination obtained by multiple consecutive samplings is the same as the judgment threshold, the target bit string is subjected to check bit analysis according to the data frame format recorded in the current serial port communication protocol to determine the main serial port communication protocol; it should be noted that the final number of samplings for the target bit string can be set by technical personnel based on experience, but should not exceed the maximum allowable number of samplings for the target bit string.
[0096] In this step, the sampling method for the target bit string specifically includes the following: since the first byte frame data contained in the target bit string does not contain a start bit, that is, the first bit of the target bit string is a data bit, the total length of the data bits and check bits contained in the data frame format recorded in the current serial communication protocol is counted and recorded as M, where M is a positive integer, and the initial position of the first sampling window is set to the M+1th bit of the target bit string. The first first bit combination falling within the first sampling window is extracted from the target bit string according to the first window length, and then the first sampling window is continued to be moved along the target bit string according to the window moving step. Then, the next first bit combination falling within the first sampling window is extracted from the target bit string according to the first window length, and so on.
[0097] Exemplarily, according to the specific content about the preset protocol set provided in the above step S301, if the current serial port communication protocol is the 7E / O1 protocol, the window movement step is determined to be 10 bits, the first window length is 2 bits, the total length of the data bits and check bits contained in the data frame format recorded by the 7E / O1 protocol is 8 bits, sampling begins from the 9th bit of the target bit string, the 9th and 10th bits of the target bit string are used as the first bit combination obtained by the first sampling, the 19th and 20th bits of the target bit string are used as the first bit combination obtained by the second sampling, and so on.
[0098] In this application, by removing a serial port communication protocol from the preset protocol set, and then sampling and threshold comparison analysis of the target bit string based on its recorded data frame format to assist in determining the main serial port communication protocol, the entire implementation process is relatively flexible, efficient and reliable, and does not require human intervention.
[0099] In step S403 of some embodiments, regarding the step of performing check bit analysis on the target bit string according to the data frame format recorded in the current serial port communication protocol to determine the primary serial port communication protocol, the corresponding implementation process may include, but is not limited to, the following steps S501 to S503:
[0100] Step S501: Determine the check bit length contained in the data frame format recorded in the current serial communication protocol to make the next step, as described in detail below:
[0101] If the parity bit length included in the data frame format is zero (i.e., there is no parity bit), the current serial port communication protocol is directly output as the main serial port communication protocol; if the parity bit length included in the data frame format is not zero (i.e., there is a 1-bit even / odd parity bit), it is determined that the current serial port communication protocol includes a first serial port communication protocol and a second serial port communication protocol that differ only in the parity bit type. The data frame format recorded by the first serial port communication protocol can be set to include a 1-bit even parity bit, and the data frame format recorded by the second serial port communication protocol can be set to include a 1-bit odd parity bit, and then the following steps S502 to S503 are continued to be executed.
[0102] Exemplarily, according to the specific content of the preset protocol set provided in the above step S301, if the current serial port communication protocol is the 7E / O1 protocol, then the first serial port communication protocol contained therein is determined to be the 7E1 protocol and the second serial port communication protocol is determined to be the 7O1 protocol; if the current serial port communication protocol is the 7E / O2 protocol, then the first serial port communication protocol contained therein is determined to be the 7E2 protocol and the second serial port communication protocol is determined to be the 7O2 protocol; if the current serial port communication protocol is the 8E / O1 protocol, then the first serial port communication protocol contained therein is determined to be the 8E1 protocol and the second serial port communication protocol is determined to be the 8O1 protocol; if the current serial port communication protocol is the 8E / O2 protocol, then the first serial port communication protocol contained therein is determined to be the 8E2 protocol and the second serial port communication protocol is determined to be the 8O2 protocol.
[0103] Step S502: Set the total length of the data bits and the check bits contained in the data frame format recorded in the current serial communication protocol as the second window length.
[0104] Step S503: Sample the target bit string according to the window moving step size and the second window length, and then select the first serial port communication protocol, the second serial port communication protocol, or the third serial port communication protocol as the main serial port communication protocol output according to the numerical distribution of the second bit combination obtained by multiple consecutive samplings; it should be noted that the final number of sampling times for the target bit string can be set by technical personnel based on experience, but should not exceed the maximum allowable number of sampling times for the target bit string.
[0105] The third serial port communication protocol is updated based on the current serial port communication protocol. The data frame formats recorded in the third serial port communication protocol and the current serial port communication protocol differ in data bit length and parity bit length, but have the same data frame length. That is, the data frame formats recorded in the third serial port communication protocol and the current serial port communication protocol have the same start bit length and stop bit length, and the total length of the data bits and parity bits is the same. Since it has been determined in step S501 that the parity bit length in the data frame format recorded in the current serial port communication protocol is not zero (i.e., there is a one-bit even / odd parity bit), the parity bit length in the data frame format recorded in the third serial port communication protocol is zero (i.e., there is no parity bit), and the data bit length in the data frame format recorded in the third serial port communication protocol is one bit longer than the data bit length in the data frame format recorded in the current serial port communication protocol.
[0106] Exemplarily, according to the specific content of the preset protocol set provided in the above step S301, if the current serial port communication protocol is the 7E / O1 protocol, it is determined that the third serial port communication protocol is updated to be the 8N1 protocol; if the current serial port communication protocol is the 7E / O2 protocol, it is determined that the third serial port communication protocol is updated to be the 8N2 protocol.
[0107] In this step, the sampling method of the target bit string specifically includes the following: since the first byte frame data contained in the target bit string does not contain a start bit, that is, the first bit of the target bit string is a data bit, the initial position of the second sampling window is set to the first bit of the target bit string, and the first second bit combination falling within the second sampling window is extracted from the target bit string according to the second window length, and then the second sampling window is continued to be moved along the target bit string according to the window moving step, and then the next second bit combination falling within the second sampling window is extracted from the target bit string according to the second window length, and so on.
[0108] Exemplarily, according to the specific content about the preset protocol set provided in the above step S301, if the current serial port communication protocol is the 7E / O1 protocol, the window movement step is determined to be 10 bits, the second window length is determined to be 8 bits, sampling is started from the 1st bit of the target bit string, the 1st to 8th bits of the target bit string are used as the second bit combination obtained by the first sampling, the 11th to 18th bits of the target bit string are used as the second bit combination obtained by the second sampling, and so on.
[0109] In this step, the implementation method of determining the main serial port communication protocol based on the numerical distribution of the second bit combination obtained by multiple consecutive samplings specifically includes the following: if the number of all binary values 1 contained in the second bit combination obtained by each sampling is an even number, then the first serial port communication protocol is output as the main serial port communication protocol; if the number of all binary values 1 contained in the second bit combination obtained by each sampling is an odd number, then the second serial port communication protocol is output as the main serial port communication protocol; if the number of all binary values 1 contained in the second bit combination obtained by multiple consecutive samplings is both odd and even (for example, the number of all binary values 1 contained in the second bit combination obtained by the first two samplings is an odd number, but the number of all binary values 1 contained in the second bit combination obtained by the third sampling is an even number), then the third serial port communication protocol is output as the main serial port communication protocol.
[0110] In this application, the main serial port communication protocol is formally determined by sampling the target bit string and judging the value distribution law according to the data frame format recorded in the current serial port communication protocol. The entire implementation process is relatively flexible, efficient and reliable, and no manual intervention is required.
[0111] In step S302 of some embodiments, another implementation of segmenting and extracting all byte data from the target bit string according to the primary serial port communication protocol may include the following:
[0112] (1) setting the data bit length contained in the data frame format recorded in the main serial port communication protocol to the third window length;
[0113] (2) According to the window moving step and the third window length, the target bit string is sampled to obtain all byte data; it should be noted that the final number of sampling times for the target bit string is the maximum allowed number of sampling times for the target bit string.
[0114] Specifically, since the first byte frame data contained in the target bit string does not contain a start bit, that is, the first bit of the target bit string is a data bit, the initial position of the third sampling window is set to the first bit of the target bit string, and the first third bit combination falling within the third sampling window is extracted from the target bit string according to the third window length, and then the third sampling window is continued to be moved along the target bit string according to the window moving step, and then the next third bit combination falling within the third sampling window is extracted from the target bit string according to the third window length, and so on; finally, all the third bit combinations obtained by sampling are directly output as all corresponding byte data.
[0115] Exemplarily, according to the specific content about the preset protocol set provided in the above step S301, if the current serial port communication protocol is the 7E / O1 protocol, the window movement step is determined to be 10 bits, the first window length is 7 bits, sampling starts from the 1st bit of the target bit string, the 1st to 7th bits of the target bit string are used as the third bit combination obtained by the first sampling, the 11th to 17th bits of the target bit string are used as the third bit combination obtained by the second sampling, and so on.
[0116] In step S104 of some embodiments, the gateway establishes a communication connection with the slave device through its slave serial port, and can decode all byte data into corresponding character data according to the decoding application layer protocol (such as ASCII protocol, etc.) equipped in the database inside the gateway, and then splice all character data to obtain a character string, and then send the character string to the slave device through the slave serial port.
[0117] In step S104 of some embodiments, the baud rate and main serial port communication protocol of the master device are used as the communication parameters of the master device, and the baud rate, data bits, check bits and stop bits of the main serial port provided in the gateway are configured so that the main serial port can normally recognize the communication electrical signals subsequently sent by the master device.
[0118] On this basis, the gateway can receive the data sent by the master device and unpack the message, record the slave device corresponding to the station number information contained in the message as the target slave device, and then repack the message according to the communication parameters of the target slave device and forward it to the target slave device via the slave serial port connected to the target slave device. Subsequently, it receives the response data returned by the target slave device, unpacks and repacks it, and then forwards it to the master device, completing the bridging of the master and slave devices under different communication parameter configurations.
[0119] In some embodiments, the above-mentioned communication compatibility control method also includes: configuring the slave serial port connected to the slave device provided in the gateway in a polling manner according to a preset serial port communication parameter set, so as to achieve reliability and stability of communication between the gateway and the slave device.
[0120] Specifically, the following operations are performed in a polling manner: a serial port communication parameter is removed from the preset serial port communication parameter set and used as the current serial port communication parameter; the gateway sends a query message to the slave device through the slave serial port according to the current serial port communication parameter; if a correct response message is received within a preset time period, the current serial port communication parameter is determined to be the communication parameter of the slave device, and the communication parameter of the slave serial port is configured accordingly; if no response message is received within the preset time period or an incorrect response message is received, a new serial port communication parameter is removed from the preset serial port communication parameter set and the same query judgment and analysis is performed.
[0121] See also Figure 2 , Figure 2 This is a schematic diagram of an optional structural composition of a communication compatibility control device provided in an embodiment of the present application, which can implement the above-mentioned communication compatibility control method. The device is applied to a gateway, and the device may include but is not limited to the following:
[0122] The first module 601 is configured to sample the communication electrical signal transmitted on the receiving signal line of the main serial port through an interrupt mechanism to obtain multiple level signals when the main serial port provided by the gateway cannot recognize the communication electrical signal sent by the main device;
[0123] The second module 602 is configured to determine the baud rate of the master device according to the multiple pulse widths corresponding to the multiple level signals, and convert the multiple level signals to obtain a target bit string;
[0124] The third module 603 is used to parse the target bit string to obtain the main serial port communication protocol and all byte data;
[0125] The fourth module 604 is configured to decode all byte data to obtain character strings and send them to the slave device, and configure the master serial port according to the baud rate of the master device and the communication protocol of the master serial port.
[0126] It can be understood that the contents of the above method embodiments are all applicable to the embodiments of the present device, the functions specifically implemented by the embodiments of the present device are the same as the functions specifically implemented by the above method embodiments, and the beneficial effects achieved by the embodiments of the present device are also the same as the beneficial effects achieved by the above method embodiments.
[0127] See also Figure 3 , Figure 3 This is a schematic diagram of an optional structural composition of a communication compatibility control system provided in an embodiment of the present application, which can implement the above-mentioned communication compatibility control method. The system includes a master device 701, a gateway 702, and a slave device 703. The master device 701 is connected to the gateway 702, and the gateway 702 is connected to the slave device 703. They respectively perform the following application functions:
[0128] The main device 701 is used to send communication electrical signals;
[0129] The gateway 702 is configured to, when the master serial port provided with the gateway cannot recognize the communication electrical signal, sample the communication electrical signal transmitted on the receiving signal line of the master serial port through an interrupt mechanism to obtain multiple level signals; determine the baud rate of the master device based on multiple pulse widths corresponding to the multiple level signals, and convert the multiple level signals to obtain a target bit string; parse the target bit string to obtain the master serial port communication protocol and all byte data; decode all the byte data to obtain a character string and send it to the slave device, and configure the master serial port according to the baud rate of the master device and the master serial port communication protocol;
[0130] The slave device 703 is configured to receive the character string.
[0131] In some embodiments, the master device 701 is further configured to configure and periodically calibrate operating parameters of the slave device 703 according to system memory parameters when a communication link is formed between the master device 701 and the slave device 703 via the gateway 702 .
[0132] Specifically, the master device 701 configures the operating parameters of the slave device 703 using the system memory parameters via communication. It then periodically obtains the current operating parameters of the slave device 703 and compares them with the system memory parameters, promptly identifying and correcting errors. This prevents operational anomalies caused by mismatched operating parameters of the slave device 703. Furthermore, when the end user replaces the slave device 703, the aforementioned method can be used to automatically configure the newly connected slave device, thereby achieving plug-and-play functionality, reducing the professional requirements and workload of on-site engineering and technical personnel.
[0133] It can be understood that the contents of the above method embodiments are all applicable to the present system embodiments, the functions specifically implemented by the present system embodiments are the same as the functions specifically implemented by the above method embodiments, and the beneficial effects achieved by the present system embodiments are also the same as the beneficial effects achieved by the above method embodiments.
[0134] An embodiment of the present application also provides a gateway, which may be a Modbus gateway. The gateway includes a memory and a processor. The memory stores a computer program, and the processor implements the above-mentioned communication compatibility control method when executing the computer program.
[0135] It can be understood that the contents of the above method embodiments are all applicable to this gateway embodiment, the functions specifically implemented by this gateway embodiment are the same as the functions specifically implemented by the above method embodiments, and the beneficial effects achieved by this gateway embodiment are also the same as the beneficial effects achieved by the above method embodiments.
[0136] See also Figure 4 , Figure 4 A schematic diagram illustrating the structure of a gateway according to another embodiment, the gateway comprising:
[0137] The processor 801 can be implemented as a general-purpose CPU (Central Processing Unit), a microprocessor, an application-specific integrated circuit (ASIC), or one or more integrated circuits, and is used to execute relevant programs to implement the technical solutions provided in the embodiments of the present application.
[0138] The memory 802 can be implemented in the form of a read-only memory (ROM), a static storage device, a dynamic storage device, or a random access memory (RAM). The memory 802 can store an operating system and other application programs. When the technical solutions provided by the embodiments of the present application are implemented through software or firmware, the relevant program code is stored in the memory 802 and is called by the processor 801 to execute the technical solutions provided by the embodiments of the present application.
[0139] Input / output interface 803, used to implement information input and output;
[0140] Communication interface 804 is used to realize communication interaction between the gateway and other devices. Communication can be achieved through wired means (such as USB, network cable, etc.) or wireless means (such as mobile network, WIFI, Bluetooth, etc.);
[0141] Bus 805 transmits information between the various components of the gateway (such as processor 801, memory 802, input / output interface 803 and communication interface 804);
[0142] The processor 801 , the memory 802 , the input / output interface 803 and the communication interface 804 are connected to each other in communication within the gateway via the bus 805 .
[0143] An embodiment of the present application also provides a computer-readable storage medium, which stores a computer program. When the computer program is executed by a processor, it implements the above-mentioned communication compatibility control method.
[0144] It can be understood that the contents of the above method embodiments are all applicable to the present storage medium embodiment, the functions specifically implemented by the present storage medium embodiment are the same as the functions specifically implemented by the above method embodiments, and the beneficial effects achieved by the present storage medium embodiment are also the same as the beneficial effects achieved by the above method embodiments.
[0145] The memory, as a non-transient computer-readable storage medium, can be used to store non-transient software programs and non-transient computer executable programs. In addition, the memory may include a high-speed random access memory and may also include a non-transient memory, such as at least one disk storage device, a flash memory device, or other non-transient solid-state storage device. In some embodiments, the memory may optionally include a memory remotely arranged relative to the processor, and these remote memories may be connected to the processor via a network. Examples of the above-mentioned network include, but are not limited to, the Internet, an intranet, a local area network, a mobile communication network, and combinations thereof.
[0146] The embodiments described in the embodiments of this application are intended to more clearly illustrate the technical solutions of the embodiments of this application and do not constitute a limitation on the technical solutions provided by the embodiments of this application. Those skilled in the art will appreciate that with the evolution of technology and the emergence of new application scenarios, the technical solutions provided in the embodiments of this application are also applicable to similar technical problems.
[0147] Those skilled in the art will understand that the technical solutions shown in the figures do not constitute a limitation on the embodiments of the present application, and may include more or fewer steps than shown in the figures, or a combination of certain steps, or different steps.
[0148] The system embodiment described above is merely illustrative. The units described as separate components may or may not be physically separate, i.e., they may be located in one place or distributed across multiple network units. Some or all of the modules may be selected based on actual needs to achieve the objectives of this embodiment.
[0149] Those skilled in the art will appreciate that all or some of the steps in the methods, systems, and functional modules / units in the devices disclosed above may be implemented as software, firmware, hardware, or appropriate combinations thereof.
[0150] The terms "first", "second", "third", "fourth", etc. (if any) in the specification of the present application and the above-mentioned drawings are used to distinguish similar objects and are not necessarily used to describe a specific order or sequential order. It should be understood that the data used in this way can be interchangeable where appropriate, so that the embodiments of the present application described herein can be implemented in an order other than those illustrated or described herein. In addition, the terms "including" and "having" and any variations thereof are intended to cover non-exclusive inclusions, for example, a process, method, system, product or device that includes a series of steps or units is not necessarily limited to those steps or units clearly listed, but may include other steps or units that are not clearly listed or inherent to these processes, methods, products or devices.
[0151] It should be understood that in this application, "at least one (item)" means one or more, and "plurality" means two or more. "And / or" is used to describe the association relationship of associated objects, indicating that three relationships may exist. For example, "A and / or B" can mean: only A exists, only B exists, and A and B exist at the same time, where A and B can be singular or plural. The character " / " generally indicates that the previous and next associated objects are in an "or" relationship. "At least one of the following items" or similar expressions refers to any combination of these items, including any combination of single items or plural items. For example, at least one of a, b or c can mean: a, b, c, "a and b", "a and c", "b and c", or "a and b and c", where a, b, c can be single or multiple.
[0152] In the several embodiments provided in this application, it should be understood that the disclosed systems and methods can be implemented in other ways. For example, the system embodiments described above are merely illustrative. For example, the division of the above units is merely a logical function division. In actual implementation, there may be other division methods, such as multiple units or components can be combined or integrated into another system, or some features can be ignored or not executed. Another point is that the mutual coupling or direct coupling or communication connection shown or discussed can be through some interfaces, indirect coupling or communication connection of systems or units, which can be electrical, mechanical or other forms.
[0153] The units described above as separate components may or may not be physically separate, and the components shown as units may or may not be physical units, that is, they may be located in one place or distributed across multiple network units. Some or all of these units may be selected according to actual needs to achieve the purpose of the solution of this embodiment.
[0154] In addition, the functional units in the various embodiments of the present application may be integrated into a single processing unit, or each unit may exist physically separately, or two or more units may be integrated into a single unit. The aforementioned integrated units may be implemented in the form of hardware or software functional units.
[0155] If the integrated unit is implemented in the form of a software functional unit and sold or used as an independent product, it can be stored in a computer-readable storage medium. Based on this understanding, the technical solution of the present application, or the part that contributes to the prior art, or all or part of the technical solution can be embodied in the form of a software product, which is stored in a storage medium and includes multiple instructions for enabling a computer device (which can be a personal computer, server, or network device, etc.) to execute all or part of the steps of the methods of various embodiments of the present application. The aforementioned storage medium includes: various media that can store programs, such as a USB flash drive, a mobile hard disk, a read-only memory (ROM), a random access memory (RAM), a magnetic disk or an optical disk.
[0156] The preferred embodiments of the present invention are described above with reference to the accompanying drawings, but are not intended to limit the scope of the present invention. Any modifications, equivalent substitutions, and improvements made by those skilled in the art without departing from the scope and essence of the present invention should be within the scope of the present invention.
Claims
1. A communication compatibility control method, characterized in that: Applied to a gateway, the method includes: When the main serial port provided by the gateway cannot recognize the communication electrical signal sent by the main device, the communication electrical signal transmitted on the receiving signal line of the main serial port is sampled through the interrupt mechanism to obtain multiple level signals; Determining the baud rate of the master device according to the multiple pulse widths corresponding to the multiple level signals, and converting the multiple level signals to obtain a target bit string; Parsing the target bit string to obtain the main serial port communication protocol and all byte data; All the byte data are decoded to obtain a character string and sent to the slave device, and the master serial port is configured according to the baud rate of the master device and the master serial port communication protocol.
2. The communication compatibility control method according to claim 1, characterized in that: The step of determining the baud rate of the master device according to the multiple pulse widths corresponding to the multiple level signals, and converting the multiple level signals to obtain a target bit string includes: Screening multiple pulse widths corresponding to the multiple level signals to obtain a minimum pulse width; Determining the baud rate of the master device by looking up a table according to the minimum pulse width; For each level signal, determining a ratio of a pulse width of the level signal to the minimum pulse width, and then converting the level signal according to the ratio and a polarity parameter of the level signal to obtain a corresponding sub-bit string; The multiple sub-bit strings corresponding to the multiple level signals are concatenated to obtain the target bit string.
3. The communication compatibility control method according to claim 1, wherein: The target bit string is parsed to obtain the main serial port communication protocol and all byte data including: According to a preset protocol set, the target bit string is matched and parsed to obtain the main serial port communication protocol; According to the main serial port communication protocol, the target bit string is segmented and extracted to obtain all the byte data.
4. The communication compatibility control method according to claim 3, characterized in that: The preset protocol set includes multiple serial communication protocols with different data frame formats; The matching and parsing of the target bit string according to the preset protocol set to obtain the primary serial port communication protocol includes: Remove a serial port communication protocol from the preset protocol set and use it as the current serial port communication protocol; According to the data frame format recorded in the current serial communication protocol, a window movement step size, a first window length, and a judgment threshold are set; wherein the window movement step size is the data frame length, and the first window length is the total length of the start bit and the stop bit included in the data frame format; Sampling the target bit string according to the window moving step size and the first window length; If the first bit combination obtained by the sampling is different from the judgment threshold, returning to the step of removing a serial port communication protocol from the preset protocol set and setting it as the current serial port communication protocol; If the first bit combination obtained by continuous multiple samplings is the same as the judgment threshold, then a check bit analysis is performed on the target bit string according to the data frame format to determine the primary serial port communication protocol.
5. The communication compatibility control method according to claim 4, characterized in that: The performing check bit analysis on the target bit string according to the data frame format to determine the primary serial port communication protocol includes: If the check bit length included in the data frame format is zero, the current serial port communication protocol is used as the main serial port communication protocol; If the check bit length included in the data frame format is not zero, determining that the current serial port communication protocol includes a first serial port communication protocol and a second serial port communication protocol that differ only in the check bit type; Setting the total length of the data bits and the check bits included in the data frame format to a second window length; Sampling the target bit string according to the window moving step size and the second window length; The first serial port communication protocol, the second serial port communication protocol, or the third serial port communication protocol is selected as the main serial port communication protocol based on the numerical distribution of the second bit combination obtained through multiple consecutive samplings; wherein the third serial port communication protocol is updated based on the current serial port communication protocol, and the data frame formats recorded in the third serial port communication protocol and the current serial port communication protocol have different data bit lengths and check bit lengths, but the data frame lengths are the same.
6. The communication compatibility control method according to any one of claims 1 to 5, characterized in that: The method further comprises: According to a preset serial port communication parameter set, a slave serial port provided on the gateway and connected to the slave device is configured in a polling manner.
7. A communication compatibility control device, characterized in that: Applied to a gateway, the device includes: The first module is configured to sample the communication electrical signal transmitted on the receiving signal line of the main serial port through an interrupt mechanism to obtain multiple level signals when the main serial port provided by the gateway cannot recognize the communication electrical signal sent by the main device; A second module is configured to determine the baud rate of the master device according to the multiple pulse widths corresponding to the multiple level signals, and convert the multiple level signals to obtain a target bit string; The third module is used to parse the target bit string to obtain the main serial port communication protocol and all byte data; The fourth module is used to decode all the byte data to obtain a character string and send it to the slave device, and configure the master serial port according to the baud rate of the master device and the master serial port communication protocol.
8. A communication compatible control system, characterized in that: Including master device, gateway and slave device; The main device is used to send communication electrical signals; The gateway is configured to sample the communication electrical signal transmitted on the receiving signal line of the main serial port through an interrupt mechanism to obtain multiple level signals when the main serial port provided therewith cannot recognize the communication electrical signal; Determining the baud rate of the master device according to the multiple pulse widths corresponding to the multiple level signals, and converting the multiple level signals to obtain a target bit string; Parsing the target bit string to obtain the main serial port communication protocol and all byte data; Decoding all the byte data to obtain a character string and sending it to the slave device, and configuring the master serial port according to the baud rate of the master device and the master serial port communication protocol; The slave device is used to receive the character string.
9. The communication compatible control system according to claim 8, characterized in that: The master device is further configured to configure and periodically calibrate operating parameters of the slave device according to system memory parameters when a communication link is formed between the master device and the slave device via the gateway.
10. A gateway, characterized in that: The gateway includes a memory and a processor, the memory stores a computer program, and the processor implements the method according to any one of claims 1 to 6 when executing the computer program.
Citation Information
Cited By
Communication compatibility control method
CN122120870A