-
Notifications
You must be signed in to change notification settings - Fork 0
/
Serial.java
79 lines (61 loc) · 2.07 KB
/
Serial.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
package Communication;
import com.fazecast.jSerialComm.SerialPort;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class Serial {
private final SerialPort comPort;
private BufferedReader in;
public Serial(String portName){
this.comPort = SerialPort.getCommPort("ttyACM0");
this.comPort.setBaudRate(115200);
this.comPort.setNumDataBits(8);
this.comPort.setParity(SerialPort.NO_PARITY);
this.comPort.setNumStopBits(1);
if(this.comPort.openPort()){
this.comPort.setComPortTimeouts(SerialPort.TIMEOUT_READ_BLOCKING, 1000, 0);
this.in = new BufferedReader(new InputStreamReader(this.comPort.getInputStream()));
}
}
public boolean IsOpenPort(){
return this.comPort.openPort();
}
public void readBoardFromSerial(){
char[][] board = new char[8][8];
try {
String msg = this.in.readLine();
System.out.println(msg);
if(msg.length() >= 64){
int idx = 0;
for (int i = 0; i < board.length; i++) {
for (int j = 0; j < board[i].length; j++) {
board[i][j] = msg.charAt(idx);
idx++;
}
}
}
System.out.println("\n----------------");
for (int i = 0; i < board.length; i++) {
for (int j = 0; j < board[i].length; j++) {
System.out.print(board[i][j]);
}
System.out.println();
}
System.out.println("\n----------------");
} catch (IOException e) {
throw new RuntimeException(e);
}
}
public void closePort(){
this.comPort.closePort();
}
public static void main(String[] args){
Serial s = new Serial("ttyACM0");
if(s.IsOpenPort()){
System.out.println("abriu");
while(true){
s.readBoardFromSerial();
}
}
}
}