• Home
  • Raspberry Pi
  • Codesys eCockpit
  • Python
  • Docker
  • Embedded Programming
Brighter Side Tech
  • Home
  • Raspberry Pi
  • Codesys eCockpit
  • Python
  • Docker
  • Embedded Programming
No Result
View All Result
  • Home
  • Raspberry Pi
  • Codesys eCockpit
  • Python
  • Docker
  • Embedded Programming
No Result
View All Result
Brighter Side Tech
No Result
View All Result
Home Automation

Parsing JSON Data in Codesys

Mwesigwa Peter by Mwesigwa Peter
January 25, 2025
in Automation, Codesys eCockpit
0
Parsing JSON Data in Codesys

Parsing JSON Data in Codesys

0
SHARES
98
VIEWS
Share on FacebookShare on Twitter

Parsing JSON Data in Codesys is quite an important operation especially when exchanging data over the REST API. The data sent over the REST API is usually sent in JSON format. This means that data is packaged(serialized) in a JSON string at the source and sent to the requesting party which must then deserialize(unpackage) the data to extract the information.

The PRO JSON library

In Codesys, JSON data can de parsed or deserialized using the PRO JSON library developed by tvm that can be downloaded from the Codesys Forge website. To extract information from a JSON string, we use the JSON_TO_STRUCT function block which is part of the library. The function takes in as its inputs, the JSON string to deserialize, the size of the string and a structure of variables of type JSONVAR

JSONVARs

A JSONVAR represents each data value in the JSON string. So during the parsing process, each data value will be extracted and stored in a variable of type JSONVAR. Now, depending on the type of the value, a special property representing the value type will be called in order to access the value

The JSON_TO_STRUCT function block

The main input parameters of the function bock are:

  • Execute – Triggers the deserialisation process
  • JSONString – Pointer to JSON String to be deserialized
  • JSONVars – Pointer to Structure of JSONVARs each reperesenting each value in the JSON string

When the String has successfully been deserialized, the output Done parameter will be set to TRUE

Example

String to deserialize

{
    "temp": 22.1531,
    "humid": 39,
    "running": false,
    "name": "Motor 1"
}

Create Structure of JSONVARs

As you can clearly see from the JSON string, we have 4 parameters. So we should create a Structure of JSONVARs with 4 variables each representing each parameter in the JSON string

TYPE MOTOR :
STRUCT
	temp : JSONVAR;
	humid : JSONVAR;
	running : JSONVAR;
	name : JSONVAR;
END_STRUCT
END_TYPE

Declare variables

In a new program unit, we now create all the variables and Function Block Instances we need for this action

PROGRAM PLC_PRG
VAR
	ParseJSON : JSON_TO_STRUCT; // FB Instance
	sJsonString : STRING(1000); //json string
	MOTOR_DATA : MOTOR; // struct of json vars (humid, temp running, name)

	// data variable
	temp : REAL;
	humid : DINT;
	running : BOOL;

END_VAR

Define Function block instance parameters and access deserialized data

ParseJSON(Execute :=,
		JSONString:= ADR(sJsonString),
		JSONStringSize := SIZEOF(sJsonString),
		JSONVars := ADR(MOTOR_DATA_LIST),
		NumberOfVars := SIZEOF(MOTOR_DATA_LIST) / SIZEOF(JSONVAR)
		);


IF ParseJSON.Done THEN
		temp := MOTOR_DATA.temp.Number;
		humid := MOTOR_DATA.humid.Integer;
		running := MOTOR_DATA.running.Boolean;
END_IF

As you can see, we have created a Function Block instance of type JSON_TO_STRUCT called ParseJSON and we have defined the instance parameters as required. To start the process of parsing the JSON string, we set the Execute parameter to TRUE and wait unitil the Done parameter is set.

We can then access the data by accessing the various properties representing each value type, namely, Number for floats/ Real, Integer for int and Boolean for Booleans.

Previous Post

Configure Raspberry Pi Serial ports as COM ports for Codesys Runtime

Mwesigwa Peter

Mwesigwa Peter

Related Posts

Configure Raspberry Pi Serial ports as COM ports for Codesys Runtime
Automation

Configure Raspberry Pi Serial ports as COM ports for Codesys Runtime

Introduction Codesys Runtime for Linux ARM SL allows you to program your Raspberry Pi according t the IEC 61131-6 standard...

by Mwesigwa Peter
October 7, 2024
TCP Socket Client Implementation in Codesys
Codesys eCockpit

TCP Socket Client Implementation in Codesys

This is a TCP socket client implementation in codesys to run on your PLC and connect to any TCP Socket...

by Mwesigwa Peter
January 11, 2024

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Parsing JSON Data in Codesys
Automation

Parsing JSON Data in Codesys

by Mwesigwa Peter
January 25, 2025
0

Parsing JSON Data in Codesys is quite an important operation especially when exchanging data over the REST API. The data...

Read moreDetails
Configure Raspberry Pi Serial ports as COM ports for Codesys Runtime

Configure Raspberry Pi Serial ports as COM ports for Codesys Runtime

October 7, 2024
ARM error: no member named ‘IP’ in ‘NVIC_Type’ Solved

ARM error: no member named ‘IP’ in ‘NVIC_Type’ Solved

July 18, 2024
TCP Socket Client Implementation in Codesys

TCP Socket Client Implementation in Codesys

January 11, 2024
Modbus TCP Master Simulator

Modbus TCP Master Simulator Tool – Free Download

October 14, 2023
Parsing JSON Data in Codesys

Parsing JSON Data in Codesys

by Mwesigwa Peter
January 25, 2025
0

Parsing JSON Data in Codesys is quite an important operation especially when exchanging data over the REST API. The data...

Configure Raspberry Pi Serial ports as COM ports for Codesys Runtime

Configure Raspberry Pi Serial ports as COM ports for Codesys Runtime

by Mwesigwa Peter
October 7, 2024
0

Introduction Codesys Runtime for Linux ARM SL allows you to program your Raspberry Pi according t the IEC 61131-6 standard...

ARM error: no member named ‘IP’ in ‘NVIC_Type’ Solved

ARM error: no member named ‘IP’ in ‘NVIC_Type’ Solved

by Mwesigwa Peter
July 18, 2024
0

This is how to solve the error: no member named 'IP' in 'NVIC_Type' Open the misc.c file : AppData\Local\Arm\Packs\Keil\STM32F1xx_DFP\2.4.1\Device\StdPeriph_Driver\src\misc.c, if you are...

TCP Socket Client Implementation in Codesys

TCP Socket Client Implementation in Codesys

by Mwesigwa Peter
January 11, 2024
0

This is a TCP socket client implementation in codesys to run on your PLC and connect to any TCP Socket...

About Brighterside Tech

Brighter Side Tech

As a hobbyist Engineer and Programmer , i love to share my learnings and solutions to technical challenges that i face during work and studying. My website gives me the ability to express my ideas and such solutions to the wider audience

Contact: admin@brightersidetech.com

Browse by Category

  • Automation
  • Codesys eCockpit
  • Docker
  • Embedded Programming
  • Python
  • Raspberry Pi
https://youtu.be/Lkrma5f60rs

Recent News

Parsing JSON Data in Codesys

Parsing JSON Data in Codesys

January 25, 2025
Configure Raspberry Pi Serial ports as COM ports for Codesys Runtime

Configure Raspberry Pi Serial ports as COM ports for Codesys Runtime

October 7, 2024
ARM error: no member named ‘IP’ in ‘NVIC_Type’ Solved

ARM error: no member named ‘IP’ in ‘NVIC_Type’ Solved

July 18, 2024
  • Home
  • Raspberry Pi
  • Codesys eCockpit
  • Python
  • Docker
  • Embedded Programming

© 2024 Brighterside Tech.

No Result
View All Result
  • Home
  • Raspberry Pi
  • Codesys eCockpit
  • Python
  • Docker
  • Embedded Programming

© 2024 Brighterside Tech.