BakeoutController  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 
7 import java.io.IOException;
8 import java.io.InputStream;
9 import java.io.OutputStream;
10 
14 final class RXTXPortWrapper implements SerialPort, PortCommunicator,
15  PortConfiguration {
16 
20  private RXTXPort port;
21 
27  private final String portName;
28 
32  private boolean isPortOpen;
33 
37  RXTXPortWrapper(String portName){
38  this.portName = portName;
39  }
40 
45  @Override public PortCommunicator getCommunicator(){
46  return this;
47  }
48 
54  @Override public InputStream getInputStream() throws IOException {
55  assertPortOpen();
56  return this.port.getInputStream();
57  }
58 
64  @Override public OutputStream getOutputStream() throws IOException {
65  assertPortOpen();
66  return this.port.getOutputStream();
67  }
68 
72  @Override public PortConfiguration getConfig(){
73  return this;
74  }
75 
79  @Override public int getBaudRate(){
80  return this.port.getBaudRate();
81  }
82 
86  @Override public int getStopBits(){
87  return this.port.getStopBits();
88  }
89 
93  @Override public int getDataBits(){
94  return this.port.getDataBits();
95  }
96 
100  @Override public int getParityBits(){
101  return this.port.getParity();
102  }
103 
109  @Override public void setConfig(PortConfiguration newConfig) throws
110  UnsupportedCommOperationException {
111  this.port.setSerialPortParams(
112  newConfig.getBaudRate(), newConfig.getDataBits(),
113  newConfig.getStopBits(), newConfig.getParityBits()
114  );
115  }
116 
121  @Override public void open() throws PortInUseException {
122  if (!this.isPortOpen){
123  this.port = new RXTXPort(this.portName);
124  this.isPortOpen = true;
125  }
126  }
127 
131  @Override public boolean isPortOpen(){
132  return this.isPortOpen;
133  }
134 
138  @Override public void close(){
139  if (this.isPortOpen){
140  this.port.close();
141  this.isPortOpen = false;
142  }
143  }
144 
148  private void assertPortOpen() throws IOException {
149  if(!this.isPortOpen){
150  throw new IOException("Attempted to access a resource that " +
151  "requires the port to be open");
152  }
153  }
154 }
Git Repo