-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathslave-connection.js
More file actions
66 lines (57 loc) · 1.71 KB
/
Copy pathslave-connection.js
File metadata and controls
66 lines (57 loc) · 1.71 KB
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
import ModbusRTU from 'modbus-serial'
import {slaveConfig} from "./config.js";
/*let client = new ModbusRTU();
export default async function connect() {
//console.log("-- call connect")
if(client.isOpen){
client.close();
}
client.setTimeout(2000);
client.setID(1);
try{
await client.connectRTU(slaveConfig.port, {baudRate: slaveConfig.baudRate, parity: slaveConfig.parity});
return client;
} catch(e){
console.log(e);
}
//await client.connectRTU(slaveConfig.port, {baudRate: slaveConfig.baudRate, parity: slaveConfig.parity});
// return client;
//return true;
}*/
let client = new ModbusRTU(); //check connect시에 new해야 할 수도
var networkErrors = ["ESOCKETTIMEDOUT", "ETIMEDOUT", "ECONNRESET", "ECONNREFUSED", "EHOSTUNREACH"];
/*
export default async function connect() {
client.setTimeout(2000);
client.setID(1);
try{
await client.connectRTU(slaveConfig.port, {baudRate: slaveConfig.baudRate, parity: slaveConfig.parity});
return client;
} catch(e){
console.log(e);
}
//return true;
}
*/
var connectClient = function()
{
if(client.isOpen) {
console.log('modbustru close');
client.close();
}
client.setTimeout(2000);
client.setID(1);
console.log('modbustru connect');
client.connectRTU(slaveConfig.port, {baudRate: slaveConfig.baudRate, parity: slaveConfig.parity})
.then(function() {
console.log("Connected"); })
.catch(function(e) {
if(e.errno) {
if(networkErrors.includes(e.errno)) {
console.log("ModbusRTU need to reconnect");
}
}
console.log(e.message);
});
}
export { client, connectClient }