Mazak MtConnect Connector
There exit some online MTConnect Agents providing the data model for Mazak machines.
This tutorial shows how to create your own connector for MTConnect based machine protocols. The tutorial can be easily modified for other web-control based protocols.
Preconditions
-
HumanOS IoT Designer installed on development client (Check Installation Manual)
-
Check the access to url
http://mtconnect.mazakcorp.com:5719
.
Step 1: Create a IoT Project with OPC-UA Server
-
Create an empty IoT designer project
-
Add a REST API connector as template to your project
Step 2: Design the WebControl Device
-
Add the DataNode
OperationMode
to the device template. Use the data typeSystem.String
.NOTEThe address is left blank. This data node is fed by the TaskProcessor (next step).
-
Add the task processor to the device template.
- Select the
MazakConnector
node and click [+] button to add the task processor
- Set the ScriptFile setting to
MTConnectPayloadProcessor.cs
- Select the
Step 3: Write C# Script
Next step is to write the processing script.
-
Select the
HumanOS.UHAL.WebControl
plugin and click [+] button on the project view (left)- Add a
Blank Http Stream Script
. File name isMTConnectPayloadProcessor.cs
NOTEThis script is called cyclic by the Task Processor. It reads data from the Mazak MTConnect Agent and processes the payload and sets the extracted data to the data nodes.
- Add a
-
Open the script file and change the class name to
MTConnectPayloadProcessor
.using HumanOS.Kernel;
using HumanOS.Kernel.Communication;
using HumanOS.Kernel.UHAL.InfoModel;
using HumanOS.Kernel.UHAL.Script;
namespace HumanOS.IoT.Designer.Library.Scripts
{
/// <summary>
/// Example to handle http streams
/// </summary>
public class MTConnectPayloadProcessor : TAbstractHttpStreamScriptObject
{
///<see cref="TAbstractHttpStreamScriptObject"/>
public override void handleStream(IKernelAccess Kernel, TDeviceSchemaInfo DeviceInfo, IHttpStream DataStream)
{
}
}
} -
Reading from http source.
current
is the sub url from the target MTConnect agent to get current values.GET
is the HTTP methodtext/xml
is the expected content type which is XML for MTConnect
THttpResponse Response = DataStream.request("current", "GET", "", "text/xml", new Dictionary<string, string>());
-
Especially for MTConnect, make sure the correct namespace is set before parsing the XML file
In our example we use
urn:mtconnect.org:MTConnectStreams:2.0
.XDocument Doc = XDocument.Parse(Response.Content);
XmlNamespaceManager Manager = new XmlNamespaceManager(Doc.CreateReader().NameTable);
Manager.AddNamespace("m", "urn:mtconnect.org:MTConnectStreams:2.0"); -
Get the correct data node and set the parsed value
The best way is to select first data node by name out of you device information model, and then use the node id to query the data node from the node space.
IDataNode<string> Node1 = Kernel.NodeSpace.getNode<IDataNode<string>>(DeviceInfo.DataNodes.First(n => n.Name == "OperationMode").Id);
Node1.passValue(Doc.XPathSelectElement("//m:*[@dataItemId='mode']", Manager)?.Value, false);NOTEUse the
dataItemId
to query the data in the MTConnect XML. To do so, the recursive query syntax is used://m:*[@dataItemId='mode']
.
The complete C# script file is:
using CyberTech.Diagnostics;
using HumanOS.Kernel;
using HumanOS.Kernel.Communication;
using HumanOS.Kernel.Communication.Http;
using HumanOS.Kernel.DataModel;
using HumanOS.Kernel.UHAL.Device;
using HumanOS.Kernel.UHAL.InfoModel;
using HumanOS.Kernel.UHAL.Script;
using HumanOS.Kernel.Utils;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Xml;
using System.Xml.Linq;
using System.Xml.XPath;
namespace HumanOS.IoT.Designer.Library.Scripts
{
/// <summary>
/// Example to handle http streams
/// </summary>
public class MTConnectPayloadProcessor : TAbstractHttpStreamScriptObject
{
///<see cref="TAbstractHttpStreamScriptObject"/>
public override void handleStream(IKernelAccess Kernel, TDeviceSchemaInfo DeviceInfo, IHttpStream DataStream)
{
THttpResponse Response = DataStream.request("current", "GET", "", "text/xml", new Dictionary<string, string>());
try
{
IDataNode<string> Node1 = Kernel.NodeSpace.getNode<IDataNode<string>>(DeviceInfo.DataNodes.First(n => n.Name == "OperationMode").Id);
//TLogger.writeInfo(Response.Content);
XDocument Doc = XDocument.Parse(Response.Content);
XmlNamespaceManager Manager = new XmlNamespaceManager(Doc.CreateReader().NameTable);
Manager.AddNamespace("m", "urn:mtconnect.org:MTConnectStreams:2.0");
//Set the Operation Mode as String
Node1.passValue(Doc.XPathSelectElement("//m:*[@dataItemId='mode']", Manager)?.Value, false);
}
catch (Exception Exc)
{
Logger.writeError($"Failed to read data. {Exc.Message}");
}
}
}
}
Step 4: Connect to the Mazak
-
Create a new device from the device template.
Select the
Devices
node of the default target and click [+] button. -
Deploy and run HumanOS IoT Gateway
-
Start the UAExpert and connect to the gateway
opc.tcp://localhost:4840
. Check the value of the OperationMode.
Questions
-
Where do you find the correct namespace in a MTConnect payload?
Answer
First open the URL http://mtconnect.mazakcorp.com:5719/current in your browser. Then check the first line of the XML stream. Here the namespace 'urn:mtconnect.org:MTConnectStreams:2.0' is set.
-
What do you have to do to read the Part Counter (dataItemId =
PartCountAct
?Answer
- Find the data item in the xml stream. It is an integer.
- Add a new data node to the device template. Set the data type to "System.Int32"
- Open the script. Add a line to search for the data node.
- Query the value and convert the value to an integer.
- Set the integer value to the data node.
-
What happens if you read a non-existing dataItemId?
Answer
The value null is returned. The null value must be handled, especially if you want to convert the data to another data type.