nodes7comm

Library to establish a connection with S7 PLC's

Usage no npm install needed!

<script type="module">
  import nodes7comm from 'https://cdn.skypack.dev/nodes7comm';
</script>

README

nodes7comm

This library allows communication to S7-300/400/1200/1500 PLCs using the Siemens S7 Ethernet protocol.

This library is based entirely from nodeS7.

Get Started

  • First you need to enable GET/PUT Access. Right click on PLC -> Properties -> Protection and security -> Connections mechanisms -> Check GET/PUT Access.
  • For access to a DB you must disable Optimized Block Access in TIA Portal for that DB. Right click on DB -> Properties -> Attributes -> Uncheck Optimized Block Access.
  • More info: snap7

Installation

npm install nodes7comm

Methods

initiateConnecton


import { NodeS7Comm, Nodes7CommConfig } from 'nodes7comm';

const options: Nodes7CommConfig = {
    host: '192.168.1.200',
};

const s7Client = new NodeS7Comm(options);
s7Client.initiateConnection();

s7Client.on('error', (err) => {
    // Error events
});

s7Client.on('disconnected', () => {
    // When we are reading or writing and a timeout ocurred
});

s7Client.on('connected', () => {
    // When we connect to the PLC
});

s7Client.on('connect-timeout', () => {
    // When we are unable to establish a connection
});

Nodes7CommConfig: Interface

| propertie | type | required | default | description | |-|-|-|-|-| |host|string |true| | Address of the PLC| |port|number |false| 102 | Port to stablish connection| |rack|number |false| 0 | Rack of PLC | |slot|number |false| 1 | Slot of PLC | |connectionTimeout|number |false| 5000 | Timeout to establish a connection | |requestTimeout|number |false| 1500 | Timeout of each request | |localTSAP|number |false| | | |remoteTSAP|number |false| | | |connectionName|string |false| ${host} | | |optimize|boolean |false| true | Enable optimization of packages sent to PLC | |autoReconnect|boolean |false| true | Auto connect after disconnect | |logLevel| 'none'
'error'
'warn'
'info' | false | 'none' | Show logs in console|

readTags


// This function read values in given plc directions
s7Client.readTags('Q0.0').then((value) => {
    console.log(value); // { 'Q0.0': false }
});

s7Client.readTags(['DB100,REAL22', 'M0.0']).then((values) => {
    console.log(values); // { 'DB100,REAL22': 20.5, 'M0.0': false }
});

writeTags


// This function write values in given plc directions
s7Client.writeTags('Q0.0', false).then((newValues) => {
    console.log(newValues); // { 'Q0.0': false }
});

// Arrays must have same length
s7Client.writeTags(['DB100,REAL22', 'DB99,S0.50'], [32.3, 'Hello' ]).then((newValues) => {
    console.log(newValues); // { 'DB100,REAL22': 32.3, 'DB99,S0.50': 'Hello' }
});

addTranslationTags


// This function add a name to each directions, for better manage for your app

const tags = {
    analog: 'DB100,REAL22',
    name: 'DB99,S0.50',
    output: 'Q0.0',
};

s7Client.addTranslationTags(tags);

// Note that this time we are reading the keys of the above object
s7Client.readTags(['analog', 'name', 'output']).then((values) => {
    console.log(values); // { analog: 32.29999923706055, name: 'Hello', output: true }
});

const moreTags = {
    active: 'DB100,X0.0',
};

s7Client.addTranslationTags(moreTags);

// Apply on writeTags() too
s7Client.writeTags(['analog', 'active'], [50.5, true]).then((values) => {
    console.log(values); // { analog: 50.5, active: true }
});

deleteTranslationTag


// Delete a tag from translation object
s7Client.deleteTranslationTag('analog');

s7Client.readTags('analog').then((values) => {
    console.log(values);
}).catch(err => {
    console.log(err); // Failed to find a match for: analog
});

addTags


// We can save tags in the instance for read all stored tags
s7Client.addTags(['DB100,X0.0']);

const tags = {
    input: 'I0.0',
    tagBool: 'M6.4',
};
s7Client.addTranslationTags(tags); // If we want to store alias, we need first add these tags in the traslation

s7Client.addTags(Object.keys(tags)); // Array of tags


readAllTags


const tags = {
    input: 'I0.0',
    tagBool: 'M6.4',
};
s7Client.addTranslationTags(tags);

// Add he keys of the abject above
s7Client.addTags(Object.keys(tags));

s7Client.readAllTags().then((values) => {
    console.log(values); // { input: false, tagBool: false }
});

removeTags


// Remove tags from the instance
s7Client.removeTags('input');

s7Client.readAllTags().then((values) => {
    console.log(values); // { tagBool: false }
});


Supported address

| Operand identifier | Data type | Examples | |-|-|-| | Input (I) | Bool
Byte
Char
Word
Int
DWord
DInt
Real
LReal | I1.0
IB3
IC4
IW22
II24
ID26
ID140
IR1400
ILR1404 | | Output (Q) | Bool
Byte
Char
Word
Int
DWord
DInt
Real
LReal | Q0.2
QB2
QC4
QW20
QI22
QD24
QDI28
QR32
QLR36 | | Memory (M) | Bool
Byte
Char
Word
Int
DWord
DInt
Real
LReal | M2.2
MB0
MC2
MW220
MI28
MD40
MD100
MR2000
MLR2004 | | Data Block (DB) | Bool
Byte
Char
Word
Int
DWord
DInt
Real
LReal
String | DB5,X2.2
DB6,B0
DB10,C2
DB10,W220
DB10,I28
DB12,D40
DB2,D100
DB100,R2
DB101,LR6
DB99,S0.50 |