Detailed Description
The technical solutions in the embodiments of the present invention will be clearly and completely described below with reference to the drawings in the embodiments of the present invention, and it is obvious that the described embodiments are only a part of the embodiments of the present invention, and not all of the embodiments. All other embodiments, which can be derived by a person skilled in the art from the embodiments given herein without making any creative effort, shall fall within the protection scope of the present invention.
The embodiment of the invention provides a 485 multi-master communication method, which is applied to each node in a standard 485 communication network, each node in the standard 485 communication network can be used as a first node, and other external interfaces are not required to be connected, so that data transmission is realized. Specifically, as shown in fig. 1, the method includes:
step 101, the first node determines an idle waiting time, which is a waiting time from no data transmission to data transmission of the first node on the bus.
Specifically, when there is no data transmission on the bus, the nodes that need to occupy the bus, that is, the nodes that need to send data, determine their respective idle waiting time.
The determining, by the first node, the idle waiting time of the first node specifically includes:
a1, obtaining the random attribute information of the first node, wherein the random attribute information of the first node comprises the address information of the first node, and/or the hardware parameter of the first node in the current environment, and/or any random number in 0-255.
and a2, carrying out summation operation on the random attribute information of the first node to obtain a first reference value.
a3, determining the idle waiting time of the first node according to a first formula: t is1=M1mod N1+t1(ii) a Wherein, T1Is the idle latency of the first node, M1Is a first reference value, t1For conflict-free minimum latency, N1Is the first modulus value.
For example, it is assumed that 100 machines are connected to the bus, each machine is a node, and addresses of the 100 machines are numbered in sequence as 01, 02, … …, and 100. When there is no data to transmit on the bus, all nodes need to send data to occupy the bus, and each node calculates its own idle latency. Taking the first node as a machine with an address of 62 as an example, the idle waiting time of the node is calculated, specifically, the obtained random attribute information of the first node includes that the address information of the first node is 62, the current ambient temperature of the first node is 20 degrees, and the random number obtained from (0-255) is 89, thenFirst reference value M1Equal to 171, i.e. M162+20+ 89; if the first modulus is 32, there is no conflict with the minimum waiting time t125ms, the idle waiting time of the first node is 36ms according to the first formula.
Step 102, detecting whether data is transmitted on the bus in the idle waiting time.
Because the idle waiting time of the first node calculated according to the method of step 101 is different, it needs to be determined whether there is data to be transmitted on the bus within the idle waiting time of the first node, and if there is no data to be transmitted on the bus, step 103 is executed; if there is data transmission on the bus, it is necessary to wait until the node sending the data finishes data transmission, and then calculate the idle waiting time according to step 101, and the cycle is repeated.
Illustratively, on the basis of the example of step 101, the first node detects whether there is data to be sent on the bus within 36 ms. If there is no data transmission on the bus, go to step 103; if the idle waiting time of the machine with the address of 60 is 30ms, when the idle time on the bus reaches 30ms, the machine with the address of 60 starts to send data, at this time, the machine with the address of 62 may detect that there is data transmission on the bus at 36ms, and the machine with the address of 62 needs to wait until all the data of the machine with the address of 60 is sent, and then calculate the idle waiting time again according to the step 101, and repeat the cycle.
And 103, if not, the first node sends data when the idle waiting time is expired.
The format of the data sent by the first node may use a standard serial port data format or a non-standard serial port data format.
The embodiment of the invention provides a 485 multi-master communication method, which comprises the steps of determining the idle waiting time of a first node, and detecting whether data transmission exists on a bus within the idle waiting time of the first node; if not, the first node transmits the data upon expiration of the first node's idle wait time. Therefore, each node on the 485 bus is used as a first node, the master-slave relation is avoided, the idle waiting time of the first node is calculated, the first node sends data when the idle waiting time expires, data communication is not required to be carried out in a host polling mode in the prior art, and the problems that the single host communication polling time is too long and the communication efficiency is low are solved.
Further, as shown in fig. 2, while performing step 103, the method further includes:
and step 104, determining whether the transmitted data have collision according to a collision detection method.
In the embodiment of the present invention, because there is no master-slave relationship between nodes on the 485 communication bus, when a first node occupies the bus to transmit data, the first node also receives data transmitted by itself, and then, according to the data transmitted and received by itself, the first node determines whether the transmitted data is transmitted in a collision, where the collision is that when other nodes are the same as idle time of the first node, the data is transmitted at the same time, and at this time, the data on the bus will occupy the bus to cause a collision.
As shown in fig. 3, the determining, by the first node, whether the data to be transmitted conflicts according to the conflict detection method specifically includes:
b1, the first node determines whether the parity bit of the transmitted data is correct.
If the first node determines that the parity bit of the transmitted data is correct, performing step b 2; if the first node determines that the parity bit of the transmitted data is erroneous, step b6 is performed.
b2, if the first node determines that the parity bit of the transmitted data is correct, the first node determines whether the transmitted data length is correct.
If the first node determines that the sent data length is correct, executing step b 3; if the first node determines that the sent data length is incorrect, step b6 is executed.
b3, if the first node determines that the length of the data is correct, the first node determines whether the stop bit of the sent data is correct.
If the first node determines that the stop bit of the transmitted data is correct, executing step b 4; if the first node determines that the stop bit of the transmitted data is erroneous, step b6 is performed.
b4, if the first node determines that the stop bit of the transmitted data is correct, the first node determines whether the data frame of the transmitted data is correct.
If the first node determines that the stop bit of the transmitted data is correct, executing step b 5; if the first node determines that the stop bit of the transmitted data is erroneous, step b6 is performed.
b5, if the first node determines that the data frame of the transmitted data is correct, the first node determines that the transmitted data has no conflict.
b6, the first node determines that the transmitted data is in conflict.
The order of the above steps b1, b2, b3, b4 may be adjusted, and the present invention is illustrated by only one of them. Meanwhile, the parity bit in step b1, the data length in step b2, the stop bit in step b3, and the data frame in step b4 may be set according to a method in the prior art, which is not described again.
According to the step 104, when the first node determines that the transmitted data is not collided, executing the step 105; when the first node determines that the transmitted data is in conflict, step 106 is executed.
And 105, if the transmitted data are determined not to have collision, continuing to transmit the data.
Of course, in the subsequent data sending process, whether the sent data conflict can be judged in real time until the data sending is finished.
And step 106, if the sending conflict of the sent data is determined, detecting whether the conflict times reach the preset conflict times.
For example, assuming that the preset number of collisions is 4, when data sent by the machine with the address of 62 collides, the number of collisions of the machine with the address of 62 is added by 1, and when the number of collisions reaches 4 after adding 1, step 107 is executed; if the number of collisions has not reached 4 times after adding 1, step 108 is performed.
And 107, if so, calculating the back-off time of the first node according to a back-off delay algorithm.
Wherein calculating the back-off time of the first node according to the back-off delay algorithm comprises:
c1, acquiring address information of a second node, wherein the second node is a node which normally sends data before no data is transmitted on the bus;
c2, calculating the back-off time of the first node according to a second formula:
T2=|S1-S2|×t2+t3;
wherein, T2Is the back-off time of the first node, S1Is address information of the first node, S2Is address information of the second node, t2Is a unit back-off delay time, t3Is the minimum back-off delay time.
After the delay time of the first node is calculated according to the back-off delay algorithm, the first node detects again whether there is data transmission on the bus after the delay time expires, and proceeds to step 101 to perform subsequent operations.
For example, assuming that the sending of data by the machine with address 03 is finished before the sending of data by the machine with address 62, the machine with address 03 is the second node, and the address information of the second node is 03; if unit back-off delay time t210ms, minimum backoff delayTime of flight t3Is 100ms, then the back-off time T of the first node is calculated according to the second formula2Equal to 690ms, i.e. T2The calculated backoff time of the first node is much longer than the idle waiting time of the first node, so that the frequently colliding machines temporarily exit from the bus contention, and after the backoff time expires, the bus contention is detected to determine whether there is data transmission on the bus, so as to re-enter the bus contention.
And step 108, if the delay time does not reach the preset delay time, calculating the delay time of the first node according to a conflict delay algorithm.
Wherein calculating the delay time of the first node according to the collision delay algorithm comprises:
d1, acquiring second random attribute information of the first node.
It should be noted that the contents of the acquired second random attribute information and the first random attribute information may be the same, and are only acquired for distinguishing different times. Reference may be made to the above description of the first random attribute information for the second random attribute information.
d2, carrying out summation operation on the second random attribute information to obtain a second reference value.
d3, calculating the delay time of the first node according to a third formula:
T3=(M2mod N2)+t4-k×t5
wherein, T3Is a delay time of the first node, M2Is the second reference value, N2Is the second modulus, t4K is the number of collisions of the first node, t5Is the collision latency.
After the delay time of the first node is calculated according to the collision delay algorithm, the first node detects whether data transmission exists on the bus again after the delay time expires, and the process continues to step 101 to perform subsequent operations.
For example, assuming that the number of collisions at the machine with the address 62 is 1, the obtaining of the second random attribute information of the machine with the address 60 includes: address information 62, temperature of current environment 19 deg. and random number 9 selected from (0-255), and obtained second reference value M2Equal to 90, i.e. M262+19+ 9; minimum waiting time t if there is no conflict416ms, collision latency t52ms and the second modulus is 8, the delay time T of the first node is calculated according to a third formula3And the calculated delay time of the first node is far less than the idle waiting time of the first node, so that the time is recalculated according to a collision delay algorithm under the condition of limited collision times in order to reduce the occurrence of subsequent collisions. In order to reduce the probability of subsequent collision, the success rate of retransmission of the machine with collision is provided, the priority of transmission of the machine with collision is provided in turn according to the subtraction of a certain time from the number of collision in calculation, and whether data transmission exists on the bus is detected after the waiting delay time expires, so as to re-enter the bus competition.
The 485 multi-master communication method provided by the embodiment of the invention determines the idle waiting time of the first node and detects whether data transmission exists on a bus within the idle waiting time of the first node; if not, the first node transmits the data upon expiration of the first node's idle wait time. However, when there are many nodes on which the bus is attached, the idle waiting time of several devices may be the same as the idle waiting time of the first node, and when the idle waiting time expires, the first node and the several devices may transmit data at the same time, so that the first node collides with the several devices, and the back-off time or the delay time of the several devices is recalculated according to different algorithms according to the number of times of collision, and since the delay time is smaller than the idle waiting time, the time for which the machine that collided waits is shorter than the machine that did not collide, and the collision detection of the next time completes the transmission of data. According to the method provided by the embodiment of the invention, each node on the 485 bus is taken as a first node, no master-slave relation exists, and the idle waiting time of the first node is calculated, so that the first node sends data when the idle waiting time of the first node expires, data communication is not required to be carried out in a host polling mode in the prior art, and the problems of long communication polling time and low communication efficiency of a single host are solved. Meanwhile, reliable and efficient operation of multi-master communication can be achieved through collision detection.
The embodiment of the invention provides a 485 multi-master communication device, as shown in fig. 4, comprising:
a first determining unit 401, configured to determine an idle waiting time, where the idle waiting time is a time that a first node waits from no data transmission on a bus to data transmission;
a detecting unit 402, configured to detect whether there is data transmission on the bus within the idle waiting time;
a sending unit 403, configured to send data when the idle waiting time expires when the detecting unit 402 detects that there is no data transmission on the bus within the idle waiting time.
Further, as shown in fig. 5, the apparatus further includes: a second determination unit 404, a calculation unit 405.
Wherein, the second determining unit 404 is configured to determine whether the transmitted data is in collision according to a collision detection method;
the detecting unit 402 is further configured to detect whether the number of collisions reaches a preset number of collisions if the second determining unit 404 determines that the transmitted data have collisions;
the calculating unit 405 is configured to calculate a backoff time of the first node according to a backoff delay algorithm if the detecting unit 402 detects that the collision frequency reaches a preset collision frequency;
the calculating unit 405 is further configured to calculate the delay time of the first node according to a collision delay algorithm if the detecting unit 402 detects that the number of collisions does not reach the preset number of collisions;
the backoff time of the first node is greater than the idle waiting time of the first node, and the idle waiting time of the first node is greater than the delay time of the first node.
Optionally, as shown in fig. 6, the first determining unit 401 includes: a first obtaining sub-unit 4011, a first calculating sub-unit 4012 and a determining sub-unit 4013;
the first obtaining sub-unit 4011 is configured to obtain random attribute information of the first node, where the random attribute information of the first node includes address information of the first node, and/or a hardware parameter of the first node in a current environment, and/or any random number from 0 to 255;
the first calculating sub-unit 4012 is configured to perform summation operation on the random attribute information of the first node to obtain a first reference value;
a determining subunit 4013, configured to determine an idle latency of the first node according to a first formula, where the first formula is: t is1=M1mod N1+t1(ii) a Wherein, T1Is the idle latency of the first node, M1Is a first reference value, t1For conflict-free minimum latency, N1Is the first modulus value.
Optionally, the second determining unit 404 is specifically configured to determine whether the parity of the transmitted data is correct; if the data length of the sent data is correct, determining whether the data length of the sent data is correct; if so, determining whether the stop bit of the sent data is correct; if the data frame is correct, determining whether the data frame of the transmitted data is correct; and if the data is correct, determining that no conflict occurs.
Optionally, as shown in fig. 6, the calculating unit 405 includes: a second acquisition sub-unit 4051, a second calculation sub-unit 4052.
The second obtaining subunit 4051 is configured to obtain address information of a second node, where the second node is a node that normally sends data before no data is transmitted on the bus;
the second calculating subunit 4052 is configured to calculate the back-off time of the first node according to a second formula, where the second formula is: t is2=|S1-S2|×t2+t3(ii) a Wherein, T is2Is the back-off time of the first node, S1Is address information of the first node, S2Is address information of the second node, t2Is a unit back-off delay time, t3Is the minimum back-off delay time.
Optionally, the second obtaining sub-unit 4051 is further configured to obtain second random attribute information of the first node; the second calculating subunit 4052 is further configured to perform summation operation on the second random attribute information to obtain a second reference value; calculating the delay time of the first node according to a third formula, wherein the third formula is as follows: t is3=(M2mod N2)+t4-k×t5(ii) a Wherein, T is3Is a delay time of the first node, the M2Is the second reference value, the N2Is a second modulus value, said t4For conflict-free minimum latency, k is the number of conflicts of the first node, t5Is the collision latency.
The embodiment of the invention provides a 485 multi-master communication device, which is characterized in that whether data is transmitted on a bus within the idle waiting time of a first node is detected by determining the idle waiting time of the first node; if not, the first node transmits the data upon expiration of the first node's idle wait time. Therefore, each node on the 485 bus is used as a first node, the master-slave relation is avoided, the idle waiting time of the first node is calculated, the first node sends data when the idle waiting time expires, data communication is not needed to be carried out in a host polling mode in the prior art, and the problems that the single host communication polling time is too long and the communication efficiency is low are solved.
In the several embodiments provided in the present application, it should be understood that the disclosed system, apparatus and method may be implemented in other manners. For example, the above-described apparatus embodiments are merely illustrative, and for example, the division of the units is only one logical division, and other divisions may be realized in practice, for example, a plurality of units or components may be combined or integrated into another system, or some features may be omitted, or not executed. In addition, the shown or discussed mutual coupling or direct coupling or communication connection may be an indirect coupling or communication connection through some interfaces, devices or units, and may be in an electrical, mechanical or other form.
The units described as separate parts may or may not be physically separate, and parts displayed as units may or may not be physical units, may be located in one place, or may be distributed on a plurality of network units. Some or all of the units can be selected according to actual needs to achieve the purpose of the solution of the embodiment.
In addition, functional units in the embodiments of the present invention may be integrated into one processing unit, or each unit may be physically included alone, or two or more units may be integrated into one unit. The integrated unit can be realized in a form of hardware, or in a form of hardware plus a software functional unit.
The integrated unit implemented in the form of a software functional unit may be stored in a computer readable storage medium. The software functional unit is stored in a storage medium and includes several instructions for causing a computer device (which may be a personal computer, a server, or a network device) to execute some steps of the methods according to the embodiments of the present invention. And the aforementioned storage medium includes: various media capable of storing program codes, such as a usb disk, a removable hard disk, a Read-Only Memory (ROM), a Random Access Memory (RAM), a magnetic disk, or an optical disk.
Finally, it should be noted that: the above examples are only intended to illustrate the technical solution of the present invention, but not to limit it; although the present invention has been described in detail with reference to the foregoing embodiments, it will be understood by those of ordinary skill in the art that: the technical solutions described in the foregoing embodiments may still be modified, or some technical features may be equivalently replaced; and such modifications or substitutions do not depart from the spirit and scope of the corresponding technical solutions of the embodiments of the present invention.