Giter Club home page Giter Club logo

twincat.jsonextension's Introduction

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.

twincat.jsonextension's People

Contributors

dependabot[bot] avatar fbarresi avatar stefanbesler avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

twincat.jsonextension's Issues

Installation Question

How do I install the Extension, do i have to import the .cs code or add it as a library?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.