/
SerialComm.java
executable file
·187 lines (164 loc) · 6.36 KB
/
SerialComm.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
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
/*
* The MIT License
*
* Copyright 2017 KSat e.V.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package com.ksatstuttgart.usoc.controller.communication;
import com.ksatstuttgart.usoc.data.DataSource;
import com.ksatstuttgart.usoc.data.SerialEvent;
import java.awt.event.ActionEvent;
import static java.lang.Thread.sleep;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import jssc.SerialPort;
import jssc.SerialPortException;
import jssc.SerialPortEvent;
import jssc.SerialPortEventListener;
/**
* <h1>SerialComm</h1>
* This class provides methods to communicate via a specified serial port.
* <p>
*
*
* @author Valentin Starlinger
* @version 1.0
*/
public class SerialComm {
private SerialPort serialPort;
private ArrayList<SerialListener> listener = new ArrayList<>();
private static final String[] BAUDRATES = {"38400", "57600"};
private static final String[] COMMANDS = {"Ping", "Testing", "Camtest", "Cameras", "Abort", "Prelaunch", "Active", "Reset", "LaunchMacro"};
private static SerialComm instance;
public static SerialComm getInstance() {
if (instance == null) {
instance = new SerialComm();
}
return instance;
}
public void start(String sp, int baudrate) {
if (!sp.isEmpty()) {
if (serialPort != null && serialPort.isOpened()) {
close();
}
serialPort = new SerialPort(sp);//"/dev/tty.usbserial-A702WKDS");
try {
serialPort.openPort();//Open serial port //serialPort.setParams(9600, 8, 1, 0);
serialPort.setParams(baudrate, SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE);
int mask = SerialPort.MASK_RXCHAR + SerialPort.MASK_CTS + SerialPort.MASK_DSR;//Prepare mask
serialPort.setEventsMask(mask);//Set mask
serialPort.addEventListener(new SerialPortReader());//Add SerialPortEventListener
} catch (SerialPortException e) {
//System.out.println(e);
error("Error: Couldn't open Serialport.\n");
}
}
}
public void close() {
try {
serialPort.closePort();
} catch (SerialPortException ex) {
error("Error: Couldn't close Serialport.");
}
}
public void send(String msg) {
if(!isOpen()){
error("Error: Serial port is not connected!");
return;
}
try {
serialPort.writeBytes(msg.getBytes(StandardCharsets.US_ASCII));
} catch (SerialPortException ex) {
error("Error: Couldn't send message: " + msg + ".");
}
}
public static void sendAction(ActionEvent e) {
System.out.println(SerialCommand.valueOf(e.getActionCommand()).getCommand()+"0\n");
if (SerialComm.getInstance().isOpen()) {
String command = SerialCommand.valueOf(e.getActionCommand()).getCommand()+"0\n";
SerialComm.getInstance().send(command);
}
}
public ArrayList<String> getPorts() {
ArrayList<String> al = new ArrayList<>();
al.addAll(Arrays.asList(jssc.SerialPortList.getPortNames()));
return al;
}
public boolean isOpen() {
return serialPort != null && serialPort.isOpened();
}
public List<String> getAvailableCommands() {
ArrayList<String> commands = new ArrayList<>();
for (SerialCommand command : SerialCommand.values()) {
commands.add(command.toString());
}
return commands;
}
public List<String> getAvailableBaudrates() {
return Arrays.asList(SerialComm.BAUDRATES);
}
/**
* This class reads chars from the serial port and reports new incoming
* data. Incoming data is detected by newLine characters.
*/
private class SerialPortReader implements SerialPortEventListener {
String buffer = "";
@Override
public void serialEvent(SerialPortEvent event) {
if (event.isRXCHAR()) {//If data is available
try {
for (byte c : serialPort.readBytes(event.getEventValue())) {
//every time a newLine char is read, send the previous chars
//to all listeners
if (c == '\n') {
messageReceived(new SerialEvent(buffer, event.getPortName(),
System.currentTimeMillis(), DataSource.SERIAL));
System.out.println(buffer);
buffer = "";
} else {
buffer += (char)c;
}
}
} catch (SerialPortException ex) {
error("Error: Couldn't read incoming Bytes.");
}
}
}
}
private void messageReceived(SerialEvent e) {
for (SerialListener l : listener) {
l.messageReceived(e);
}
}
private void error(String msg) {
for (SerialListener l : listener) {
l.error(msg);
}
}
public void addSerialListener(SerialListener l) {
listener.add(l);
}
public void removeSerialListener(SerialListener l) {
listener.remove(l);
}
}