Skip to main content
Version: 2.7

Mazak MtConnect Connector

There exit some online MTConnect Agents providing the data model for Mazak machines.

URL: http://mtconnect.mazakcorp.com/

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

  1. HumanOS IoT Designer installed on development client (Check Installation Manual)

  2. Check the access to url http://mtconnect.mazakcorp.com:5719.

Step 1: Create a IoT Project with OPC-UA Server

  1. Create an empty IoT designer project

  2. Add a REST API connector as template to your project

Step 2: Design the WebControl Device

  1. Add the DataNode OperationMode to the device template. Use the data type System.String.

    NOTE

    The address is left blank. This data node is fed by the TaskProcessor (next step).

  2. 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

Step 3: Write C# Script

Next step is to write the processing script.

  1. Select the HumanOS.UHAL.WebControl plugin and click [+] button on the project view (left)

    • Add a Blank Http Stream Script. File name is MTConnectPayloadProcessor.cs

    NOTE

    This 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.

  2. 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)
    {

    }
    }
    }
  3. Reading from http source.

    • current is the sub url from the target MTConnect agent to get current values.
    • GET is the HTTP method
    • text/xml is the expected content type which is XML for MTConnect
    THttpResponse Response = DataStream.request("current", "GET", "", "text/xml", new Dictionary<string, string>());
  4. 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");
  5. 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);
    NOTE

    Use 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

  1. Create a new device from the device template.

    Select the Devices node of the default target and click [+] button.

  2. Deploy and run HumanOS IoT Gateway

  3. Start the UAExpert and connect to the gateway opc.tcp://localhost:4840. Check the value of the OperationMode.

Questions

  1. 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.


  2. What do you have to do to read the Part Counter (dataItemId = PartCountAct?

    Answer

    1. Find the data item in the xml stream. It is an integer.
    2. Add a new data node to the device template. Set the data type to "System.Int32"
    3. Open the script. Add a line to search for the data node.
    4. Query the value and convert the value to an integer.
    5. Set the integer value to the data node.


  3. 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.