Skip to content

Commit

Permalink
Added examples for custom read/write script to readme
Browse files Browse the repository at this point in the history
  • Loading branch information
crycode-de committed Apr 12, 2021
1 parent 62c5b87 commit 699c54c
Showing 1 changed file with 26 additions and 0 deletions.
26 changes: 26 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,19 @@ At the beginning of the custom read script, `buffer` will be the received/curren
The content of the `value` variable at the end of the custom read script will be used as new value for the state.
If `value` is `undefined`, it will be ignored. Using this you are able to filter messages in the custom read script by data parts.

##### Example for a custom read script

Check the first three bytes in the received buffer to match fixed values.
If matched, read an 16 bit signed integer value from the buffer bytes 3 and 4 and divide it by 10.

```js
if (buffer[0] === 0xC2 && buffer[1] === 0x10 && buffer[2] === 0x0F) {
value = buffer.readInt16BE(3) / 10;
}
```

Cause of `value` is only set when the first three bytes matched, all other data will be ignored and won't set a new value to the state.

#### Custom write script

In a write script you have to modify (or replace) the `buffer` variable.
Expand All @@ -101,6 +114,19 @@ At the beginning of the custom write script, `buffer` will be the current CAN me

The content of the `buffer` variable at the end of the custom write script will be used as new data for the CAN message.

##### Example for a custom write script

Prepare a new buffer with fixed values.
Write the state value into the buffer as a signed 16 bit integer, beginning at the fifth byte in the buffer.

```js
buffer = Buffer.from([0x30, 0x00, 0xFA, 0x06, 0x7E, 0x00, 0x00]);
buffer.writeInt16BE(value, 5);
```

The new `buffer` will then be set as the `.json` state.
If the *autosend* option is enabled for the message, the message will be automatically send.

## Usage in scripts

You can handle/modify the `<messageId>.json` or `<messageId>.<parserId>` states in your scripts.
Expand Down

0 comments on commit 699c54c

Please sign in to comment.