-
Notifications
You must be signed in to change notification settings - Fork 0
/
example.js
100 lines (87 loc) · 2.24 KB
/
example.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
const __modbus = require('./');
const modbus = new __modbus({
ip: '127.0.0.1',
port: 502,
id: 1,
wordSwapped: true,
decimalDigits: null,
debug: true,
timeout: 1000,
});
console.log('is Little Endian?', modbus.isLittleEndian); // true or false
const numbers = [
{
value: -32,
type: 'int16',
},
{
value: -32,
type: 'uint16',
},
{
value: 64,
type: 'uint16',
},
{
value: 0xff11eeaa,
type: 'uint32',
},
{
value: 123456,
type: 'int32',
},
{
value: -123456,
type: 'int32',
},
{
value: 0.123456,
type: 'float32',
digits: 6,
},
{
value: -10.123456,
type: 'float32',
digits: 6,
},
{
value: 0.23522297317322227,
type: 'float64',
},
];
async function routine(){
let address = 0;
for(const tmp of numbers){
console.log(`=============== REGISTERS ADDR: ${address} =====================`);
const value = modbus.numToWords(tmp.value, tmp.type);
const directConvertion = modbus.wordsToNum(value, tmp.type);
console.log('bytes:',value.map(item => item.toString(16)));
console.log('convert:', tmp.value, directConvertion, directConvertion == tmp.value);
const write = await modbus.writeRegisters(address,[...value]);
console.log('write:',write);
const read = await modbus.readHoldingRegisters(address, modbus.wordsLength(tmp.type));
console.log('read:', read);
if(read){
const number = modbus.wordsToNum(read, tmp.type, tmp.digits);
console.log('type:', tmp.type);
console.log('modbus:', tmp.value, number, number == tmp.value);
}
// get next address by adding current address with data type length in words
address += modbus.wordsLength(tmp.type);
}
console.log(`=============== COILS ADDR: ${address} =====================`);
const writeCoils = await modbus.writeCoils(address, [1,0,0,1,0]);
console.log('writeCoils', writeCoils);
const readCoils = await modbus.readCoils(address, 5);
console.log('readCoils', readCoils);
const readDiscreteInputs = await modbus.readDiscreteInputs(address, 5);
console.log('readDiscreteInputs', readDiscreteInputs);
console.log(`=============== END ${Date.now()} ==================`);
console.log(modbus.config.longByteOrder);
setTimeout(routine, 500);
}
routine();
setTimeout(() => {
console.log('FINISHED !!!');
process.exit(0);
}, 5000);