Save Multiple Register Values to a CSV File
You can use Flows to save multiple register values to a CSV file on your Edge device.
In an industrial environment, engineers must keep a close eye on the performance of different machines in a factory. To achieve this, they collect raw data at regular intervals from various Programmable Logic Controller (PLC) registers. This data can be aggregated, saved in a file, and used by other applications using Flows. Even if the collection intervals differ, the data can be conveniently accessed using global variables. By analyzing this data, engineers can make informed decisions to improve the productivity of the factory.
Follow the steps to Connect a Device and configure the following parameters:
- Device Type: Simulator
- Driver Name: Generator
- Enable Alias Topics: Select the checkbox.
After connecting the device, Add Tags the device. Each tag will collect data from different types of inputs.
- Name: Select S - Random value generator
- Value Type: Select int64
- Polling Interval: Enter 5
- Tag Name: Enter input1
- Name: Select S - Random value generator
- Value Type: Select float64
- Polling Interval: Enter 5
- Tag Name: Enter input2
Click the copy icon to copy the raw topic of both tags you created. Store them securely as you will refer to them later when configuring the flows.
You can use multiple input nodes in a flow to save values in a database or save values to a CSV file.
To save values from multiple registers to a CSV file:
Note: A similar method could be used to save values in a MySQL database.
- In Manufacturing Connect Edge, navigate to the Flows Manager and create a new flow. See Create a Flow to learn more.
Drag two Function nodes and two Datahub Subscribe nodes to the canvas and wire them together.
- Double-click one of the Function nodes. The Edit function node dialog box appears.
- On Message: Add the following lines to save the value to a Global Variable (output1), which can be accessed by other flows, nodes, and functions: var temp = (JSON.parse(msg.payload)).value; global.set("output1",temp); return msg;
Click Done.
- Double-click the other Function node. The Edit function node dialog box appears.
- On Message: Enter the following lines of code. var temp = (JSON.parse(msg.payload)).value; global.set("output2",temp); return msg;
Click Done.
- Double-click one of the DataHub Subscribe nodes. The Edit Datahub Subscribe node dialog box appears.
- Datahub: Configure the Datahub connection if needed. See the Step 3: Configure Connector Nodes section in Create a Flow to learn more.
- Topic: Paste the topic for input1 tag.
- Name: Enter Input1.
Click Done.
Repeat the last step for the other DataHub Subscribe node and paste the topic for input2 tag.
On the same canvas, drag an inject node, a Function node, and a Write file node and wire them together as shown.
- Double-click the inject node. The Edit inject node dialog box appears.
- Repeat: Select an interval for every 10 seconds to read the global variable values.
Click Done.
- Double-click the Function node. The Edit function node dialog box appears.
- On Message: Enter the following lines of code to set up the output for the CSV file. //create CSV var temp = global.get("output1") + "," + global.get("output2"); msg.payload = temp; return msg;
Click Done.
- Double-click the Write file node. The Edit write file node dialog box appears.
- Filename: Enter a filename prefaced with a forward slash ( / ).
- The forward slash is required in the filename. Example: /ftp-data/sample.csv
- The file is created on the Manufacturing Connect Edge device.
- If a subdirectory already exists, you can supply the full path to the file. Example: /ftp-data/sampleor /logfiles/sample.
Action: Select append to file from the drop-down menu and click Done.
- Click Deploy to save the flow.
Drag the Sidebar up from the bottom of the canvas and select the Debug icon.
- Click the button on the left side of the inject (timestamp) node to inject the current timestamp into the write file node, which creates the CSV file locally if it does not already exist. In this example, the /ftp-data/sample.csv file is saved.
To read the output of the CSV file, refer to Create, Write to, and Read from a Log File.
Note: Change the name of the file with the CSV file you created to save multiple register values.
The CSV file holds the current register values from the two inputs and updates them in real-time.