Upload
phamcong
View
240
Download
0
Embed Size (px)
Citation preview
OPCwithVisualStudio
Hans-PetterHalvorsen,M.Sc.
Software• MatrikonOPCSimulationServer• VisualStudio• MeasurementStudio–Add-onpackagetoVisualStudiocreatedbyNationalInstruments(samevendorasLabVIEW)
MeasurmentStudio• Add-onpackagetoVisualStudiocreatedbyNationalInstruments
• SamevendorasLabVIEW• MakesitpossibletocommunicatewithanOPCDAServerfromVisualStudioCode
• UsestheDataSocket Library(sameasinLabVIEW)
OPC
Hans-PetterHalvorsen,M.Sc.
WhatisOPC?• Astandardthatdefinesthecommunicationofdatabetweendevicesfromdifferentmanufactures
• RequiresanOPCserverthatcommunicateswiththeOPCclients
• OPCallows“plug-and-play”,givesbenefitsasreducesinstallationtimeandtheopportunitytochooseproductsfromdifferentmanufactures
• Differentstandards:“Real-time”data(OPCDA),Historicaldata(OPCHDA),Alarm&Eventdata(OPCAE),etc.
OPC
OPCServer OPCClientRead/WriteData
DataStorage
OPCServer
OPCClient
WriteData
DataStorage
OPCClient
ReadData
MeasurementData
Monitoring andAnalysis
OPC-Server
Network
OPC-Client OPC-ClientOPC-Client
Process
ProcessData
DataAcquisitionPLC,PAC,DCS,SCADA
TypicalOPCScenario
Driver
SensorsActuators
OPCSpecifications
OPCDA
OPCHDA
OPCA&E
OPCUA
“Classic”OPC “NextGeneration”OPC
...(Manyothers)
OPCSpecifications• OPCDA(DataAccess)ThemostcommonOPCspecificationisOPCDA,whichisusedtoreadandwrite“real-time”data.WhenvendorsrefertoOPCgenerically,theytypicallymeanOPCDA.• OPCHDA(HistoricalDataAccess)• OPCA&E(Alarms&Events)• ...(manyothers)TheseOPCspecificationarebasedontheOLE,COM,andDCOMtechnologiesdevelopedbyMicrosoftfortheMicrosoftWindowsoperatingsystemfamily.ThismakesitcomplicatedtomakeitworkinamodernNetwork!TypicallyyouneedaTunneller SoftwareinordertosharetheOPCdatainanetwork(betweenOPCServersandClients)
• OPCUA(UnifiedArchitecture)OPCUAeliminatingtheneedtouseaMicrosoftWindowsbasedplatformofearlierOPCversions.OPCUAcombinesthefunctionalityoftheexistingOPCinterfaceswithnewtechnologiessuchasXMLandWebServices(HTTP,SOAP)
NextGenerationOPC
OPCClassic OPCUA
OPCDAServer
OPCHDAServer
OPCA&EServer
COM/DCOM XML,HTTP,SOAP
Cross-platformWindowsonly Allspecificationscollectedinone(DA,HDA,A&E)
NextGenerationOPC
OPCClient(DCOM)
OPCDAOPCHDA
OPCA&E
OPCUAServer OPCUAClient
Protocols:“UABinary”or“UAXML”
Windows,Linux,Mac,Embedded,VxWorks
Simpler!!
(everything builtintoone)
NextGenerationOPC
OPCClassicServer OPCClassicClient
OPCUAServer OPCUAClientNetwork/Internet
ToopenDCOMthrough firewallsdemandedalargehole inthefirewall!Impossible torouteoverInternet!
Nohole infirewall(UAXML)orjustasimpleneedlestick(UABinary)isnecessaryEasytorouteoverInternet!
Network
ClassicOPCvs.OPCUAClassicOPC(DCOM) OPCUA
OPCServer
OPCClient
OPCClient
OPCClient
Windows
Windows
Windows
WindowsOPCUAServer
OPCUAClient
OPCUAClient
ClassicOPCrequiresaMicrosoftWindowsoperatingsystemtoimplementCOM/DCOMserverfunctionality.ByutilizingSOAandWebServices,OPCUAisaplatform-independentsystemthateliminatesthepreviousdependencyonaWindowsoperatingsystem.ByutilizingSOAP/XMLoverHTTP,OPCUAcandeployonavarietyofembeddedsystemsregardlessofwhetherthesystemisageneralpurposeoperatingsystem, suchasWindows,oradeterministicreal-timeoperatingsystem. http://www.ni.com/white-paper/13843/en/
OPCUAClient
Theserver(orclients)canbeanembeddedsystem,LINUX,Windows, etc.
MatrikonOPCSimulationServer
Hans-PetterHalvorsen,M.Sc.
Matrikon OPCExplorer– ConnecttoServer
1
2
Matrikon OPCExplorer- AddTags
1
2
3
4
5
Double-click
Finished
MatrikonOPCExplorer(OPCClient)
TheMatrikonOPCExplorerisusefulfortesting.YoucanuseitforwritingandreadingOPCTags
MeasurementStudio
Hans-PetterHalvorsen,M.Sc.
MeasurmentStudio• Add-onpackagetoVisualStudiocreatedbyNationalInstruments
• SamevendorasLabVIEW• MakesitpossibletocommunicatewithanOPCDAServerfromVisualStudioCode
• UsestheDataSocketLibrary(sameasinLabVIEW)
MeasurementStudio• MeasurementStudioisanadd-ontoVisualStudio.• MeasurementStudioisusedfordevelopmentofmeasurement,controlandmonitoringapplicationsusing.NETandVisualStudio.
• MeasurementStudiohasalibrary(DataSocket library)thatmakesitpossibletocommunicatewithOPCDAserversthatwewilluseisthislabwork
• DownloadSoftwarehere:http://www.ni.com/academic/download
20
Software
VisualStudioEditionsq IhaveVisualStudio2013Ø YoucanInstallanduseMeasurementsStudio2015withoutproblems
q IhaveVisualStudio2015Ø MeasurementStudio2015isdesignedtoworkwithVisualStudio2013(andoldereditions).
Therefore,MeasurementStudio2015doesnotinstallshippingexamplesanddoesnotintegratewithVisualStudio2015
Ø Thismeans,ifyouinstallMeasurementStudio2015withVisualStudio2015,theMeasurementStudio.NETcontrolsarenotintheToolbox,andyoudonothaveaMeasurementStudiomenuitemintheVisualStudio2015toolbar.
Ø Followtheseguidelinestodothismanually:http://home.hit.no/~hansha/documents/control/opc/resources/Using%20Measurement%20Studio%202015.pdf
MeasurementStudio2015MeasurementStudio2015doesnothaveintegrationfeaturesforVisualStudio2015.IfyouinstallMeasurementStudio2015withVisualStudio2015,theMeasurementStudio.NETcontrolsarenotintheToolbox,youdonothaveaMeasurementStudiomenuitemintheVisualStudio2015 toolbar,and.licxwillnotbeautomaticallygenerated.WehaveplanstomakechangestoMeasurementStudiothatwillhelpusbetterkeepupwithnewversions.Unfortunately,thesechangesareaquiteabitmorecostlythanit wouldseem,particularlytheVisualStudioHelpintegration,sothesechangesmaynotcomesoon.
ThissituationisthesameaspreviousMeasurementStudiosoftwareasinthefollowingarticle'UsingMeasurementStudio2013withMicrosoftVisualStudio2013':http://digital.ni.com/public.nsf/allkb/C51E3B38578FAD2786257C070069F386
VisualStudio2015isnot supportedofficiallyinMeasurementStudio2015;however,asintheabovearticle,youcanaddtheMeasurementStudio.NETcontrolstotheToolboxmanuallyandcancreate.licx filesmanually.IhaveattachedaHelpDocumentonthistopicsoyoucanrefertothe AddingMeasurementStudio2015UserInterfaceControls totheToolbox sectionformoreinformationonHowtoAddcontrols.Thissectionalsodescribeshowthesecontrolsarelicensed.ThefollowingsectionsdiscussadditionaltopicstoconsiderwhenusingMeasurementStudio2015withVisualStudio2015.
RebeccaCostinNationalInstrumentsApplicationsEngineeringwww.ni.com/support
VisualStudio2013+MeasurementStudioSelect“NewProject”inVisualStudio:
MakesuretoselecttheDataSocketLibrary
Selectthe“NIWindowsApplication”Template
12
3
4
5
76
8
MeasurementStudio isan“Add-in”forVisualStudiocreatedbyNationalInstruments.
Note!Youdontneedtousethe“MeasurmentStudioTemplates”– YoucanuseanordinaryWinFormAppalso!!
VisualStudio2015+MeasurementStudioYoucanuseanordinaryWinFormApp
Thenyouneedto:1. AddReferences(Assemblies)
Manually2. Change/UpdatetheLicenseFile
(Licenses.licx)
AddingReferencestoyourProjectYouneedtoaddtheseAssemblies(.dllfiles)toyourVisualStudioProject
LocatethefollowingAssembliesonyourharddrive:NationalInstruments.Common.dllNationalInstruments.Net.dll
C:\ProgramFiles…\NationalInstruments\MeasurementStudioVSXXXX\DotNET\Assemblies\Current\...
LicenseFile
NationalInstruments.Net.DataSocket,NationalInstruments.Net,Version=13.0.45.242,Culture=neutral,PublicKeyToken=4febd62461bf11a4
Youmayneed toupdatetheLicenseFilewithcorrectVersionNumberandPublicKeyToken accordingtotheinstalledAssembliesyouhaveonyourharddrive
OPCReadExample
Hans-PetterHalvorsen,M.Sc.
OPCRead
ClickButtontogetlatestValuefromOPCServer
MatrikonOPCServer/OPCExplorer VisualStudioApp
ReadfromOPCServerusingVisualStudiousingNationalInstruments;usingNationalInstruments.Net;…stringopcUrl;doubleopcValue;
opcUrl ="opc://localhost/Matrikon.OPC.Simulation/Bucket Brigade.Real4";
DataSocketdataSocket =newDataSocket();
if(dataSocket.IsConnected)dataSocket.Disconnect();
dataSocket.Connect(opcUrl, AccessMode.Read);dataSocket.Update();opcValue =Convert.ToDouble(dataSocket.Data.Value);
Note!ThisCodeSnippet readsonly onevalueonce,you canusee.g.aTimer inordertoreadvalues atspecific intervals.
usingNationalInstruments;usingNationalInstruments.Net;usingSystem;usingSystem.Windows.Forms;
namespaceOPC_Read{public partialclassForm1:Form{DataSocketdataSocket=newDataSocket();
public Form1(){InitializeComponent();
stringopcUrl;opcUrl ="opc://localhost/MATRIKON.OPC.Simulation/Bucket Brigade.Real4";
if(dataSocket.IsConnected)dataSocket.Disconnect();
dataSocket.Connect(opcUrl, AccessMode.Read);}
privatevoidbtnReadOpc_Click(object sender, EventArgse){dataSocket.Update();
txtReadOpcValue.Text =dataSocket.Data.Value.ToString();}
}}
OPCWriteExample
Hans-PetterHalvorsen,M.Sc.
WriteOPCExampleMatrikonOPCServer/OPCExplorer
VisualStudioApp
usingNationalInstruments;usingNationalInstruments.Net;…stringopcUrl;doubleopcValue;
opcUrl ="opc://localhost/Matrikon.OPC.Simulation/Bucket Brigade.Real4";
DataSocketdataSocket =newDataSocket();
if(dataSocket.IsConnected)dataSocket.Disconnect();
dataSocket.Connect(opcUrl, AccessMode.Write);
opcValue=Convert.ToDouble(txtWriteOpcValue.Text);dataSocket.Data.Value=opcValue;dataSocket.Update();
WritefromOPCServerusingVisualStudio
usingNationalInstruments;usingNationalInstruments.Net;usingSystem;usingSystem.Windows.Forms;
namespaceOPC_Write{publicpartialclassForm1:Form{DataSocketdataSocket=newDataSocket();
publicForm1(){InitializeComponent();
stringopcUrl;opcUrl="opc://localhost/MATRIKON.OPC.Simulation/BucketBrigade.Real4";
if(dataSocket.IsConnected)dataSocket.Disconnect();
dataSocket.Connect(opcUrl,AccessMode.Write);}
privatevoidbtnWriteOpc_Click(objectsender,EventArgse){doubleopcValue=0;
opcValue=Convert.ToDouble(txtWriteOpcValue.Text);
dataSocket.Data.Value =opcValue;
dataSocket.Update();}
}}
OPCWrite/ReadExample
Hans-PetterHalvorsen,M.Sc.
Write/ReadExample
usingSystem;usingSystem.Windows.Forms;usingNationalInstruments;usingNationalInstruments.Net;
namespaceOPCExample{publicpartialclassForm1:Form{
DataSocketdataSocketRead=newDataSocket();DataSocketdataSocketWrite=newDataSocket();
publicForm1(){InitializeComponent();
stringopcUrl;opcUrl="opc://localhost/MATRIKON.OPC.Simulation/BucketBrigade.Real4";
if(dataSocketRead.IsConnected)dataSocketRead.Disconnect();
dataSocketRead.Connect(opcUrl,AccessMode.Read);
if(dataSocketWrite.IsConnected)dataSocketWrite.Disconnect();
dataSocketWrite.Connect(opcUrl,AccessMode.Write);
}
privatevoidbtnReadOpc_Click(objectsender,EventArgse)…
privatevoidbtnWriteOpc_Click(objectsender,EventArgse)…
}}
privatevoidbtnReadOpc_Click(objectsender,EventArgse){
dataSocketRead.Update();
txtReadOpcValue.Text=dataSocketRead.Data.Value.ToString();
}
privatevoidbtnWriteOpc_Click(objectsender,EventArgse){
doubleopcValue=0;
opcValue=Convert.ToDouble(txtWriteOpcValue.Text);
dataSocketWrite.Data.Value=opcValue;
dataSocketWrite.Update();}
AdditionalFeatures
Hans-PetterHalvorsen,M.Sc.
AdditionalFeatures• UsingaTimerinordertoread/write Datafrom/tothe OPCServeratspecific Intervals
• Trending/PlottingData– Usingthe ”WaveformGraph”Control includedwithMeasurment Studio
• UsingOOP,i.e.,CreateandUseClassesinyourCode
Timer
publicForm1(){InitializeComponent();
timer1.Start();}
privatevoidtimer1_Tick(object sender, EventArgs e){…//ReadfromOPC…//Scaling…//PlotData
}
TimerEvent:
Initialization:
Properties:
StructureyourCodeproperly!!DefineClassesandMethodswhichyoucanusehere
InVisualStudio youmaywanttouseaTimerinsteadofaWhileLoopinordertoreadvaluesatspecificintervals.
YoumayspecifytheTimerIntervalinthePropertiesWindow
Selectthe“Timer”componentintheToolbox
Double-clickontheTimerobjectinordertocreatetheEvent
2
3
4
1
TrendingDatawithMeasurementStudio
…{
…
waveformGraph.PlotYAppend(analogDataIn);
}
Youmayusethe“WaveformGraph”ControlincludedwithMeasurementStudio
Youonlyneedonelineofcode,e.g.intheTimerEvent:
NameofyourWaveformGraph Control NameoftheMethodtouseNameofthevariablewithTemperaturedata
OOP:Classes,FieldsandMethodsclassCar //Class{
publicstringcolor;//Field
//MethodpublicvoidShowCarColor(){
MessageBox.Show("MyCaris:"+color);}
}CarmyCar =newCar();//WecreateanInstanceoftheClass
myCar.color ="blue"; //WesetavalueforthecolorField
myCar.ShowCarColor(); //WecalltheMethod
1
2
3
Example:
OOP:ObjectOrientedProgramming
YoushoulduseOOPtechniques inyourapplication!Createyourownclasses,fieldsandMethods
MakesuretouseOOP(ObjectOrientedProgramming) inyour solutions
Hans-PetterHalvorsen,M.Sc.
UniversityCollegeofSoutheastNorwaywww.usn.no
E-mail:[email protected]:http://home.hit.no/~hansha/