Write an OPC client with VB to access WinCC
OPC is an industrial standard that is established by some world-renowned automation systems and hardware, software companies and Microsoft (Microsoft). [O represents OLE (object links and embedded), P (Process Process), C (Control Control). OLE has been redefined from object-oriented to object and renamed Active X.
WinCC is a work control software that Siemens has adopted the most advanced technology and Microsoft companies in the automation field. WinCC is Windows Control Center. WinCC is a powerful and fully open monitoring system that can be used to complete small-scale simple process monitoring applications, or to complete complex applications. In any case, WinCC can generate a beautiful and convenient human-machine dialog interface that enables the operator to clearly manage and optimize the production process. It integrated OLE for Process Control servers allows process data to be accessed by other applications (OPC clients).
WinCC provides OPC client control: Siemens OPC Daautomation 2.0 (Sopcdauto.dll), this control is the control we want to use in VB, we can also use universal OPC client control: OPC Automation 2.0.
In WinCC, there is a shortcomings used by Siemens OPC Daautomation 2.0, but it is not very detailed. I have encountered a lot of problems in use, and I will write it together, and share it with you.
First, OPC connection
Join nearly Siemens OPC Daautomation 2.0 in "Reference" and then begin to define global variables. In this program, I used two OPC groups for OPC access, so defined global variables. We must first define the OPC service type and computer node name. Define the OPC group and the OPC tag group. And define the OPC's label array and value, note that the value group must be set to Variant.
'OPC processing: Only for WinCC
Const servername = "opcserver.wincc" 'OPC type
Const nodename = "guk" 'Name, ie computer name
'Dim NodeName as String
DIM WITHEVENTS MyOpcserVer AS OPCServer 'OPC Service
DIM MyOpcGroupColl as opcgroups'
DIM WITHEVENTS MyOpcGroupOut as opcgroup 'OPC group, this program uses two groups OPC connection
DIM WITHEVENTS MyOpcGroupin As Opcgroup
DIM MyOpciteMcollin As Opcitems' OPC Tag Group
DIM MyOpCItemcollout as opcitems
DIM ServerHandlesin () AS Long 'handle
DIM ServerHandLesout () As long
Dim Errorsin () AS Long 'error handle
Dim Errorsout () as long
Dim WatchDataReadItem (100) AS String 'Records OPC Label
DIM WATCHDATAREADVALUE (100) AS VARIANT 'Store OPC DIM WatchDataWriteItem (100) AS String' Record OPC Label
DIM WatchDataWriteValue (100) as variant 'Store the value of OPC
After defining all variables, we will perform an OPC connection. Before you want to configure the OPC tag name you want to access, our WatchDataReadItem, WatchDataWriteItem, join the appropriate label name, note: These two arrays must be 1 Start, can't start from 0.
After configuring the label, you have to connect OPC. As the face program:
1, ClientHandles1 first configure the list of the name, which will be used when reading the value of the OPC tag
2, generate an OPC object,
3, perform an OPC tag connection
To this: OPC connection is successful, we can read and write to OPC.
'------------------------------------- --------------------
'SUB STARTCLIENT ()
'Purpose: Connect to OPC_SERVER, create groups and add entries
'------------------------------------- --------------------
Private sub startclient ()
DIM ITEMNUM AS INTEGER
DIM TARNSCATIONID AS Long
DIM CANCEID AS Long
Dim ClientHandles1 (100) as long
DIM II AS INTEGER
ON Error Goto HandleError
FOR II = 0 to 100
ClientHandles1 (II) = II First configure the licensed index, which will be used when the value of the OPC tag is read
Next II
TARNSCATIONID = 1
'Nodename = xprofile.getvalue ("system", "nodename")
'Generate OPC objects,
Set myopcserver = new opcserver
Myopcserver.connect ServerName, Nodename
Set myopcgroupcoll = myopcserver.opcgroups
Myopcgroupcoll.defaultgroupisactive = true
Set myopcgroupin = myopcgroupcoll.add ("mygroupin")
Set myopcgroupout = myopcgroupcoll.add ("mygroupout")
Set myopcitemcollin = myopcgroupin.opcitems
Set myopcitemcollout = myopcgroupout.opcitems
'Connection to OPC tags
IF WRITEIDEX> 0 THEN
Myopcitemcollout.addItems WriteItemidex, WatchDataWriteItem, ClientHandles1, ServerHandLesout, Errorsout 'Initializing OCP connection
Myopcgroupout.issubscribed = true
End ifif readitemidex> 0 THEN
MyopcitemCollin.Additems ReadItemidex, WatchDataReadItem, ClientHandles1, ServerHandlesin, Errorsin 'initialization OCP connection
Myopcgroupin.issubscribed = true
END IF
EXIT SUB
HandleError:
Needopcrestart = TRUE
Xlog1.log "OPCL connection error"
End Sub
Second, OPC label reading and writing
Reads for OPC tags can be read through the MyopcGroupin group with myopcgroupout's DataChange event. The event has multiple parameters: where NumItems is the number of indicator changes, clientHandles is the label index of the value, ItemValues is the data of the value, the specific meaning of ClientHandles (1) is its corresponding label array Index, the value of the OPC tag that it is referred to is in ItemVALUES (1). In general, when the event is just connected, the event will read all the OPC tag values that all of them (the order is different, to pass the clientHandles index), which will only trigger the event when data changes. It also transmits data that has changed, without changing data does not appear in the itemValues in this event.
Private sub myopcgroupout_datachange (Byval Numitems As Long, ClientHandles () As long, itemValues () As Variant, Qualities () As long, timestamps () AS DATE
'Generate data changes to the next level, and the control of the roots is different.
For ii = 1 to NumItems
WatchDataWriteValue (ClientHandles (II) - 1) = ItemVALUES (II) 'Read the value of the changed value
Next II
End Sub
The write to OPC can have synchronous and asynchronous points, for a large number of data transfer, asynchronous is better, but for a small amount of data, synchronous performance is better.
To assign a value data, you must first assign value data. Note: The value data should be started by 0, that is, the value group and the tag data are not one, one correspondence, the value is more than the label, this, in WinCC Nothing in the description, but in my actual use, otherwise the data is misalign, see the procedure below.
This is a program that is returned after the cleaning is completed. Contains unpacking process,
Private Sub Showsuccess (MSG As String)
DIM Location As String
DIM NOWTIME AS STRING
Dim logstr as string
DIM Value () AS String
DIM II, TEMP AS INTEGER
DIM ISPACK AS BOOLEAN
DIM Shead, SDELIMITED, STAIL AS STRING
Location = Xprofile.GetValue (WatchPoint (Nowrunid), "location")
NOWTIME = now
Logstr = "Pull" & Location & "Success" & msgxlog1.log logstr
Logstr = "" & msg
Xlog2.log logstr 'record data
'Data upload
'If you have a package structure, the package structure is displayed.
ISPACK = Xprofile.GetValue (WatchPoint (Nowrunid), "IsRechead")
If WatchPointPointRbegin (NOWRUNID) <0 THEN EXIT SUB
IF ispack then
Shead = Xprofile.GetValue (WatchPoint (Nowrunid), "Rechead")
SDELIMITED = Xprofile.getValue (WatchPoint (Nowrunid), "RecDelimiter")
Stail = Xprofile.GetValue (WatchPoint (Nowrunid), "Recend")
Value = Split (MSG, SDELIMITED)
For ii = 0 to Ubound (value) - 1
Temp = WatchPointRbegin (NOWRUNID) II
IF Temp> WatchPointrend (NOWRUNID) THEN EXIT for
WatchDataReadValue (Temp - 1) = Value (II 1) 'Value begins with 0, less than Item 1, so minus one. There is a header, accounting for one, delayed back
Next II
Else
WatchDataReadValue (WatchPointRend (NOWRUNID) - 1) = msg
END IF
MyopcGroupin.syncWrite Readtemidex, ServerHandlesin, WatchDataReadValue, Errorsin 'data upload
'Record the time for successful execution
Xprofile.SetValue WatchPoint (NOWRUNID), "Lasttime", NOWTIME
End Sub
Third, the OPC connection is disconnected.
After the OPC client is connected, the server resource is to be taken, so if you do not need to use OPC, the OPC connection must be disconnected.
The disconnected program is quite simple, release the resource. as follows,
Sub stopclient ()
ON Error ResMe next
'----------- Release group and server object
MyopcgroupColl.removell
'----------- Disconnect with the server and clear
MyOpcserver.disconnect
Set myopcitemcollin = Nothing
Set myopcitemcollout = Nothing
Set myopcgroupin = Nothing
Set myopcgroupout = Nothing
Set myopcgroupcoll = Nothing
Set myopcserver = Nothing
End Sub
But in the actual use, it is found that frequent connections and disconnections will cause a large amount of resource of the server, and finally let the server errors. So minimize the unnecessary OPC connection and disconnection.
Conclusion:
The OPC use is used as a DCOM, so the OPC client can run on a computer running, but you must configure the access to DCOM, if you don't want to charge God, use the same username and password to log in with the same username with the client. Cheng. If you want to configure DCOM, please see the configuration of the DCOM. Reference:
"WinCC online help"
About the Author:
Gu Yu, senior programmer, Hunan University graduated, has been engaged in Linux kernel research, engaged in the development of enterprise information platform, currently engaged in SCADA development, mainly using different types of remote equipment through the same means to collect data collection, And integrated into the enterprise information platform. I like to form some integration in my work.
Come from:
http://blog.9cbs.net/monkst/archive/2003/02/13/15037.aspx