Master-slave communication method, electronic device, and storage medium
By exchanging flag bits between the master and slave devices in the RS485 bus system, the problem of command frame conflict with the current process is resolved, enabling timely response and reducing response latency.
Patent Information
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2025-11-11
- Publication Date
- 2026-03-20
AI Technical Summary
In RS485 bus systems used in industrial automation, if the host or slave device is executing other processes when it receives a command frame, it will cause a conflict between the command frame and the current process, resulting in a response delay.
When the host receives a command frame, it sends a flag bit to interrupt the current process of the target slave according to the preset conditions of the command. Before sending the command frame, the host reminds the slave to prepare. After receiving the flag bit, the slave interrupts its current process to execute the command frame first. The host also interrupts its process before receiving the response frame and forwards the response frame in a timely manner.
By interrupting the process in advance, the slave device can be ensured to respond to high-priority command frames in a timely manner, avoiding conflicts between command frames and the current process, reducing response latency, and alleviating the pressure on master-slave communication.
Smart Images

Figure CN121125392B_ABST
Abstract
Description
TECHNICAL FIELD
[0001] The present application relates to the technical field of communication, in particular to a master-slave communication method, an electronic device and a storage medium. BACKGROUND
[0002] In the field of industrial automation, RS485 bus system has been widely used in long-distance communication due to its simple hardware design, flexible control and low cost. In a typical engineering deployment, the RS485-based communication system can adopt a master-slave architecture, that is, a host is connected to multiple slaves, each slave has a unique communication address, and the host and the slave exchange data through addressing.
[0003] Currently, when the host computer communicates with the master or the slave, the host computer sends a command frame to the master, the master executes and responds to the command frame, or the host computer sends a command frame to the slave through the master, and the slave executes and responds to the command frame.
[0004] However, if the master or the slave is executing other processes when receiving the command frame, the command frame will conflict with the current process, resulting in response delay. SUMMARY
[0005] The present application provides a master-slave communication method, an electronic device and a storage medium to solve the problem that if the master or the slave is executing other processes when receiving the command frame, the command frame will conflict with the current process, resulting in response delay, and to achieve the purpose of reducing response delay.
[0006] In a first aspect, the present application provides a master-slave communication method applied to a master in a master-slave communication system, the master-slave communication system comprising one master and multiple slaves, and the method comprising:
[0007] When the master receives a command frame sent by a host computer for a target slave and determines that the command symbol in the command frame meets a first preset condition, the master sends a first flag bit to the target slave to make the target slave interrupt the current process; the first flag bit is used to indicate that the master is about to send the command frame;
[0008] The master sends the command frame to the target slave to make the target slave execute the command frame.
[0009] In a possible design, the method further comprises:
[0010] The master receives a second flag bit sent by the target slave, the second flag bit being sent by the target slave after generating a response frame according to the command frame; the second flag bit is used to indicate that the target slave is about to send the response frame;
[0011] The host interrupts the current process;
[0012] The host receives the response frame sent by the target slave, and sends the response frame to the upper computer.
[0013] In a possible design, the method further includes:
[0014] The host sends the command frame to the target slave when receiving the command frame sent by the upper computer and determining that the command symbol in the command frame meets the second preset condition;
[0015] The host receives the third flag bit sent by the target slave, and sends the third flag bit to the upper computer; the third flag bit is sent by the target slave in the case that the target slave continues to execute the current process and stores the command frame; the third flag bit is used to indicate that the target slave is busy, so that the upper computer stops sending other command frames.
[0016] In a possible design, the first preset condition includes: the command symbol indicates that the command frame is an operation type command frame; or, the command symbol indicates that the command frame is a read type command frame and all data identifiers in the command symbol belong to a preset list.
[0017] The second preset condition includes: the command symbol indicates that the command frame is a read type command frame and all data identifiers in the command symbol do not belong to a preset list.
[0018] In a possible design, the method further includes:
[0019] The host interrupts the current process when receiving the command frame sent by the upper computer and directed to the host;
[0020] The host executes the command frame;
[0021] The host generates a response frame according to the command frame;
[0022] The host sends the response frame to the upper computer.
[0023] The method provided by the first aspect, when the host receives the command frame sent by the upper computer and determines that the command symbol in the command frame meets the first preset condition, the host sends the first flag bit to the target slave computer, so that the host can know that the command frame needs to be executed in priority, so as to notify the target slave computer in advance through the first flag bit that the preparation of receiving the command frame needs to be made. The target slave computer interrupts the current process, so that the target slave computer can interrupt the current process before the command frame arrives, to ensure that the command frame with high priority can be responded in time, to avoid the conflict between the command frame and the current process, thereby avoiding the response delay. The host sends the command frame to the target slave computer, and the target slave computer executes the command frame, since the target slave computer has interrupted the current process, that is, the resources have been released, the target slave computer can execute the command frame immediately without queuing and waiting, so that the timely response of the command frame is realized, and the response delay is reduced.
[0024] In the second aspect, the application provides a master-slave communication method applied to a slave computer in a master-slave communication system, the master-slave communication system comprising one host computer and a plurality of slave computers, and the method comprises the following steps.
[0025] The target slave computer receives the first flag bit sent by the host computer; the first flag bit is sent by the host computer when the host computer receives the command frame sent by the upper computer and determines that the command symbol in the command frame meets the first preset condition; and the first flag bit is used to indicate that the host computer is about to send the command frame.
[0026] The target slave computer interrupts the current process.
[0027] The target slave computer receives and executes the command frame sent by the host computer.
[0028] In a possible design, the method further comprises the following steps.
[0029] The target slave computer generates a response frame according to the command frame;
[0030] The target slave computer sends a second flag bit to the host computer to make the host computer interrupt the current process; and the second flag bit is used to indicate that the target slave computer is about to send the response frame.
[0031] The target slave computer sends the response frame to the host computer, so that the host computer sends the response frame to the upper computer.
[0032] In a possible design, the method further comprises the following steps.
[0033] The target slave computer receives the command frame sent by the host computer when the host computer receives the command frame sent by the upper computer and determines that the command symbol in the command frame meets the second preset condition.
[0034] The target slave continues to execute the current process and stores the command frame;
[0035] The target slave sends a third flag bit to the host, so that the host sends the third flag bit to the master; the third flag bit is used to indicate that the target slave is busy, so that the master stops sending other command frames.
[0036] The method provided by the second aspect and the possible designs of the second aspect has the beneficial effects of the first aspect and the possible designs of the first aspect, which will not be described here.
[0037] In a third aspect, the present application provides a master-slave communication device, including: a module for executing the method in the first aspect and any possible design of the first aspect.
[0038] In a fourth aspect, the present application provides a master-slave communication device, including: a module for executing the method in the second aspect and any possible design of the second aspect.
[0039] In a fifth aspect, the present application provides an electronic device, including a first processor, the first processor executes a computer executable program or instructions in a memory to implement the master-slave communication method in the first aspect to the second aspect and any possible design of the first aspect to the second aspect.
[0040] In a sixth aspect, the present application provides an electronic device, including at least one memory and at least one second processor, the memory stores a computer executable program or instructions, and the second processor executes the computer executable program or instructions to implement the master-slave communication method in the first aspect to the second aspect and any possible design of the first aspect to the second aspect.
[0041] In a seventh aspect, the present application provides a computer readable storage medium, the computer readable storage medium stores a computer executable program or instructions, and the computer executable program or instructions is executed by a processor to implement the master-slave communication method in the first aspect to the second aspect and any possible design of the first aspect to the second aspect.
[0042] In an eighth aspect, the present application provides a computer program product, including: execution instructions, the execution instructions are stored in a readable storage medium, and at least one processor of an electronic device can read the execution instructions from the readable storage medium, and the at least one processor executes the execution instructions to make the electronic device implement the master-slave communication method in the first aspect to the second aspect and any possible design of the first aspect to the second aspect.
[0043] The above description is merely an overview of the technical solutions of the embodiments of this application. In order to better understand the technical means of the embodiments of this application and to implement them in accordance with the contents of the specification, and to make the above and other objects, features and advantages of the embodiments of this application more obvious and understandable, specific implementation methods of this application are described below. Attached Figure Description
[0044] Figure 1 This is a schematic diagram of a master-slave communication system provided in an embodiment of this application.
[0045] Figure 2 Signaling interaction of a master-slave communication method provided in an embodiment of this application Figure 1 .
[0046] Figure 3 Signaling interaction of a master-slave communication method provided in an embodiment of this application Figure 2 .
[0047] Figure 4 Signaling interaction of a master-slave communication method provided in an embodiment of this application Figure 3 .
[0048] Figure 5 Signaling interaction of a master-slave communication method provided in an embodiment of this application Figure 4 .
[0049] Figure 6 A schematic diagram of the structure of a master-slave communication device provided in an embodiment of this application. Figure 1 .
[0050] Figure 7 A schematic diagram of the structure of a master-slave communication device provided in an embodiment of this application. Figure 2 .
[0051] Figure 8 A schematic diagram of the structure of an electronic device provided in an embodiment of this application. Figure 1 .
[0052] Figure 9 A schematic diagram of the structure of an electronic device provided in an embodiment of this application. Figure 2 . Detailed Implementation
[0053] In the present application, "at least one" means one or more, and "multiple" means two or more. The "and / or" describes the association relationship of the associated objects, which means that there can be three relationships, for example, A and / or B, which can represent the following three cases: A exists alone, A and B exist together, and B exists alone, where A and B can be singular or plural. The character " / " generally represents an "or" relationship between the front and rear associated objects. "At least one" or similar expressions refer to any combination of these items, including single or multiple items in any combination. For example, at least one of a, b, or c alone, can represent: a alone, b alone, c alone, combination of a and b, combination of a and c, combination of b and c, or combination of a, b and c, where a, b, c can be single or multiple. In addition, the terms "first", "second" are only for descriptive purposes, and cannot be understood as indicating or implying relative importance.
[0054] The terms "center", "longitudinal", "transverse", "upper", "lower", "left", "right", "front", "back", and the like indicate the orientation or positional relationship based on the orientation or positional relationship shown in the drawings, and are only for the convenience of describing the present application and simplifying the description, and cannot be understood as indicating or implying that the devices or elements referred to must have a particular orientation, be constructed and operated in a particular orientation, and therefore cannot be understood as limiting the present application.
[0055] The terms "connected" and "connected" should be broadly understood, for example, the "connected" or "connected" of the circuit structure can mean physical connection, but also means electrical connection or signal connection, for example, it can be directly connected, that is, physically connected, or indirectly connected through at least one intermediate element, as long as the circuit is connected, it can also be the internal connection of two elements; In addition to signal connection through the circuit, signal connection through media medium, such as radio waves, can also be referred to. For those skilled in the art, the specific meaning of the above terms in the present application can be understood according to the specific circumstances.
[0056] Exemplarily, the present application provides a master-slave communication method, an electronic device and a storage medium, wherein the master sends a first flag bit to the target slave in advance when the command symbol in the command frame meets the first preset condition before sending the command frame to the target slave, so that the target slave interrupts the current process, and then the master sends the command frame. Therefore, the target slave can execute the command frame in priority when receiving the command frame, so as to respond to the command frame in time, thereby avoiding process conflict and reducing response delay.
[0057] Among them, the master-slave communication method provided by the present application is executed by an electronic device, or by a master-slave communication device in the electronic device.
[0058] The electronic device can be a master or a slave in a master-slave communication system, and the master or the slave can be an electric energy meter, a capacitor, a circuit breaker, or the like.
[0059] The electronic device can also be a server, a desktop computer, a mobile phone, a tablet computer, a notebook computer, a wearable device, a vehicle-mounted device, an augmented reality (AR) / virtual reality (VR) device, or the like.
[0060] The master-slave communication device can be implemented by a combination of software and / or hardware, for example, the master-slave communication device can be a transceiver chip. Alternatively, the master-slave communication device can be an application (APP), a webpage, or a public account, or the like.
[0061] For simplicity of description, the master and the slave are taken as examples for description in the embodiments of the present application.
[0062] In the following, the master-slave communication system provided by the embodiments of the present application is described. Figure 1 The master-slave communication system provided by the embodiments of the present application is described.
[0063] Please refer to Figure 1 , Figure 1 The structure of a master-slave communication system provided by an embodiment of the present application is shown in FIG. 1. As shown in FIG. 1, the master-slave communication system of the present application can include one master 11 and a plurality of slaves 12. Figure 1
[0064] Among them, Figure 1 The number of slaves 12 is taken as an example of 3 for illustration, and the number of slaves 12 is not limited in the present application.
[0065] The master 11 can be an electric energy meter or a capacitor, and the slave 12 can also be an electric energy meter or a capacitor.
[0066] In the master-slave communication system, the slave 12 can also be configured to be used as a master, which is not limited in the present application.
[0067] The master 11 is in communication connection with the plurality of slaves 12, respectively.
[0068] The plurality of slaves 12 can also be in communication connection with each other, Figure 1 which is not shown in FIG. 1.
[0069] For example, the master 11 and the slave 12 can be directly or indirectly connected through wired or wireless communication mode. The slaves 12 can be directly or indirectly connected through wired or wireless communication mode.
[0070] The wired communication mode can be coaxial cable, optical fiber, digital subscriber line (DSL), etc. The wireless communication mode can be Bluetooth, infrared, wireless (Wi-Fi), microwave, etc.
[0071] In some specific examples, the host 11 and the plurality of slaves 12 can communicate through a ModBus communication protocol based on an RS485 bus.
[0072] In some specific examples, the host 11 and the plurality of slaves 12 are provided with an RJ45 (registered jack 45) interface. The host 11 and the plurality of slaves 12 can communicate through the RJ45 interface according to the RS-485 protocol.
[0073] In the following, the following embodiments of the application will be described with the master-slave communication system having the structure as shown in the accompanying drawings. Figures 2 to 5 The master-slave communication method provided by the application will be described in detail. Figure 2
[0074] Please refer to Figure 2 , Figure 1 The signaling interaction of a master-slave communication method provided by an embodiment of the application Figure 2 . As shown in the accompanying drawings, the method comprises: Figure 2
[0075] S101, the host sends a command frame to the host.
[0076] Correspondingly, the host receives the command frame sent by the host.
[0077] The host can be a desktop computer, a mobile phone, a tablet computer, an industrial computer or a server, etc.
[0078] The command frame is a data packet encapsulated according to a preset format, and the command frame comprises a frame header, a target address, a command symbol, a data packet and a check bit, etc.
[0079] The frame header is used to identify the starting position of the command frame by the receiving party.
[0080] The target address is used to identify the destination of the command frame. In the master-slave communication system, the host and each slave have a unique address, and the host can determine the destination of the command frame according to the target address when receiving the command frame.
[0081] The command symbol is used to indicate the type of the command frame.
[0082] For example, when the master-slave communication system is an electric energy meter system, the types of the command frames can include operation type command frames and reading type command frames. The operation type command frames are, for example, command frames for controlling the electric energy meter to close or open, writing the purchased electric energy or the amount, and warning of insufficient electric energy. The reading type command frames are, for example, command frames for reading the current electric current, electric energy, electric power, and voltage.
[0083] Further, when the command frame is a reading type command frame, the command symbol further includes a data identification field, which is used to indicate the type of data to be read.
[0084] For example, when the command frame is an operation type command frame, the command symbol can be 0x01. When the command frame is a reading type command frame, the data identification of the electric current can be 0x11, the data identification of the electric energy can be 0x12, the data identification of the electric power can be 0x13, and the data identification of the voltage can be 0x14. The command symbol can be 0x02 0x11, 0x02 0x12, 0x02 0x13, or 0x02 0x14.
[0085] 0x02 0x11 indicates that the command frame is a reading type command frame and the type of data to be read is the electric current; 0x02 0x12 indicates that the command frame is a reading type command frame and the type of data to be read is the electric energy; 0x02 0x13 indicates that the command frame is a reading type command frame and the type of data to be read is the electric power; and 0x02 0x14 indicates that the command frame is a reading type command frame and the type of data to be read is the voltage.
[0086] The data packet carries specific parameters related to the command symbol.
[0087] The check code is used to check the integrity of the command frame. For example, the check code can be a cyclic redundancy check (CRC) code.
[0088] Based on this, the host computer can control the master or the target slave to execute the command frame.
[0089] S102, the host sends a first flag bit to the target slave when receiving the command frame sent by the host computer to the target slave and determining that the command symbol in the command frame satisfies a first preset condition.
[0090] Correspondingly, the target slave receives the first flag bit sent by the host. The first flag bit is sent by the host when receiving the command frame sent by the host computer to the target slave and determining that the command symbol in the command frame satisfies a first preset condition.
[0091] The host can determine, according to the target address in the command frame, whether the command frame is a command frame for the target slave or a command frame for the host. Specifically, if the target address is the address of the host, the host determines that the command frame is a command frame for the host; if the target address is the address of the slave, the host determines that the command frame is a command frame for the target slave.
[0092] The target slave is one of the plurality of slaves in the master-slave communication system, i.e., the slave indicated by the target address in the command frame.
[0093] For example, the master-slave communication system includes one host and three slaves, the address of the host is 0x00, the addresses of the slaves are 0x01, 0x02 and 0x03 respectively, and the command frame A received by the host is shown in Table 1 as follows:
[0094] Table 1
[0095]
[0096] In Table 1, the target address 0x01 is a slave address, and the host can determine that the command frame is a command frame for the target slave with the address 0x01.
[0097] Based on this, the host can first distinguish the command frames for different devices, so as to timely forward the command frame to the target slave when the command frame is a command frame for the target slave.
[0098] The command symbol in the command frame is used to indicate the type of the command frame.
[0099] Different types of command frames have different execution priorities, based on which the first preset condition and the second preset condition can be set according to the execution priorities of different types of command frames. The command frame whose command symbol meets the first preset condition is a command frame that needs to be executed preferentially, and the command frame whose command symbol meets the second preset condition is a command frame that does not need to be executed preferentially.
[0100] For example, when the master-slave communication system is an electric energy meter system:
[0101] For operation-type command frames, if the target slave executes the current process first and then responds to the command frame, it may cause frame loss, frame error and slow response, and further may cause system abnormalities and serious consequences.
[0102] For read-type command frames, different types of data have different execution priorities.
[0103] For example, when the command frame indicates that the voltage needs to be read, the host computer can determine whether the power meter is faulty according to the voltage. If the target slave machine executes the current process first and then responds to the command frame, the response can be too slow, and the fault cannot be identified in time. Therefore, the command frame needs to be executed preferentially. When the command frame indicates that the power needs to be read, the host computer can only query the current power for power statistics, and the real-time requirement is not high. Therefore, the command frame does not need to be executed preferentially.
[0104] Therefore, the operation type command frame needs to be responded to in real time by the target slave machine, and therefore needs to be executed preferentially. The execution priority of the read type command frame is related to the type of data to be read.
[0105] In some examples, the first preset condition and the second preset condition can be as follows.
[0106] The first preset condition includes that the command symbol indicates that the command frame is an operation type command frame, or the command symbol indicates that the command frame is a read type command frame and all data identifiers in the command symbol belong to a preset list.
[0107] The second preset condition includes that the command symbol indicates that the command frame is a read type command frame and all data identifiers in the command symbol do not belong to the preset list.
[0108] The preset list is used to indicate data identifiers that need to be executed preferentially when the command frame is a read type command frame. For example, the preset list includes current and voltage. That is, when the command symbol indicates that the command frame is a read type command frame and the data identifier in the command symbol is current or voltage, the command frame needs to be executed preferentially.
[0109] Based on the example in Table 1, 0x01 indicates an operation type command frame, 0x02 indicates a read type command frame, the data identifier of the current is 0x11, the data identifier of the power is 0x12, the data identifier of the energy is 0x13, and the data identifier of the voltage is 0x14. In Table 1, the command symbol of the command frame A is 0x02 0x14. The host computer can determine that the command frame A is a read type command frame. Further, the data identifier in the command symbol is 0x14, and the host computer can determine that the command frame A indicates that the type of data to be read is voltage. If the preset list includes current and voltage, the host computer determines that the command symbol in the command frame A satisfies the first preset condition.
[0110] Based on this, when the host computer receives the command frame sent by the host computer to the target slave machine and determines that the command symbol in the command frame satisfies the first preset condition, the host computer can know that the command frame needs to be executed preferentially. The host computer can send a first flag bit to the target slave machine to notify the target slave machine to prepare to receive the command frame in advance.
[0111] The first flag is used to indicate that the host is about to send a command frame. The first flag can be a frame message, or the first flag can be a hardware interrupt signal, for example, the first pin is pulled low. The form of the first flag is not limited in the application.
[0112] Therefore, when the target slave receives the first flag, it can know that the command frame sent by the host needs to be executed in priority next time, so as to realize timely response to the command frame.
[0113] S103, the target slave interrupts the current process.
[0114] The current process of the target slave is, for example, a data acquisition process, a regular calculation process, or a state reporting process.
[0115] After receiving the first flag, the target slave knows that the host is about to send a command frame, so the target slave can interrupt the current process and release resources to execute the command frame in time.
[0116] Therefore, the target slave can interrupt the current process before the command frame arrives, so as to ensure that the command frame with high priority can be responded in time, avoid the conflict between the command frame and the current process, and thus avoid causing response delay.
[0117] For example, when the first flag is a hardware interrupt signal, the target slave detects that the first pin is pulled low, triggers an interrupt, interrupts the current process, and the target slave can save the execution state of the current process to the memory to restore the current process after executing the command frame.
[0118] S104, the host sends a command frame to the target slave.
[0119] Correspondingly, the target slave receives the command frame.
[0120] It should be noted that S103 and S104 have no sequence, the target slave can interrupt the current process first, or the host can send the command frame first, as long as the target slave can execute the command frame in priority when receiving the command frame. The execution sequence of S103 and S104 is not limited in the application. Figure 3 Fig. 2 shows a case where the target slave interrupts the current process first and then the host sends the command frame.
[0121] S105, the target slave executes the command frame.
[0122] Since the target slave has interrupted the current process, the resources have been released, and the target slave can execute the command frame immediately without queuing and waiting, so as to realize timely response to the command frame and reduce the response delay.
[0123] In the embodiment of the present application, the host computer sends a command frame to the master computer. When the master computer receives the command frame sent by the host computer and determines that the command symbol in the command frame meets the first preset condition, the master computer sends a first flag bit to the target slave computer, so that the master computer can know that the command frame needs to be executed in priority, so as to notify the target slave computer in advance through the first flag bit that it needs to be prepared to receive the command frame. The target slave computer interrupts the current process, so that the target slave computer can interrupt the current process before the command frame arrives, to ensure that the command frame with high priority can be responded in time, to avoid the conflict between the command frame and the current process, thereby avoiding the response delay. The master computer sends the command frame to the target slave computer, and the target slave computer executes the command frame. Since the target slave computer has interrupted the current process, i.e. the resources have been released, the target slave computer can execute the command frame immediately without queuing and waiting, so as to realize the timely response of the command frame and reduce the response delay.
[0124] In addition, if the command frame conflicts with the current process, it will also cause too much pressure on the master-slave communication. Based on the embodiment of the present application, the command frame can be responded in stages, to reduce the pressure on the master-slave communication.
[0125] Based on the above exemplary description, after S105, the master-slave communication method further includes S106 to S1010.
[0126] Please refer to Figure 3 , Figure 2 The signaling interaction of a master-slave communication method provided by an embodiment of the present application Figure 3 . As shown in Figure 3 , after S105, the master-slave communication method further includes:
[0127] S106, the target slave computer generates a response frame according to the command frame.
[0128] After executing the command frame, the target slave computer generates a response frame according to the execution result. The format of the response frame is similar to that of the command frame. The response frame can include a frame header, a target address, a data packet, and a check bit, etc. The target address is the address of the host computer. The execution result can be encapsulated in the data packet.
[0129] For example, when the command frame is the command frame A shown in Table 1, the response frame can be as shown in Table 2:
[0130] Table 2
[0131]
[0132] Based on this, the target slave computer can feed back the execution of the command frame to the host computer or the host computer through the response frame.
[0133] S107, the target slave computer sends a second flag bit to the host computer.
[0134] Since the target slave does not know whether the host is executing a process or not, the target slave can send a second flag bit to the host in advance before sending the response frame, so as to notify the host in advance to prepare to receive the response frame.
[0135] The second flag bit is used to indicate that the target slave is about to send the response frame. The second flag bit can be a frame message, or the second flag bit can be a hardware interrupt signal, for example, the second pin is pulled low. The form of the second flag bit is not limited in the application.
[0136] Therefore, when the host receives the second flag bit, the host can know that the response frame sent by the target slave needs to be received in priority next time, so as to forward the response frame to the upper computer in time.
[0137] S108, the host interrupts the current process.
[0138] For example, the current process of the host is processing data of other slaves, communicating with the upper computer, etc.
[0139] After the host receives the second flag bit, the host knows that the target slave is about to send the response frame, so that the host can interrupt the current process and release resources, so as to forward the response frame to the upper computer in time.
[0140] Therefore, the host can interrupt the current process before the response frame arrives, so as to ensure that the response frame can be forwarded to the upper computer in time, avoid the conflict between the response frame and the current process, and avoid causing response delay.
[0141] For example, when the second flag bit is a hardware interrupt signal, the host detects that the second pin is pulled low, triggers an interrupt, interrupts the current process, and the host can save the execution state of the current process to the memory, so as to restore the current process after forwarding the response frame to the upper computer.
[0142] S109, the target slave sends the response frame to the host.
[0143] Correspondingly, the host receives the response frame sent by the target slave.
[0144] It should be noted that S108 and S109 have no sequence, the host can interrupt the current process first, or the target slave can send the response frame first, as long as the host can send the command frame to the upper computer in priority when receiving the response frame. The execution sequence of S108 and S109 is not limited in the application. Figure 4 Fig. 4 shows a case that the host interrupts the current process first and then the target slave sends the response frame.
[0145] S1010, the host sends the response frame to the upper computer.
[0146] Correspondingly, the host computer receives the response frame sent by the master computer.
[0147] Since the host computer has interrupted the current process, i.e., the resource has been released, the host computer can immediately send the response frame to the master computer without queuing, thereby realizing timely sending of the response frame and enabling the master computer to receive the response frame in time, thereby reducing response delay.
[0148] Based on this, the host computer sends the first flag bit to the target slave computer before sending the command frame, and the target slave computer interrupts its current process upon receiving the first flag bit, thereby being able to execute the command frame in time upon receiving the command frame, avoiding conflict between the command frame and the process, thereby shortening response time. The target slave computer sends the second flag bit to the host computer before obtaining the response frame and sending it to the host computer, and the host computer interrupts its current process upon receiving the second flag bit, thereby being able to forward the response frame to the master computer in time upon receiving the response frame, avoiding conflict between the response frame and the process, thereby further shortening response time.
[0149] Based on the above exemplary description, after S101, when the host computer receives the command frame sent by the master computer to the target slave computer and determines that the command symbol in the command frame meets the second preset condition, the host computer can send the command frame to the target slave computer in the following Figure 4 manner.
[0150] Please refer to Figure 4 , Figure 3 the signaling interaction of a master-slave communication method provided by an embodiment of the application Figure 4 . As shown in Figure 4 , when the host computer receives the command frame sent by the master computer to the target slave computer and determines that the command symbol in the command frame meets the second preset condition, the master-slave communication method further includes:
[0151] S201, when the host computer receives the command frame sent by the master computer to the target slave computer and determines that the command symbol in the command frame meets the second preset condition, the host computer sends the command frame to the target slave computer.
[0152] Correspondingly, the target slave computer receives the command frame sent by the host computer upon determining that the command symbol in the command frame meets the second preset condition.
[0153] As described in S102, upon determining that the command symbol in the command frame meets the second preset condition, the host computer can know that the command frame does not need to be executed preferentially, i.e., the target slave computer does not need to interrupt its process, and the host computer can directly send the command frame to the target slave computer.
[0154] S202, the target slave computer continues to execute the current process and stores the command frame.
[0155] After receiving the command frame, the target slave can continue to execute the current process. The target slave can first store the command frame, for example, store the command frame in a buffer, wait for the current process to be executed, and then take the command frame from the buffer and execute the command frame.
[0156] S203, the target slave sends the third flag bit to the host.
[0157] Correspondingly, the host receives the third flag bit sent by the target slave.
[0158] Since the target slave does not execute the command frame temporarily, the target slave can send the third flag bit to the host, so as to inform the host or the upper computer that the target slave is busy.
[0159] The third flag bit is used to indicate that the target slave is busy. The third flag bit can be a frame message, or the third flag bit can be a hardware busy signal, for example, a third pin is pulled low. The form of the third flag bit is not limited in the application.
[0160] Therefore, when the host receives the third flag bit, the host can know that the target slave is busy, so as to feedback to the upper computer in time.
[0161] It should be noted that S202 and S203 have no sequence, the target slave can execute S202 first and then execute S203, or execute S203 first and then execute S202, or execute S202 and S203 at the same time, that is, the target slave can send the third flag bit first, or store the command frame first, or store the command frame and send the third flag bit at the same time. The execution sequence of S202 and S203 is not limited in the application. Figure 5 The case of storing the command frame first and then sending the third flag bit is shown in FIG.
[0162] S204, the host sends the third flag bit to the upper computer.
[0163] Correspondingly, the upper computer receives the third flag bit sent by the host.
[0164] S205, the upper computer stops sending other command frames.
[0165] When the upper computer receives the third flag bit, the upper computer can know that the target slave is busy, and the upper computer can stop sending other command frames, so as to reduce the pressure of the master-slave communication. After the target slave executes the current process, the target slave executes the command frame and obtains a response frame, the host sends the response frame to the upper computer, and when the upper computer receives the response frame, the upper computer resumes to send other command frames. After the target slave executes the current process and executes the command frame, the target slave can send the response frame in the manner of S106 to S1010, which is not limited in the application.
[0166] If the host computer periodically sends command frames, it needs to receive a response frame before sending the next period's command frames. Based on this, the host computer stops sending other command frames. When the host computer receives the response frame, it resumes sending other command frames, thus avoiding frame loss and frame errors and ensuring the accuracy of master-slave communication.
[0167] Based on this, the host can perform different processing when it determines that the command symbol in the command frame meets the first or second preset condition, thereby achieving hierarchical response for different types of command frames. When the first preset condition is met, the first flag bit is used to prompt the target slave to interrupt the current process and prioritize the processing of the command frame, reducing response latency. When the second preset condition is met, the command frame is sent directly, and the third flag bit is used to prompt the host to wait and stop sending other command frames, reducing the pressure on master-slave communication.
[0168] Based on the above exemplary description, after S101, when the host receives a command frame sent by the host computer, the host can execute the following: Figure 5 The method shown.
[0169] Please see Figure 5 , Figure 4 Signaling interaction of a master-slave communication method provided in an embodiment of this application Figure 5 .like Figure 6 As shown, when the host receives a command frame sent by the host computer, the master-slave communication method also includes:
[0170] S301. When the host receives a command frame sent by the host computer, it interrupts the current process.
[0171] If the host determines that the command frame sent by the host computer is a command frame for the host, the host can directly interrupt the current process in order to execute the command frame in a timely manner.
[0172] S302, Host executes command frame.
[0173] S303, The host generates a response frame based on the command frame.
[0174] S304. The host computer sends a response frame to the host computer.
[0175] Based on this, the host can prioritize executing command frames and send response frames in a timely manner, so that the host computer can receive the response frames in a timely manner, thereby reducing response latency.
[0176] In addition, the host can also use a method similar to that described in S102 to first determine the priority of the command frame, decide whether to execute the current process to store the command frame first, or interrupt the current process to execute the command frame first, thereby further reducing the response latency.
[0177] Exemplarily, the present application also provides a master-slave communication device.
[0178] Please refer to Figure 6 , Figure 1 The structure of a master-slave communication device provided for an embodiment of the present application Figure 6 .
[0179] As Figures 2 to 5 shown, the first master-slave communication device 100 can exist independently or can be integrated in other devices, and can realize mutual communication with the slave devices mentioned above, for realizing the operations corresponding to the master in any of the method embodiments described above.
[0180] The first master-slave communication device 100 can include a first transceiver unit 101. The first transceiver unit 101 can also be referred to as a communication interface or a communication unit. The first transceiver unit 101 can realize the corresponding communication functions in the method embodiments described above.
[0181] Optionally, the first master-slave communication device 100 can also include a processing unit, which can read instructions and / or data in a storage unit, and the processing unit is configured to perform data processing, so that the first master-slave communication device 100 realizes the method embodiments described above.
[0182] Optionally, the first master-slave communication device 100 can also include a storage unit, which can be configured to store instructions and / or data.
[0183] The first master-slave communication device 100 can be configured to perform the actions performed by the master in the method embodiments described above. The first master-slave communication device 100 can be the master or a component configurable to the master.
[0184] Optionally, the first transceiver unit 101 can include a sending unit and a receiving unit. The sending unit is configured to perform the sending operations in the method embodiments described above. The receiving unit is configured to perform the receiving operations in the method embodiments described above.
[0185] It should be noted that the first master-slave communication device 100 can include the sending unit and not include the receiving unit. Alternatively, the first master-slave communication device 100 can include the receiving unit and not include the sending unit. Specifically, whether the sending unit and the receiving unit are included can depend on whether the sending action and the receiving action are included in the above-mentioned schemes performed by the first master-slave communication device 100.
[0186] As an example, the first master-slave communication device 100 is configured to perform the actions performed by the master in the embodiments shown in the method embodiments described above. Figure 7 .
[0187] The first master-slave communication device 100 can include a first transceiver unit 101.
[0188] The first transceiver unit 101 is configured to send a first flag bit to the target slave when receiving a command frame sent by the host computer and determining that a command symbol in the command frame meets a first preset condition, so as to make the target slave interrupt a current process; the first flag bit is used to indicate that the host computer is about to send a command frame.
[0189] The first transceiver unit 101 is configured to send a command frame to the target slave, so that the target slave executes the command frame.
[0190] It should be understood that the respective units perform the above-mentioned corresponding processes, which have been described in detail in the above-mentioned method embodiments, and thus will not be described here again for the sake of brevity.
[0191] In some examples, the first master-slave communication device 100 further includes a processing unit.
[0192] The first transceiver unit 101 is further configured to receive a second flag bit sent by the target slave, the second flag bit being sent by the target slave after generating a response frame according to the command frame; the second flag bit is used to indicate that the target slave is about to send a response frame.
[0193] The processing unit is configured to interrupt a current process.
[0194] The first transceiver unit 101 is further configured to receive a response frame sent by the target slave and send the response frame to the host computer.
[0195] In some examples, the first transceiver unit 101 is further configured to send a command frame to the target slave when receiving a command frame sent by the host computer and determining that a command symbol in the command frame meets a second preset condition.
[0196] The first transceiver unit 101 is further configured to receive a third flag bit sent by the target slave and send the third flag bit to the host computer; the third flag bit is sent by the target slave in a case where the target slave continues to execute a current process and stores the command frame; the third flag bit is used to indicate that the target slave is busy, so that the host computer stops sending other command frames.
[0197] In some examples, the first preset condition includes that the command symbol indicates that the command frame is an operation type command frame; or the command symbol indicates that the command frame is a read type command frame and all data identifiers in the command symbol belong to a preset list.
[0198] The second preset condition includes that the command symbol indicates that the command frame is a read type command frame and all data identifiers in the command symbol do not belong to a preset list.
[0199] In some examples, the first master-slave communication device 100 further includes a processing unit.
[0200] The processing unit is configured to interrupt a current process when receiving a command frame sent by the host computer and to execute the command frame; and generate a response frame according to the command frame.
[0201] The first transceiving unit 101 is further configured to send a response frame to the host computer.
[0202] Exemplarily, the application further provides a master-slave communication device.
[0203] Please refer to Figure 7 , Figure 2 The structure of a master-slave communication device provided by an embodiment of the application is shown in Figure 7 .
[0204] As Figures 2 to 5 shown, the second master-slave communication device 200 can exist independently or be integrated in other devices, and can communicate with the host computer mentioned above to implement the operation of the slave.
[0205] The second master-slave communication device 200 can include a second transceiving unit 201 and a processing unit 202. The second transceiving unit 201 can also be referred to as a communication interface or a communication unit. The processing unit 202 can read instructions and / or data in the storage unit to enable the second master-slave communication device 200 to implement the foregoing method embodiments, and the second transceiving unit 201 can implement the corresponding communication functions, and the processing unit 202 is configured to perform data processing.
[0206] Optionally, the second master-slave communication device 200 can further include a storage unit, which can be configured to store instructions and / or data.
[0207] The second master-slave communication device 200 can be configured to perform the actions performed by the slave in the foregoing method embodiments. The second master-slave communication device 200 can be the slave or a component configurable to the slave.
[0208] Optionally, the second transceiving unit 201 can include a sending unit and a receiving unit. The sending unit is configured to perform the sending operations in the foregoing method embodiments. The receiving unit is configured to perform the receiving operations in the foregoing method embodiments.
[0209] It should be noted that the second master-slave communication device 200 can include the sending unit and not include the receiving unit. Alternatively, the second master-slave communication device 200 can include the receiving unit and not include the sending unit. Specifically, whether the sending unit and the receiving unit are included can depend on whether the sending action and the receiving action are included in the above-mentioned schemes performed by the second master-slave communication device 200.
[0210] As an example, the second master-slave communication device 200 is configured to perform the actions performed by the slave in the embodiments shown in the foregoing Figure 8 .
[0211] The second master-slave communication device 200 can include a second transceiving unit 201 and a processing unit 202.
[0212] The second transceiver unit 201 is used to receive a first flag bit sent by the host; the first flag bit is sent by the host when it receives a command frame for the target slave device sent by the host computer and determines that the command character in the command frame meets the first preset condition; the first flag bit is used to indicate that the host is about to send a command frame.
[0213] Processing unit 202 is used to interrupt the current process;
[0214] The second transceiver unit 201 is used to receive command frames sent by the host.
[0215] Processing unit 202 is used to execute command frames.
[0216] In some examples, processing unit 202 is also used to generate a response frame based on the command frame;
[0217] The second transceiver unit 201 is also used to send a second flag bit to the host so that the host can interrupt the current process; the second flag bit is used to indicate that the target slave is about to send a response frame; and to send a response frame to the host so that the host can send a response frame to the host computer.
[0218] In some examples, the second master-slave communication device 200 also includes a storage unit.
[0219] The second transceiver unit 201 is also used to receive a command frame sent by the host computer for the target slave when it receives a command frame sent by the host computer and determines that the command symbol in the command frame meets the second preset condition.
[0220] Processing unit 202 is also used to continue executing the current process;
[0221] Storage unit, used to store command frames;
[0222] The second transceiver unit 201 is also used to send a third flag bit to the host so that the host sends the third flag bit to the host computer; the third flag bit is used to indicate that the target slave is busy so that the host computer stops sending other command frames.
[0223] Figure 1 A schematic diagram of the structure of an electronic device provided in an embodiment of this application. Figure 8 .like Figure 9 As shown, the electronic device may include a first processor 301, which, when executing a computer-executable program or instruction in the memory, implements the master-slave communication method in the above method embodiment.
[0224] The electronic device can be used to perform the various steps and / or processes corresponding to the electronic devices in the above method embodiments.
[0225] Figure 2A structural diagram of an electronic device provided by an embodiment of the present application Figure 9 As shown in The electronic device can include a second processor 401 and a memory 402, and the memory 402 stores a computer program, and the second processor 401 executes the computer program to implement the master-slave communication method in the method embodiment.
[0226] The electronic device can be used to execute each step and / or process corresponding to the electronic device in the method embodiment.
[0227] The electronic device of the present application can be used to execute the technical solutions of the foregoing method embodiments, and the implementation principles and technical effects are similar, wherein the operations implemented by each module can be further referred to the related descriptions of the method embodiments, which will not be described here. The modules herein can also be replaced by components or circuits.
[0228] The present application can divide the functional modules of the electronic device according to the foregoing method examples, for example, each functional module can be divided corresponding to each function, or two or more functions can be integrated in one processing module. The integrated module can be realized in the form of hardware or in the form of software functional module. It should be noted that the division of modules in each embodiment of the present application is illustrative, and is only a logical function division, and another division mode can be used in actual implementation.
[0229] Another embodiment of the present application further provides a computer readable storage medium, and the computer readable storage medium stores a computer program, and the computer program is executed by a processor to implement the master-slave communication method in the method embodiment.
[0230] The present application further provides a program product, and the program product includes execution instructions stored in a computer readable storage medium. At least one processor of the electronic device can read the execution instructions from the computer readable storage medium, and the at least one processor executes the execution instructions to make the electronic device implement the master-slave communication method in the method embodiment.
[0231] The present application further provides a chip, and the chip is connected with a memory, or the chip integrates the memory, and when a software program stored in the memory is executed, the master-slave communication method in the method embodiment is implemented.
[0232] In several embodiments provided in the present application, it should be understood that the disclosed system, device and method can be implemented in other manners. For example, the division of the above-described device embodiments is only a logical function division, and there can be another division manner for the actual implementation, for example, multiple units or components can be combined or integrated into another system, or some features can be ignored or not executed. In addition, the displayed or discussed mutual couplings or direct couplings or communication connections between different units, can be indirect couplings or communication connections through some interfaces, devices or units, and can be in electrical, mechanical or other forms.
[0233] Those skilled in the art can understand that the combination of features of different embodiments means to be within the scope of the present application and form different embodiments, although some embodiments herein include certain features included in other embodiments. For example, in the claims, any one of the claimed embodiments can be used in any combination.
[0234] The above-described embodiments are only used to illustrate the technical solutions of the present application, rather than limit them; although the present application has been described in detail with reference to the foregoing embodiments, those skilled in the art should understand that they can still modify the technical solutions recorded in the foregoing embodiments, or make equivalent replacements for some technical features; and these modifications or replacements do not make the essence of the corresponding technical solutions deviate from the spirit and scope of the technical solutions of the embodiments of the present application.
Claims
1. A master-slave communication method, characterized in that, A host applied in a master-slave communication system, the master-slave communication system comprising a host and multiple slaves, the method comprising: When the host receives a command frame for the target slave from the host computer and determines that the command character in the command frame meets a first preset condition, the host sends a first flag bit to the target slave to cause the target slave to interrupt its current process. The first flag bit is used to indicate that the host is about to send the command frame. The command frame includes a frame header, a target address, a command character, a data packet, and a checksum bit. The host sends the command frame to the target slave device, so that the target slave device executes the command frame; The host receives a second flag bit sent by the target slave device, the second flag bit being sent by the target slave device after generating a response frame according to the command frame; the second flag bit is used to indicate that the target slave device is about to send the response frame. The host interrupts the current process; The host receives the response frame sent by the target slave and sends the response frame to the host computer. The method further includes: when the host receives a command frame for the target slave from the host computer and determines that the command character in the command frame meets the second preset condition, the host sends the command frame to the target slave. The host receives the third flag bit sent by the target slave and sends the third flag bit to the host computer; the third flag bit is sent by the target slave while continuing to execute the current process and storing the command frame; the third flag bit is used to indicate that the target slave is busy, so that the host computer stops sending other command frames. The first preset condition includes: the command symbol indicates that the command frame is an operation command frame; or, the command symbol indicates that the command frame is a read command frame and all data identifiers in the command symbol belong to a preset list; the second preset condition includes: the command symbol indicates that the command frame is a read command frame and all data identifiers in the command symbol do not belong to a preset list.
2. The method according to claim 1, characterized in that, The method further includes: When the host receives a command frame sent by the host computer, it interrupts the current process. The host executes the command frame; The host generates a response frame based on the command frame; The host computer sends the response frame to the host computer.
3. A master-slave communication method, characterized in that, A slave device applied in a master-slave communication system, wherein the master-slave communication system includes a master and multiple slave devices, the method comprising: The target slave device receives a first flag bit sent by the host; the first flag bit is sent by the host when it receives a command frame for the target slave device sent by the host computer, and determines that the command character in the command frame meets a first preset condition; the first flag bit is used to indicate that the host is about to send the command frame; the command frame includes a frame header, a target address, a command character, a data packet, and a check bit in sequence; The target slave device interrupts the current process; The target slave device receives and executes the command frame sent by the host; The target slave device generates a response frame based on the command frame; The target slave device sends a second flag bit to the host device, causing the host device to interrupt the current process; the second flag bit is used to indicate that the target slave device is about to send the response frame. The target slave device sends the response frame to the host device, so that the host device sends the response frame to the host computer. The method further includes: the target slave receiving the command frame sent by the host when it receives a command frame for the target slave from the host computer and determines that the command character in the command frame satisfies a second preset condition; The target slave device continues to execute the current process and stores the command frame; The target slave device sends a third flag bit to the host device, so that the host device sends the third flag bit to the host computer; the third flag bit is used to indicate that the target slave device is busy, so that the host computer stops sending other command frames; The first preset condition includes: the command symbol indicates that the command frame is an operation command frame; or, the command symbol indicates that the command frame is a read command frame and all data identifiers in the command symbol belong to a preset list; the second preset condition includes: the command symbol indicates that the command frame is a read command frame and all data identifiers in the command symbol do not belong to a preset list.
4. An electronic device, characterized in that, include: First processor; The first processor is configured to execute a computer-executable program or instructions in the memory, causing the electronic device to perform the master-slave communication method according to any one of claims 1-2 or 3.
5. A computer-readable storage medium, characterized in that, The computer-readable storage medium stores a computer-executable program or instructions, the computer-executable program or instructions being configured to perform the master-slave communication method according to any one of claims 1-2 or 3.
Citation Information
Patent Citations
Data transmission method for master equipment and slave equipment
CN103685578A
Task scheduling for distributed data processing
CN115362434A