Siemens Tutorial
Tutorial to transform Siemens UDT to JSON
This tutorial has been tested against the Siemens Simatic 1507 PLC version 2.7.0, being programmed and controlled with TIA Portal version 16.
Ensure the FairCom server is installed and running.
Confirm server access by running the FairCom API Explorer. The typical URL is
https://localhost:8443/.
Siemens UDT to JSON transform tutorials
Export the schema that pertains to the user-defined data type that will be serialized and sent to FairCom Edge using your Siemens development environment TIA Portal.
Navigate to PLC data types.
Right-click the desired data type.
Select Generate source from blocks/including all dependent blocks to save the
.UDTfile to disk.Copy the
.UDTfile to the machine that you are performing your FairCom development on.Observe the
.UDTfile contents.Example 1..UDTfile contentsTYPE "Machine_Status"
TITLE = Realtime status of stamper 1
VERSION : 0.1
STRUCT
XPos : Real;
YPos : Real;
StatusCode : Int;
Info : String[54];
CycleID : Array[1.."Cycle_ID_Array_Length"] of Char;
END_STRUCT;
END_TYPE
Make a note of the root
TYPEname — for example,"Machine_Status"in Example 1, “.UDTfile contents”, you will need this rootTYPEname to Create the Siemens UDT-to-JSON transform.Note
If your UDT has multiple nested data structures, use the root TYPE name of the parent structure that nests all other structures. Often it is the last structure listed in the
.UDTfile.Prepare the UDT for insertion into JSON, using the following conversions so that the
.UDTfile can be passed to FairCom Edge inside a JSON document.Replace any array-length variables with their actual integer values.
Example 2. Replace array-length variables with actual integer valuesCycleID : Array[1.."Cycle_ID_Array_Length"] of Char;
to
CycleID : Array[1..30] of Char;
Replace double-quotes/quotation marks with single quotes/apostrophes.
Replace End-Of-Line control chars (
\r\nin Windows) with the following 2-character string:\n
Example 3. ConversionThe listed conversions in Step 3 convert the
.UDTfile contents in Example 1, “.UDTfile contents” to:TYPE 'Machine_Status'\nTITLE = Realtime status of stamper 1\nVERSION : 0.1\nSTRUCT\n XPos : Real;\n YPos : Real;\n StatusCode : Int;\n Info : String[54];\n CycleID : Array[1..30] of Char;\nEND_STRUCT;\nEND_TYPE\n
The project running in your PLC must be configured to connect to the FairCom Edge MQTT broker and publish MQTT messages to that broker which contains a serialized version of your UDT data. How that is done depends on the PLC model and the development language and environment used to program it.
Navigate to and open the FairCom Edge
services.jsonfile in the<faircom>\configfolder.Locate the
"listeners"array in theservices.jsonfile and determine which one of the following objects matches how your PLC connects to the MQTT broker."serviceName": "mqtt1883""serviceName": "mqtts8883""serviceName": "mqttws9001""serviceName": "mqttwss9002"
Ensure that the
"enabled"property is set totruefor the relevant block.Observe the MQTT topic that the messages are being published to and how often they are being published.
Open
services.jsonfrom the<faircom>\configfolder in a text editor.Locate the
"transformServices"object and ensure the"enabled"property is set totrue."transformServices": [ { "serviceName": "siemensUdt2Json", "serviceLibrary": "siemensudtservice.dll", "enabled": true } ]Note
The
"serviceLibrary"property value will differ depending on your operating system.Save any changes.
Restart the server for the changes to take effect.
Ensure changes made to the PLC project are uploaded to the PLC.
Set the PLC to RUN mode.
Validate that the PLC is connected and publishing messages to the FairCom EdgeMQTT broker:
Open the FairCom MQ Explorer (https://localhost:8443//MQExplorer/index.html).
Select MQTT Management from the button (
).Click (
) to open the Manage Subscriptions window.Select the MQTT topic that the PLC is publishing to from the Topic dropdown menu.
Click (
).Close the Manage Subscriptions window.
Observe your messages, displayed under Incoming Messages each time the PLC publishes a message to that topic.
Note
The format of the messages is binary.
If the topic is not visible or the messages are not arriving in the topic:
Check for error messages in the
CTSTATUS.FCSfile in the FairCom Edgedatafolder.Perform any necessary debugging on the PLC side.
Open the FairCom MQ Explorer (https://localhost:8443//MQExplorer/index.html).
Select API Explorer from the Control menu (
).Replace the JSON in the API Request editor with the following JSON:
Update the
"schema"property value with the converted UDT schema noted in Conversion.Update the
"rootTypeName"property value with the root type name noted from the.UDTfile contents.
{ "api": "hub", "apiVersion": "1.0", "action": "createTransform", "params": { "transformName": "PLCData_transform", "transformActions": [ { "transformService": "siemensUdt2Json", "transformStepMethod": "siemensUdtPayloadToJson", "inputFields": [ "source_payload" ], "outputFields": [ "aValidFieldName" ], "transformParams": { "schema": "ReplaceWithConvertedUdtSchema", "rootTypeName": "ReplaceWithRootName", "udtDebugTracing": true } } ] }, "authToken": "replaceWithAuthTokenFromCreateSession" }Click Apply defaults to JSON request (
) to replace the
"authToken"value with the valid one from your session.Click Send request (
) to issue the JSON-based request.Observe the response and ensure the action completed successfully.
Note
"errorCode"with a value of0indicates success."errorCode"with a non-zero value indicates a failure. See Errors and contact FairCom for more information about an error.
Select MQ API from the Select API dropdown menu.
Select
"configureTopic"from the JSON Actions dropdown menu.Replace the JSON in the API Request editor with the following JSON:
{ "api": "mq", "apiVersion": "1.0", "action": "configureTopic", "params": { "topic": "ReplaceWithTopicName", "transformName": "PlcData_transform", "outputPayloadField": "aValidFieldName" }, "authToken": "replaceWithAuthTokenFromCreateSession" }Update the
"topic"property value with the name of the MQTT topic that the PLC is publishing to.Click Apply defaults to JSON request (
) to replace the
"authToken"value with the valid one from your session.Click Send request (
) to issue the JSON-based request.Observe the response and ensure the action completed successfully.
Note
"errorCode"with a value of0indicates success."errorCode"with a non-zero value indicates a failure. See Errors and contact FairCom for more information about an error.
Open the FairCom MQ Explorer (https://localhost:8443//MQExplorer/index.html).
Select MQ Explorer from the button (
).Observe your messages, displayed under Incoming Messages each time the PLC publishes a message to that topic.