The content of the invention
The technical problem to be solved by the invention is to provide a kind of PLC man-machine communications method and system, are led to by unified
Interrogate protocol format, compatible different PLC communications protocol.What the present invention was realized in:
A kind of PLC man-machine communications method, comprises the following steps:
Step A:The mark ID of the PLC communications protocol that man-machine interface host computer selects user, protocol definition file and logical
News parameter is sent to man-machine interface slave computer;
The protocol definition file and communications parameter of each PLC communications protocol are prestored in the man-machine interface host computer;It is described
Protocol definition file includes the title and mark ID of corresponding PLC communications protocol, corresponding PLC master slave mode and communication driving journey
Sequence;When PLC is as communication slave station, the protocol definition file also includes the mark ID of each internal register, word length in the PLC
Parameter, address realm, read-write mode and display Name;
Step B:Man-machine interface slave computer determines to need the PLC communicated according to the mark ID that receives, and according to the mark
Know the master slave mode that protocol definition file corresponding to ID judges the PLC that needs communicate;
Step C:The master slave mode for the PLC that man-machine interface slave computer communicates as needed is communicated with the PLC;
When the PLC for needing to communicate is as communication main website, the step C includes:
Man-machine interface slave computer opens the power down not save register, and pass through ID pairs of the mark with receiving of man-machine interface
The communication driver of the PLC in the protocol definition file answered is communicated with the PLC;PLC can be according to corresponding communications parameter
Established with man-machine interface and connect and access the power down not save register;
When the PLC for needing to communicate is as communication slave station, the step C includes:
Man-machine interface slave computer receives the mark for the internal register for needing to access in the PLC that man-machine interface host computer is sent
Know ID, and the read-write mode of the internal register is obtained according to the mark ID of the internal register received, and according to the read-write
Mode parses the actual address deviant of the internal register;
The mark ID of the internal register received and actual address deviant are sent to the PLC by man-machine interface slave computer
Communication driver, to call the communication driver, and communicated according to corresponding communications parameter and the PLC.
Further, the integer-bit system of the read-write mode of internal register including the internal register, from decimal place to
The system and decimal place system of integer-bit carry;The step of actual address deviant for parsing internal register, includes:
According to the integer-bit system of internal register and decimal place system by the integer figure of the internal register according to and it is small
Digit data is converted to the decimal system;
If m is to be converted to the integer figure evidence after the decimal system, n is to be converted to the decimal digits evidence after the decimal system, and y is interior for this
The actual address deviant of portion's register, then y=m × 2x+ n, wherein, x is the hex value from decimal place to integer-bit carry.
Further, the read-write mode of internal register also includes the addressing mode of the internal register;When needing to access
During PLC multiple internal registers, it is interior that man-machine interface slave computer always according to the mark ID of the internal register received obtains this
The word length parameter and addressing mode of portion's register;
The man-machine interface slave computer is on parsing after the actual address deviant of an internal register, according to described
Word length parameter and addressing mode calculate the actual address deviant of next internal register, and are calculated accordingly inside other
The actual address deviant of register.
Further, methods described also comprises the following steps:When the actual address deviant being calculated is beyond described
During the scope of location, man-machine interface slave computer sends corresponding prompting.
Further, the edit format of the protocol definition file is XML, coded system UTF-8.
A kind of PLC man-machine communications system, is communicated for PLC and man-machine interface, including man-machine interface host computer and people
Machine interface slave computer;
The man-machine interface host computer be used for by the mark ID of PLC communications protocol of user's selection, protocol definition file and
Communications parameter is sent to man-machine interface slave computer;
The protocol definition file and communications parameter of each PLC communications protocol are prestored in the man-machine interface host computer;It is described
Protocol definition file includes the title and mark ID of corresponding PLC communications protocol, corresponding PLC master slave mode and communication driving journey
Sequence;When PLC is as communication slave station, the protocol definition file also includes the mark ID of each internal register, word length in the PLC
Parameter, address realm, read-write mode and display Name;
Man-machine interface slave computer is additionally operable to determine PLC that needs communicate according to the mark ID that receives, meanwhile, according to this
Protocol definition file corresponding to mark ID judges the master slave mode for the PLC that needs communicate, and the PLC communicated as needed principal and subordinate
Pattern is communicated with the PLC;
When the PLC for needing to communicate is as communication main website, the power down that man-machine interface slave computer opens man-machine interface does not preserve
Register, and pass through the communication driver and the PLC of the PLC in the corresponding protocol definition file of mark ID with receiving
Communicated;PLC can be established according to corresponding communications parameter and man-machine interface and be connected and access the power down not save register;
When the PLC for needing to communicate is as communication slave station, man-machine interface slave computer receives what man-machine interface host computer was sent
The mark ID of internal register accessed is needed in the PLC, and it is interior according to the mark ID of the internal register received to obtain this
The read-write mode of portion's register, meanwhile, the actual address deviant of the internal register is parsed according to the read-write mode, and will connect
The mark ID and actual address deviant of the internal register received are sent to the communication driver of the PLC, to call this logical
Driver is interrogated, and is communicated according to corresponding communications parameter and the PLC.
Further, the integer-bit system of the read-write mode of internal register including the internal register, from decimal place to
The system and decimal place system of integer-bit carry;Man-machine interface slave computer is according to the integer-bit system and decimal place of internal register
The integer figure evidence of the internal register and decimal digits evidence are converted to the decimal system by system;And it is interior according to equation below to parse this
The actual address deviant of portion's register:
If m is to be converted to the integer figure evidence after the decimal system, n is to be converted to the decimal digits evidence after the decimal system, and y is interior for this
The actual address deviant of portion's register, then y=m × 2x+ n, wherein, x is the hex value from decimal place to integer-bit carry.
Further, the read-write mode of internal register also includes the addressing mode of the internal register;When needing to access
During PLC multiple internal registers, it is interior that man-machine interface slave computer always according to the mark ID of the internal register received obtains this
The word length parameter and addressing mode of portion's register;
The man-machine interface slave computer is on parsing after the actual address deviant of an internal register, according to described
Word length parameter and addressing mode calculate the actual address deviant of next internal register, and are calculated accordingly inside other
The actual address deviant of register.
Further, when the actual address deviant being calculated exceeds the address realm, man-machine interface slave computer
Send corresponding prompting.
Further, the edit format of the protocol definition file is XML, coded system UTF-8.
The present invention establishes corresponding protocol definition file in host computer according to different PLC communication protocol types, and fixed
Adopted communications parameter.Protocol definition file includes the title and mark ID, corresponding PLC master slave mode of corresponding PLC communications protocol
And communication driver.The PLC that user needs to communicate by man-machine interface upper computer selecting, man-machine interface host computer will be corresponding
Mark ID, protocol definition file and the communications parameter of PLC communications protocol are sent to man-machine interface slave computer, man-machine interface slave computer
It can be determined to need the PLC communicated according to the mark ID received, and the protocol definition file according to corresponding to mark ID is true
Fixed PLC master slave mode, is then communicated according to PLC master slave mode by different modes and PLC.The present invention passes through system
One communications protocol form defines man-machine interface and PLC communications protocol, and man-machine interface can be made to adapt to different types of PLC communications
Agreement, it is convenient to be communicated with different types of PLC.
Embodiment
In order to make the purpose , technical scheme and advantage of the present invention be clearer, it is right below in conjunction with drawings and Examples
The present invention is further elaborated.
The present invention mainly defines man-machine interface and PLC (programmable logic controller (PLC)) by using unified protocol format
Between communications protocol, make the compatible different PLC communications protocol of man-machine interface, it is convenient to communicated with various PLC.
As shown in figure 1, before PLC man-machine communication's methods using the present invention, need to first predefine and PLC communications protocol, definition method
Comprise the following steps:
Firstly, it is necessary to the protocol definition file according to corresponding to being established PLC communications protocol title.Protocol definition file name
Can be of the same name with communications protocol, using XML edit format and UTF-8 coded system.Secondly, it is also necessary to distributed for communications protocol
First uniqueness ID, to identify communications protocol.It is also desirable to set the type of communications protocol, and assisted according to type for communication
View setting communications parameter.The type of communications protocol includes network communication protocol, serial port communicating protocol and bus protocol etc., root
The hardware connection mode between man-machine interface and PLC can be specified according to the type of communications protocol, and sets the silent of communications protocol accordingly
Recognize communications parameter.If PLC does communication main website, just completed to this and man-machine interface and the PLC communications protocol are determined
Justice, if PLC does communication slave station, on the basis of abovementioned steps are completed, also need to complete the internal register to the PLC
Definition, just calculate the definition completed to man-machine interface and the PLC communications protocol.The step of internal register for defining PLC, includes:
Corresponding display Name in man-machine interface is set according to PLC internal register title, display Name can be with inside
Register is of the same name.The second uniqueness ID is distributed for register in communications protocol, with marker register.According to the reality of register
Word length sets corresponding word length parameter.Corresponding address realm is set according to the access profile of register.Set the reading of register
WriteMode.It is noted that the register of all open read-writes in PLC can only be defined.The definition register the step of
In, the setting procedure of the read-write mode of register includes:
The addressing mode of definition register.The value of addressing mode is generally Bi t, Byte, Word, DWord, corresponds to respectively
Position, byte, word and double word, and AddressWidth (address width) attribute are used cooperatively, and are mainly used to definition register skew
The interval (length/Mode of the interval equal to AddressWidth length) of value.Illustrate:Assuming that register X
AddressWidth attributes and Mode attributes are all Bit, then at intervals of 1, register X access mode is X0, X1, X2;Assuming that
Register Y AddressWidth attributes are Word, and Mode (pattern) attribute is Byte, then at intervals of Word/Byte=2, i.e., 1
Individual Word length is equal to 2 Byte length, so the access mode of register is Y0, Y2, Y4;Assuming that register Z
AddressWidth attributes are DWord, and Mode attributes are Byte, then at intervals of DWord/Byte=4, i.e., 1 DWord length
Equal to 4 Byte length, so the access mode of register is Z0, Z4, Z8.
Integer-bit system in definition register.The value of integer-bit system is generally Dec, Oct, Hex, corresponds to ten respectively and enters
System, octal system and hexadecimal.
System in definition register from from decimal place to integer-bit carry.It is general from the system of decimal place to integer-bit carry
Defined in the form of 2 n powers, value generally 0,3,4,8 etc., when value is 0, represent no decimal point;When value is 3,
Represent data after decimal point enter be made as 23 powers, i.e. octal system, i.e. number range after decimal point be 0-7, whenever arrival 8
When, enter 1 to integer-bit;When value is 4, represent data after decimal point enter be made as 24 powers, i.e. hexadecimal, i.e., after decimal point
Number range be 0-15, whenever reaching 16, enter 1 to integer-bit, the rest may be inferred.
Decimal place system in definition register.Decimal place system is worth to define the carry value of numerical value behind decimal point
Generally None, Dec, Oct and Hex, nothing, the decimal system, octal system and hexadecimal are corresponded to respectively, wherein when decimal bit length is
When 0, the value of decimal place system is None, indicates no decimal point.
Man-machine interface and PLC communications protocol are defined using method provided by the invention, no matter PLC why species
Which kind of mode type, the addressing of its internal register use, and can realize man-machine interface and PLC data interaction.
After definition is better than the communications protocol with PLC, you can using PLC man-machine communications method provided by the invention on man-machine boundary
Face is communicated with PLC.Based on the communications protocol defined, the present invention proposes a kind of PLC man-machine communications system, for PLC
Communicated with man-machine interface.As shown in figure 5, the system includes man-machine interface host computer and man-machine interface slave computer.Man-machine boundary
The protocol definition file and communications parameter of each PLC communications protocol are prestored in the host computer of face.Protocol definition file includes corresponding PLC
The title of communications protocol and mark ID, corresponding PLC master slave mode and communication driver etc..These data can be passed through by user
Man-machine interface host computer editor is simultaneously packaged into PKG files and is sent in man-machine interface slave computer, in case subsequent calls.Protocol definition
The edit format of file uses XML, and coded system uses UTF-8.When PLC is as communication slave station, protocol definition file also wraps
Include mark ID, word length parameter, address realm, read-write mode and display Name of each internal register in the PLC etc..
When man-machine interface and PLC are communicated, user selects to need the PLC communicated first.User is by selecting PLC
Communications protocol selects to need the PLC communicated, and user select PLC communications protocol be according to the title of PLC communications protocol come
Selection.Man-machine interface host computer can use PC, and corresponding software is installed in PC, and user can select to need by software
The PLC of communication, and specific communications parameter is defined, the PLC that then software selects according to user finds corresponding protocol definition
File, and protocol definition file, the mark ID of the protocol definition file and communications parameter are packaged into PKG files together and are issued to
Man-machine interface slave computer.
After man-machine interface slave computer receives the mark ID of PLC communications protocol, needs are determined according to the mark ID received
The PLC of communication, meanwhile, the master slave mode for the PLC for needing to communicate according to protocol definition file corresponding with mark ID judgement, and
The PLC communicated as needed master slave mode is communicated with the PLC.Specifically include two kinds of situations:
First, when the PLC for needing to communicate is as communication main website, the power down that man-machine interface slave computer opens man-machine interface is not protected
Deposit register, and by the communication driver of the PLC in the corresponding protocol definition file of mark ID with receiving with should
PLC is communicated;PLC can be established according to corresponding communications parameter and man-machine interface and be connected and access the power down not save register.
2nd, when the PLC for needing to communicate is as communication slave station, man-machine interface slave computer receives man-machine interface host computer and sent
The PLC in need the mark ID of internal register that accesses, and obtained according to the mark ID of the internal register received should
The read-write mode of internal register, meanwhile, the actual address deviant of the internal register is parsed according to the read-write mode, and incite somebody to action
The mark ID and actual address deviant of the internal register received are sent to the communication driver of the PLC, are somebody's turn to do with calling
Communication driver, and communicated according to corresponding communications parameter and the PLC.
The integer-bit system of the read-write mode of internal register including the internal register, from decimal place to integer-bit carry
System and decimal place system.When parsing the actual address deviant of the internal register, man-machine interface slave computer is according to inside
The integer figure evidence of the internal register and decimal digits evidence are converted to ten by the integer-bit system and decimal place system of register
System, and parse according to equation below the actual address deviant of the internal register:
If m is to be converted to the integer figure evidence after the decimal system, n is to be converted to the decimal digits evidence after the decimal system, and y is interior for this
The actual address deviant of portion's register, then y=m × 2x+ n, wherein, x is the hex value from decimal place to integer-bit carry.
The read-write mode of internal register also includes the addressing mode of the internal register, when need access PLC it is multiple
During internal register, man-machine interface slave computer obtains the internal register always according to the mark ID of the internal register received
Word length parameter and addressing mode.Man-machine interface slave computer on parsing after the actual address deviant of an internal register,
The actual address deviant of next internal register is calculated according to word length parameter and addressing mode, and other are calculated accordingly
The actual address deviant of internal register.When the actual address deviant being calculated exceeds address realm, man-machine interface
Slave computer will send corresponding prompting.
As shown in fig. 6, present invention also offers a kind of PLC man-machine communications method, comprise the following steps:
Step A:The mark ID of the PLC communications protocol that man-machine interface host computer selects user, protocol definition file and logical
News parameter is sent to man-machine interface slave computer.The protocol definition file of each PLC communications protocol is prestored in man-machine interface host computer
And communications parameter.The edit format of protocol definition file uses XML format, and coded system uses UTF-8.Protocol definition file bag
Include the title and mark ID, corresponding PLC master slave mode and communication driver of corresponding PLC communications protocol.When PLC is as communication
During slave station, protocol definition file also includes the mark ID of each internal register, word length parameter, address realm, read-write side in the PLC
Formula and display Name.
Step B:Man-machine interface slave computer determines to need the PLC communicated according to the mark ID that receives, and according to the mark
Know the master slave mode that protocol definition file corresponding to ID judges the PLC that needs communicate.
Step C:The master slave mode for the PLC that man-machine interface slave computer communicates as needed is communicated with the PLC.Including two
Kind situation:
First, when the PLC for needing to communicate is as communication main website, step C includes:
Man-machine interface slave computer opens the power down not save register, and pass through ID pairs of the mark with receiving of man-machine interface
The communication driver of the PLC in the protocol definition file answered is communicated with the PLC;PLC can be according to corresponding communications parameter
Established with man-machine interface and connect and access the power down not save register.
2nd, when the PLC for needing to communicate is as communication slave station, step C includes:
Man-machine interface slave computer receives the mark for the internal register for needing to access in the PLC that man-machine interface host computer is sent
Know ID, and the read-write mode of the internal register is obtained according to the mark ID of the internal register received, meanwhile, according to the reading
WriteMode parses the actual address deviant of the internal register, and by the mark ID of the internal register received and practically
Location deviant is sent to the communication driver of the PLC, to call the communication driver, and according to corresponding communications parameter with being somebody's turn to do
PLC is communicated.
The integer-bit system of the read-write mode of internal register including the internal register, from decimal place to integer-bit carry
System and decimal place system.When parsing the actual address deviant of internal register, man-machine interface slave computer is posted according to inside
The integer figure evidence of the internal register and decimal digits evidence are converted to ten and entered by the integer-bit system and decimal place system of storage
Make, and the actual address deviant of the internal register is parsed according to equation below:
If m is to be converted to the integer figure evidence after the decimal system, n is to be converted to the decimal digits evidence after the decimal system, and y is interior for this
The actual address deviant of portion's register, then y=m × 2x+ n, wherein, x is the hex value from decimal place to integer-bit carry.
The read-write mode of internal register also includes the addressing mode of the internal register, when need access PLC it is multiple
During internal register, man-machine interface slave computer obtains the internal register always according to the mark ID of the internal register received
Word length parameter and addressing mode.Man-machine interface slave computer on parsing after the actual address deviant of an internal register,
The actual address deviant of next internal register is calculated according to word length parameter and addressing mode, and other are calculated accordingly
The actual address deviant of internal register.When the actual address deviant being calculated exceeds address realm, man-machine interface
Slave computer will send corresponding prompting.
The foregoing is merely illustrative of the preferred embodiments of the present invention, is not intended to limit the invention, all essences in the present invention
All any modification, equivalent and improvement made within refreshing and principle etc., should be included in the scope of the protection.