forked from yaacov/node-modbus-serial
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpolling_UDP.js
138 lines (112 loc) · 3.72 KB
/
polling_UDP.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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
/* eslint-disable no-console, spaced-comment, func-call-spacing, no-spaced-func */
//==============================================================
// This is an example of polling (reading) Holding Registers
// on a regular scan interval with timeouts enabled.
// For robust behaviour, the next action is not activated
// until the previous action is completed (callback served).
//==============================================================
"use strict";
//==============================================================
// create an empty modbus client
const ModbusRTU = require ("modbus-serial");
const client = new ModbusRTU();
let mbsStatus = "Initializing..."; // holds a status of Modbus
// Modbus 'state' constants
const MBS_STATE_INIT = "State init";
const MBS_STATE_IDLE = "State idle";
const MBS_STATE_NEXT = "State next";
const MBS_STATE_GOOD_READ = "State good (read)";
const MBS_STATE_FAIL_READ = "State fail (read)";
const MBS_STATE_GOOD_CONNECT = "State good (port)";
const MBS_STATE_FAIL_CONNECT = "State fail (port)";
// Modbus UDP configuration values
const mbsId = 1;
const mbsPort = 502;
const mbsHost = "192.168.1.11";
const mbsScan = 1000;
const mbsTimeout = 5000;
let mbsState = MBS_STATE_INIT;
//==============================================================
const connectClient = function()
{
// close port (NOTE: important in order not to create multiple connections)
client.close(() => {
console.log("Client closed");
});
// set requests parameters
client.setID (mbsId);
client.setTimeout (mbsTimeout);
// try to connect
client.connectUDP (mbsHost, { port: mbsPort })
.then(function()
{
mbsState = MBS_STATE_GOOD_CONNECT;
mbsStatus = "Connected, wait for reading...";
console.log(mbsStatus);
})
.catch(function(e)
{
mbsState = MBS_STATE_FAIL_CONNECT;
mbsStatus = e.message;
console.log(e);
});
};
//==============================================================
const readModbusData = function()
{
// try to read data
client.readHoldingRegisters (0, 18)
.then(function(data)
{
mbsState = MBS_STATE_GOOD_READ;
mbsStatus = "success";
console.log(data.buffer);
})
.catch(function(e)
{
mbsState = MBS_STATE_FAIL_READ;
mbsStatus = e.message;
console.log(e);
});
};
//==============================================================
const runModbus = function()
{
let nextAction;
switch (mbsState)
{
case MBS_STATE_INIT:
nextAction = connectClient;
break;
case MBS_STATE_NEXT:
nextAction = readModbusData;
break;
case MBS_STATE_GOOD_CONNECT:
nextAction = readModbusData;
break;
case MBS_STATE_FAIL_CONNECT:
nextAction = connectClient;
break;
case MBS_STATE_GOOD_READ:
nextAction = readModbusData;
break;
case MBS_STATE_FAIL_READ:
if (client.isOpen) { mbsState = MBS_STATE_NEXT; }
else { nextAction = connectClient; }
break;
default:
// nothing to do, keep scanning until actionable case
}
console.log();
console.log(nextAction);
// execute "next action" function if defined
if (nextAction !== undefined)
{
nextAction();
mbsState = MBS_STATE_IDLE;
}
// set for next run
setTimeout (runModbus, mbsScan);
};
//==============================================================
runModbus();