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

Stringtyps added and old improved & RAW added #447

Merged
merged 5 commits into from
Apr 12, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
61 changes: 35 additions & 26 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -163,32 +163,35 @@ If a value is marked with this flag, then the value will be stored into a variab
This variable can be then later used in any formula for other parameters. E.g., the following formula can set: `(x * sf['40065']) + 50;`

## Data types
- `uint16be` - `Unsigned 16 bit (Big Endian): AABB => AABB`
- `uint16le` - `Unsigned 16 bit (Little Endian): AABB => BBAA`
- `int16be` - `Signed 16 bit (Big Endian): AABB => AABB`
- `int16le` - `Signed 16 bit (Little Endian): AABB => BBAA`
- `uint32be` - `Unsigned 32 bit (Big Endian): AABBCCDD => AABBCCDD`
- `uint32le` - `Unsigned 32 bit (Little Endian): AABBCCDD => DDCCBBAA`
- `uint32sw` - `Unsigned 32 bit (Big Endian Word Swap): AABBCCDD => CCDDAABB`
- `uint32sb` - `Unsigned 32 bit (Big Endian Byte Swap): AABBCCDD => DDCCBBAA`
- `int32be` - `Signed 32 bit (Big Endian): AABBCCDD => AABBCCDD`
- `int32le` - `Signed 32 bit (Little Endian): ABBCCDD => DDCCBBAA`
- `int32sw` - `Signed 32 bit (Big Endian Word Swap): AABBCCDD => CCDDAABB`
- `int32sb` - `Signed 32 bit (Big Endian Byte Swap): AABBCCDD => DDCCBBAA`
- `uint64be` - `Unsigned 64 bit (Big Endian): AABBCCDDEEFFGGHH => AABBCCDDEEFFGGHH`
- `uint64le` - `Unsigned 64 bit (Little Endian): AABBCCDDEEFFGGHH => HHGGFFEEDDCCBBAA`
- `uint8be` - `Unsigned 8 bit (Big Endian): AA => AA`
- `uint8le` - `Unsigned 8 bit (Little Endian): AA => AA`
- `int8be` - `Signed 8 bit (Big Endian): AA => AA`
- `int8le` - `Signed 8 bit (Little Endian): AA => AA`
- `floatbe` - `Float (Big Endian): AABBCCDD => AABBCCDD`
- `floatle` - `Float (Little Endian): AABBCCDD => DDCCBBAA`
- `floatsw` - `Float (Big Endian Word Swap): AABBCCDD => CCDDAABB`
- `floatsb` - `Float (Big Endian Byte Swap): AABBCCDD => DDCCBBAA`
- `doublebe` - `Double (Big Endian): AABBCCDDEEFFGGHH => AABBCCDDEEFFGGHH`
- `doublele` - `Double (Little Endian): AABBCCDDEEFFGGHH => HHGGFFEEDDCCBBAA`
- `string` - `String (Zero-end): ABCDEF\0 => ABCDEF\0`
- `stringle` - `String (Little Endian, Zero-end): BADCFE\0 => ABCDEF\0`
- `uint16be` - `Unsigned 16 bit (Big Endian): AABB => AABB`
- `uint16le` - `Unsigned 16 bit (Little Endian): AABB => BBAA`
- `int16be` - `Signed 16 bit (Big Endian): AABB => AABB`
- `int16le` - `Signed 16 bit (Little Endian): AABB => BBAA`
- `uint32be` - `Unsigned 32 bit (Big Endian): AABBCCDD => AABBCCDD`
- `uint32le` - `Unsigned 32 bit (Little Endian): AABBCCDD => DDCCBBAA`
- `uint32sw` - `Unsigned 32 bit (Big Endian Word Swap): AABBCCDD => CCDDAABB`
- `uint32sb` - `Unsigned 32 bit (Big Endian Byte Swap): AABBCCDD => DDCCBBAA`
- `int32be` - `Signed 32 bit (Big Endian): AABBCCDD => AABBCCDD`
- `int32le` - `Signed 32 bit (Little Endian): ABBCCDD => DDCCBBAA`
- `int32sw` - `Signed 32 bit (Big Endian Word Swap): AABBCCDD => CCDDAABB`
- `int32sb` - `Signed 32 bit (Big Endian Byte Swap): AABBCCDD => DDCCBBAA`
- `uint64be` - `Unsigned 64 bit (Big Endian): AABBCCDDEEFFGGHH => AABBCCDDEEFFGGHH`
- `uint64le` - `Unsigned 64 bit (Little Endian): AABBCCDDEEFFGGHH => HHGGFFEEDDCCBBAA`
- `uint8be` - `Unsigned 8 bit (Big Endian): AABB => BB`
- `uint8le` - `Unsigned 8 bit (Little Endian): AABB => AA`
- `int8be` - `Signed 8 bit (Big Endian): AABB => BB`
- `int8le` - `Signed 8 bit (Little Endian): AABB => AA`
- `floatbe` - `Float (Big Endian): AABBCCDD => AABBCCDD`
- `floatle` - `Float (Little Endian): AABBCCDD => DDCCBBAA`
- `floatsw` - `Float (Big Endian Word Swap): AABBCCDD => CCDDAABB`
- `floatsb` - `Float (Big Endian Byte Swap): AABBCCDD => DDCCBBAA`
- `doublebe` - `Double (Big Endian): AABBCCDDEEFFGGHH => AABBCCDDEEFFGGHH`
- `doublele` - `Double (Little Endian): AABBCCDDEEFFGGHH => HHGGFFEEDDCCBBAA`
- `string` - `String 8 bit (Zero-end): ABCDEF\0 => ABCDEF\0`
- `stringle` - `String 8 bit (Little Endian, Zero-end): ABCDEF\0 => BADCFE\0`
- `string16` - `String 16 bit (Zero-end): \0A\0B\0C\0D\0E\0F\0\0 => ABCDEF\0`
- `string16le`- `String 16 bit (Little Endian, Zero-end): A\0B\0C\0D\0E\0F\0\0\0 => ABCDEF\0`
- `rawhex` - `String with value in hex representation AABBCCDD.... => AABBCCDD....`

The following description was copied from [here](http://www.chipkin.com/how-real-floating-point-and-32-bit-data-is-encoded-in-modbus-rtu-messages/)

Expand Down Expand Up @@ -300,6 +303,12 @@ There are some programs in folder `test` to test the TCP communication:
### **WORK IN PROGRESS**
-->
## Changelog
### x.x.x (2024-01-28)
* (PLCHome) String based on 16 bit values big endian as well as little endian
* (PLCHome) Raw data as a hex string
* (PLCHome) Fix issue stringle was always converted to number for slave
* (PLCHome) Enable formula for strings and hex strings

### 6.1.0 (2023-12-14)
* (nkleber78) Implement the connection keepAlive

Expand Down
41 changes: 41 additions & 0 deletions lib/common.js
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,29 @@
_len += 2;
}
return str;
case 'string16':
case 'string16le':
// find length
_len = 0;
const corr = ((type === 'string16') ? 1 : 0);
while (_len < len * 2) {
const pos = offset * 2 + _len
Fixed Show fixed Hide fixed
if (buffer[pos] || buffer[pos + 1]) {
str += String.fromCharCode(buffer[pos + corr] + (buffer[pos + (1 - corr)] << 8));
} else {
break;
}
_len += 2;
}
return str;
case 'rawhex':
// find length
_len = 0;
while (_len < len * 2) {
str += buffer[offset * 2 + _len].toString(16).padStart(2, '0')
Fixed Show fixed Hide fixed
_len += 1;
}
return str;
default:
throw new Error(`Invalid type: ${type}`);
return 0;
Expand Down Expand Up @@ -311,6 +334,24 @@
}
}
break;
case 'string16':
case 'string16le':
if (value === null) value = 'null';
value = value.toString();
_len = (value.length + 1);
buffer = Buffer.alloc(len * 2);
for (let b = 0; b < _len && b < len; b++) {
buffer.writeInt16LE((value.charCodeAt(b) << ((type === 'string16') ? 8 : 0)), b * 2);
}
break;
case 'rawhex':
if (value === null) value = '';
value = value.toString();
const _buffer = Buffer.from(value, 'hex');
// fix length
buffer = Buffer.alloc(len * 2);
_buffer.copy(buffer);
break
Fixed Show fixed Hide fixed
default:
throw new Error(`Invalid type: ${type}`);
buffer = Buffer.alloc(2);
Expand Down
Loading
Loading