Embodiment
As shown in Figure 1, it is the topological diagram of Modbus RTU network, it is mainly made up of a Modbus RTU main website and several Modbus RTU slave stations, wherein between each station, connects by RS-485 bus, and accesses respectively the terminal resistance of 120 Ω at 485 bus two ends.In Modbus RTU network, between each station, communication all adopts this bus transfer, and each correct communication message all meets Modbus RTU transport protocol standard.
Regulation according to Modbus RTU transport protocol standard: first communication initiates request by main website to slave station, and the main website of Modbus RTU can only initiate a communications transaction at every turn, in network, all slave stations all can receive this request message, then by resolving the address field content of this message, judge whether this content is this station address, if make corresponding response, otherwise abandon this message.Wherein the effective range of address field is 1-247, and other No. 0 address is broadcast address, and all slave stations all do not carry out message while receiving " No. 0 address " request and reply.
Regulation according to Modbus RTU transport protocol standard: Modbus RTU adopts master-slave communication pattern.First network service initiate request (carrying the station address of slave station) by main website to certain slave station, this request is received by slave stations all in network, slave station receives after request, the station address that carries in request and the station address of itself are compared, if identical, illustrate that this request mails to our station, need to reply, if not identical, illustrate that this request and our station are irrelevant, abandon request.
That is to say, in Modbus RTU network, if network arbitrary node initiating communication, other all nodes all can receive this message, and then each slave station determines whether by the address field of detection messages our station data are to determine whether reply.Based on this, the embodiment of the present invention is recognized: when slave station is wanted to obtain station address, can be with the identity of interim main website (pseudo-main website), in network, send the multiplexing detection bag that comprises certain address, if this address is taken by other slave station, slave station will receive that other slave station that takies this address replys accordingly so, if this address is not taken by other slave station, slave station can not be received accordingly and reply, and now slave station can take this station address.Based on this thinking, the invention provides following technical thought.
Please refer to Fig. 2, is the schematic flow sheet of the first embodiment of Modbus RTU slave station automatic acquisition station address method provided by the invention, and it comprises:
Step S21, slave station send address multiplex detection packet to the communication link of the Modbus RTU network at place.
Wherein, slave station can be the new slave station networking, and it will complete networking must obtain an occupation of land location not taken by current other slave station that has completed networking.Or slave station can be the slave station having networked, but due to failure cause, need to again obtain station address, thereby avoid this tributary address to be taken by other communication slave stations in failure process.
Wherein, address multiplex detection packet comprises interim station address.
Wherein, whether address multiplex detection packet is taken by other slave station of Modbus RTU network for the interim station address of surveying wherein.From Modbus RTU host-host protocol, if this interim station address is taken by certain slave station in Modbus RTU network, receive after address multiplex detection packet when this slave station so, will do accordingly and reply.
Wherein, the particular content of address multiplex detection packet is mainly made up of Modbus RTU diagnostic function code 08H and subfunction code 00H00H, according to Modbus rtu protocol code requirement, protocol data bag (being Protocol Data Unit, referred to as " PDU ") corresponding to this address multiplex detection packet can be expressed as: 08H00H00H.
Concrete, step S21 can comprise: when slave station detects the communication link free time, produce the random delay time; In the time that the random delay time arrives, slave station sends address multiplex detection packet to communication link.Herein, the communication link free time refers to that this shows that previous frame message is sent completely from least 3.5 character times till now time that finally receives character.Herein, the generation of random delay time can be called random delay function and realized by slave station.Herein, detecting communication link when idle, do not send immediately address multiplex detection packet but probability that the benefit that sends again address multiplex detection packet after taking time delay a period of time is the address multiplex detection packet that can the new log equipment of Avoids or reduces (being new networking slave station) sends conflicts with other communication messages.
Step S22, when result of detection for this interim station address not by Modbus RTU network in other slave station while taking, slave station takies the fixed communication address of this interim station address as this slave station.
Wherein, in the time that slave station at least twice transmission does not all receive that containing the address multiplex detection packet of same interim station address detection is replied, result of detection is not just taken by other slave station in Modbus RTU network for interim station address, can improve like this accuracy.Herein, twice identical address multiplex detection packet should keep enough transmission intervals, to ensure that other slave station has time enough response.
Wherein, after not receiving and detecting and reply and refer to that slave station sends address multiplex detection packet, in official hour, do not receive any message, although or receive that in official hour message, the message of receiving are not all replying of doing for address multiplex detection packet.
In addition, in the process of slave station automatic acquisition address, in Modbus RTU, main website will not do any processing to this behavior, and, when main website receives after address multiplex detection packet, because this message is non-main website request-reply, main website sets it as invalid packet and casts out.
The present embodiment, each new networking slave station all will be simulated master function, send address multiplex detection packet at network idle phase, these slave stations can be referred to as to interim pseudo-main website.And will can not do any reaction to address multiplex detection behavior in this course for real Modbus RTU main website, and just set it as an invalid communications packets to ignoring.Between pseudo-main website and real Modbus ASCII main website, be to act on the different periods, between the two, do not conflict.And after new networking slave station completes mailing address automatic acquisition, the function of interim pseudo-main website will be lost.
The present embodiment, whether the interim station address being sent in address multiplex detection packet detection address multiplex bag to communication link by slave station is taken by other slave station, when result of detection is while being unoccupied, slave station takies this interim station address, thereby solve the how problem of automatic acquisition station address of slave station, thereby realize the automatic network-building of the network equipment, make can realize task control and separating that Modbus RTU communicates by letter in planned network systematic procedure, strengthen the flexibility of network design and management, simultaneously in the replacement of the network equipment and the process of expansion, can realize the plug and play of equipment, without configuring static mailing address, strengthen the utilance of mailing address, in addition, in the process of single or multiple new networking slave station automatic acquisitions address, Modbus RTU main website will not participate in station address and assigns or intervene, greatly reduce the load of main website.
Please refer to Fig. 3, is the schematic flow sheet of the second embodiment of Modbus RTU slave station automatic acquisition station address method provided by the invention, and it comprises:
Step S31, slave station according to the idle address of network address list calculated address value minimum as interim station address.
The step S32-step S33 identical with step S21-step S22 difference.
Wherein, network address list is for recording the station address having been taken by other slave station that slave station detects.
Wherein, in network address list, the length of each element is a byte, and because tributary address in Modbus RTU is 1-247, therefore the maximum length of network address list is 247.
Wherein, for slave station, it is empty that network address list is initially, and is CA when slave station receives station address
icommunication data packet time, show this station address CA
itaken by other slave stations, slave station is by CA
iadd in network address list, wherein CA
i∈ U, U={1,2 ..., 247}.In addition, when slave station is with station address CA
isurvey as interim station address, if receive while replying accordingly, also show this station address CA
ioccupied, slave station is also added in network address list.
Wherein, the idle address that slave station obtains address value minimum according to network address list is during as interim station address, and wherein idle address is in 1-247 station address, to remove in network address list to have detected and had { FCA by occupied station address
i| FCA
i∈ U-AT}, wherein FCA
ifor idle address set, AT is used station address set (wherein this set element is corresponding one by one with network address list content) for this slave station has detected.Obviously, idle network address number is greater than 1, and then step S31 selects the wherein minimum interim station address of conduct, i.e. MIN (FCA
i).
Please refer to Fig. 4, is the schematic flow sheet of the 3rd embodiment of Modbus RTU slave station automatic acquisition station address method provided by the invention, and it comprises:
The step S41-step S42 identical with step S11-step S12.
Step S43, slave station, according to the fixed communication address obtaining, send networking request bag to main website.
Wherein, networking request bag R (CA
i) request function code be 11H, the networking request bag R (CA based on Modbus RTU
i) form be: MIN (FCA
i), 11H, CRC_L, CRC_H.
Wherein, adopted the mode of step S41 and step S42 to obtain the slave station transmission networking request bag R (CA of fixed communication address
i) carry out after still needing to wait for idle and random delay a period of time of network.
Step S44, slave station response main website receive send after networking request bag for asking the request report of slave station essential information.
Wherein, in each slave station, all there is the parameter θ of the essential information at this station of mark
i, this parameter comprises the essential information such as type, function of slave station.
Wherein, R (CA in the time that main website in Modbus RTU receives networking request bag
i), illustrate that station address is MIN (FCA
i) slave station asking networking identification, main website, by request report slave station ID (function code: 11H), identifies the essential information of the slave station of this request networking identification so, to complete networking identification.
Wherein, in waiting for the slave station of main website cognitive phase, when it is received after main website request message, by facility information θ
iaccording to Modbus RTU function code 11H specification encapsulation, then reply to main website, can enter normal communication mode to this for this slave station.
In addition, when slave station takies behind the fixed communication address of this interim station address as this slave station, even if slave station, in the time that networking request stage receives the address multiplex detection packet containing this fixed communication address, is also made corresponding detection and replied, represent that this station address is occupied.
In addition, analyzed from above embodiment: for main website in Modbus RTU, in processing communication things process, it receives network service bag and is totally divided into three kinds of situations:
1, the request response message of slave station.This message normally occurs in Modbus RTU main website and sends after communication request, the response that in network, corresponding slave station is made.In this process, Modbus RTU slave station is according to this message of protocol specification normal process.
2, the slave station request message that newly networks.The method of the automatic acquisition station address providing according to the embodiment of the present invention, in the address detected stage, main station will not participate in any roles such as address assignment.And obtain after effective address when slave station, cannot be obtained the information of this slave station by main website, initiatively send networking request communication message bag to network primary station by slave station.In the time that main website receives the message bag of this form, representing has new slave station to add in network, waits for main website identification.Then Modbus RTU main website initiatively initiates request slave station ID to this slave station, identifies device type and the function etc. of this slave station.
3, Network Abnormal message.This message generally comprises, garble message (comprise that CRC inspection is wrong, parity errors, character overflow etc.), unsolicited message.Wherein, for main website, address multiplex probe messages also will be considered unsolicited message.
Please refer to Fig. 5; it is the detail flowchart of slave station automatic acquisition station address provided by the invention; many details that it has comprised the embodiment of the present invention; this contributes to help to understand the present invention; but to those skilled in the art; it is only preferably technical scheme of the present invention that this many details limits the technical scheme forming, and should not be construed as unique implementation of the present invention or the restriction to protection range.The flow process of Fig. 5 comprises:
Step S501, after slave station starts, initialization self configuration.
Wherein initialization self configuration comprises system configuration initialization, communication initialization, parameter initialization etc.
In addition, slave station automatic acquisition station address mode provided by the invention needs define grid address table AT, and its maximum length is 247, and each element is without symbol-byte type, and its effect is mainly used in storing this equipment station and detects by used mailing address.
Then, definition address multiplex probation counter C1 and networking request counter C2, be respectively used to calculate the number of times of the multiplexing detection times of interim station address and networking request identification.
Finally, slave station configures relevant communication timer, response timeout timer and random delay timer etc.
Step S502, after device initialize completes, then according to network address list AT, obtain minimum idle address, prepare address multiplex probe messages, and zero clearing counter C1 and C2.The content according to the present invention, if now network is communicating message transmissions, continues to wait for link idle.If now link is idle condition, produce a random number according to preset random delay strategy, then set delay time, open time delay timer.
In the slave station of time delay wait, need the information of Real-Time Monitoring communication link.If before time delay finishes, network link, without any communication behavior, sends off-the-shelf address multiplex detection packet.If link packet transmission detected in this stage, abandon the chance that this address multiplex sends, then wait for link idle next time.In this simultaneously, slave station receiver message information.
Step S503, in the time that slave station has received new message, check basic communication mistake, comprising CRC(Cyclic Redundancy Check, cyclic redundancy check (CRC) code) verification, character parity, character overflow checking, the checking of communication message length and the overtime checking of interframe character etc.If verification inerrancy enters step S504, resolve this message, then enter step S505; If wherein there is a condition not meet and enter step S507.
Step S504, inspection address field content, obtain station address wherein.
If the station address that step S505 step S504 obtains is identical with the interim station address of our station, illustrate that this station address is taken by other slave stations, then enter S506 and process.If this content is not equal to our station temporary address, show that other equipment stations communicate by letter, then cast out this invalid communications packets, directly enter step 507.
Step 506, through step S505 judgement, conflict with other stations in the address that our station has obtained, according to the rule of " first detection, first obtains ", our station must be abandoned taking of this address, avoids address conflict.First network address list need to be upgraded in this station, and this station address is added in network address list, provides reliability foundation for obtaining the idle address of network next time.Then our station again again obtains the idle address of minimal network and prepares address multiplex detection packet according to network address list, waits for link idle, prepares the address detected on Xin Yi road, and this step is as step S502.Meanwhile, this station needs zero clearing counter C1 and C2, avoids historical data to bring error message to testing result.
Step S507, judge response whether overtime.The present invention surveys to judge by address multiplex whether the acquired address of our station repeats with other stations.In step S502, S506, S510, send after address multiplex detection packet at every turn, all will start the response timeout timer of our station.If the set of response timeout flag bit, enters step S508, detect whether there is new message otherwise enter step S502.
Step S508, request response timeout detected when this communication station, by address multiplex probation counter C1 cumulative 1.
Herein, the effect of address multiplex probation counter C1 is for recording this station, the address of existing use have been sent and detected bag number of times, and does not all receive the relevant detection in this station and reply.Can strengthen the reliability that uses this address to the repeated detection of address multiplex, avoid the misleading because network is busy or garble brings.
Step S509, judge that address multiplex tests the higher limit whether having reached.If C1 accumulative total response timeout number of times is more than or equal to this higher limit, enter next step S511, show that this station has obtained network idle address success, successfully enters networking request stage.If this Counter Value is less than this higher limit, enter step S510.
Step S511, send after address multiplex detection packet in view of the last time, do not receive respective communication station and reply,, in this step, send networking request, and restart response timeout timer.
In the address detected stage, after new traffic is successfully obtained station address from station equipment, possesses basic communication ability.But this process is between network service slave station, by obtaining this free time address in the mode of pseudo-main website request, network primary station does not participate in the task of this address automatic acquisition, and (being slave station) joined in this system not know there is new network node.Object of the present invention is exactly to realize can detecting that in any stage of the network operation network has the access of new traffic equipment.So at networking cognitive phase, this new traffic slave node is by sending specific request identification message bag, the information such as type, function of notice main website identification equipment of itself to network primary station.In step S511, when this station detects network idle, send networking request message bag to bus links.Also starting-up response overtime timer of while, and zero clearing networking request counter C2, then enter in step S512 and detect link packet.
An important function of the present invention can be supported multiple from station equipment while access network exactly simultaneously, and completes the task of corresponding automatic acquisition address.Obtain the slave station of the idle address of network, when having detected that other slave stations detect this station address when whether occupied, this slave station should be replied test, as step S514, show that this address is occupied, then enter step S516 and judge whether response timeout.
As due to communication failure or other reasons, when after slave station request networking request, do not receive the identification request from system main website, then there is response timeout, enter step S517, cumulative to request networking number of times.Then enter step S518, whether the networking request number of times of judgement transfinites.If this networking request number of times of station has exceeded the higher limit allowing, abandon, to the taking of bus, reentering address detected pattern, from newly obtaining the idle address of network.Otherwise, reenter step S511, attempt initiating networking request with main website.
If detect that at step S514 new message is the request our station ID of main website, illustrate that main website has identified this communication slave station and entered network, then enter step S519 and reply network primary station required parameter.In this answering, without calling random delay strategy, because this request is initiated by main website, and the Modbus RTU main website that is controlled by of our station is dispatched.When new networking slave station responds after main website request, this station also formally enters normal communication mode simultaneously, and the process of whole slave station automatic acquisition mailing address finishes.
Please refer to Fig. 6, is the flow chart that the identification of Modbus RTU main website newly enters network slave station, and it comprises:
First in step S601, in the time that main website detects new link packet in Modbus RTU, then enter step S602, carry out basic communication process at once.Do not meet the basic communication checking of Modbus rtu protocol if step S602 detects this message, abandon this error message, then return by step S607.If correct message inspection is striven for when this message, enter step S603.
Step S603, judge receive message whether be request-reply.According to the substance of Modbus rtu protocol, first network using Modbus RTU main website initiates request to certain station of network, and then Target Station responds according to the content of request.So in the time that main website receives new message, first judgement whether the function of target slave station reply.If so, enter step S606 and carry out packet parsing according to Modbus rtu protocol, finally return by step S607.If not the Target Station of request is replied, enter step S604.
Step 604, main website identification newly enter Network Communication slave station.In communication process flow chart, increased the measuring ability of networking request message.When receiving, network primary station not our station request, but while meeting the request communication message of report slave station ID, showing has new communication slave station to wait for main website identification in network, in step S604, judged result is true, enter step S605, increase the task of initiative recognition slave station ID.If the message receiving is neither request-reply, the networking request of the communication equipment that neither newly network, abandons this communication message.This has in fact also embodied on the other hand Modbus RTU main website and has ignored address multiplex detection process.
Please refer to Fig. 7, with regard to the schematic diagram of a kind of practical application of the present invention, it analyzes time period network communication link message information content introduction the present invention simultaneously by detection.
As shown in Figure 7, suppose to exist in the existing network based on Modbus RTU four nodes (node is Modbus RTU main website or Modbus RTU slave station), comprising a Modbus RTU main website and three Modbus RTU slave station (S1, S2, S4), wherein the station address of three Modbus RTU slave stations is respectively 1,2,4.Modbus RTU network using time triggering mode, at time period [t
0, t
0+ Δ T
p] in, Modbus RTU main website is according to successively each slave node of poll (S1, S2, S3) of network task.
If at t=t
iin the moment, network newly adds communication slave station Si, due to [t
0, t
0+ Δ T
p] Modbus RTU main website is in poll slave station in the time period, so in this process network always in busy condition.When Si is at t=t
0+ Δ T
ptime detect that link is idle condition, at once obtain idle mailing address and prepare address multiplex detection packet according to AT.
But, due at [t
i, t
0+ Δ T
p] in the time period, slave station Si does not also do any address multiplex test, so AT be sky, the communication station address that Si obtains is for the first time 1.According to random delay strategy provided by the invention, it is Δ t that Si arranges delay time
3.If in this time delay process, link remains idle condition always, after delay time finishes, horse back sends the address multiplex detection packet about address 1.
Obviously, because the slave station S1 that communicates by letter in network has taken this station address, so in the time that S1 receives Si about the multiplexing test of address 1, will reply.Then, Si will join address 1 in AT according to this response, show that this station address has been used by other stations, then again obtain minimum address blank.
Because address 1 is occupied, according to network address list, the idle address of minimum obtaining is for the second time 2.Obviously this address is also taken by communication slave station S2.S2 sends after the multiplexing test pack of address 2, and by the test of replying of receiving from S2, then Si abandons address 2 equally, and scheduler Table A T.Adopt and use the same method, Si will continue to obtain minimum idle address 3.Owing to using this address without any slave station in network, as figure show, Si sends continuously and all do not receive mailing address for three times is 3 reply, Si will forever take this address.It should be noted that and between address multiplex test, need the reserved abundant time to the response of corresponding station, as shown in Figure 4, Δ t
2for the response timeout time, and there is Δ t
2> Δ t
3.
Please refer to Fig. 8, is the structural representation of the embodiment of slave station provided by the invention, and it comprises:
Interim station address generation module 1, for according to the idle address of network address list calculated address value minimum as described interim station address, described network address list is for recording the station address having been taken by other slave station that described slave station detects.
Detecting module 2, communication link for from the Modbus RTU network to place sends address multiplex detection packet, described address multiplex detection packet comprises interim station address, and whether described address multiplex detection packet is used for surveying described interim station address and is taken by described other slave station of Modbus RTU network;
Processing module 3, for when result of detection being described interim station address while not taken by described other slave station of Modbus RTU network, described slave station takies the fixed communication address of this interim station address as described slave station.Wherein, processing module 3 comprises: random delay time generation unit 31, in the time detecting the described communication link free time, produces the random delay time; Transmitting element 32, in the time that described delay time at any time arrives, described slave station sends described address multiplex detection packet to the communication link of place Modbus RTU network.
Networking module 4, for according to described fixed communication address, sends networking request bag to main website; And for respond described main website receive send after described networking request bag for asking the request report of described slave station essential information.
The present embodiment, whether the interim station address being sent in address multiplex detection packet detection address multiplex bag to communication link by slave station is taken by other slave station, when result of detection is while being unoccupied, slave station takies this interim station address, thereby solve the how problem of automatic acquisition station address of slave station, thereby realize the automatic network-building of the network equipment, make can realize task control and separating that Modbus RTU communicates by letter in planned network systematic procedure, strengthen the flexibility of network design and management, simultaneously in the replacement of the network equipment and the process of expansion, can realize the plug and play of equipment, without configuring static mailing address, strengthen the utilance of mailing address, in addition, in the process of single or multiple new networking slave station automatic acquisitions address, Modbus RTU main website will not participate in station address and assigns or intervene, greatly reduce the load of main website.
Finally explanation is, above embodiment is only unrestricted in order to technical scheme of the present invention to be described, although the present invention is had been described in detail with reference to preferred embodiment, those of ordinary skill in the art is to be understood that, can modify or be equal to replacement technical scheme of the present invention, and not departing from aim and the scope of technical solution of the present invention, it all should be encompassed in the middle of claim scope of the present invention.