Simple HEX format conversion to Numeric (Introduction article to the Program Object)

The barebones of using a Program Object to execute a data conversion.

Working with MQTT, it is even more frequent to get data in JSON payloads.

The new JSON Toolkit allows to filter the data or repackage it so it can be used on Niagara as standard points (Numeric, Boolean, Enumeric, String).

Some IoT devices though might send you data in "raw" format, all packaged within a JSON payload.

 

In a specific case, I had a payload with a few information to filter. One being my sensor data.

The sensor data was packaged in 6 bytes which were sent as HEX.

For example, the "data" part of my JSON would return "0221CE002D02" (02 21 CE 00 2D 02 separated in bytes). The part "CE 00" would be a Temperature, with swapped bytes (so "00CE" in HEX).

All of this is manageable with the standard blocks, JSON Toolkit to extract the JSON payload part I need, and then work with "String" manipulation objects available in "kitControl - String".

But then, I realized there isn't any available block to do the HEX to Numeric conversion...

 

If you are a developer, you will develop your own block on a personal library. But what if you are not a developer and cannot find any module from third party?

Well, you can easily use a "Program" object. The coding necessary for the conversion is minimal (like 1 row!) so it is a lightweight method to process such data.

I can start by dragging the Program object to a wire sheet

 

Double click it and change view to "Program Editor" (on the top right of the Main View Panel).

The tab "Edit" will allow me to write some code.

Use the following code (paste it over the existing one)

public void onStart() throws Exception
{
// start up code here
}

public void onExecute()
{
// in slot called 'in' of type BString
// out slot called 'out' of type BInteger
// convert in and set out...
setOut(Integer.parseInt(getIn(), 16));
}

public void onStop() throws Exception
{
// shutdown code here
}

 

Note that the "16" part in the code line is the "radix" to be used while parsing. It is "16" now as we are parsing from HEX to DEC. You can use the same code and change the radix to convert from other formats such as binary for example.

 

Then change tab to "Slots" and add 2 slots. Right click on the slots window and select "Add slot".

Set it as follow

("Execute on Change" will make sure that the code is executed when your input pin value changes. "Summary" is used to make the pin visible on the block shape within the wire sheet)

 

Add another slot, and set it as follow

("Readonly" makes the pin an output, which is read only from the wire sheet. "Summary" is used to make the pin visible on the block shape within the wire sheet)

 

Press now the "Compile" icon on the toolbar (number "1" in the image below). The program should compile successfully (number "2" showing the green dot)

 

Note: the "program is not signed" warning is there because by default Niagara doesn't have any certificate to use to sign your code. Another article will describe the process of getting your workbench ready to sign your code and pass the certificate to your hosts

 

Go back to the wire sheet where you added the program object and give it a meaningful name.

This is what I came up with. The program is block "HexToInt"

 

What if I want to make a conversion from DEC to HEX?

You can use a similar method to the above. The Java program line would obviously change.

The code below would work:

public void onStart() throws Exception
{
// start up code here
}

public void onExecute()
{
// in slot called 'in' of type BInteger
// out slot called 'out' of type BString
// convert in and set out...
setOut(Integer.toHexString(getIn()));
}

public void onStop() throws Exception
{
// shutdown code here
}

Remember to add the "In" and the "Out" slots as described on the steps above, but this time the "In" slot will be of type "baja integer" and the "Out" slot will be of type "baja string".