Switched to use input and output streams
This commit is contained in:
parent
48e0c065b6
commit
4499824965
1 changed files with 38 additions and 7 deletions
|
|
@ -30,6 +30,10 @@ import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
import com.fazecast.jSerialComm.SerialPort;
|
import com.fazecast.jSerialComm.SerialPort;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.io.InputStream;
|
||||||
|
import java.io.OutputStream;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* ZigBeePort implementation using jSerialComm library
|
* ZigBeePort implementation using jSerialComm library
|
||||||
*
|
*
|
||||||
|
|
@ -52,7 +56,10 @@ public class ZigBeeJSerialCommPort implements ZigBeePort {
|
||||||
private final String portName;
|
private final String portName;
|
||||||
private final int baudRate;
|
private final int baudRate;
|
||||||
private final FlowControl flowControl;
|
private final FlowControl flowControl;
|
||||||
|
|
||||||
private SerialPort serialPort;
|
private SerialPort serialPort;
|
||||||
|
private InputStream serialInputstream;
|
||||||
|
private OutputStream serialOutputstream;
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
@ -132,6 +139,9 @@ public class ZigBeeJSerialCommPort implements ZigBeePort {
|
||||||
throw new RuntimeException("Error opening serial port: " + portName);
|
throw new RuntimeException("Error opening serial port: " + portName);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
serialInputstream = serialPort.getInputStream();
|
||||||
|
serialOutputstream = serialPort.getOutputStream();
|
||||||
|
|
||||||
switch (flowControl) {
|
switch (flowControl) {
|
||||||
case FLOWCONTROL_OUT_NONE:
|
case FLOWCONTROL_OUT_NONE:
|
||||||
serialPort.setFlowControl(SerialPort.FLOW_CONTROL_DISABLED);
|
serialPort.setFlowControl(SerialPort.FLOW_CONTROL_DISABLED);
|
||||||
|
|
@ -156,7 +166,11 @@ public class ZigBeeJSerialCommPort implements ZigBeePort {
|
||||||
|
|
||||||
try {
|
try {
|
||||||
logger.info("Closing Serial port: '" + portName + "'");
|
logger.info("Closing Serial port: '" + portName + "'");
|
||||||
|
purgeRxBuffer();
|
||||||
serialPort.closePort();
|
serialPort.closePort();
|
||||||
|
|
||||||
|
serialInputstream = null;
|
||||||
|
serialOutputstream = null;
|
||||||
serialPort = null;
|
serialPort = null;
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
logger.warn("Error closing portName portName: '" + portName + "'", e);
|
logger.warn("Error closing portName portName: '" + portName + "'", e);
|
||||||
|
|
@ -165,7 +179,14 @@ public class ZigBeeJSerialCommPort implements ZigBeePort {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void write(int value) {
|
public void write(int value) {
|
||||||
serialPort.writeBytes(new byte[]{(byte) value}, 1);
|
if (serialPort != null)
|
||||||
|
throw new RuntimeException("Unable to write, Serial port is not open.");
|
||||||
|
|
||||||
|
try {
|
||||||
|
serialOutputstream.write(value);
|
||||||
|
} catch (IOException e) {
|
||||||
|
logger.error("Was unable to write to serial port.", e);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|
@ -175,16 +196,26 @@ public class ZigBeeJSerialCommPort implements ZigBeePort {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public int read(int timeout) {
|
public int read(int timeout) {
|
||||||
byte[] buff = new byte[1];
|
if (serialPort != null)
|
||||||
serialPort.setComPortTimeouts(SerialPort.TIMEOUT_READ_BLOCKING, timeout, 0);
|
throw new RuntimeException("Unable to read, Serial port is not open.");
|
||||||
serialPort.readBytes(buff, 1);
|
|
||||||
return buff[0];
|
try {
|
||||||
|
return serialInputstream.read();
|
||||||
|
} catch (IOException e) {
|
||||||
|
logger.error("Was unable to read from serial port.", e);
|
||||||
|
}
|
||||||
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void purgeRxBuffer() {
|
public void purgeRxBuffer() {
|
||||||
while(serialPort.bytesAwaitingWrite() != 0) {
|
if (serialPort != null)
|
||||||
try {Thread.sleep(100);} catch (InterruptedException e) {e.printStackTrace();}
|
return;
|
||||||
|
|
||||||
|
try {
|
||||||
|
serialOutputstream.flush();
|
||||||
|
} catch (IOException e) {
|
||||||
|
logger.error("Was unable to flush serial data.", e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue