Skip to content

Commit

Permalink
Added ability to run serial tests on Mac/linux using socat serial emu…
Browse files Browse the repository at this point in the history
…lator
  • Loading branch information
steveohara committed Jul 10, 2024
1 parent 0d4fa3f commit 11c2e19
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 23 deletions.
3 changes: 2 additions & 1 deletion RELEASE_NOTES.md
Original file line number Diff line number Diff line change
Expand Up @@ -253,4 +253,5 @@ _(**NOT BACKWARDS COMPATIBLE**)_
* Updated jserialcomm to 2.10.4

## Version 3.2.2
* Add support for providing an optional java.net proxy for. (#151)
* Add support for providing an optional java.net proxy for. (#151)
* Added support for testing serial comms on Mac/Linux
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,9 @@
package com.ghgande.j2mod.modbus.utils;

import com.ghgande.j2mod.modbus.procimg.*;
import com.ghgande.j2mod.modbus.procimg.Record;
import com.ghgande.j2mod.modbus.slave.ModbusSlave;
import com.ghgande.j2mod.modbus.util.Observable;
import com.ghgande.j2mod.modbus.util.Observer;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
* All the master unit tests extend this class so that the system will automatically
Expand All @@ -31,13 +28,12 @@
* @version 2.0 (March 2016)
*/
public class AbstractTestModbus {
private static final Logger logger = LoggerFactory.getLogger(AbstractTestModbus.class);
public static ModbusSlave slave = null;
public static final int UNIT_ID = 15;
public static final int PORT = 2502;
public static final String LOCALHOST = "127.0.0.1";

private static Observer observer = new ObserverMonitor();
private static final Observer observer = new ObserverMonitor();
protected static Observable updatedRegister;
protected static String updatedArgument;

Expand Down Expand Up @@ -162,7 +158,7 @@ protected static SimpleProcessImage getSimpleProcessImage() {
*
* @return True if Windows OS
*/
boolean isWindows() {
static boolean isWindows() {
return System.getProperty("os.name").startsWith("Windows");
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
import com.ghgande.j2mod.modbus.slave.ModbusSlaveFactory;
import com.ghgande.j2mod.modbus.util.SerialParameters;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;

import java.io.IOException;
Expand All @@ -40,11 +39,14 @@ public class AbstractTestModbusSerialASCIIMaster extends AbstractTestModbus {

protected static ModbusSerialMaster master;
protected static ModbusSlave slave;
protected static final String MASTER_PORT = isWindows() ? "CNCA0" : "/dev/ttys001";
protected static final String SLAVE_PORT = isWindows() ? "CNCB0" : "/dev/ttys002";
protected static final String SERIAL_PORT_ERROR = "Cannot initialise serial tests - %s%n" +
"This is likely caused by not having the serial emulator running%n" +
"For Windows, the com2com driver needs to be installed and configured so that CNCA0 is connected to CNCB0.%n" +
"For Mac/linux the socat utility can be used e.g. socat -d -d pty,raw,echo=0 pty,raw,echo=0%n" +
"to connect /dev/ttys001 to /dev/ttys002";

@Before
public void windowsOnly() {
org.junit.Assume.assumeTrue(isWindows());
}

@BeforeClass
public static void setUpSlave() {
Expand All @@ -53,15 +55,15 @@ public static void setUpSlave() {

// Create master
SerialParameters parameters = new SerialParameters();
parameters.setPortName("CNCA0");
parameters.setPortName(MASTER_PORT);
parameters.setOpenDelay(1000);
parameters.setEncoding(Modbus.SERIAL_ENCODING_ASCII);
master = new ModbusSerialMaster(parameters);
master.connect();
}
catch (Exception e) {
tearDownSlave();
fail(String.format("Cannot initialise tests - %s", e.getMessage()));
fail(String.format(SERIAL_PORT_ERROR, e.getMessage()));
}
}

Expand Down Expand Up @@ -92,7 +94,7 @@ static ModbusSlave createSerialSlave(boolean RTU) throws Exception {

// Create a serial slave
SerialParameters parameters = new SerialParameters();
parameters.setPortName("CNCB0");
parameters.setPortName(SLAVE_PORT);
parameters.setOpenDelay(1000);
parameters.setEncoding(RTU ? Modbus.SERIAL_ENCODING_RTU : Modbus.SERIAL_ENCODING_ASCII);
slave = ModbusSlaveFactory.createSerialSlave(parameters);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
import com.ghgande.j2mod.modbus.facade.ModbusSerialMaster;
import com.ghgande.j2mod.modbus.util.SerialParameters;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;

import static org.junit.Assert.fail;
Expand All @@ -33,27 +32,22 @@
*/
public class AbstractTestModbusSerialRTUMaster extends AbstractTestModbusSerialASCIIMaster {

@Before
public void windowsOnly() {
org.junit.Assume.assumeTrue(isWindows());
}

@BeforeClass
public static void setUpSlave() {
try {
slave = createSerialSlave(true);

// Create master
SerialParameters parameters = new SerialParameters();
parameters.setPortName("CNCA0");
parameters.setPortName(MASTER_PORT);
parameters.setOpenDelay(1000);
parameters.setEncoding(Modbus.SERIAL_ENCODING_RTU);
master = new ModbusSerialMaster(parameters);
master.connect();
}
catch (Exception e) {
tearDownSlave();
fail(String.format("Cannot initialise tests - %s", e.getMessage()));
fail(String.format(SERIAL_PORT_ERROR, e.getMessage()));
}
}

Expand Down

0 comments on commit 11c2e19

Please sign in to comment.