Skip to content

add support for DOUBLE type (64bit float BE), may be aliased as LR #6

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

Open
wants to merge 2 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
10 changes: 10 additions & 0 deletions src/addressParser/nodes7.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,13 +89,15 @@ class AddressParserNodeS7 {
switch (match_area) {
case "X":
case "BYTE":
case "SBYTE":
case "CHAR":
case "STRING":
case "INT":
case "DINT":
case "WORD":
case "DWORD":
case "REAL":
case "DOUBLE":
case "DT":
case "DTZ":
case "DTL":
Expand Down Expand Up @@ -127,6 +129,9 @@ class AddressParserNodeS7 {
case "R":
dataType = "REAL";
break;
case "LR":
dataType = "DOUBLE";
break;
default:
throw new NodeS7Error('ERR_PARSE_DB_DATATYPE', `Unknown DB data type "${match_area}" for address "${address}"`, { item: address });
}
Expand Down Expand Up @@ -189,6 +194,9 @@ class AddressParserNodeS7 {
case "R":
dataType = "REAL";
break;
case "LR":
dataType = "DOUBLE";
break;
default:
throw new NodeS7Error('ERR_PARSE_DATATYPE', `Unknown data type "${dataType}" for address "${address}"`, { item: address });
}
Expand Down Expand Up @@ -251,6 +259,7 @@ class AddressParserNodeS7 {
break;
case "DT":
case "DTZ":
case "DOUBLE":
dataTypeLength = 8;
break;
case "REAL":
Expand All @@ -266,6 +275,7 @@ class AddressParserNodeS7 {
break;
case "X":
case "BYTE":
case "SBYTE":
case "CHAR":
dataTypeLength = 1;
break;
Expand Down
10 changes: 10 additions & 0 deletions src/s7item.js
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,8 @@ function toBCD(n) {
function getValueByDataType(buffer, type, offset, bitOffset, length = 1) {
let year, month, day, hour, min, sec, ms_1, ms_2, ns;
switch (type) {
case "DOUBLE":
return buffer.readDoubleBE(offset);
case "REAL":
return buffer.readFloatBE(offset);
case "DWORD":
Expand All @@ -288,6 +290,8 @@ function getValueByDataType(buffer, type, offset, bitOffset, length = 1) {
return buffer.readUInt16BE(offset);
case "BYTE":
return buffer.readUInt8(offset);
case "SBYTE":
return buffer.readInt8(offset);
case "CHAR":
return buffer.toString('ascii', offset, offset + length);
case "STRING":
Expand Down Expand Up @@ -358,6 +362,7 @@ function bufferWriteByDataType(buffer, data, type, offset, length = 1) {

// type check
switch (type) {
case "DOUBLE":
case "REAL":
case "DWORD":
case "DINT":
Expand All @@ -366,6 +371,7 @@ function bufferWriteByDataType(buffer, data, type, offset, length = 1) {
case "INT":
case "WORD":
case "BYTE":
case "SBYTE":
if (typeof data !== 'number') throw new NodeS7Error('ERR_INVALID_ARGUMENT', `Data for item of type '${type}' must be a number`);
break;
case "CHAR":
Expand Down Expand Up @@ -404,6 +410,8 @@ function bufferWriteByDataType(buffer, data, type, offset, length = 1) {


switch (type) {
case "DOUBLE":
return buffer.writeDoubleBE(data, offset);
case "REAL":
return buffer.writeFloatBE(data, offset);
case "DWORD":
Expand All @@ -418,6 +426,8 @@ function bufferWriteByDataType(buffer, data, type, offset, length = 1) {
return buffer.writeUInt16BE(data, offset);
case "BYTE":
return buffer.writeUInt8(data, offset);
case "SBYTE":
return buffer.writeInt8(data, offset);
case "CHAR":
// this is supposed to be a clean buffer, no need to empty it first
return buffer.write(data, offset, length, 'ascii');
Expand Down