forked from ZHONG007/node-modbus-serial
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpolling_RTU.js
135 lines (110 loc) · 3.63 KB
/
polling_RTU.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
/* 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
var ModbusRTU = require ("modbus-serial");
var client = new ModbusRTU();
var mbsStatus = "Initializing..."; // holds a status of Modbus
// Modbus 'state' constants
var MBS_STATE_INIT = "State init";
var MBS_STATE_IDLE = "State idle";
var MBS_STATE_NEXT = "State next";
var MBS_STATE_GOOD_READ = "State good (read)";
var MBS_STATE_FAIL_READ = "State fail (read)";
var MBS_STATE_GOOD_CONNECT = "State good (port)";
var MBS_STATE_FAIL_CONNECT = "State fail (port)";
// Modbus configuration values
var mbsId = 1;
var mbsScan = 1000;
var mbsTimeout = 5000;
var mbsState = MBS_STATE_INIT;
// Upon SerialPort error
client.on("error", function(error) {
console.log("SerialPort Error: ", error);
});
//==============================================================
var connectClient = function()
{
// set requests parameters
client.setID (mbsId);
client.setTimeout (mbsTimeout);
// try to connect
client.connectRTUBuffered ("COM9", { baudRate: 9600, parity: "even", dataBits: 8, stopBits: 1 })
.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);
});
};
//==============================================================
var readModbusData = function()
{
// try to read data
client.readHoldingRegisters (5, 1)
.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);
});
};
//==============================================================
var runModbus = function()
{
var 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();