C # OPC operation

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.