-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathModbusServer.java
96 lines (86 loc) · 2.99 KB
/
ModbusServer.java
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
import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.sql.Timestamp;
public class ModbusServer implements Runnable {
private Engine engine;
public Socket node;
private boolean close = false;
public ModbusServer(Engine engine) throws IOException {
this.engine = engine;
}
public void run() {
ServerSocket ss;
try {
ss = new ServerSocket(502);
while (true) {
node = ss.accept();
String time = new Timestamp(System.currentTimeMillis())
.toString();
System.out.println(time + " - Connected node "
+ node.getInetAddress().getHostAddress());
InputStream in = node.getInputStream();
this.close=false;
while (true) {
while (in.available() == 0) {
Thread.currentThread().sleep(100);
if (close)
break;
}
Thread.currentThread().sleep(100);
if (close)
break;
byte[] data = in.readNBytes(in.available());
String hex = Engine.bytesToHex(data);
time = new Timestamp(System.currentTimeMillis()).toString();
System.out.println(time + " - Node: " + hex);
int res=engine.ncli.sendData(data);
// engine.mqtt.sendMsg("data", hex);
engine.lastData = data;
if(res==-1) break;
}
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public int sendData(byte[] data) throws InterruptedException {
if (this.node == null) {
String time = new Timestamp(System.currentTimeMillis()).toString();
System.out.println(time + " - Waiting for node ...");
while (node == null)
Thread.currentThread().sleep(100);
}
try {
if (this.node.getOutputStream() == null) {
this.node=null;
this.close=true;
return -1;
}
if (!node.isConnected()) {
this.node=null;
this.close=true;
return -1;
}
OutputStream out = this.node.getOutputStream();
out.write(data);
out.flush();
} catch (IOException e) {
e.printStackTrace();
try {
this.node.close();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
this.node=null;
this.close=true;
return -1;
}
return 0;
}
}