Advertisement
Guest User

Untitled

a guest
Oct 21st, 2019
125
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.62 KB | None | 0 0
  1. using System;
  2. using Opc.UaFx.Client;
  3.  
  4. namespace BitAPI.Controllers
  5. {
  6.  
  7.     /*
  8.         This is a copy of the demo made by Thomas (it's on Blackboard)
  9.         It has some minors changes and is called from the API
  10.     */
  11.     class OPCConnection
  12.     {
  13.         //Class-wide server object for easy accessibility
  14.         OpcClient accessPoint;
  15.  
  16.         //string serverURL = "opc.tcp://192.168.0.122:4840";  //Physical PLC
  17.         string serverURL = "opc.tcp://127.0.0.1:4840";      //Simulated PLC
  18.  
  19.  
  20.         //Main for testing
  21.         public void CallMachine()
  22.         {
  23.             OPCConnection prg = new OPCConnection();
  24.  
  25.             //The using statement of .NET automatically cleans up and automatically disposes
  26.             //of objects that implements the IDisposable interface.
  27.             using (prg.accessPoint = new OpcClient(prg.serverURL))
  28.             {
  29.                 //Connect to server
  30.                 prg.ConnectToServer();
  31.  
  32.                 //Keep looping for testing
  33.                 while (true)
  34.                 {
  35.                     prg.ReadFromServer();
  36.                     prg.WriteToServer();
  37.  
  38.                     System.Threading.Thread.Sleep(500); //Hang out for half a second (testing)
  39.                 }
  40.             }
  41.         }
  42.  
  43.  
  44.         // MAPPING OF OPC UA NODES
  45.         // Example: 'ns=6;s=::Program:Cube.Status.Parameter[3].Value'
  46.         // The node destination address can be a little tricky, but can be easily found for each variable through UaExpert.
  47.         // The 'ns=6' indicates that the variable is on NamespaceIndex 6, which all of the Beer Production variables will be.
  48.         // The 's=' following it, indicates that the value to be read is a string type ('i=' would indicate integer etc.),
  49.         // after which the actual path of the variable is defined ('::Program:Cube.Status.Parameter[3]').
  50.         // Note: some values will require specifically targeting the value ('...Status.Parameter[3].Value').
  51.         // Read further: https://documentation.unified-automation.com/uasdkhp/1.0.0/html/_l2_ua_node_ids.html
  52.         // REMEMBER TO READ THE MANUAL CAREFULLY!
  53.  
  54.         public void ReadFromServer()
  55.         {
  56.             //Read example node values
  57.             var temperature = accessPoint.ReadNode("ns=6;s=::Program:Cube.Status.Parameter[3].Value");
  58.             var movement = accessPoint.ReadNode("ns=6;s=::Program:Cube.Status.Parameter[4].Value");
  59.             var CntrlCmd = accessPoint.ReadNode("ns=6;s=::Program:Cube.Command.CntrlCmd");
  60.             var BatchId = accessPoint.ReadNode("ns=6;s=::Program:Cube.Admin.Parameter[0].ID");
  61.  
  62.             //Print values to console window
  63.             Console.WriteLine("Batch id bitches:" + BatchId);
  64.             Console.WriteLine("Temp: {0,-5} Movement: {1,-5} Batch ID: {2,-5}", temperature, movement, CntrlCmd);
  65.         }
  66.  
  67.         public void WriteToServer()
  68.         {
  69.             //Writes a random int between 1-5 to the Cube.Command.CntrlCmd node on the server.
  70.             //This is for testing purposes only, to see if the value changes correctly!
  71.             Random rnd = new Random();
  72.  
  73.             accessPoint.WriteNode("ns=6;s=::Program:Cube.Command.CntrlCmd", 1);
  74.             System.Threading.Thread.Sleep(10000);
  75.             accessPoint.WriteNode("ns=6;s=::Program:Cube.Command.CmdChangeRequest", true);
  76.            
  77.         }
  78.  
  79.         public void ConnectToServer()
  80.         {
  81.             accessPoint.Connect();
  82.         }
  83.         public void DisconnectFromServer()
  84.         {
  85.             accessPoint.Disconnect();
  86.             accessPoint.Dispose(); //Clean up in case it wasn't automatically handled
  87.         }
  88.     }
  89. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement