-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathModbusClient.java
More file actions
67 lines (57 loc) · 2.09 KB
/
ModbusClient.java
File metadata and controls
67 lines (57 loc) · 2.09 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
67
import java.io.BufferedInputStream;
import java.io.BufferedWriter;
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 ModbusClient implements Runnable {
private Engine engine;
private Socket srv;
public ModbusClient(Engine engine) throws IOException {
this.engine = engine;
}
public void run() {
while (true) {
try {
srv=new Socket(engine.realModbusServer,502);
String time = new Timestamp(System.currentTimeMillis()).toString();
System.out.println(time + " - Connected to server "
+ srv.getInetAddress().getHostAddress());
InputStream in=srv.getInputStream();
while (true) {
while (in.available() == 0) {
Thread.currentThread().sleep(100);
}
byte[] data=in.readNBytes(in.available());
String hex=Engine.bytesToHex(data);
time = new Timestamp(System.currentTimeMillis()).toString();
System.out.println(time+" - Server: "+hex);
int ret=engine.nsrv.sendData(data);
if(ret==-1) break;
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
public int sendData(byte[] data) throws InterruptedException {
if(this.srv==null) {
String time = new Timestamp(System.currentTimeMillis()).toString();
System.out.println(time + " - Waiting for server ...");
while(srv==null) Thread.currentThread().sleep(100);
}
try {
if(this.srv.getOutputStream()==null) return -1;
OutputStream out=this.srv.getOutputStream();
out.write(data);
out.flush();
} catch (IOException e) {
e.printStackTrace();
return -1;
}
return 0;
}
}