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
var E_TRAFFIC_SIGNAL = Red: '' Yellow: 10 Green: ''; /* OR */ var E_TRAFFIC_SIGNAL = ;
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
var iecstruct = ; var E_TriggerFunction = TRG_RE: '' TRG_FE: '' TRG_HOLD: ''; var ST_ScenarioTrigger = ; var ST_Scenario = ; var Scenarios = 'ST_Scenario' 100; var obj = Scenarios;obj0sName = "First scenario"; console; // Back to buffer:Scenarios; /* Buffer can be specified: */var buffer = Scenariosbytelength;/* .. put code to insert data into buffer here */var obj = Scenarios;/* You can also write to a buffer */Scenarios;
Reading/Writing arrays directly
var iecstruct = ; /* MyArray : ARRAY[1..10] OF BOOL; */var MyArray = iecstructBOOL 10;var obj = MyArray; /* StructArray : ARRAY[1..25] OF ST_Simple; */var ST_Simple = ;var StructArray = ST_Simple 25;var obj = StructArray;
Dual syntax for arrays
var ST_Scenario = ; /* Is the same as */ var ST_Scenario = ;
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.