BakeoutController-Basic  0.1
A Controller for the Omicron vacuum chamber
RXTXPortWrapper.java
1 package kernel.serial_ports;
2 
3 import gnu.io.PortInUseException;
4 import gnu.io.RXTXPort;
5 import gnu.io.UnsupportedCommOperationException;
6 import org.jetbrains.annotations.Contract;
7 import org.slf4j.Logger;
8 import org.slf4j.LoggerFactory;
9 
10 import java.io.IOException;
11 import java.io.InputStream;
12 import java.io.OutputStream;
13 
17 public final class RXTXPortWrapper implements SerialPort, PortCommunicator,
19 
23  private static Logger log = LoggerFactory.getLogger(RXTXPortWrapper.class);
24 
28  private RXTXPort port;
29 
35  private final String portName;
36 
40  private boolean isPortOpen;
41 
45  private PortConfiguration desiredPortConfiguration;
46 
51  private Thread shutdownThread;
52 
56  RXTXPortWrapper(String portName){
57  writeLogEntryForInitialization(portName);
58  this.portName = portName;
59  }
60 
65  @Contract(pure = true)
66  @Override public PortCommunicator getCommunicator(){
67  return this;
68  }
69 
75  @Override public InputStream getInputStream() throws IOException {
76  assertPortOpen();
77  writeLogEntryForInputStreamRequest();
78  return this.port.getInputStream();
79  }
80 
86  @Override public OutputStream getOutputStream() throws IOException {
87  assertPortOpen();
88  writeLogEntryForOutputStreamRequest();
89  return this.port.getOutputStream();
90  }
91 
95  @Contract(pure = true)
96  @Override public PortConfiguration getConfig(){
97  return this;
98  }
99 
103  @Contract(pure = true)
104  @Override public int getBaudRate(){
105  return this.port.getBaudRate();
106  }
107 
111  @Contract(pure = true)
112  @Override public int getStopBits(){
113  return this.port.getStopBits();
114  }
115 
119  @Contract(pure = true)
120  @Override public int getDataBits(){
121  return this.port.getDataBits();
122  }
123 
127  @Contract(pure = true)
128  @Override public int getParityBits(){
129  return this.port.getParity();
130  }
131 
136  @Override public void setConfig(PortConfiguration newConfig) {
137  this.desiredPortConfiguration = newConfig;
138  }
139 
146  @Override public void open() throws PortInUseException,
147  UnsupportedCommOperationException {
148  if (!this.isPortOpen){
149  this.port = new RXTXPort(this.portName);
150  this.isPortOpen = true;
151 
152  shutdownThread = new PortShutdown(this);
153  Runtime.getRuntime().addShutdownHook(shutdownThread);
154 
155  try {
156  setConfigurationParametersForOpenPort();
157  } catch (IOException error){
158  throw new UnsupportedCommOperationException(
159  "Unable to set port configuration parameters"
160  );
161  }
162 
163  writeLogEntryForPortOpen();
164  }
165  }
166 
170  @Override public boolean isPortOpen(){
171  return this.isPortOpen;
172  }
173 
177  @Override public void close(){
178  if (this.isPortOpen){
179  this.port.close();
180  this.isPortOpen = false;
181 
182  Runtime.getRuntime().removeShutdownHook(shutdownThread);
183 
184  writeLogEntryForPortClosed();
185  }
186  }
187 
191  private void assertPortOpen() throws IOException {
192  if (!this.isPortOpen){
193  throw new IOException("Attempted to access a resource that " +
194  "requires the port to be open");
195  }
196  }
197 
205  private void setConfigurationParametersForOpenPort() throws
206  UnsupportedCommOperationException, IOException {
207  assertPortOpen();
208  int baudRate = desiredPortConfiguration.getBaudRate();
209  int dataBits = desiredPortConfiguration.getDataBits();
210  int stopBits = desiredPortConfiguration.getStopBits();
211  int parityBits = desiredPortConfiguration.getParityBits();
212 
213  this.port.setSerialPortParams(
214  baudRate, dataBits, stopBits, parityBits
215  );
216 
217  writeLogEntryForParametersSet(baudRate, dataBits, stopBits,
218  parityBits);
219  }
220 
224  private void writeLogEntryForInitialization(String portName){
225  log.debug(
226  "Port wrapper initialized for RS232 port with name {}",
227  portName
228  );
229  }
230 
235  private void writeLogEntryForInputStreamRequest(){
236  log.debug(
237  "Input stream requested for RS232 port {}", this.portName
238  );
239  }
240 
245  private void writeLogEntryForOutputStreamRequest(){
246  log.debug(
247  "Output stream requested for RS232 port {}", this.portName
248  );
249  }
250 
261  private void writeLogEntryForParametersSet(
262  int baudRate, int dataBits, int stopBits, int parityBits
263  ){
264  String parameterTemplate = "Parameter {} for port {} was set to {}.";
265 
266  log.info(
267  parameterTemplate, "Baud Rate", portName, baudRate
268  );
269  log.info(
270  parameterTemplate, "Data Bits", portName, dataBits
271  );
272  log.info(
273  parameterTemplate, "Stop Bits", portName, stopBits
274  );
275  log.info(
276  parameterTemplate, "Stop Bits", portName, parityBits
277  );
278  }
279 
280  private void writeLogEntryForPortOpen(){
281  log.info("Port {} was opened.", portName);
282  }
283 
284  private void writeLogEntryForPortClosed(){
285  log.info("Port {} was closed", portName);
286  }
287 
292  private final class PortShutdown extends Thread {
293 
297  private final Logger log = LoggerFactory.getLogger(PortShutdown.class);
298 
302  private final SerialPort port;
303 
307  public PortShutdown(SerialPort port){
308  this.port = port;
309  }
310 
314  @Override
315  public void run() {
316  log.info("Port {} caught shutdown signal. Closing", portName);
317  this.port.close();
318  log.info("Port {} successfully closed", portName);
319  }
320  }
321 }
void setConfig(PortConfiguration newConfig)
Git Repo