# iecstruct

> Datatype parser/writer for IEC 61131-3 types and structs

Latest version **0.0.8** (published 2014-06-01) · 0 weekly downloads

## Install

```sh
npm install iecstruct
pnpm add iecstruct
yarn add iecstruct
bun add iecstruct
```

## Health

**Score 10/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support; low quality score; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.0.8 |
| Published | 2014-06-01 |
| First published | 2014-05-14 |
| Weekly downloads | 0 |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 0 |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 17 |
| Author | Håkon Nessjøen |
| Maintainers | haakon |
| Keywords | iec, 61131-3, twincat, plc, ads, beckhoff |

## Links

- npm: https://www.npmjs.com/package/iecstruct
- Repository: https://github.com/haakonnessjoen/iecstruct
- Issues: https://github.com/haakonnessjoen/iecstruct/issues
- npm.io page: https://npm.io/package/iecstruct

## Alternatives

- [csv-to-markdown-table](https://npm.io/package/csv-to-markdown-table.md) — 47.0K weekly downloads
- [@sapphire/ratelimits](https://npm.io/package/@sapphire/ratelimits.md) — 4.4K weekly downloads
- [js-csvparser](https://npm.io/package/js-csvparser.md) — 2.0K weekly downloads
- [@adadapted/js-sdk](https://npm.io/package/@adadapted/js-sdk.md) — 251 weekly downloads
- [@grapecity/spread-sheets-sparklines](https://npm.io/package/@grapecity/spread-sheets-sparklines.md) — 103 weekly downloads

## Recent versions

- 0.0.8 (latest) — 2014-06-01
- 0.0.7 — 2014-05-23
- 0.0.6 — 2014-05-17
- 0.0.5 — 2014-05-17
- 0.0.4 — 2014-05-17
- 0.0.3 — 2014-05-17
- 0.0.2 — 2014-05-17
- 0.0.1 — 2014-05-14

## README

iecstruct
=========

A NodeJS library for parsing and saving IEC 61131-3 structs and types from/to binary buffers.

Support for types
-----------------

All variable types are supported, look in table below for syntax.

Variable type | ST Syntax | iecstruct syntax
--- | --- | ---
ARRAY | ARRAY[1..25] OF VARIABLETYPE | new iecstruct.ARRAY(VARIABLETYPE, 25)
STRUCT | TYPE name : STRUCT .. END STRUCT | new iecstruct.STRUCT()
STRING | STRING[20] | new iecstruct.STRING(20)
WSTRING | WSTRING[20] | new iecstruct.WSTRING(20)
ENUM | **see note 1** | **see note 1**
BOOL | BOOL | iecstruct.BOOL
BYTE | BYTE | iecstruct.BYTE
WORD | WORD | iecstruct.WORD
DWORD | DWORD | iecstruct.DWORD
SINT | SINT | iecstruct.SINT
USINT | USINT | iecstruct.USINT
INT | INT | iecstruct.INT
UINT | UINT | iecstruct.UINT
DINT | DINT | iecstruct.DINT
UDINT | UDINT | iecstruct.UDINT
LINT | LINT | iecstruct.LINT
ULINT | ULINT | iecstruct.ULINT
REAL | REAL | iecstruct.REAL
LREAL | LREAL | iecstruct.LREAL
TIME | TIME | iecstruct.TIME
TIME_OF_DAY (TOD) | TOD | iecstruct.TOD
DATE | DATE | iecstruct.DATE
DATE_AND_TIME (DT) | DT | iecstruct.DT

All variable types, also included the types you "create" by making structs or arrays, have the same two methods, and a parameter:
  * asObject([buffer],[offset])
  * fromObject(object, [buffer], [offset])
  * bytelength


### asObject[buffer],[offset])

Returns a javascript object containting a javascript version of the data contained in buffer.

### fromObject(object, [buffer], [offset])

Writes the object back to the buffer, according to the variable type you are using. Se examples below.

### bytelength

Holds the number of actual bytes the specified variable/struct would need to read/write to a buffer.

**Note 1**: Heres an example of a ENUM in structured text, and then with iecstruct:

```
TYPE E_TRAFFIC_SIGNAL:
	(
		Red,
		Yellow:=10,
		Green
	);
END_TYPE
```

```javascript
var E_TRAFFIC_SIGNAL = new iecstruct.ENUM({
	Red: '',
	Yellow: 10,
	Green: ''
});

/* OR */

var E_TRAFFIC_SIGNAL = new iecstruct.ENUM()
	.addValue('Red')
	.addValue('Yellow', 10)
	.addValue('Green');

```
Enum variables will return string names for the known enum values, and plain digits if an invalid ENUM value is set/read.

Examples
--------

### Struct with structs and arrrays

```javascript
var iecstruct = require('iecstruct');

var E_TriggerFunction = new iecstruct.ENUM({
	TRG_RE: '',
	TRG_FE: '',
	TRG_HOLD: ''
});


var ST_ScenarioTrigger = new iecstruct.STRUCT()
	.addElement('eTriggerFunction', E_TriggerFunction)
	.addElement('nElementNo', iecstruct.UINT);

var ST_Scenario = new iecstruct.STRUCT()
	.addElement('sName', new iecstruct.STRING(256))
	.addElement('bScenarioEnabled', iecstruct.BOOL)
	.addElement('bScenarioInUse', iecstruct.BOOL)
	.addArray  ('Triggers', ST_ScenarioTrigger, 100);

var Scenarios = new iecstruct.ARRAY('ST_Scenario', 100);

var obj = Scenarios.asObject();
obj[0].sName = "First scenario";

console.log(obj);

// Back to buffer:
Scenarios.fromObject(obj);

/* Buffer can be specified: */
var buffer = new Buffer(Scenarios.bytelength);
/* .. put code to insert data into buffer here */
var obj = Scenarios.asObject(buffer);
/* You can also write to a buffer */
Scenarios.fromObject(obj, buffer);


```

### Reading/Writing arrays directly

```javascript
var iecstruct = require('iecstruct');

/* MyArray : ARRAY[1..10] OF BOOL; */
var MyArray = new iecstruct.ARRAY(iecstruct.BOOL, 10);
var obj = MyArray.asObject(my_iec_buffer);

/* StructArray : ARRAY[1..25] OF ST_Simple; */
var ST_Simple = new iecstruct.STRUCT()
	.addElement('bActive', iecstruct.BOOL)
	.addElement('nNumber', iecstruct.UINT);
var StructArray = new iecstruct.ARRAY(ST_Simple, 25);
var obj = StructArray.asObject(my_iec_buffer);
```

### Dual syntax for arrays
```javascript

var ST_Scenario = new iecstruct.STRUCT()
	.addElement('sName', new iecstruct.STRING(256))
	.addElement('bScenarioEnabled', iecstruct.BOOL)
	.addElement('bScenarioInUse', iecstruct.BOOL)
	.addArray  ('Triggers', ST_ScenarioTrigger, 100);

/* Is the same as */

var ST_Scenario = new iecstruct.STRUCT()
	.addElement('sName', new iecstruct.STRING(256))
	.addElement('bScenarioEnabled', iecstruct.BOOL)
	.addElement('bScenarioInUse', iecstruct.BOOL)
	.addElement('Triggers', new iecstruct.ARRAY(ST_ScenarioTrigger, 100));

```

License (MIT)
-------------
Copyright (c) 2014 Håkon Nessjøen <haakon.nessjoen@gmail.com>

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

---
_Source: https://npm.io/package/iecstruct · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
