-
Notifications
You must be signed in to change notification settings - Fork 0
/
ComPortJava.java
74 lines (60 loc) · 2.05 KB
/
ComPortJava.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
#http://www.kuligowski.pl/java/rs232-in-java-for-windows,1
import java.io.IOException;
import java.io.OutputStream;
public class CommPortSender {
static OutputStream out;
public static void setWriterStream(OutputStream out) {
CommPortSender.out = out;
}
public static void send(byte[] bytes) {
try {
System.out.println("SENDING: " + new String(bytes, 0, bytes.length));
// sending through serial port is simply writing into OutputStream
out.write(bytes);
out.flush();
} catch (IOException e) {
e.printStackTrace();
}
}
}
public class ProtocolImpl implements Protocol {
byte[] buffer = new byte[1024];
int tail = 0;
public void onReceive(byte b) {
// simple protocol: each message ends with new line
if (b=='\n') {
onMessage();
} else {
buffer[tail] = b;
tail++;
}
}
public void onStreamClosed() {
onMessage();
}
/*
* When message is recognized onMessage is invoked
*/
private void onMessage() {
if (tail!=0) {
// constructing message
String message = getMessage(buffer, tail);
System.out.println("RECEIVED MESSAGE: " + message);
// this logic should be placed in some kind of
// message interpreter class not here
if ("HELO".equals(message)) {
CommPortSender.send(getMessage("OK"));
} else if ("OK".equals(message)) {
CommPortSender.send(getMessage("OK ACK"));
}
tail = 0;
}
}
// helper methods
public byte[] getMessage(String message) {
return (message+"\n").getBytes();
}
public String getMessage(byte[] buffer, int len) {
return new String(buffer, 0, tail);
}
}