Skip to content

Update to SDK 4.0 #21

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jul 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "Debug live on PLC",
"type": "plc-debug",
"request": "launch",
"program": "${workspaceFolder}",
"ip": "192.168.0.1"
}
]
}
52 changes: 31 additions & 21 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# JSON Application Example

## Description

This application example demonstrates the usage of the [JSON Library](https://github.com/simatic-ax/json)

This application example consists of two parts.
Expand All @@ -11,46 +12,49 @@ This application example consists of two parts.
## Install the application example and open it locally

1. Open a command line interface (CLI) and switch to the target folder like:

```sh
D:
cd \Examples
```

1. Install the application example `AX Code`

Run the following commands in a CLI

```sh
apax create @simatic-ax/ae-json-library --registry https://npm.pkg.github.com ae-json-library
axcode ae-json-library
```

### Description of the PLC program

The PLC program executes different steps:

1. CreateDocument1
1. CreateDocument1
Add an JSON element of type Integer with value `1234` in a empty JSON document `doc`

1. SerializeDocument1

doc.Serialize(doc.buffer) serializes the JSON document `doc` into the array of char (doc.buffer).

Expected result in the array of char: `{"myInt": 1234}`

In this step, the time for serializing will be measured and stored in the variable `TimeCreationDocument1`. The result od serializing will be stored in the string variable `SerializedDocument1`

1. ParseDocument1

Parse the JSON document `doc` for the `key = myInt` and get the value `1234`

1. ResetJSonDocument1

Delete all JSON elements in the JSON document `doc`. So it's possible to create a new JSON document with other JSON elements.

1. CreateDocument2

Create a JSON document with nested JSON elements. Structure of nested JSON document:
```

```cli
doc
|
+- myBoolean(value = true)
Expand All @@ -60,20 +64,18 @@ This application example consists of two parts.
+- myDint(value = 12345678)
```


1. SerializeDocument2

doc.Serialize(doc.buffer) serializes the JSON document `doc` into the array of char (doc.buffer).

Expected result in the array of char: `{"myBoolean": true, "myObject": {"myInt": 1234, "myDint": 12345678}}`

In this step, the time for serializing will be measured and stored in the variable `TimeCreationDocument2`. The result od serializing will be stored in the string variable `SerializedDocument2`


1. ParseDocument2

Parse the JSON document `doc` for the `key = myDint` and get the value `12345678`

1. ResetJSonDocument2

Delete all JSON elements in the JSON document `doc`. So it's possible to create a new JSON document with other JSON elements.
Expand All @@ -83,40 +85,48 @@ This application example consists of two parts.
1. Start a PLCSIM Advanced Instance or using a 1500 PLC. Download a TIA Portal created HWCN with a 1500 standard PLC (Default IP Address 192.168.0.1).

> The default ip address can be changed in the apax.yml
>
> ```yml
> IP_ADDRESS: "192.168.0.1"
> ```


1. If not open, open a terminal in AX Code (`CTRL+SHIFT+ö`)

1. Start the application example
1. Start the application example

```cli
apax start [1500]
```

> `1500` is required, if you use a real 1500 PLC instead of PLCSIM Advanced.

Result:

After the Download you'll see following conten in the terminal. Exit by pressing `x`.

![](docs/monoutput.png)
![/monoutput](docs/monoutput.png)

1. Monitor voa mon-File:
![](docs/monuioutput.png)

![monuioutput](docs/monuioutput.png)

1. Open the file `default.mon`
1. Enable monitoring
1. Observe the values


## Contribution

Thanks for your interest in contributing. Anybody is free to report bugs, unclear documentation, and other problems regarding this repository in the Issues section or, even better, is free to propose any changes to this repository using pull requests.

### Markdownlint-cli

This workspace will be checked by the [markdownlint-cli](https://github.com/igorshubovych/markdownlint-cli) (there is also documented ho to install the tool) tool in the CI workflow automatically.
To avoid, that the CI workflow fails because of the markdown linter, you can check all markdown files locally by running the markdownlint with:

```sh
markdownlint **/*.md --fix
```

## License and Legal information

Please read the [Legal information](LICENSE.md)
Please read the [Legal information](LICENSE.md)
10 changes: 5 additions & 5 deletions apax.yml
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,11 @@ scripts:
fi
apax watch
dependencies:
"@ax/system-strings": 3.0.4
"@simatic-ax/json": 2.0.1
"@ax/simatic-1500-clocks": 2.0.1
"@ax/system-strings": 5.0.12
"@simatic-ax/json": 4.1.3
"@ax/simatic-1500-clocks": 4.0.1
devDependencies:
"@ax/sdk": 2.0.4
"@simatic-ax/snippetscollection": 0.0.14
"@ax/sdk": 4.0.3
"@simatic-ax/snippetscollection": 0.1.3
registries:
'@simatic-ax': 'https://npm.pkg.github.com/'
3 changes: 0 additions & 3 deletions changelog.md

This file was deleted.

4 changes: 2 additions & 2 deletions src/MyProgram.st
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ PROGRAM MyProgram
Steps#ParseDocument1:
// Parse Document 1st object model
ActiveStep := 'Parse for "myInt" JSON Document {"myInt": 1234}';
deserializer.SetBuffer(REF(buffer));
deserializer.buffer := REF(buffer);
keyFound := deserializer.TryParse('myInt', ParsedValue1);
step := Steps#ResetJSonDocument1
;
Expand Down Expand Up @@ -85,7 +85,7 @@ PROGRAM MyProgram
// Parse Document for the value of the nested key `myObject.myDint` and expect `12345678`
// Get Values from a nested element
ActiveStep := 'Parse for "myInt" JSON Document {"myBoolean": true, "myObject": {"myInt": 1234, "myDint": 12345678}}';
deserializer.SetBuffer(REF(buffer));
deserializer.buffer := REF(buffer);
keyFound := deserializer.TryParse(keyArray, ParsedValue2);
step := Steps#ResetJSonDocument2;
;
Expand Down
4 changes: 2 additions & 2 deletions test/TestMyProgram.st
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ CLASS TestMyProgram
;
Steps#ParseDocument1:
// Parse Document 1st object model
deserializer.SetBuffer(REF(buffer));
deserializer.buffer := REF(buffer);
key := 'myInt';
keyFound := deserializer.TryParse(key, intValue);
step := Steps#ResetJSonDocument1
Expand All @@ -75,7 +75,7 @@ CLASS TestMyProgram
Steps#ParseDocument2:
// Parse Document 1st object model
// Get Values from a nested element
deserializer.SetBuffer(REF(buffer));
deserializer.buffer := REF(buffer);
keyArray[0] := 'myObject';
keyArray[1] := 'myInt';
keyFound := deserializer.TryParse(keyArray, intValue);
Expand Down
8 changes: 4 additions & 4 deletions test/UsingDeserializer.st
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ Class UsingDeserializer
len:= Strings.ToArray.OfCharCount(str := JSON_Entry, arr := buffer);

//Give it to the deserializer
deserializer.SetBuffer(REF(buffer));
deserializer.buffer := REF(buffer);

//Parse string
key:= 'key1';
Expand Down Expand Up @@ -91,7 +91,7 @@ Class UsingDeserializer
JSON_Entry := '{ "arraykey": [1234, 5678]}';

len:= Strings.ToArray.OfCharCount(str := JSON_Entry, arr := buffer);
deserializer.SetBuffer(REF(buffer));
deserializer.buffer := REF(buffer);

key:= 'arraykey';
keyFound := deserializer.TryParse( key, stringValue);
Expand All @@ -111,7 +111,7 @@ Class UsingDeserializer
JSON_Entry := '{ "key1": {"nestedkey1": "nested value", "nestedkey2": 456}}';

len:= Strings.ToArray.OfCharCount(str := JSON_Entry, arr := buffer);
deserializer.SetBuffer(REF(buffer));
deserializer.buffer := REF(buffer);


//Parse string
Expand Down Expand Up @@ -144,7 +144,7 @@ Class UsingDeserializer
JSON_Entry := '{ "key1": {"nestedkey1": "nested value", "nestedkey2": {"nested_nested_key" : 1234}}}';

len:= Strings.ToArray.OfCharCount(str := JSON_Entry, arr := buffer);
deserializer.SetBuffer(REF(buffer));
deserializer.buffer := REF(buffer);

//Get Values from a nested element
keyArray[0] := 'key1';
Expand Down