-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathSerialPortListener.java
105 lines (90 loc) · 2.85 KB
/
SerialPortListener.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
103
104
105
package com.farrellf.TelemetryGUI;
/**
* Establishes RS232 communication and perpetually listens for incoming data.
*
* @author Farrell Farahbod
* @version 1.0
*/
import gnu.io.*;
import java.io.*;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;
import java.util.Scanner;
public class SerialPortListener implements Runnable {
private Database db;
private SerialPort rs232;
private InputStream rs232istream;
private Scanner rs232scanner;
private String line;
private String value;
private int number;
public SerialPortListener(Database db) {
this.db = db;
}
/**
* Get a list of available serial ports.
*
* @return An array representing the ports
*/
public Object[] getSerialPorts() {
List<String> list = new ArrayList<String>();
@SuppressWarnings("unchecked")
Enumeration<CommPortIdentifier> ports = CommPortIdentifier.getPortIdentifiers();
while(ports.hasMoreElements()) {
CommPortIdentifier port = ports.nextElement();
if(port.getPortType() == CommPortIdentifier.PORT_SERIAL)
list.add(port.getName());
}
return list.toArray();
}
/**
* Attempt to create an RS232 link.
*
* @param port String name, for example: /dev/ttyUSB0
* @param baudRate Baud rate
* @return Returns true if link was created
*/
public Boolean establishConnection(String port, int baudRate) {
try {
rs232 = (SerialPort) CommPortIdentifier.getPortIdentifier(port).open("Robot Telemetry", 20000); // appname, timeout in ms
rs232.setSerialPortParams(baudRate, SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE);
rs232istream = rs232.getInputStream();
rs232scanner = new Scanner(rs232istream);
System.out.println("Connected to " + port + " at " + baudRate + " baud.");
return true;
} catch (Exception e) {
System.err.println("Error setting up serial communications with port " + port + ".");
System.err.println(e.getClass().toString());
return false;
}
}
/**
* A separate thread to constantly monitor the RS232 link.
*/
@Override
public void run() {
while(true) {
try {
// interpret incoming lines of text
line = rs232scanner.nextLine();
if(line.length() == 65)
line = line.substring(3); // trim \x1B[H ASCII escape sequence
if(line.length() != 62)
continue; // empty or corrupt line
try {
value = line.substring(12, 18);
number = Integer.parseInt(value);
// populate the db
db.addValue(line.substring(0, line.indexOf(" ")), number);
//System.out.println("line = \"" + line + "\", value = " + value + ", number = " + number);
} catch(Exception e) {
//System.err.println("Error processing line: \"" + line + "\"");
}
} catch(Exception e) {
System.err.println("One or more errors occured.");
System.err.println(e.getClass().toString());
}
}
}
}