-
Notifications
You must be signed in to change notification settings - Fork 0
/
CommunicateArduino
153 lines (126 loc) · 4.01 KB
/
CommunicateArduino
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
/** Classe encarregada de trocar informações entre o programa central e os dispositivos.
* Isto é realizado por meio de comunicação por portas seriais via rede Bluetooth.
*/
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import gnu.io.CommPortIdentifier;
import gnu.io.PortInUseException;
import gnu.io.SerialPort;
import gnu.io.SerialPortEvent;
import gnu.io.SerialPortEventListener;
import gnu.io.UnsupportedCommOperationException;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.TooManyListenersException;
public class CommunicateArduino {
/** The port we're normally going to use. */
private static final String PORT_NAMES[] = {
//"/dev/tty.wchusbserial1420", // Mac OS X
//"/dev/tty.Bluetooth-Incoming-Port",
"/dev/tty.HC-05-DevB",
// "/dev/ttyACM0", // Raspberry Pi
//"/dev/ttyUSB0", // Linux
//"COM3", // Windows
};
public static InputStream input;
public static OutputStream output;
private static final int TIME_OUT = 2000;
private static final int DATA_RATE = 9600;
public static ArrayList<Byte> bytesRead = new ArrayList<Byte>();
// Contem os serialPort
ArrayList<SerialPort> ports = new ArrayList<SerialPort>();
// acha e inicia a conexão com as portas seriais (dispositivos)
public void initialize() {
Enumeration<?> portEnum = CommPortIdentifier.getPortIdentifiers();
// acha uma instância de porta serial
while (portEnum.hasMoreElements()) {
CommPortIdentifier currPortId = (CommPortIdentifier) portEnum.nextElement();
String currPortName = currPortId.getName();
if (currPortId.getPortType() == CommPortIdentifier.PORT_SERIAL && //se for porta serial
currPortName.contains("/dev/tty.HC-05")) { //se o nome ainda nao existir
SerialPort serialPort;
try {
serialPort = (SerialPort) currPortId.open(this.getClass().getName(), TIME_OUT);
serialPort.setSerialPortParams(DATA_RATE,
SerialPort.DATABITS_8,
SerialPort.STOPBITS_1,
SerialPort.PARITY_NONE);
serialPort.notifyOnDataAvailable(true);
serialPort.addEventListener(new SinglePort(currPortName, serialPort));
ports.add(serialPort);
} catch (Exception e) {
System.err.println(e.toString() + " in port: " + currPortName);
continue;
}
}
}
}
// fecha as portas abertas
public synchronized void close() {
for (SerialPort sp: ports) {
if (sp != null) {
sp.removeEventListener();
sp.close();
}
}
}
// adiciona os dados recebidos do dispositivo
public void addData(byte data) {
bytesRead.add(data);
}
// verifica se há dados disponíveis
public boolean dataAvailable() {
if (bytesRead.size() >= 5) {
return true;
}
return false;
}
// retorna os valores guardados
public InputData getInputValue() {
if (dataAvailable()) {
int inputDeviceNum = bytesRead.get(0);
long inputTime = bytesRead.get(1) & 0xFF;
inputTime |= (bytesRead.get(2) << 8) & 0xFFFF;
inputTime |= (bytesRead.get(3) << 16) & 0xFFFFFF;
inputTime |= (bytesRead.get(4) << 24) & 0xFFFFFFFF;
bytesRead.remove(4);
bytesRead.remove(3);
bytesRead.remove(2);
bytesRead.remove(1);
bytesRead.remove(0);
return new InputData(inputDeviceNum, inputTime);
}
return new InputData(7, 0);
}
// limpa a lista
public void clearInputData() {
bytesRead.clear();
}
// envia um comando ao dispositivo
public static synchronized void writeData(int data) {
try {
output.write(data);
output.flush();
} catch (Exception e) {
System.out.println("Could not write to port.");
}
}
// retorna a porta serial
public OutputStream getOutput(int i) {
try {
return ports.get(i).getOutputStream();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
// retorna o numero de dispositivos conectados
public int numConnections() {
return ports.size();
}
// retorna o número de informações recebidas dos dispositivos
public int numInputsRead() {
return bytesRead.size() / 5;
}
}