Realizes serial communication between Omron PLC and computer with VB - Database & Sql Blog Articles
1 Introduction In the field of industrial control, plc is widely used as a stable and reliable controller. But it also has its own shortcomings, that is, the data processing and management capabilities are weak, and can not provide users with a good interface. The computer just can make up for the lack of plc. It not only has strong data processing and management capabilities, but also provides users with a very beautiful and easy to operate interface. Combining the plc with the computer enables the system to acquire and store data in a timely manner, and to process and use the data. The key to the combination is the communication between the plc and the computer. This article takes omron's cpmia small plc as an example to discuss in detail the principle of plc and computer communication and how to use pb to realize communication between plc and computer. Glass Aquariums,Aquarium Fish Tank,Glass Turtle Tank,Glass Fish Tank Sensen Group Co., Ltd.  , https://www.sunsunglobal.com
2 Passing principle and method The upper computer should be able to monitor the state of the lower device through plc, and it is necessary to realize the communication between the upper computer and the plc. In general industrial control, rs232c is used. The host computer first sends an instruction to query the data to the plc (actually querying the status of the terminal in the plc and the value of the dm area, etc.), and after receiving the upper instruction, the plc performs the verification (fcs check code) to see if it is correct. If it is correct, the data is transmitted to the host computer (including the first and last check bytes). Otherwise, plc refuses to transfer data to the host computer. The upper station receives the data transmitted by plc, and also judges whether it is correct or not. If it is correct, it receives, otherwise, it refuses to receive.
Since cpm1a does not provide a serial communication port, we use the peripheral ports it provides for communication. The connection between plc and the computer is realized by the special cable cqm1-cif01 provided by omron, and the hardware connection diagram is shown in FIG.
3 communication between the plc and the computer The communication between the computer and the plc is performed in units of "frames", and the computer has a higher priority in the process of communication. First, the computer issues a command frame to plc, and then plc responds by sending a response frame back to the computer. The format of the command frame and response frame is as follows:
(1) Command frame format. In order to facilitate the communication between the computer and the plc, cpm1a specifies the corresponding format for the commands and responses exchanged in the computer connection communication. When the computer sends a command, the command data master preparation format is as shown in Figure 2.
Where @ is placed in the first place, indicating that starting with @, the device number is the device number of the connected plc that the host computer recognizes. The identification code is the command code, which is used to set the operation that the user wants the upper computer to complete. The fcs is the frame check code. Once the communication error occurs, the fcs can be found in time. The end character is "*" and the cr carriage return, indicating the end of the command.
(2) Response frame format. The response frame format corresponding to the command format issued by plc is shown in FIG.
Among them, the exception code can determine whether the command sent by the computer is executed correctly. Others have the same meaning as in the text. The body is only returned when there is data read.
4 communication program design In order to make full use of the powerful functions of computer data processing, we can use computer priority method to write programs on the computer to realize the communication between the computer and plc, the computer sends a command to plc to initiate communication, plc automatically returns the response. . In this paper, vb is used to write the communication program between the computer and the plc. A communication control-application communication control (mscomm) is provided in vb to implement the function of transmitting and receiving through the serial port.
The properties of the mscomm control are described below:
*commport: Set the communication port number. The program must specify the serial port number to be used. The WINOOWs system uses the set port to communicate with the outside world.
*portopen: Set the status of the communication port. If true, the communication port is open, otherwise it is closed.
*settings: Set the communication port parameters, the format is "bbbb,p
, d, s", where bbbb is the communication rate (baud rate), p is the communication check mode (parity), d is the number of data bits, s is the number of stop bits, and its setting should be consistent with the setting of plc .
*input: The characters that are passed to the input buffer are read into the program.
*output: Writes characters to the output buffer.
*inbuffercount: Returns the number of characters in the receive buffer.
*outbuffercount: Returns the number of characters in the output buffer.
*inputlen: Sets the length of the serial port to read in the string.
*inputmode: Sets the way to receive data.
*rthreshold: Sets the number of characters that cause a receive event.
*commevent: Returns the value of the oncomm event *oncomm event: This event is fired whether an error or event occurs.
(1) Initialization of control parameters.
The initialization procedure is as follows:
Mscomm.comport=2 `Use serial port com2
Mscomm.settings="9600, e, 7, 2" `baud rate 9600, even parity, 7 data bits, 2 stop bits
Mscomm.portopen=true `Open the communication port, prepare for communication (2) Calculate the check code fcs, calculate the vb custom function of fcs as follows:
Function fcs(byval inputstr as string) as string
Dim slen, i, xorresult as integer
Dim tempfes as string
Slen=len(inputstr) `Request input string length
Xorresult = 0
For i = 1 to slen
Xorresult = xorresult xor asc(mid$(inputstr, i, 1)) `bitwise XOR
Next i
Tempfes=hex$(xorresult) `convert to hexadecimal
If len(tempfes)=1then tempfes =“0â€+tempfes
Fcs = tempfes
End function
(3) Computer and plc communication program.
Mainly a custom function.
Function readdata(byval inputstr as string, byval num as integer) as string
Dim outputstr as string
Dim instring as string
Dim returnstr as string
Dim endstring as string
Dim fcsstring as string
Dim returnfcsstring as string)
Mscomm.inbuffercount=0
Outputstr=inputstr+fcs(inputstr)+“*†` gives the command frame
Mscomm.output=outputstr+chr$(13) `Transfer command frame to plc
Do
Doevents
Loop while mscomm.inbuffercount < 15
Instring=mscomm.input `Get the response frame of plc' end code judgment
Endstring = mid$(instring, len(instring) -
Num- 5, 2)
If endstring = "13" then
Readdata = "error"
Exit function
Elseif endstring = "14" then
Readdata = "error"
Exit function
Elseif endstring = "15" then
Readdata = "error"
Exit function
Elseif endstring = "18" then
Readdata = "error"
Exit function
Elseif endstring = "a3" then
Readdata = "error"
Exit function
Elseif endstring = "a8" then
Readdata = "error"
Exit function
End if
`Response frame check
Endstring = mid$(instring, 1, len(instring) - 4)
Returnfcsstring = mid$(instring,len(instring) - 3, 2)
Fcsstring = fcs(endstring)
If fcsstring <> returnfcsstring then
Readdata = "error"
Exit function
End if
Returnstr = mid$(instring,
Len(instring) - num - 3, num)
Readdata = returnstr
End function
As can be seen from the above program, the computer should perform fcs check on the response frame returned by the plc, and use the exception code to exclude the returned abnormal data, which not only improves the correctness of the computer to obtain information, but also improves the real-time performance of the computer monitoring.
5 Conclusion The communication method introduced in this paper has been implemented in a set of mechatronics equipment developed by our school's cims research center. After trial operation, it proves that this communication method is stable and reliable, and it is indeed a very effective method. The plc is connected with the computer communication network, plc is used as the lower computer, and the computer is used as the upper computer to form an automatic control system with complementary advantages, which realizes “centralized management and decentralized controlâ€. Each plc subsystem or remote workstation controls each controlled object at the production site, and uses the network connection to form a plc integrated control, which satisfies the transition of modern automation system to informationization, networking and intelligence.