Skip to content

Commit

Permalink
ZWave check that serial port is open before sending data (openhab#1167)
Browse files Browse the repository at this point in the history
Signed-off-by: Chris Jackson <[email protected]>
  • Loading branch information
cdjackson authored and doubled-ca committed Aug 14, 2016
1 parent 0bbacdd commit 3e2516f
Showing 1 changed file with 16 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -78,18 +78,18 @@ public void initialize() {
try {
CommPortIdentifier portIdentifier = CommPortIdentifier.getPortIdentifier(portId);
CommPort commPort = portIdentifier.open("org.openhab.binding.zwave", 2000);
this.serialPort = (SerialPort) commPort;
this.serialPort.setSerialPortParams(115200, SerialPort.DATABITS_8, SerialPort.STOPBITS_1,
serialPort = (SerialPort) commPort;
serialPort.setSerialPortParams(115200, SerialPort.DATABITS_8, SerialPort.STOPBITS_1,
SerialPort.PARITY_NONE);
this.serialPort.enableReceiveThreshold(1);
this.serialPort.enableReceiveTimeout(ZWAVE_RECEIVE_TIMEOUT);
serialPort.enableReceiveThreshold(1);
serialPort.enableReceiveTimeout(ZWAVE_RECEIVE_TIMEOUT);
logger.debug("Starting receive thread");
this.receiveThread = new ZWaveReceiveThread();
this.receiveThread.start();
receiveThread = new ZWaveReceiveThread();
receiveThread.start();

// RXTX serial port library causes high CPU load
// Start event listener, which will just sleep and slow down event loop
serialPort.addEventListener(this.receiveThread);
serialPort.addEventListener(receiveThread);
serialPort.notifyOnDataAvailable(true);

logger.info("Serial port is initialized");
Expand Down Expand Up @@ -123,9 +123,9 @@ public void dispose() {
}
receiveThread = null;
}
if (this.serialPort != null) {
this.serialPort.close();
this.serialPort = null;
if (serialPort != null) {
serialPort.close();
serialPort = null;
}
logger.info("Stopped ZWave serial handler");

Expand Down Expand Up @@ -302,6 +302,12 @@ public void run() {
@Override
public void sendPacket(SerialMessage serialMessage) {
byte[] buffer = serialMessage.getMessageBuffer();
if (serialPort == null) {
logger.debug("NODE {}: Port closed sending REQUEST Message = {}", serialMessage.getMessageNode(),
SerialMessage.bb2hex(buffer));
return;
}

logger.debug("NODE {}: Sending REQUEST Message = {}", serialMessage.getMessageNode(),
SerialMessage.bb2hex(buffer));

Expand Down

0 comments on commit 3e2516f

Please sign in to comment.