Skip to content

Commit

Permalink
make speed settable (nanoCUL uses 38400)
Browse files Browse the repository at this point in the history
  • Loading branch information
aploese committed Jan 4, 2024
1 parent 2d5451d commit 8f2778a
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 14 deletions.
2 changes: 1 addition & 1 deletion de.ibapl.fhz4j.console/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<parent>
<groupId>de.ibapl.fhz4j</groupId>
<artifactId>fhz4j-project</artifactId>
<version>2.1.1-SNAPSHOT</version>
<version>2.2.0-SNAPSHOT</version>
<relativePath>..</relativePath>
</parent>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
import de.ibapl.fhz4j.protocol.lacrosse.tx2.LaCrosseTx2Message;
import de.ibapl.spsw.api.SerialPortSocket;
import de.ibapl.spsw.api.SerialPortSocketFactory;
import de.ibapl.spsw.api.Speed;
import de.ibapl.spsw.logging.LoggingSerialPortSocket;
import de.ibapl.spsw.logging.TimeStampLogging;
import de.ibapl.spsw.ser2net.Ser2NetProvider;
Expand Down Expand Up @@ -235,6 +236,11 @@ public static void main(String[] args) throws FileNotFoundException, IOException
opt.setType(String.class);
optg.addOption(opt);

opt = new Option("s", "speed", true, "serial port speed (baudrate)");
opt.setArgName("speed");
opt.setType(int.class);
optg.addOption(opt);

opt = new Option("r", "ser2net", true, "ser2net host:port");
opt.setArgName("ser2net");
opt.setType(String.class);
Expand Down Expand Up @@ -304,32 +310,40 @@ public static void main(String[] args) throws FileNotFoundException, IOException

final File logFile = File.createTempFile("cul_", ".txt", Paths.get(".").toFile());

final Speed speed;
if (cmd.hasOption("speed")) {
speed = Speed.fromNative(Integer.parseInt(cmd.getOptionValue("speed")));
} else {
speed = Speed._9600_BPS;
}

if (cmd.hasOption("ser2net")) {
new Main().runSer2Net(cmd.getOptionValue("ser2net"), protocols, logFile);
new Main().runSer2Net(cmd.getOptionValue("ser2net"), protocols, speed, logFile);
}

if (cmd.hasOption("port")) {
new Main().runLocalPort(cmd.getOptionValue("port"), protocols, logFile);
new Main().runLocalPort(cmd.getOptionValue("port"), protocols, speed, logFile);
}

}

private final Set<Short> DEVICES_HOME_CODE = new HashSet<>();

public void runSer2Net(String ser2net, Set<Protocol> protocols, File logFile) {
public void runSer2Net(String ser2net, Set<Protocol> protocols, Speed speed, File logFile) {
try {
LOG.log(Level.INFO, "LOG File: {0}", logFile.getAbsolutePath());
String[] split = ser2net.split(":");
SerialPortSocket serialPort = LoggingSerialPortSocket.wrapWithAsciiOutputStream(new Ser2NetProvider(split[0], Integer.parseInt(split[1])), new FileOutputStream(logFile), false, TimeStampLogging.NONE);
run(serialPort, protocols);
run(serialPort, protocols, speed);
} catch (Exception ex) {
throw new RuntimeException(ex);
}
}

public void runLocalPort(String port, Set<Protocol> protocols, File logFile) {
public void runLocalPort(String port, Set<Protocol> protocols, Speed speed, File logFile) {
try {
ServiceLoader<SerialPortSocketFactory> sl = ServiceLoader.load(SerialPortSocketFactory.class);
ServiceLoader<SerialPortSocketFactory> sl = ServiceLoader.load(SerialPortSocketFactory.class
);
Iterator<SerialPortSocketFactory> i = sl.iterator();
if (!i.hasNext()) {
throw new RuntimeException("No provider for SerialPortSocketFactory found, pleas add one to you class path ");
Expand All @@ -338,15 +352,15 @@ public void runLocalPort(String port, Set<Protocol> protocols, File logFile) {

LOG.log(Level.INFO, "LOG File: {0}", logFile.getAbsolutePath());
SerialPortSocket serialPort = LoggingSerialPortSocket.wrapWithAsciiOutputStream(serialPortSocketFactory.open(port), new FileOutputStream(logFile), false, TimeStampLogging.UTC);
run(serialPort, protocols);
run(serialPort, protocols, speed);
} catch (Exception ex) {
throw new RuntimeException(ex);
}
}

public void run(SerialPortSocket serialPortSocket, Set<Protocol> protocols) throws Exception {
public void run(SerialPortSocket serialPortSocket, Set<Protocol> protocols, Speed speed) throws Exception {
final FhzListener listener = new FhzListener();
try (CulAdapter culAddapter = new CulAdapter(serialPortSocket, listener)) {
try (CulAdapter culAddapter = new CulAdapter(serialPortSocket, listener, speed)) {
listener.fhzAdapter = culAddapter;
try {
try {
Expand Down
2 changes: 1 addition & 1 deletion de.ibapl.fhz4j.parser.cul/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<parent>
<groupId>de.ibapl.fhz4j</groupId>
<artifactId>fhz4j-project</artifactId>
<version>2.1.1-SNAPSHOT</version>
<version>2.2.0-SNAPSHOT</version>
<relativePath>..</relativePath>
</parent>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,12 +117,24 @@ public void run() {
* @param serialPortSocket
* @param fhzDataListener
*/
public CulAdapter(SerialPortSocket serialPortSocket, CulMessageListener fhzDataListener) throws IOException {
@Deprecated
private CulAdapter(SerialPortSocket serialPortSocket, CulMessageListener fhzDataListener) throws IOException {
this(serialPortSocket, fhzDataListener, Speed._9600_BPS);
}

/**
* Create a CUL adapter and initialize the serial port with (speed),8,n,1.
*
* @param serialPortSocket
* @param fhzDataListener
* @param sped
*/
public CulAdapter(SerialPortSocket serialPortSocket, CulMessageListener fhzDataListener, Speed sped) throws IOException {
if (!serialPortSocket.isOpen()) {
throw new IllegalStateException("serial port " + serialPortSocket.getPortName() + " is not open");
}

serialPortSocket.setSpeed(Speed._9600_BPS);
serialPortSocket.setSpeed(sped);
serialPortSocket.setDataBits(DataBits.DB_8);
serialPortSocket.setParity(Parity.NONE);
serialPortSocket.setStopBits(StopBits.SB_1);
Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<artifactId>fhz4j-project</artifactId>

<packaging>pom</packaging>
<version>2.1.1-SNAPSHOT</version>
<version>2.2.0-SNAPSHOT</version>
<name>FHZ4J</name>
<inceptionYear>2009</inceptionYear>
<url>https://github.com/aploese/fhz4j</url>
Expand Down

0 comments on commit 8f2778a

Please sign in to comment.