Equipment communication method and device, equipment and storage medium

By using a synchronization and termination character mechanism between devices, the baud rates of the master and slave devices are automatically matched, solving the problem of low baud rate adjustment efficiency in existing technologies and achieving efficient and stable device communication.

CN120979872APending Publication Date: 2025-11-18JINAN MAIWEI INTELLIGENT TECHNOLOGY CO LTD
View PDF 0 Cites 0 Cited by

Patent Information

Application Number
CN202511420036.X
Authority / Receiving Office
CN · China
Patent Type
Applications(China)
Current Assignee / Owner
Filing Date
2025-09-30
Publication Date
2025-11-18

AI Technical Summary

Technical Problem

In existing technologies, serial communication between devices requires manual adjustment of the baud rate, which is inefficient and has significant limitations, making it difficult to efficiently match communication between multiple devices.

Method used

In the first communication mode, a synchronization character is sent using the current baud rate to obtain the character to be verified returned by the slave device. If they match, a termination character is sent to enter the data communication mode. If they do not match, the baud rate is re-determined, thus achieving automatic baud rate matching between the master and slave devices.

Benefits of technology

It achieves automatic matching of baud rates between master and slave devices, avoiding manual debugging, simplifying the process, and improving communication efficiency and stability.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN120979872A_ABST
    Figure CN120979872A_ABST
Patent Text Reader

Abstract

The invention discloses a device communication method and device, a device and a storage medium, relates to the technical field of data communication, is applied to a master device, and comprises the following steps: in a first communication mode, sending a synchronous character to a slave device by using a current baud rate, and then obtaining a to-be-verified character returned by the slave device based on a self fixed baud rate; the current baud rate is a baud rate value determined based on a preset baud rate sequence, and the to-be-verified character is a character locally and correspondingly received by the slave device after the master device sends a synchronous character to the slave device; if the to-be-verified character is consistent with the synchronous character, sending a termination character to the slave device by using the current baud rate, so that the slave device terminates the first communication mode when receiving the termination character, and enters a second communication mode; and if not, re-determining the current baud rate from the baud rate sequence. Therefore, the master device can be automatically matched with the baud rate of the slave device, manual debugging is avoided, no complex calculation process exists, implementation is easy, and stability is good.
Need to check novelty before this filing date? Find Prior Art

Description

TECHNICAL FIELD

[0001] The present application relates to the technical field of data communication, in particular to a device communication method and device, apparatus and storage medium. BACKGROUND

[0002] In embedded system and microcontroller development, industrial control and automation, consumer electronics and Internet of Things, computer peripherals and communication, etc. scenarios, device serial communication plays an important role. The communication rate of serial communication, especially UART (Universal Asynchronous Receiver / Transmitter) serial communication, is mainly defined by the baud rate. The most common mode is that the communication parties need to agree on a fixed baud rate in advance, and all data transmission is based on this rate. In some cases, there is no explicit baud rate value between the two parties, and manual adjustment is needed to match successfully. Moreover, if the baud rate is fixed, each adjustment needs to reconfigure the hardware and reprogram the program, which is low in efficiency. This fixed baud rate mode has certain limitations for communication between multiple devices.

[0003] Therefore, how to efficiently match the baud rate to realize communication between multiple devices is a problem to be solved by those skilled in the art. SUMMARY

[0004] The purpose of the embodiments of the present application is to provide a device communication method, apparatus, device and storage medium. According to the consistency between the characters returned from the slave device and the characters sent by the master device, it can be determined whether the baud rate of the master-slave device is matched, avoiding manual debugging of device communication; and without calculating the baud rate, there is no complex calculation process, the implementation is simple, and the stability is good; and the problem of baud rate matching of master-slave device communication can be solved.

[0005] To solve the above technical problems, in a first aspect, the embodiments of the present application provide a device communication method applied to a master device, comprising:

[0006] In the first communication mode, the synchronization characters are sent to the slave device by using the current baud rate, and then the to-be-verified characters returned by the slave device based on its own fixed baud rate are obtained. The first communication mode is a communication mode for synchronizing the baud rate of the master device and the slave device. The current baud rate is a baud rate value determined based on a pre-set baud rate sequence. The to-be-verified characters are characters corresponding to the synchronization characters received locally by the slave device after the master device sends the synchronization characters to the slave device.

[0007] If the to-be-verified characters are consistent with the synchronization characters, the termination characters are sent to the slave device by using the current baud rate, so that the slave device terminates the first communication mode when receiving the termination characters, and enters the second communication mode. The second communication mode is a mode for data communication between the master device and the slave device.

[0008] If the to-be-verified character is inconsistent with the synchronization character, a current baud rate is re-determined from the baud rate sequence, and the step of sending the synchronization character to the slave device by using the current baud rate is jumped to.

[0009] In some embodiments, the step of sending the synchronization character to the slave device by using the current baud rate comprises:

[0010] The current baud rate is screened from the baud rate sequence based on a preset screening strategy;

[0011] The synchronization character is sent to the slave device by using the current baud rate;

[0012] The preset screening strategy is a first screening strategy constructed in descending or ascending order of baud rate values, or a second screening strategy constructed based on dichotomy.

[0013] In some embodiments, the current baud rate is re-determined from the baud rate sequence, comprising:

[0014] A target baud rate value adjacent to the current baud rate is determined from the baud rate sequence;

[0015] If the target baud rate value is a single baud rate value, the target baud rate value is determined as a new current baud rate;

[0016] If the target baud rate value is two baud rate values, a new current baud rate is determined from the two baud rate values in descending or ascending order.

[0017] In some embodiments, after the step of sending the synchronization character to the slave device by using the current baud rate, the method further comprises:

[0018] It is judged whether the to-be-verified character returned by the slave device based on its fixed baud rate is acquired within a preset waiting time;

[0019] If the to-be-verified character is not acquired within the preset time, a new current baud rate is determined by using dichotomy based on a relationship between a highest baud rate value or a lowest baud rate value in the baud rate sequence and the current baud rate;

[0020] The synchronization character is sent to the slave device by using the new current baud rate.

[0021] In some embodiments, the step of judging whether the to-be-verified character returned by the slave device based on its fixed baud rate is acquired within a preset waiting time comprises:

[0022] A sending time required by the current baud rate for sending each bit of data is determined;

[0023] Determine the total transmission time required for transmitting the synchronization character using the current baud rate according to the number of bits corresponding to the synchronization character;

[0024] Determine the sum of the total transmission time and the preset redundancy time as the current preset waiting time;

[0025] Determine whether the to-be-verified character returned by the slave device based on the fixed baud rate of the slave device is acquired within the preset waiting time.

[0026] In a second aspect, an embodiment of the present application provides a device communication method applied to a slave device, and the method comprises the following steps:

[0027] In a first communication mode, a to-be-verified character is transmitted to a master device based on a fixed baud rate of the slave device; the first communication mode is a communication mode used for baud rate synchronization of the master device and the slave device; the to-be-verified character is a character locally received by the slave device after the master device transmits a synchronization character to the slave device using a current baud rate in a preset baud rate sequence;

[0028] A termination character transmitted by the master device is received, and the first communication mode is terminated, and then a second communication mode is entered; the termination character is a character transmitted by the master device when the master device determines that the to-be-verified character is consistent with the synchronization character; the master device re-determines the current baud rate from the baud rate sequence when the master device determines that the to-be-verified character is inconsistent with the synchronization character; the second communication mode is a mode used for data communication between the master device and the slave device.

[0029] In a third aspect, an embodiment of the present application provides a device communication apparatus applied to a master device, and the apparatus comprises the following steps:

[0030] A verification character receiving module is configured to transmit a synchronization character to a slave device using a current baud rate in a first communication mode, and then acquire a to-be-verified character returned by the slave device based on a fixed baud rate of the slave device; the first communication mode is a communication mode used for baud rate synchronization of the master device and the slave device; the current baud rate is a baud rate value determined based on a preset baud rate sequence; the to-be-verified character is a character locally received by the slave device after the master device transmits the synchronization character to the slave device;

[0031] A termination character transmitting module is configured to transmit a termination character to the slave device using the current baud rate when the to-be-verified character is consistent with the synchronization character, so that the slave device terminates the first communication mode and enters a second communication mode when the termination character is received; the second communication mode is a mode used for data communication between the master device and the slave device.

[0032] A jump module is configured to re-determine the current baud rate from the baud rate sequence when the to-be-verified character is inconsistent with the synchronization character, and jump to the step of transmitting the synchronization character to the slave device using the current baud rate.

[0033] In a fourth aspect, an embodiment of the present application provides a device communication apparatus, applied to a slave device, comprising:

[0034] The verification character sending module is configured to send, in the first communication mode, a to-be-verified character to the master device based on a fixed baud rate of the slave device; the first communication mode is a communication mode used for baud rate synchronization of the master device and the slave device; and the to-be-verified character is a character locally received by the slave device after the master device sends a synchronization character to the slave device using a current baud rate in a pre-set baud rate sequence.

[0035] The mode switching module is configured to receive a termination character sent by the master device, terminate the first communication mode, and then enter a second communication mode; the termination character is a character sent by the master device to the slave device when the master device determines that the to-be-verified character is consistent with the synchronization character; the master device re-determines the current baud rate in the baud rate sequence when the master device determines that the to-be-verified character is inconsistent with the synchronization character; and the second communication mode is a mode used for data communication between the master device and the slave device.

[0036] In a fifth aspect, an embodiment of the present application provides an electronic device, comprising:

[0037] The memory is configured to store a computer program.

[0038] The processor is configured to execute the computer program to implement the steps of the device communication method.

[0039] In a sixth aspect, an embodiment of the present application provides a computer readable storage medium, which stores a computer program; when the computer program is executed by a processor, the steps of the device communication method are implemented.

[0040] It can be seen from the technical scheme that the master device in the scheme can send a synchronization character to the slave device in the first communication mode by using a current baud rate in the synchronization mode, and then acquire a to-be-verified character returned by the slave device based on a fixed baud rate of the slave device; the first communication mode is a communication mode for synchronizing the baud rate of the master device and the slave device; the current baud rate is a baud rate value determined based on a pre-set baud rate sequence, and the to-be-verified character is a character locally received by the slave device after the master device sends the synchronization character to the slave device; if the to-be-verified character is consistent with the synchronization character, a termination character is sent to the slave device by using the current baud rate, so that the slave device terminates the first communication mode and enters a second communication mode when the termination character is received; the second communication mode is a mode for data communication between the master device and the slave device; if the to-be-verified character is inconsistent with the synchronization character, the current baud rate is re-determined from the baud rate sequence, and the step of sending the synchronization character to the slave device by using the current baud rate is jumped to. In this way, the master device in the scheme can select a suitable baud rate to realize communication compatibility with the slave device, and whether the baud rates of the master device and the slave device match can be determined according to the consistency between the character returned by the slave device and the character sent by the master device; in this way, the baud rate of the master device can be automatically matched with the baud rate of the slave device, and manual debugging of device communication is avoided; and in the process of matching the baud rates of the master device and the slave device, the baud rate does not need to be calculated, there is no complex calculation process, the implementation is simple, and the stability is good. BRIEF DESCRIPTION OF DRAWINGS

[0041] In order to more clearly illustrate the embodiments of the present application, the drawings needed in the embodiments will be briefly introduced below. Obviously, the drawings in the following description are only some embodiments of the present application, and other drawings can be obtained by those skilled in the art without creative labor on the basis of these drawings.

[0042] Figure 1 A device communication method flow chart is provided for the embodiments of the present application.

[0043] Figure 2 A specific device communication method flow chart is provided for the embodiments of the present application.

[0044] Figure 3 A master-slave device architecture schematic diagram is provided for the embodiments of the present application.

[0045] Figure 4 Another specific device communication method flow chart is provided for the embodiments of the present application.

[0046] Figure 5 Still another specific device communication method flow chart is provided for the embodiments of the present application.

[0047] Figure 6This is a schematic diagram of a device communication apparatus provided in an embodiment of the present invention;

[0048] Figure 7 This is a schematic diagram of another device communication device structure provided in an embodiment of the present invention;

[0049] Figure 8 This is a structural diagram of an electronic device provided in an embodiment of the present invention. Detailed Implementation

[0050] The technical solutions of the embodiments of the present invention will be clearly and completely described below with reference to the accompanying drawings. Obviously, the described embodiments are only some embodiments of the present invention, and not all embodiments. Based on the embodiments of the present invention, all other embodiments obtained by those of ordinary skill in the art without creative effort are within the protection scope of the present invention.

[0051] The terms "comprising" and "having," and any variations thereof, in the specification and accompanying drawings of this invention are intended to cover non-exclusive inclusion. For example, a process, method, system, product, or apparatus that includes a series of steps or units is not limited to the steps or units listed, but may include steps or units not listed.

[0052] To enable those skilled in the art to better understand the present invention, the present invention will be further described in detail below with reference to the accompanying drawings and specific embodiments.

[0053] like Figure 1 As shown, this embodiment discloses a device communication method applied to a master device, including:

[0054] Step S11: In the first communication mode, the synchronization character is sent to the slave device using the current baud rate, and then the character to be verified is returned by the slave device based on its own fixed baud rate. The first communication mode is a communication mode used for baud rate synchronization between the master device and the slave device. The current baud rate is a baud rate value determined based on a pre-set baud rate sequence, and the character to be verified is the character received locally by the slave device after the master device sends the synchronization character to the slave device.

[0055] In the embodiment, the master and the slave device ensure the baud rate consistent before data communication, i.e. baud rate synchronization in the first communication mode; in the process of baud rate synchronization between the master and the slave device, the master first sends a synchronization character to the slave device at the current baud rate obtained by screening; for example, the master sends a synchronization character to the slave device at the highest baud rate in the pre-set baud rate sequence as the current baud rate; then the slave device may have a mismatch between the fixed baud rate and the current baud rate of the master, and the received character may be different from the synchronization character, at this time the slave device can directly return the character received from the master (denoted as a to-be-verified character, which may be different from the synchronization character) to the master at the fixed baud rate of the slave device.

[0056] In a specific embodiment, sending the synchronization character to the slave device at the current baud rate can include: obtaining the current baud rate from the baud rate sequence based on a pre-set screening strategy; sending the synchronization character to the slave device at the current baud rate; wherein the pre-set screening strategy is a first screening strategy constructed in the order of baud rate value from large to small or from small to large, or a second screening strategy constructed based on dichotomy. Specifically, the sequence of baud rates selectable by the master and the corresponding screening strategy can be pre-set; for example, screening in the order of baud rate value from large to small or from small to large; or screening the baud rate based on dichotomy, such as starting from the middle value of the baud rate. Further, the master can send the synchronization character to the slave device requiring baud rate synchronization based on the current baud rate obtained by screening; and after sending, the master is in a state of waiting to receive the to-be-verified character returned by the slave device.

[0057] In another specific embodiment, after the synchronization character is sent to the slave device using the current baud rate, the method can further include: determining whether the to-be-verified character returned by the slave device based on the fixed baud rate of the slave device is acquired within a preset waiting time; if the to-be-verified character is not acquired within the preset time, determining a new current baud rate using a bisection method based on a relationship between the highest baud rate value or the lowest baud rate value in the baud rate sequence and the current baud rate; and sending the synchronization character to the slave device using the new current baud rate. Specifically, after the master device sends the synchronization character to the slave device at the current baud rate, the slave device can not receive the synchronization character due to a baud rate mismatch. In this case, the master device can set a waiting time, i.e., start timing after sending the synchronization character to the slave device. If the to-be-verified character returned by the slave device is not acquired within the preset waiting time, it indicates that the baud rates are mismatched, and in this case, it is possible that the slave device does not receive the synchronization character. To speed up the baud rate synchronization process, a suitable baud rate can be selected as the new current baud rate based on the bisection method, in combination with the current baud rate and the highest value and the lowest value in the pre-set baud rate sequence. Then, the master device sends the synchronization character to the slave device using the new baud rate, and continues to wait for the to-be-verified character returned by the slave device.

[0058] In another specific embodiment, determining whether the to-be-verified character returned by the slave device based on the fixed baud rate of the slave device is acquired within a preset waiting time can include: determining a transmission time required for transmitting each bit of data at the current baud rate; determining a total transmission time required for transmitting the synchronization character at the current baud rate according to a number of bits corresponding to the synchronization character; determining the preset waiting time as a sum of the total transmission time and a preset redundancy time; and determining whether the to-be-verified character returned by the slave device based on the fixed baud rate of the slave device is acquired within the preset waiting time. Specifically, to adapt the baud rate synchronization of the master device and the slave device, the waiting time after the master device sends the synchronization character can be dynamically adjusted. Here, the transmission speed of data at different baud rates is considered, for example, the time required for transmitting one bit of data at 230400 bps is about 4.3 us, and the time required for transmitting one bit of data at 115200 bps is about 8.6 us. The synchronization character is set as a fixed-bit character, i.e., the time required for transmitting the synchronization character at different baud rates is different. Here, the total transmission time required for transmitting the character is considered, and a redundancy time is set considering the transmission delay. The waiting time is dynamically set in combination with the total transmission time and the redundancy time and different baud rates, i.e., the master device waits for a different time to receive the to-be-verified character returned by the slave device after transmitting the synchronization character to the slave device at different current baud rates. In this way, the characteristics of different baud rates can be adapted, and the waiting time can be dynamically updated to avoid excessively long or short waiting time.

[0059] Step S12, if the to-be-verified character is consistent with the synchronization character, a termination character is sent to the slave device by using the current baud rate, so that the slave device terminates the first communication mode and enters the second communication mode when receiving the termination character; the second communication mode is a mode for data communication between the master device and the slave device.

[0060] Further, through the above steps, the master device can obtain the to-be-verified character returned by the slave device after sending the synchronization character to the slave device at the screened current baud rate. It can be understood that if the current baud rate of the master device matches the fixed baud rate of the slave device, the synchronization character should be consistent with the to-be-verified character, that is, there is no error in the transmission process; at this time, the master device can determine that the current baud rate matches the fixed baud rate of the slave device, and the master device can realize reliable data communication with the slave device through the current baud rate. At this time, in order to perform subsequent data communication of the master device and the slave device, the master device can continue to send a termination character to the slave device at the current baud rate, so that the slave device terminates the current baud rate synchronization mode and enters the subsequent data transmission mode, that is, terminates the first communication mode and enters the second communication mode.

[0061] Step S13, if the to-be-verified character is inconsistent with the synchronization character, the current baud rate is re-determined from the baud rate sequence, and the step of sending the synchronization character to the slave device by using the current baud rate is jumped to.

[0062] Correspondingly, if the to-be-verified character returned by the slave device and obtained by the master device is inconsistent with the synchronization character sent previously, it indicates that the current baud rate of the master device does not match the fixed baud rate of the slave device; in this case, the master device needs to reselect a baud rate and perform baud rate synchronization with the slave device again, that is, the master device re-determines the current baud rate and sends the synchronization character to the slave device at the new current baud rate; until the to-be-verified character returned by the slave device is consistent with the synchronization character, or all baud rates in the baud rate sequence are sent to the slave device as the current baud rate, and finally no condition that the to-be-verified character returned by the slave device is consistent with the synchronization character is met.

[0063] In specific embodiments, re-determining the current baud rate from the baud rate sequence can include: determining a target baud rate value adjacent to the current baud rate from the baud rate sequence; if the target baud rate value is a single baud rate value, determining the target baud rate value as a new current baud rate; if the target baud rate value is two baud rate values, determining a new current baud rate from the two baud rate values in descending or ascending order. Specifically, when the characters returned by the slave device do not match the synchronization characters sent by the master device, the master device can re-determine the current baud rate; specifically, a new baud rate can be selected from the pre-set baud rate sequence, for example, the last current baud rate is a value in the middle of the baud rate sequence, considering the characters returned by the slave device that can be obtained at present, the baud rate value can be adjusted step by step to make the characters returned by the slave device consistent with the synchronization characters sent; in this way, when re-determining the baud rate, two adjacent baud rate values can be considered, and a new baud rate can be determined from the two baud rate values in descending or ascending order.

[0064] As can be seen, in the present scheme, the master device can select a suitable baud rate to achieve communication compatibility with the slave device, and according to the consistency between the characters returned by the slave device and the characters sent by the master device, it can be determined whether the baud rates of the master and slave devices match; in this way, the master device can automatically match the baud rate of the slave device, avoiding manual debugging of device communication; in the face of a master-slave device architecture or heterogeneous devices, the master device can also synchronize the baud rate for each slave device, avoiding human intervention and improving the efficiency of device baud rate synchronization; and in the process of matching the baud rates of the master and slave devices, there is no need to calculate the baud rate, there is no complex calculation process, the implementation is simple, and the stability is good.

[0065] As Figure 2 shown, the present embodiment discloses a device communication method applied to a slave device, including:

[0066] Step S21, in the first communication mode, based on the fixed baud rate of itself, send the to-be-verified characters to the master device; wherein, the first communication mode is a communication mode used for synchronizing the baud rates of the master device and the slave device; the to-be-verified characters are characters received locally by the slave device after the master device sends the synchronization characters to the slave device using the current baud rate in the pre-set baud rate sequence.

[0067] In the embodiment, the slave device needs to wait for receiving the synchronization character sent by the master device in the first communication mode, i.e. in the baud rate synchronization stage, and sends the character received by itself to the master device based on the fixed baud rate of itself, which is recorded as a to-be-verified character. It can be understood that the master device actively sends the synchronization character to the slave device at the current baud rate for screening to perform baud rate synchronization. Since there is a mismatch between the current baud rate of the master device and the fixed baud rate of the slave device, i.e. there can be a deviation between the character received by the slave device and the synchronization character sent by the master device, the slave device returns the received character to the master device based on the fixed baud rate of itself after receiving the character sent by the master device.

[0068] In step S22, the termination character sent by the master device is received, and the first communication mode is terminated, and then the second communication mode is entered. The termination character is the character sent by the master device when it is determined that the to-be-verified character is consistent with the synchronization character. When it is determined that the to-be-verified character is inconsistent with the synchronization character, the current baud rate is re-determined from the baud rate sequence. The second communication mode is a mode for data communication between the master device and the slave device.

[0069] Further, after the slave device returns the character received from the master device (recorded as a to-be-verified character) to the master device, the master device can determine the consistency between the to-be-verified character and the synchronization character sent to the slave device previously. If they are consistent, it indicates that the fixed baud rate of the slave device is matched with the current baud rate of the master device. At this time, the master device can send the termination character representing the end of baud rate synchronization to the slave device. After the slave device receives the termination character, the first communication mode is terminated, i.e. the baud rate synchronization is completed, and normal data communication, i.e. the second communication mode, can be performed. Correspondingly, if the master device determines that the to-be-verified character returned by the slave device is inconsistent with the synchronization character sent to the slave device previously, it indicates that the current baud rate of the master device is not matched with the fixed baud rate of the slave device. In this case, the master device can re-determine a new current baud rate and continue to send the synchronization character to the slave device to continue the baud rate synchronization.

[0070] As can be seen, in the scheme, the master device can send the synchronization character to the slave device at the current baud rate, and the slave device can return the character received from the master device to the master device at the fixed baud rate of itself. Then, the master device can send the termination character to the slave device or re-determine the baud rate to send the synchronization character again according to the consistency between the to-be-verified character returned by the slave device and the synchronization character sent previously. In this way, the master device can automatically match the baud rate of the slave device, and human debugging of device communication can be avoided. Moreover, in the process of matching the baud rate of the master device and the slave device, the baud rate does not need to be calculated, and there is no complex calculation process, so the implementation is simple and the stability is good.

[0071] As Figure 3The system block diagram of the related master device and multiple slave devices is shown, wherein the master device can be connected with multiple slave devices, and the master device can be matched with each slave device in baud rate and then perform subsequent data communication. The device communication method of the present scheme will be introduced below in combination with the specific flowcharts of the master device and the slave device, and specifically includes:

[0072] In combination with Figure 3 It can be understood that since the master and slave baud rates can be different, and the baud rates of each slave device are also different; in the present scheme, no matter which slave device is accessed, the master device will send synchronization characters at different baud rates in the system set baud rate range to perform synchronization attempts, and after the slave device receives the synchronization characters, the slave device will return the received characters to the master device at the inherent baud rate of the slave device. If the master device can correctly receive the synchronization characters, the master device is synchronized successfully, and the master and slave devices remain the current baud rate to work.

[0073] Further, the master and slave devices first need to perform baud rate synchronization, and then perform data communication after the baud rate synchronization; for example Figure 4As shown, in the baud rate synchronization stage, i.e. the first communication mode, the master device is first set, and the baud rate interval is set in the master device. In the process of baud rate synchronization with the slave device, the baud rate can be first set to the highest, i.e. the current baud rate. Then the synchronization character (such as 0x5a) is sent to the slave device at the current baud rate, and the state of waiting for receiving the to-be-verified character returned by the slave device is entered. It can be understood that the slave device may not receive the synchronization character due to the fixed baud rate of the slave device being different from the current baud rate of the master device, or an error character is received. However, as long as the slave device can receive the synchronization character, the character received by the slave device is returned to the master device at the baud rate inherent to the slave device, and then the slave device enters the state of receiving the end-of-synchronization character. Correspondingly, after receiving the to-be-verified character returned by the slave device, the master device can compare whether the to-be-verified character is consistent with the synchronization character sent to the slave device previously. If yes, it indicates that the current baud rate corresponding to the master device matches the fixed baud rate of the slave device. At this time, the termination character (such as 0xa5) can be sent to the slave device, i.e. the character representing the end of baud rate matching, which represents that the normal working mode (the second communication mode) can be entered, i.e. the baud rate synchronization of the master-slave device is successful, and subsequent normal data communication can be performed. If the to-be-verified character received by the master device is inconsistent with the synchronization character sent to the slave device, it indicates that the current baud rate of the master device does not match the fixed baud rate of the slave device. At this time, the master device needs to re-adjust the current baud rate, and try from the highest baud rate to the lowest baud rate. If the to-be-verified character received is still inconsistent with the synchronization character sent to the slave device, the matching fails, and the baud rate synchronization process of the device ends. In order to optimize the time required for synchronization and quickly enter the normal working mode, after the master device sends the synchronization character at a certain baud rate, the to-be-verified character returned by the slave device can be received. The time for waiting for the return character is not fixed, but is the time for sending one frame at the baud rate. For example, at 230400 bps, the time for sending one bit is about 4.3 us, and the time for waiting for the return of one frame is about 51.6 us. At 115200 bps, the time for sending one bit is about 8.6 us, and the time for waiting for the return of one frame is about 103.2 us. In the actual process, considering the signal transmission delay, the waiting time can be reasonably left with a certain margin to avoid the setting being too strict to cause the waiting to end too early.

[0074] Further, as Figure 5As shown, in order to avoid the slave device directly entering the normal mode at the beginning to cause a synchronization error by mis-taking the synchronization character sample as the synchronization end character, a flag bit is set to represent the state of the first communication mode, such as the slave device setting a flag bit, the flag bit is first set to 0, and the state of waiting for the baud rate synchronization is entered, that is, waiting to receive the synchronization character sent by the master device; then, after receiving the character sent by the master device, the received character can be returned to the master device according to the fixed baud rate of the slave device, and the flag bit is set to 1 to indicate that only the synchronization end character (termination character) needs to be received to enter the normal data communication mode; then, if the slave device does not receive the synchronization end character, the flag bit can be set to 0 to re-enter the state of waiting to receive the synchronization character; it can be understood that the slave device can also refresh its own state based on the set waiting time, and if the slave device does not receive the synchronization termination character sent by the master device within a period of time during the waiting process to receive the synchronization end character, it can be considered that the current communication state of the master-slave device is unstable and does not meet the requirements of normal data communication; at this time, the state of the slave device can be automatically refreshed to receive the synchronization character sent by the master device to determine the baud rate again. Correspondingly, if the slave device receives the synchronization end character sent by the master device and the current flag bit is 1, it indicates that the baud rate of the master-slave device is matched, and the normal data communication can be performed, that is, the baud rate synchronization phase is ended.

[0075] As can be seen, in the present scheme, the master device can select a suitable baud rate to realize the communication compatibility of the slave device, and according to the consistency between the characters returned by the slave device and the characters sent by the master device, it can be determined whether the baud rate of the master-slave device is matched; in this way, the master device can automatically match the baud rate of the slave device, and in a complex and variable working environment, efficient and stable data interaction can be realized without manual intervention, thereby effectively improving the overall operation efficiency and reliability of the system; it can be understood that in the hot plug scene of heterogeneous devices (such as different developers and different generations of products), the master device can also automatically send synchronization characters to the slave device to match the baud rate of the master-slave device; and in the process of matching the baud rate of the master-slave device, the baud rate does not need to be calculated, and there is no complex calculation process, the master device can also set the waiting time corresponding to different baud rates to reduce the synchronization delay, and the whole implementation is simple and has good stability.

[0076] As shown in Figure 6 The present embodiment discloses a device communication apparatus applied to a master device, comprising:

[0077] The verification character receiving module 11 is used in the first communication mode to send a synchronization character to the slave device using the current baud rate, and then obtain the character to be verified returned by the slave device based on its own fixed baud rate; the first communication mode is a communication mode for baud rate synchronization between the master device and the slave device; the current baud rate is a baud rate value determined based on a preset baud rate sequence, and the character to be verified is the character received locally by the slave device after the master device sends the synchronization character to the slave device;

[0078] The termination character sending module 12 is used to send a termination character to the slave device using the current baud rate when the character to be verified is consistent with the synchronization character, so that the slave device terminates the first communication mode and enters the second communication mode when it receives the termination character; the second communication mode is a mode for data communication between the master device and the slave device.

[0079] Jump module 13 is used to re-determine the current baud rate from the baud rate sequence and jump to the step of sending the synchronization character to the slave device using the current baud rate when the character to be verified is inconsistent with the synchronization character.

[0080] Therefore, in this solution, the master device can select an appropriate baud rate to achieve communication compatibility with the slave device. Based on the consistency between the characters returned by the slave device and the characters sent by the master device, it can be determined whether the baud rates of the master and slave devices match. This enables the master device to automatically match the baud rate of the slave device, avoiding manual debugging of device communication. Furthermore, the baud rate matching process between the master and slave devices does not require baud rate calculation, has no complex calculation process, is simple to implement, and has good stability.

[0081] like Figure 7 As shown, this embodiment discloses a device communication apparatus applied to a slave device, comprising:

[0082] The verification character sending module 21 is used to send a character to be verified to the master device based on its own fixed baud rate in the first communication mode; wherein, the first communication mode is a communication mode for baud rate synchronization between the master device and the slave device; the character to be verified is the character received locally by the slave device after the master device sends a synchronization character to the slave device using the current baud rate in a pre-set baud rate sequence;

[0083] The mode switching module 22 is used to receive the termination character sent by the master device, terminate the first communication mode, and then enter the second communication mode; the termination character is the character sent by the master device to the slave device when it determines that the character to be verified is consistent with the synchronization character; when the master device determines that the character to be verified is inconsistent with the synchronization character, it re-determines the current baud rate from the baud rate sequence; the second communication mode is the mode for data communication between the master device and the slave device.

[0084] Therefore, in the scheme, the master device can send the synchronization character to the slave device at the current baud rate, and the slave device can return the character received from the master device to the master device at the fixed baud rate of the slave device; then the master device can send the termination character to the slave device or re-determine the baud rate and send the synchronization character again according to the consistency between the synchronization character sent previously and the to-be-verified character returned by the slave device. In this way, the master device can automatically match the baud rate of the slave device, and human debugging of device communication can be avoided. Moreover, in the process of matching the baud rate of the master device and the slave device, the baud rate does not need to be calculated, and there is no complex calculation process, so that the implementation is simple and the stability is good.

[0085] Further, the embodiment of the application further discloses an electronic device, Figure 8 is an electronic device structure diagram according to an exemplary embodiment, the content in the figure cannot be considered as any limitation on the use range of the application. The electronic device can specifically include at least one processor 31, at least one memory 32, a power supply 33, a communication interface 34, an input / output interface 35 and a communication bus 36. The memory 32 is used to store a computer program, the computer program is loaded and executed by the processor 31 to realize the related steps in the device communication method disclosed in any of the preceding embodiments. In addition, the electronic device in the embodiment can be an electronic computer.

[0086] In the embodiment, the power supply 33 is used to provide working voltage for each hardware device on the electronic device; the communication interface 34 can create a data transmission channel between the electronic device and the external device, and the communication protocol followed by the communication interface 34 can be any communication protocol applicable to the technical solution of the application, which is not limited specifically herein; the input / output interface 35 is used to obtain external input data or output data to the outside, and the specific interface type can be selected according to the specific application needs, which is not limited specifically herein.

[0087] In addition, the memory 32 as a carrier of resource storage can be a read-only memory, a random access memory, a magnetic disk or an optical disk, and the resources stored thereon can include an operating system 321, a computer program 322 and the like, and the storage mode can be temporary storage or permanent storage.

[0088] The operating system 321 is used to manage and control each hardware device on the electronic device and the computer program 322, and can be Windows Server, Netware, Unix, Linux and the like. In addition to the computer program capable of completing the device communication method executed by the electronic device disclosed in any of the preceding embodiments, the computer program 322 can further include a computer program capable of completing other specific work.

[0089] Further, the present application also discloses a computer readable storage medium for storing a computer program, wherein the computer program is executed by a processor to implement the device communication method disclosed above. For the specific steps of the method, refer to the corresponding content disclosed in the foregoing embodiments, and no longer be repeated here.

[0090] Further, the present application also discloses a computer program product comprising computer programs / instructions, which are executed by a processor to implement the device communication method disclosed above.

[0091] The various embodiments in the specification are described in a progressive manner, and each embodiment focuses on the difference from other embodiments. For the device disclosed by the embodiments, since it corresponds to the method disclosed by the embodiments, the description is relatively simple, and the relevant parts refer to the method part.

[0092] The skilled person can further realize that the units and algorithm steps of the examples described in combination with the embodiments disclosed herein can be realized by electronic hardware, computer software or a combination of both. In order to clearly show the interchangeability of hardware and software, the components and steps of the examples have been described in the above description. Whether the functions are realized in hardware or software depends on the specific application and design constraints of the technical solution. The skilled person can use different methods to realize the described functions for each specific application, but such implementation should not be considered beyond the scope of the present application.

[0093] The steps of the method or algorithm described in combination with the embodiments disclosed herein can be directly implemented by hardware, a software module executed by a processor, or a combination of both. The software module can be placed in a random access memory (RAM), a memory, a read-only memory (ROM), an electrically programmable ROM, an electrically erasable programmable ROM, a register, a hard disk, a removable disk, a CD-ROM, or any other form of storage medium known in the technical field.

[0094] Finally, it needs to be pointed out that in this document, relational terms such as first and second and the like can only be intended to distinguish one entity or operation from another entity or operation without necessarily requiring or implying any actual such relationship or order between such entities or operations. Moreover, the terms "comprising", "including", or any other variant thereof are intended to cover a non-exclusive inclusion, such that a process, method, article, or apparatus that comprises a list of elements does not include only those elements but can also include other elements not expressly listed or inherent to such process, method, article, or apparatus. Without more limitations, an element defined by the statement "comprising a" does not exclude the existence of additional identical elements in the process, method, article, or apparatus including the element.

[0095] The above has introduced the technical solutions provided by the present application in detail, and the principles and implementation manners of the present application have been described by applying specific examples. The above example explanations are only applicable to helping understand the method of the present application and its core idea; meanwhile, for the general technical personnel in the field, the specific implementation manners and application ranges will be changed according to the idea of the present application. In conclusion, the content of the specification should not be understood as a limitation of the present application.

Claims

1. A device communication method characterized by, Applied to a master device, comprising: In a first communication mode, a synchronization character is sent to a slave device using a current baud rate, and then a to-be-verified character returned by the slave device based on its own fixed baud rate is acquired; the first communication mode is a communication mode used for baud rate synchronization of the master device and the slave device; the current baud rate is a baud rate value determined based on a preset baud rate sequence, and the to-be-verified character is a character locally received by the slave device corresponding to the synchronization character after the master device sends the synchronization character to the slave device; If the to-be-verified character is consistent with the synchronization character, a termination character is sent to the slave device using the current baud rate, so that the slave device terminates the first communication mode when the termination character is received and enters a second communication mode; the second communication mode is a mode for data communication between the master device and the slave device; If the to-be-verified character is inconsistent with the synchronization character, a current baud rate is re-determined from the baud rate sequence, and the step of sending a synchronization character to a slave device using a current baud rate is jumped to.

2. The device communication method of claim 1, wherein, The step of sending a synchronization character to a slave device using a current baud rate comprises: A current baud rate is screened from the baud rate sequence based on a preset screening strategy; The synchronization character is sent to the slave device using the current baud rate; The preset screening strategy is a first screening strategy constructed in descending or ascending order of baud rate values, or a second screening strategy constructed based on dichotomy.

3. The device communication method of claim 1, wherein, The step of re-determining a current baud rate from the baud rate sequence comprises: A target baud rate value adjacent to the current baud rate is determined from the baud rate sequence; If the target baud rate value is a single baud rate value, the target baud rate value is determined as a new current baud rate; If the target baud rate value is two baud rate values, a new current baud rate is determined from the two baud rate values in descending or ascending order.

4. The device communication method according to any one of claims 1 to 3, wherein, After the step of sending a synchronization character to a slave device using a current baud rate, the method further comprises: It is judged whether the to-be-verified character returned by the slave device based on its own fixed baud rate is acquired within a preset waiting time; If the to-be-verified character is not acquired within the preset time, a new current baud rate is determined by dichotomy based on a relationship between a highest baud rate value or a lowest baud rate value in the baud rate sequence and the current baud rate; The synchronization character is sent to the slave device using the new current baud rate.

5. The device communication method of claim 4, wherein, The step of judging whether the to-be-verified character returned by the slave device based on its own fixed baud rate is acquired within a preset waiting time comprises: A transmission time required for transmitting each bit of data using the current baud rate is determined; A total transmission time required for transmitting the synchronization character using the current baud rate is determined according to a bit number corresponding to the synchronization character; A sum of the total transmission time and a preset redundancy time is determined as a current preset waiting time; It is judged whether the to-be-verified character returned by the slave device based on its own fixed baud rate is acquired within the preset waiting time.

6. A device communication method characterized by, Applied to a slave device, comprising: In the first communication mode, the slave device sends a to-be-verified character to the master device based on a fixed baud rate of the slave device; the first communication mode is a communication mode used for baud rate synchronization between the master device and the slave device; the to-be-verified character is a character locally received by the slave device after the master device sends a synchronization character to the slave device using a current baud rate in a pre-set baud rate sequence; The master device receives a termination character sent by the master device, and terminates the first communication mode, and then enters a second communication mode; the termination character is a character sent by the master device when the master device determines that the to-be-verified character is consistent with the synchronization character; the master device re-determines a current baud rate from the baud rate sequence when the master device determines that the to-be-verified character is inconsistent with the synchronization character; the second communication mode is a mode of data communication between the master device and the slave device.

7. An apparatus for communicating comprising: Applied to a master device, comprising: A verification character receiving module, configured to, in a first communication mode, send a synchronization character to a slave device using a current baud rate, and then acquire a to-be-verified character returned by the slave device based on a fixed baud rate of the slave device; the first communication mode is a communication mode used for baud rate synchronization between the master device and the slave device; the current baud rate is a baud rate value determined based on a pre-set baud rate sequence; the to-be-verified character is a character locally received by the slave device after the master device sends the synchronization character to the slave device; A termination character sending module, configured to, when the to-be-verified character is consistent with the synchronization character, send a termination character to the slave device using the current baud rate, so that the slave device terminates the first communication mode and enters a second communication mode when receiving the termination character; the second communication mode is a mode of data communication between the master device and the slave device; A jump module, configured to, when the to-be-verified character is inconsistent with the synchronization character, re-determine a current baud rate from the baud rate sequence, and jump to the step of sending a synchronization character to a slave device using a current baud rate.

8. An apparatus for communicating comprising: Applied to a slave device, comprising: A verification character sending module, configured to, in a first communication mode, send a to-be-verified character to a master device based on a fixed baud rate of the slave device; the first communication mode is a communication mode used for baud rate synchronization between the master device and the slave device; the to-be-verified character is a character locally received by the slave device after the master device sends a synchronization character to the slave device using a current baud rate in a pre-set baud rate sequence; A mode switching module, configured to receive a termination character sent by the master device, and terminate the first communication mode, and then enter a second communication mode; the termination character is a character sent by the master device when the master device determines that the to-be-verified character is consistent with the synchronization character; the master device re-determines a current baud rate from the baud rate sequence when the master device determines that the to-be-verified character is inconsistent with the synchronization character; the second communication mode is a mode of data communication between the master device and the slave device.

9. An electronic device, comprising: Comprising: a memory for storing a computer program; a processor for executing the computer program to implement the steps of the device communication method according to any one of claims 1 to 6.

10. A computer-readable storage medium, characterized in that, The computer readable storage medium has stored thereon a computer program, which, when executed by a processor, implements the steps of the device communication method according to any one of claims 1 to 6.