-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathSimpleRead.java
102 lines (93 loc) · 3.71 KB
/
SimpleRead.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
97
98
99
100
101
102
import java.io.*;
import java.util.*;
import javax.comm.*;
public class SimpleRead implements Runnable, SerialPortEventListener {
static CommPortIdentifier portId;
static Enumeration portList;
InputStream inputStream;
OutputStream outputStream;
SerialPort serialPort;
Thread readThread;
public static void main(String[] args) {
try {
CommPortIdentifier portList1 = CommPortIdentifier.getPortIdentifier("/dev/ttyACM0");
System.out.println(portList1.getName() + " | " + portList1.getCurrentOwner() + " | " + portList1.getPortType());
} catch (Exception e) {
e.printStackTrace();
}
portList = CommPortIdentifier.getPortIdentifiers();
while (portList.hasMoreElements()) {
portId = (CommPortIdentifier) portList.nextElement();
System.out.println(portId.getName() + " | " + portId.getCurrentOwner() + " | " + portId.getPortType());
if (portId.getPortType() == CommPortIdentifier.PORT_SERIAL) {
if (portId.getName().contains("ttyACM0")) {
// if (portId.getName().equals("/dev/term/a")) {
SimpleRead reader = new SimpleRead();
}
}
}
}
public SimpleRead() {
try {
serialPort = (SerialPort) portId.open("SimpleReadApp", 2000);
} catch (PortInUseException e) {System.out.println(e);}
try {
inputStream = serialPort.getInputStream();
outputStream = serialPort.getOutputStream();
} catch (IOException e) {System.out.println(e);}
try {
serialPort.addEventListener(this);
} catch (TooManyListenersException e) {System.out.println(e);}
serialPort.notifyOnDataAvailable(true);
try {
serialPort.setSerialPortParams(9600,
SerialPort.DATABITS_8,
SerialPort.STOPBITS_1,
SerialPort.PARITY_NONE);
} catch (UnsupportedCommOperationException e) {System.out.println(e);}
readThread = new Thread(this);
readThread.start();
PubnubUtil.init();
SmartHouse.ardunio = this;
SmartHouse.start();
}
public void write(byte b) {
try {
//System.out.println("Attempting to write " + (char)b);
outputStream.write(new byte[]{b});
} catch (IOException e) {e.printStackTrace();}
}
public void run() {
try {
Thread.sleep(20000);
} catch (InterruptedException e) {System.out.println(e);}
}
public void serialEvent(SerialPortEvent event) {
switch(event.getEventType()) {
case SerialPortEvent.BI:
case SerialPortEvent.OE:
case SerialPortEvent.FE:
case SerialPortEvent.PE:
case SerialPortEvent.CD:
case SerialPortEvent.CTS:
case SerialPortEvent.DSR:
case SerialPortEvent.RI:
case SerialPortEvent.OUTPUT_BUFFER_EMPTY:
break;
case SerialPortEvent.DATA_AVAILABLE:
byte[] readBuffer = new byte[20];
try {
int numBytes = 0;
while (inputStream.available() > 0) {
numBytes = inputStream.read(readBuffer);
// System.out.println(new String(readBuffer));
}
for(int i = 0; i < numBytes; i++) {
SmartHouse.read(readBuffer[i]);
}
// System.out.print(new String(readBuffer) + " " + numBytes + "\n");
} catch (IOException e) {System.out.println(e);}
break;
}
}
}