-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- refactoring to use jSerialComm instead of rxtx
- adding reconnect support to serial port
- Loading branch information
Pavel Chuchma
authored and
Pavel Chuchma
committed
May 21, 2023
1 parent
98a9a23
commit 0c3e94a
Showing
9 changed files
with
185 additions
and
151 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
version=0.2.0-SNAPSHOT | ||
version=0.3.0-SNAPSHOT |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
89 changes: 89 additions & 0 deletions
89
...ontroller/src/main/java/org/chuma/hvaccontroller/device/AbstractSerialPortConnection.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
package org.chuma.hvaccontroller.device; | ||
|
||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import java.io.OutputStream; | ||
import java.util.Arrays; | ||
|
||
import com.fazecast.jSerialComm.SerialPort; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
public abstract class AbstractSerialPortConnection { | ||
static Logger log = LoggerFactory.getLogger(AbstractSerialPortConnection.class.getName()); | ||
protected SerialPort serialPort; | ||
protected InputStream inputStream = null; | ||
protected OutputStream outputStream = null; | ||
protected final String portName; | ||
protected boolean running = false; | ||
|
||
public AbstractSerialPortConnection(String portName) { | ||
this.portName = portName; | ||
} | ||
|
||
protected abstract void initializePort() throws IOException; | ||
|
||
protected void openSerialPort() throws IOException { | ||
log.debug("Opening serial port {}", portName); | ||
String portList = String.join(", ", Arrays.stream(SerialPort.getCommPorts()).map(sp -> "[" + sp.getSystemPortPath() + "] " + sp.getDescriptivePortName()).toArray(CharSequence[]::new)); | ||
log.info("Existing serial ports: {}", portList); | ||
if (serialPort != null) { | ||
serialPort.closePort(); | ||
} | ||
|
||
serialPort = SerialPort.getCommPort(portName); | ||
|
||
if (!serialPort.openPort()) { | ||
throw new IOException("Failed to open port " + portName + ", existing ports are: " + portList); | ||
} | ||
} | ||
|
||
public void start() { | ||
running = true; | ||
new Thread(() -> { | ||
log.debug("Starting serial port reader thread"); | ||
try { | ||
while (running) { | ||
while (inputStream == null) { | ||
try { | ||
openSerialPort(); | ||
initializePort(); | ||
inputStream = serialPort.getInputStream(); | ||
outputStream = serialPort.getOutputStream(); | ||
} catch (Exception e) { | ||
log.error("Port initialization failed, going to sleep for a moment before retry", e); | ||
try { | ||
//noinspection BusyWait | ||
Thread.sleep(10_000); | ||
} catch (InterruptedException ignored) { | ||
} | ||
} | ||
} | ||
|
||
try { | ||
readImpl(); | ||
} catch (IOException e) { | ||
log.error("Read failed", e); | ||
inputStream = null; | ||
outputStream = null; | ||
} | ||
} | ||
} finally { | ||
log.debug("Serial port reader thread finished"); | ||
} | ||
}, "ComRead-" + portName).start(); | ||
} | ||
|
||
public void stop() { | ||
running = false; | ||
if (serialPort != null) { | ||
serialPort.closePort(); | ||
serialPort = null; | ||
inputStream = null; | ||
outputStream = null; | ||
log.debug("Serial port listener closed"); | ||
} | ||
} | ||
|
||
protected abstract void readImpl() throws IOException; | ||
} |
114 changes: 44 additions & 70 deletions
114
hvac-controller/src/main/java/org/chuma/hvaccontroller/device/HvacConnector.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.