Opcautomation.opcserver Clsopcserver =Newopcautomation.opcserver (); Opcgroup clsopcgroup;opcgroups Clsopcgroups;opcitem clsopcitem;opcitem[] AllOPCItem; Boolean Bolconnect () {//return true; Try { if(Clsopcgroup = =NULL) {Clsopcserver=NewOpcserver (); T.elapsed+=NewSystem.Timers.ElapsedEventHandler (theout);//execution of events at time of arrival;T.autoreset =false;//Whether the setting is executed once (false) or always executed (true);t.enabled =true;//whether to execute the System.Timers.Timer.Elapsed event;T.start (); Clsopcserver.connect (Stropcserver, Stropcserverip); if(Clsopcserver.serverstate = = (int) opcserverstate.opcrunning) {t.stop (); T.dispose (); Clsload.writelog ("OPC Service Connection succeeded"); } clsopcgroups=clsopcserver.opcgroups; Clsopcgroup=Clsopcgroups.add (Stropcgroup); } return true; } Catch(Exception ex) {Clsload.writelog ("OPC Service connection error"+"Host IP"+ Stropcserverip +"Service Name:"+ Stropcserver +"Access Group name:"+ Stropcgroup +"Detail"+Ex. ToString ()); return false; }} Boolean Boladditem () {stringSQL; Try{List<string> items =Newlist<string>(); foreach(stringStinchStation ) {SQL="select * from Land where st= '"+ St +"'"; List<string> columns = clssql.getcolumnstrings (SQL,"off"); for(intm =3; M < columns. Count; m++) {items. ADD (Columns[m]); }} Allopcitem=NewOpcitem[items. Count]; for(intm =0; M < items. Count; m++) {Allopcitem[m]=ClsOPCGroup.OPCItems.AddItem (Items[m], M); Hw. ADD (ITEMS[M],M); } return true; } Catch(Exception ex) {Clsload.writelog ("OPC service add error"+Ex. ToString ()); return false; } } ObjectREADOPC (stringO_adrs)//byte-type variable { ObjectObjValue =""; Objectobjquality; ObjectObjtimestamp; intCT; Try{CT= (int) Hw[o_adrs]; ALLOPCITEM[CT]. Read (Convert.ToInt16 (OPCAutomation.OPCDataSource.OPCDevice), outObjValue, outObjquality, outObjtimestamp); returnObjValue; } Catch(Exception ex) {//MessageBox.Show ("read OPC address" + O_adrs + "error");Clsload.err ="Read OPC address"+ O_adrs +"Error"; Clsload.writelog ("OPC Read status"+ O_adrs +"Error"+Ex. ToString ()); return 0; } } BOOLWRITEOPC (ObjectIntstatus,stringO_adrs)// { intCT; Try{CT= (int) Hw[o_adrs]; ALLOPCITEM[CT]. Write (Intstatus); return true; } Catch(Exception ex) {Clsload.err="Write OPC address"+ O_adrs +"Error"; Clsload.writelog ("Write OPC Address status"+ O_adrs +"Error"+Ex. ToString ()); return false; } }
C # OPC operation