1 package kernel.serial_ports;
3 import gnu.io.PortInUseException;
4 import gnu.io.RXTXPort;
5 import gnu.io.UnsupportedCommOperationException;
7 import java.io.IOException;
8 import java.io.InputStream;
9 import java.io.OutputStream;
14 final class RXTXPortWrapper
implements SerialPort, PortCommunicator,
20 private RXTXPort port;
27 private final String portName;
32 private boolean isPortOpen;
37 RXTXPortWrapper(String portName){
38 this.portName = portName;
45 @Override
public PortCommunicator getCommunicator(){
54 @Override
public InputStream getInputStream() throws IOException {
56 return this.port.getInputStream();
64 @Override
public OutputStream getOutputStream() throws IOException {
66 return this.port.getOutputStream();
72 @Override
public PortConfiguration getConfig(){
79 @Override
public int getBaudRate(){
80 return this.port.getBaudRate();
86 @Override
public int getStopBits(){
87 return this.port.getStopBits();
93 @Override
public int getDataBits(){
94 return this.port.getDataBits();
100 @Override
public int getParityBits(){
101 return this.port.getParity();
109 @Override
public void setConfig(PortConfiguration newConfig)
throws 110 UnsupportedCommOperationException {
111 this.port.setSerialPortParams(
112 newConfig.getBaudRate(), newConfig.getDataBits(),
113 newConfig.getStopBits(), newConfig.getParityBits()
121 @Override
public void open() throws PortInUseException {
122 if (!this.isPortOpen){
123 this.port =
new RXTXPort(this.portName);
124 this.isPortOpen =
true;
131 @Override
public boolean isPortOpen(){
132 return this.isPortOpen;
138 @Override
public void close(){
139 if (this.isPortOpen){
141 this.isPortOpen =
false;
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");