Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using Opc.UaFx.Client;
- namespace BitAPI.Controllers
- {
- /*
- This is a copy of the demo made by Thomas (it's on Blackboard)
- It has some minors changes and is called from the API
- */
- class OPCConnection
- {
- //Class-wide server object for easy accessibility
- OpcClient accessPoint;
- //string serverURL = "opc.tcp://192.168.0.122:4840"; //Physical PLC
- string serverURL = "opc.tcp://127.0.0.1:4840"; //Simulated PLC
- //Main for testing
- public void CallMachine()
- {
- OPCConnection prg = new OPCConnection();
- //The using statement of .NET automatically cleans up and automatically disposes
- //of objects that implements the IDisposable interface.
- using (prg.accessPoint = new OpcClient(prg.serverURL))
- {
- //Connect to server
- prg.ConnectToServer();
- //Keep looping for testing
- while (true)
- {
- prg.ReadFromServer();
- prg.WriteToServer();
- System.Threading.Thread.Sleep(500); //Hang out for half a second (testing)
- }
- }
- }
- // MAPPING OF OPC UA NODES
- // Example: 'ns=6;s=::Program:Cube.Status.Parameter[3].Value'
- // The node destination address can be a little tricky, but can be easily found for each variable through UaExpert.
- // The 'ns=6' indicates that the variable is on NamespaceIndex 6, which all of the Beer Production variables will be.
- // The 's=' following it, indicates that the value to be read is a string type ('i=' would indicate integer etc.),
- // after which the actual path of the variable is defined ('::Program:Cube.Status.Parameter[3]').
- // Note: some values will require specifically targeting the value ('...Status.Parameter[3].Value').
- // Read further: https://documentation.unified-automation.com/uasdkhp/1.0.0/html/_l2_ua_node_ids.html
- // REMEMBER TO READ THE MANUAL CAREFULLY!
- public void ReadFromServer()
- {
- //Read example node values
- var temperature = accessPoint.ReadNode("ns=6;s=::Program:Cube.Status.Parameter[3].Value");
- var movement = accessPoint.ReadNode("ns=6;s=::Program:Cube.Status.Parameter[4].Value");
- var CntrlCmd = accessPoint.ReadNode("ns=6;s=::Program:Cube.Command.CntrlCmd");
- var BatchId = accessPoint.ReadNode("ns=6;s=::Program:Cube.Admin.Parameter[0].ID");
- //Print values to console window
- Console.WriteLine("Batch id bitches:" + BatchId);
- Console.WriteLine("Temp: {0,-5} Movement: {1,-5} Batch ID: {2,-5}", temperature, movement, CntrlCmd);
- }
- public void WriteToServer()
- {
- //Writes a random int between 1-5 to the Cube.Command.CntrlCmd node on the server.
- //This is for testing purposes only, to see if the value changes correctly!
- Random rnd = new Random();
- accessPoint.WriteNode("ns=6;s=::Program:Cube.Command.CntrlCmd", 1);
- System.Threading.Thread.Sleep(10000);
- accessPoint.WriteNode("ns=6;s=::Program:Cube.Command.CmdChangeRequest", true);
- }
- public void ConnectToServer()
- {
- accessPoint.Connect();
- }
- public void DisconnectFromServer()
- {
- accessPoint.Disconnect();
- accessPoint.Dispose(); //Clean up in case it wasn't automatically handled
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement