Skip to content

Commit

Permalink
Merge pull request #447 from PLCHome/master
Browse files Browse the repository at this point in the history
Stringtyps added and old improved & RAW added
  • Loading branch information
Apollon77 authored Apr 12, 2024
2 parents 04e71c1 + c021a74 commit 6aaa147
Show file tree
Hide file tree
Showing 8 changed files with 167 additions and 108 deletions.
62 changes: 36 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,13 @@ There are some programs in folder `test` to test the TCP communication:
### **WORK IN PROGRESS**
-->
## Changelog

### **WORK IN PROGRESS**
* (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
43 changes: 42 additions & 1 deletion lib/common.js
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,29 @@ function extractValue(type, len, buffer, offset) {
_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;
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');
_len += 1;
}
return str;
default:
throw new Error(`Invalid type: ${type}`);
return 0;
Expand Down Expand Up @@ -311,6 +334,24 @@ function writeValue(type, value, len) {
}
}
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;
default:
throw new Error(`Invalid type: ${type}`);
buffer = Buffer.alloc(2);
Expand All @@ -331,4 +372,4 @@ module.exports = {
writeValue,
extractValue,
getJSModbusPath
};
};
Loading

0 comments on commit 6aaa147

Please sign in to comment.