Skip to content
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

Add String16 #31

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
Open
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
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "s7client",
"version": "1.1.0",
"version": "1.1.5",
"description": "Hi level API for node-snap7 to communication with Siemens S7 PLCs",
"homepage": "https://psi-4ward.github.io/s7client",
"main": "src/index.js",
Expand Down
56 changes: 51 additions & 5 deletions src/datatypes.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,13 +47,13 @@ const Datatypes = {
* WORD
* @type {S7ClientDatatype}
*/
WORD: _gen(2, 'UInt16BE', snap7.S7WLWord),
WORD: _gen(2, 'UInt16BE', snap7.S7WLByte),

/**
* DWORD
* @type {S7ClientDatatype}
*/
DWORD: _gen(4, 'UInt32BE', snap7.S7WLDWord),
DWORD: _gen(4, 'UInt32BE', snap7.S7WLByte),

/**
* CHAR
Expand All @@ -65,24 +65,70 @@ const Datatypes = {
formatter: v => Buffer.from(v, 'ascii'),
S7WordLen: snap7.S7WLByte
},

/**
* STRING16
* @type {S7ClientDatatype}
*/
STRING16: { // type to read an entire String[16] from PLC
bytes: 18,
parser: (buffer, offset = 0) => buffer.toString('ascii', offset + 2, buffer.readUInt8(1) + 2),
formatter: v => Buffer.concat([
Buffer.from([16, v.length <= 16 ? v.length : 16]),// maximum size | string size
Buffer.from(v.length <= 15 ? v : v.substring(0, 16), 'ascii'), // string to buffer
Buffer.from([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]) // fill zeros
]).subarray(0, 18),
S7WordLen: snap7.S7WLByte
},


/**
* STRING15
* @type {S7ClientDatatype}
*/
STRING15: { // type to read an entire String[15] from PLC
bytes: 18,
parser: (buffer, offset = 0) => buffer.toString('ascii', offset + 2, buffer.readUInt8(1) + 2),
formatter: v => Buffer.concat([
Buffer.from([15, v.length <= 15 ? v.length : 15]),// maximum size | string size
Buffer.from(v.length <= 15 ? v : v.substring(0, 15), 'ascii'), // string to buffer
Buffer.from([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]) // fill zeros
]).subarray(0, 18),
S7WordLen: snap7.S7WLByte
},


/**
* STRING20
* @type {S7ClientDatatype}
*/
STRING20: { // type to read an entire String[20] from PLC
bytes: 22,
parser: (buffer, offset = 0) => buffer.toString('ascii', offset + 2, buffer.readUInt8(1) + 2),
formatter: v => Buffer.concat([
Buffer.from([20, v.length <= 20 ? v.length : 20]),// maximum size | string size
Buffer.from(v.length <= 20 ? v : v.substring(0, 20), 'ascii'), // string to buffer
Buffer.from([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]) // fill zeros
]).subarray(0, 22),
S7WordLen: snap7.S7WLByte
},
/**
* INT
* @type {S7ClientDatatype}
*/
INT: _gen(2, 'Int16BE', snap7.S7WLWord),
INT: _gen(2, 'Int16BE', snap7.S7WLByte),

/**
* DINT
* @type {S7ClientDatatype}
*/
DINT: _gen(4, 'Int32BE', snap7.S7WLDWord),
DINT: _gen(4, 'Int32BE', snap7.S7WLByte),

/**
* REAL
* @type {S7ClientDatatype}
*/
REAL: _gen(4, 'FloatBE', snap7.S7WLReal),
REAL: _gen(4, 'FloatBE', snap7.S7WLByte),
};

module.exports = Datatypes;
4 changes: 2 additions & 2 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,7 @@ class S7Client extends EventEmitter {
WordLen: datatypes[v.type].S7WordLen,
DBNumber: v.dbnr,
Start: v.type === 'BOOL' ? v.start * 8 + v.bit : v.start,
Amount: 1
Amount: datatypes[v.type].bytes ? datatypes[v.type].bytes : 1, // changed to read size from type
}
});

Expand Down Expand Up @@ -264,7 +264,7 @@ class S7Client extends EventEmitter {
WordLen: datatypes[v.type].S7WordLen,
DBNumber: v.dbnr,
Start: v.type === 'BOOL' ? v.start * 8 + v.bit : v.start,
Amount: 1,
Amount: datatypes[v.type].bytes ? datatypes[v.type].bytes : 1, // changed to read size from type
Data: datatypes[v.type].formatter(v.value)
}));

Expand Down