usingopcinvisualstudio - telemark university...

23
1 HansPe*er Halvorsen, Telemark University College, 2013.02.19 Using OPC in Visual Studio Example: Logging Temperature Data using OPC

Upload: hatuyen

Post on 06-Feb-2018

220 views

Category:

Documents


2 download

TRANSCRIPT

1  Hans-­‐Pe*er  Halvorsen,  Telemark  University  College,  2013.02.19  

Using  OPC  in  Visual  Studio  Example:  Logging  Temperature  Data  using  OPC  

Equipment  •  Hardware:  – NI  USB-­‐TC01  Thermocouple  Measurement  Device  (or  NI  USB-­‐6008  DAQ  Device)  

•  SoKware:  – MatrikonOPC  SimulaFon  Server  – Visual  Studio  2010  – DAQmx  Driver  – Measurement  Studio  2012  for  Visual  Studio  2010  

2  

3  

What  is  OPC?  

What  is  OPC?  

4  

•  OPC  -­‐  “Open  Process  Control”  •  A  standard  that  defines  the  communicaFon  of  data  between  devices  from  different  manufactures  •  Require  an  OPC  server  that  communicates  with  the  OPC  clients  •  OPC  allows  “plug-­‐and-­‐play”,  gives  benefits  as  reduces  installaFon  Fme  and  the  opportunity  to  choose  products  from  different  manufactures  •  Different  standards:  Real-­‐Fme  data  (OPC  DA),  Historical  data  (OPC  HDA),  Alarm  &  Event  data  (OPC  AE),  etc.    

What  is  OPC?  

5  

The  user  are  free  to  choose  the  best  suited  devices,  controller  and  applicaFon  for  their  project  without  worrying  about  which  vendor  each  came  from.  

6  

What  is  OPC?  

E.g.  a  PLC  

OPC  is  the  world’s  most  popular  standards-­‐based  data-­‐connecFvity  method.  It  is  used  to  answer  one  of  the  automaFon  industry’s  biggest  challenges:  how  to  communicate  between  devices,  controllers,  and  applicaFons  without  ge^ng  caught  up  in  the  usual  custom  driver-­‐based  connecFvity  problems.  

7  

NI  USB  TC-­‐01  Thermocouple  

Device  

GeVng  Started  with  the  NI  USB-­‐TC01  Thermocouple  device    

Once  the  device  has  been  successfully  detected,  the  NI  USB-­‐TC01  Launch  Screen  will  open  (as  shown  in  Figure  below),  or  a  Windows  “Autoplay”  dialog  will  appear  allowing  you  to  select  “TC01Launcher.exe”  to  run  the  sobware.  

TC01Launcher.exe  

Note!    If  “Autoplay”  does  not  start  or  it  is  disabled,  locate  the  NI  USB-­‐TC01  under  “My  Computer”  and  double-­‐click  “TC01Launcher.exe”  to  manually  run  the  sobware.  

The  NI  USB-­‐TC01  has  a  green  LED  next  to  the  USB  cable  to  indicate  the    device  status.  When  the  LED  is  illuminated,  the  device  is  powered  and    ready  for  operaFon.  If  the  LED  is  not  illuminated,  the  device  is  not  powered    or  did  not  iniFalize.  

8  

NI  USB-­‐TC01  Thermocouple  Measurement  Device  –  Temperature  Logger  

Run  the  Temperature  Logger  to  check  if  the  device  works  as  expected  before  you  start  on  your  LabVIEW  Programming   9  

Open  the    Log  File  to  see  the  data  

How-­‐To  use  the  NI  USB  TC-­‐01  Thermocouple  Device  in  LabVIEW  

10  

The  USB-­‐TC01  Device  works  similar  as  the  USB-­‐6008  DAQ  device,  i.e.,  you  use  the  DAQ  Assistant  in  LabVIEW  

NI  USB-­‐TC01  Thermocouple  Measurement  Device  using  LabVIEW  

The  USB-­‐TC01  Device  works  similar  as  the  USB-­‐6008  DAQ  device,  i.e.,  you  use  the  DAQ  Assistant  

11  

DAQ  Assistant  for  NI  USB-­‐TC01  

12  

1

2

3

4

5

Acquire  Signals  »  Analog  Input  »  Temperature  »  Thermocouple  

Select  Physical  Channel  ai0  

Use  default  ProperFes  

Click  OK  to  Finish  

Temperature  Simulaaon  

13  

Since  we  don’t  have  a  TC01  device  per  student,  or  if  you  want  to  further  develop  your  program  at  home  (without  TC01  ),  you  can  create  a  simple  “Temperature  Simulator”  

A  simple  SubVI  that  simulates  a  Temperature  value  using  a  Random  Generator  

In  this  way  you  can  easily  switch  between  the  real  Temperature  sensor  (TC01)  and  the  Simulator      

Case  Structure  

Case  Structure  

While  Loop  

While  Loop  

14  

OPC  

Matrikon  OPC  Simulaaon  Server  

Visual  Studio  with  Measurement  Studio  

OPC  Server  

OPC  Client  

OPC  DA  

Matrikon  OPC  Explorer  

COM/DCOM  

We  will  use  the  Matrikon  OPC  SimulaFon  Server  –  which  is  a  free  of  charge  OPC  Server  for  test  purposes.    

15  

Note!  Normally,  the  OPC  Server  and  the  Clients  are  not  on  the  same  computer,  but  in  this  assignment  we  will  for  simplicity  have  everything  on  the  same  computer.  

OPC  Client  

OPC  Client  

COM/DCOM  COM/DCOM  

…  

16  

Visual  Studio  +  Measurement  Studio  Select    “New  Project”    in  Visual  Studio:  

Make  sure  to  select  the  DataSocket  Library  

Select  the  “NI  Windows  Applicaaon”  Template  

17  

1

2

3

4

5

76

8

Measurement  Studio  is  an  “Add-­‐in”  for  Visual  Studio  created  by  NaFonal  Instruments.  

Visual  Studio  +  Measurement  Studio  Example:  

18  

Read  from  OPC  Server  using  Visual  Studio  

string  opcUrl;  double  opcValue;      opcUrl  =  "opc://localhost/Matrikon.OPC.SimulaFon/Bucket  Brigade.Real4";      DataSocket  dataSocket  =  new  DataSocket();                              if  (dataSocket.IsConnected)  

 dataSocket.Disconnect();      dataSocket.Connect(opcUrl,  AccessMode.Read);  dataSocket.Update();    opcValue  =  Convert.ToDouble(dataSocket.Data.Value);    

Make  sure  you  have  Measurement  Studio  Installed!!  

Note!  This  Code  Snippet  reads  only  one  value  once,  you  can  use  e.g.  a  Timer  in  order  to  read  values  at  specific  intervals.  

Example:  

19  

Timer  

public  Form1()                  {                          IniFalizeComponent();                              amer1.Start();                  }  

private  void  amer1_Tick(object  sender,  EventArgs  e)                  {                                                  …  //Read  from  OPC                        …  //Scaling                        …  //Plot  Data                      }  

Timer  Event:  

Iniaalizaaon:  

Properaes:  

Structure  your  Code  properly!!  Define  Classes  and  Methods  which  you  can  use  here  (see  example  on  next  page)  

In  Visual  Studio  you  may  want  to  use  a  Timer  instead  of  a  While  Loop  in  order  to  read  values  at  specific  intervals.  

You  may  specify  the  Timer  Interval  in  the  ProperFes  Window  

Select  the  “Timer”  component  in  the  Toolbox  

Double-­‐click  on  the  Timer  object  in  order  to  create  the  Event  

2

3

4

1

20  

OOP:  Classes,  Fields  and  Methods  

21  

class  Car  //Class  {    

public  string  color;  //Field    //Method  public  void  ShowCarColor()  {            MessageBox.Show("My  Car  is:  "  +  color);  }  

 }   Car  myCar  =  new  Car();  //We  create  an  Instance  of  the  Class  

 myCar.color  =  "blue";  //We  set  a  value  for  the  color  Field    myCar.ShowCarColor();  //We  call  the  Method    

1

2

3

Example:  

OOP:  Object  Oriented  Programming  

Addiaonal  Resources  

•  NI  USB-­‐TC01  Thermocouple  Measurement  Device  

•  Data  AcquisiFon  in  LabVIEW    •  OPC  and  Real-­‐Time  Systems  in  LabVIEW  •  IntroducFon  to  Visual  Studio  and  C#  •  Data  AcquisiFon  in  C#    

22  h*p://home.hit.no/~hansha/  The  resources  are  located  here:  

Hans-­‐Pe*er  Halvorsen,  M.Sc.  Telemark  University  College  Faculty  of  Technology  Department  of  Electrical  Engineering,  Informaaon  Technology  and  Cyberneacs  

   E-­‐mail:  [email protected]  Blog:  h*p://home.hit.no/~hansha/    

23