Skip to content

fbarresi/TwinCAT.JsonExtension

Repository files navigation

TwinCAT.JsonExtension

TwinCAT variables to and from json

Build status Codacy Badge codecov Licence Nuget Version

Bring the power of Json.Net to TwinCAT

Tranform DUTs decorated with the custom Json-Attribute like this:

TYPE JsonDUT :
STRUCT
	{attribute 'json' := 'message'}
	sMessage : STRING := 'test';
	iResponse : INT;
	{attribute 'json' := 'status'}
	sStatus : STRING := 'success';
	{attribute 'json' := 'numbers'}
	daNumbers : ARRAY[1..3] OF DINT := [1,2,3];
END_STRUCT
END_TYPE

into this (and back) recursively and absolutely type-independent:

{
  "message": "test",
  "status" : "success",
  "numbers" : [1,2,3]
}

only calling this two extension methods on your connected AdsClient:

var json = await client.ReadJson("GVL.JsonDutVariable")
await client.WriteJson("GVL.JsonDutVariable", json);

Options

Progress indication

For lengthy operations, a progress indiciator can be used to give some feedback about the current progress. By passing a Progress<int> object as parameter to ReadJson or WriteJson it is possible to count the total number of primitive types (INT, DINT, REAL, ...) that were read or written to the PLC, respectively.

int objects = 0;
var progress = new Progress<int>();
progress.ProgressChanged += (sender, args) => { objects++; Console.CursorLeft = 0; Console.Write(objects); };

Console.WriteLine("Primitives read from PLC");
await client.ReadJson("GVL.JsonDutVariable", progress: progress);

Console.WriteLine("\nPrimitives written to  PLC");
await client.WriteJson("GVL.JsonDutVariable", json, progress: progress);

Enumeration stringify

Values of enumerations are by default started as integer values. However, sometimes it is beneficial to store said values as strings. This can be achieved by the stringify parameter.

await client.ReadJson("GVL.JsonDutVariable", force: true, stringifyEnums: true);

Read/Write without json attribute

The attributes mentioned above are optional when using this library. The following example achieves a similar result. The only difference is the instance names in the generated json file.

TYPE JsonDUT :
STRUCT
	sMessage : STRING := 'test';
	iResponse : INT;
	sStatus : STRING := 'success';
	daNumbers : ARRAY[1..3] OF DINT := [1,2,3];
END_STRUCT
END_TYPE

yields

{
  "sMessage": "test",
  "iResponse" : 0,
  "sStatus": "success",
  "daNumbers" : [1,2,3]
}

by calling the ReadJsonAsync method on your connected AdsClient

var json = await client.ReadJson("GVL.JsonDutVariable", force: true);

Have fun using this simple package and don't forget to star this project!

Referenced projects

Would you like to see the power of TwinCAT.JsonExtension in action?

Then checkout BeckhoffHttpClient, an unofficial TwinCAT function for HTTP requests

or

TwincatAdsTool your swiss knife for twincat development.

Credits

Special thanks to JetBrains for supporting this open source project.

Releases

No releases published

Sponsor this project

 

Packages

No packages published

Languages