-
Notifications
You must be signed in to change notification settings - Fork 0
/
mb-esclavo.js
64 lines (52 loc) · 2.58 KB
/
mb-esclavo.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
const ModbusTcp = require('simple-modbus').ModbusTcp
const ModbusCommandException = require('simple-modbus').ModbusCommandException
const server = new ModbusTcp.Server();
exports.puerto = puerto => {
server.listen(puerto);
}
server.onReadCoilStatus.on(command => {
console.log(`Read Coil Status - Address: ${command.coilStartAddress} Number: ${command.numberOfCoils}`)
// Respond with Success (response array must be same length as numberOfCoils)
command.success([true, false, true, false])
})
server.onReadInputStatus.on(command => {
console.log(`Read Input Status - Address: ${command.inputStartAddress} Number: ${command.numberOfInputs}`)
// Respond with Success (response array must be same length as numberOfInputs)
command.success([true, false, true, false])
})
server.onReadHoldingRegisters.on(command => {
console.log(`Read Holding Registers - Address: ${command.registerStartAddress} Number: ${command.registerLength}`)
// Respond with Success (response array must be same length as registerLength)
command.success(new Uint16Array([0xFFFF, 0x0000, 0xDEAD, 0xBEEF]))
})
server.onReadInputRegisters.on(command => {
console.log(`Read Input Registers - Address: ${command.registerStartAddress} Number: ${command.registerLength}`)
// Respond with Success (response array must be same length as registerLength)
command.success(new Uint16Array([0xFFFF, 0x0000, 0xDEAD, 0xBEEF]))
})
server.onPresetSingleRegister.on(command => {
console.log(`Preset Single Register - Address: ${command.registerAddress} - Value: ${command.registerValue}`)
// Respond with Success
command.success()
})
server.onPresetMultipleRegisters.on(command => {
console.log(`Preset Multiple Registers - Start Address: ${command.registerStartAddress} Length: ${command.registerLength} Values: ${command.registerValues}`)
// Respond with Success
command.success()
})
server.onForceSingleCoil.on(command => {
console.log(`Force Single Coil - Address: ${command.coilAddress} Value: ${command.coilStatusAsCoilStatus}}`)
// Respond with Success
command.success()
})
server.onForceMultipleCoils.on(command => {
console.log(`Force Multiple Coils - Start Address: ${command.coilStartAddress} Length: ${command.coilLength} Values: ${command.coilStatuses}}`)
// Respond with Failure
command.fail(ModbusCommandException.ILLEGAL_DATA_ADDRESS)
})
server.onCommandError.on(err => {
console.error(`Error when trying to decode a packet: ${err.message}`)
})
server.onServerError.on(err => {
console.error(`Error from underlying TCP server: ${err.message}`)
})