<istrue value="${not.archive.disabled}"/>
</or>
</condition>
- <condition property="do.mkdist">
- <and>
- <isset property="do.archive"/>
- <isset property="libs.CopyLibs.classpath"/>
- <not>
- <istrue value="${mkdist.disabled}"/>
- </not>
- <not>
- <istrue value="${modules.supported.internal}"/>
- </not>
- </and>
- </condition>
<condition property="do.archive+manifest.available">
<and>
<isset property="manifest.available"/>
<attribute name="SplashScreen-Image" value="META-INF/${splashscreen.basename}"/>
</manifest>
</target>
- <target depends="init,-init-macrodef-copylibs,compile,-pre-pre-jar,-pre-jar,-do-jar-create-manifest,-do-jar-copy-manifest,-do-jar-set-mainclass,-do-jar-set-profile,-do-jar-set-splashscreen" if="do.mkdist" name="-do-jar-copylibs">
+ <target depends="init,compile" name="-check-do-mkdist">
+ <condition property="do.mkdist">
+ <and>
+ <isset property="do.archive"/>
+ <isset property="libs.CopyLibs.classpath"/>
+ <not>
+ <istrue value="${mkdist.disabled}"/>
+ </not>
+ <not>
+ <available file="${build.classes.dir}/module-info.class"/>
+ </not>
+ </and>
+ </condition>
+ </target>
+ <target depends="init,-init-macrodef-copylibs,compile,-pre-pre-jar,-pre-jar,-do-jar-create-manifest,-do-jar-copy-manifest,-do-jar-set-mainclass,-do-jar-set-profile,-do-jar-set-splashscreen,-check-do-mkdist" if="do.mkdist" name="-do-jar-copylibs">
<j2seproject3:copylibs manifest="${tmp.manifest.file}"/>
<echo level="info">To run this application from the command line without Ant, try:</echo>
<property location="${dist.jar}" name="dist.jar.resolved"/>
<echo level="info">java -jar "${dist.jar.resolved}"</echo>
</target>
- <target depends="init,compile,-pre-pre-jar,-pre-jar,-do-jar-create-manifest,-do-jar-copy-manifest,-do-jar-set-mainclass,-do-jar-set-profile,-do-jar-set-splashscreen" if="do.archive" name="-do-jar-jar" unless="do.mkdist">
+ <target depends="init,compile,-pre-pre-jar,-pre-jar,-do-jar-create-manifest,-do-jar-copy-manifest,-do-jar-set-mainclass,-do-jar-set-profile,-do-jar-set-splashscreen,-check-do-mkdist" if="do.archive" name="-do-jar-jar" unless="do.mkdist">
<j2seproject1:jar manifest="${tmp.manifest.file}"/>
<property location="${build.classes.dir}" name="build.classes.dir.resolved"/>
<property location="${dist.jar}" name="dist.jar.resolved"/>
-
package modbus;
-import modbus.frames.ModbusFrame;
-import at.htlkaindorf.mechatronik.serial.SimpleSerial;
-import gnu.io.SerialPort;
+import com.fazecast.jSerialComm.SerialPort;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.IOException;
-import java.util.Arrays;
import java.util.List;
+
+import modbus.frames.ModbusFrame;
import modbus.frames.ModbusFrameReadCoil;
import modbus.frames.ModbusFrameReadDiscreteInput;
import modbus.frames.ModbusFrameReadHoldingRegisters;
import modbus.frames.ModbusFrameWriteMultipleRegisters;
import modbus.frames.ModbusFrameWriteRegister;
-
/**
*
* @author sx
*/
-public class ModbusTest
-{
- private SimpleSerial serial;
- private BufferedInputStream is;
- private BufferedOutputStream os;
- byte [] lastResponse;
-
-
- public ModbusTest (String port) throws Exception
- {
- serial = new SimpleSerial(port);
- serial.open();
- serial.setSerialPortParams(57600, SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE);
- is = new BufferedInputStream(serial.getInputStream());
- os = new BufferedOutputStream(serial.getOutputStream());
- }
-
- public void close ()
- {
- if (serial != null)
- serial.close();
- }
-
-
- private void sendModbusFrame (ModbusFrame requestFrame) throws IOException
- {
- os.write(requestFrame.getSlaveAddress());
- os.write(requestFrame.getFuctionCode());
- byte [] data = requestFrame.getData();
- if (data != null)
- os.write(data);
- os.write(requestFrame.getCrcBytes());
- os.flush();
-
- System.out.print(String.format("request: %02x %02x", requestFrame.getSlaveAddress(), requestFrame.getFuctionCode()));
- for (byte b : data)
- System.out.print(String.format(" %02x", b<0? b+256 : b));
- byte [] ba = requestFrame.getCrcBytes();
- for (byte b : ba)
- System.out.print(String.format(" %02x", b<0? b+256 : b));
- System.out.println();
- }
-
- private void readResponse() throws InterruptedException, IOException
- {
- Thread.sleep(500);
- if (is.available()>0)
- {
- byte [] data = new byte [is.available()];
- is.read(data);
- lastResponse = data;
- //String s = new String (data);
- //System.out.println(s);
- System.out.print("response:");
- for (byte b : data)
- System.out.print(String.format(" %02x", b));
- System.out.println();
+public class ModbusTest {
+
+ private final SerialPort serialPort;
+ private final BufferedInputStream is;
+ private final BufferedOutputStream os;
+ byte[] lastResponse;
+
+ public ModbusTest (SerialPort serialPort) throws Exception {
+ this.serialPort = serialPort;
+ this.serialPort.openPort();
+ this.serialPort.setComPortParameters(57600, 8, SerialPort.ONE_STOP_BIT, SerialPort.NO_PARITY);
+ is = new BufferedInputStream(this.serialPort.getInputStream());
+ os = new BufferedOutputStream(this.serialPort.getOutputStream());
+ System.out.println();
+ System.out.println("Try serial port " + serialPort.getSystemPortName());
+ System.out.println("--------------------------------------------");
}
- }
-
-
- public byte[] getLastResponse()
- {
- return lastResponse;
- }
-
- public String getLastResponseString (int startIndex)
- {
- StringBuilder sb = new StringBuilder();
- for (int i=startIndex; i<lastResponse.length; i++)
- {
- byte b = lastResponse[i];
- if (b==0)
- break;
- if (b>=20 && b<127)
- sb.append((char)b);
- else
- sb.append('.');
+
+ public void close () {
+ if (serialPort != null && serialPort.isOpen()) {
+ serialPort.closePort();
+ }
+ }
+
+ private void sendModbusFrame (ModbusFrame requestFrame) throws IOException {
+ try {
+ os.write(requestFrame.getSlaveAddress());
+ os.write(requestFrame.getFuctionCode());
+ byte[] data = requestFrame.getData();
+ if (data != null) {
+ os.write(data);
+ }
+ os.write(requestFrame.getCrcBytes());
+ os.flush();
+
+ System.out.print(String.format("request: %02x %02x", requestFrame.getSlaveAddress(), requestFrame.getFuctionCode()));
+ for (byte b : data) {
+ System.out.print(String.format(" %02x", b < 0 ? b + 256 : b));
+ }
+ byte[] ba = requestFrame.getCrcBytes();
+ for (byte b : ba) {
+ System.out.print(String.format(" %02x", b < 0 ? b + 256 : b));
+ }
+ System.out.println();
+
+ } catch (Exception ex) {
+ System.out.println("ERROR: cannot write to serial port " + serialPort.getSystemPortName());
+ }
+ }
+
+ private void readResponse () throws InterruptedException, IOException {
+ try {
+ Thread.sleep(500);
+ if (is.available() > 0) {
+ byte[] data = new byte[is.available()];
+ is.read(data);
+ lastResponse = data;
+ //String s = new String (data);
+ //System.out.println(s);
+ System.out.print("response:");
+ for (byte b : data) {
+ System.out.print(String.format(" %02x", b));
+ }
+ System.out.println();
+ }
+ } catch (Exception ex) {
+ System.out.println("ERROR: cannot read from serial port " + serialPort.getSystemPortName());
+ }
}
- return sb.toString();
- }
-
- public static void main(String[] args) throws Exception
- {
- List<String> ports = SimpleSerial.findAvailableComms();
- for (String port : ports)
- {
- ModbusTest mt = new ModbusTest(port);
- try
- {
- ModbusFrame requestFrame;
- System.out.println("Modbus function code 0x01 (Read coil)");
- System.out.println("-------------------------------------");
- requestFrame = new ModbusFrameReadCoil(2, 0, 4);
- mt.sendModbusFrame(requestFrame);
- mt.readResponse();
- System.out.println("");
-
- System.out.println("\nModbus function code 0x02 (Read discrete Input)");
- System.out.println("-------------------------------------------------");
- requestFrame = new ModbusFrameReadDiscreteInput(2, 0, 1);
- mt.sendModbusFrame(requestFrame);
- mt.readResponse();
- System.out.println("");
-
- System.out.println("\nModbus function code 0x03 (Read holding register)");
- System.out.println("--------------------------------------------------");
- requestFrame = new ModbusFrameReadHoldingRegisters(2, 0, 5);
- mt.sendModbusFrame(requestFrame);
- mt.readResponse();
- System.out.println("");
-
- System.out.println("\nModbus function code 0x04 (Read input register)");
- System.out.println("example 1: read temperature from LM75A");
- System.out.println("--------------------------------------------------");
- requestFrame = new ModbusFrameReadInputRegisters(2, 0x30, 1);
- mt.sendModbusFrame(requestFrame);
- mt.readResponse();
- System.out.println("");
-
- System.out.println("\nModbus function code 0x04 (Read input register)");
- System.out.println("example 2: read version string");
- System.out.println("--------------------------------------------------");
- requestFrame = new ModbusFrameReadInputRegisters(2, 0xfff0, 16);
- mt.sendModbusFrame(requestFrame);
- mt.readResponse();
- System.out.println("response as string: " + mt.getLastResponseString(3));
- System.out.println("");
-
- System.out.println("\nModbus function code 0x05 (Write single coil)");
- System.out.println("-----------------------------------------------");
- requestFrame = new ModbusFrameWriteSingleCoil(2, 2, true);
- mt.sendModbusFrame(requestFrame);
- mt.readResponse();
- System.out.println("");
-
- System.out.println("\nModbus function code 0x06 (Write single (hold) register)");
- System.out.println("----------------------------------------------------------");
- requestFrame = new ModbusFrameWriteRegister(2, 2, 10);
- mt.sendModbusFrame(requestFrame);
- mt.readResponse();
- System.out.println("");
-
- System.out.println("\nModbus function code 0x0f (Write multiple coils)");
- System.out.println("--------------------------------------------------");
- boolean [] data = new boolean[4];
- data[0] = false; data[1] = true; data[2] = true; data[3] = false;
- requestFrame = new ModbusFrameWriteMultipleCoils(2, 0, data);
- mt.sendModbusFrame(requestFrame);
- mt.readResponse();
- System.out.println("");
-
- System.out.println("\nModbus function code 0x10 (Write multiple (hold) registers)");
- System.out.println("-------------------------------------------------------------");
- int [] regValues = new int[2];
- regValues[0] = 0xffff; regValues[1] = 0xffff;
- requestFrame = new ModbusFrameWriteMultipleRegisters(2, 1, regValues);
- mt.sendModbusFrame(requestFrame);
- mt.readResponse();
- System.out.println("");
- }
- finally
- {
- mt.close();
- }
- }
- }
+ public byte [] getLastResponse () {
+ return lastResponse;
+ }
+
+ public String getLastResponseString (int startIndex) {
+ StringBuilder sb = new StringBuilder();
+ if (lastResponse != null) {
+ for (int i = startIndex; i < lastResponse.length; i++) {
+ byte b = lastResponse[i];
+ if (b == 0) {
+ break;
+ }
+ if (b >= 20 && b < 127) {
+ sb.append((char) b);
+ } else {
+ sb.append('.');
+ }
+ }
+ }
+ return sb.toString();
+ }
+
+ public static void main (String[] args) throws Exception {
+ final SerialPort[] serialPorts = SerialPort.getCommPorts();
+ for (SerialPort serialPort : serialPorts) {
+ ModbusTest mt = new ModbusTest(serialPort);
+ try {
+ ModbusFrame requestFrame;
+ System.out.println("Modbus function code 0x01 (Read coil)");
+ System.out.println("-------------------------------------");
+ requestFrame = new ModbusFrameReadCoil(2, 0, 4);
+ mt.sendModbusFrame(requestFrame);
+ mt.readResponse();
+ System.out.println("");
+
+ System.out.println("\nModbus function code 0x02 (Read discrete Input)");
+ System.out.println("-------------------------------------------------");
+ requestFrame = new ModbusFrameReadDiscreteInput(2, 0, 1);
+ mt.sendModbusFrame(requestFrame);
+ mt.readResponse();
+ System.out.println("");
+
+ System.out.println("\nModbus function code 0x03 (Read holding register)");
+ System.out.println("--------------------------------------------------");
+ requestFrame = new ModbusFrameReadHoldingRegisters(2, 0, 5);
+ mt.sendModbusFrame(requestFrame);
+ mt.readResponse();
+ System.out.println("");
+
+ System.out.println("\nModbus function code 0x04 (Read input register)");
+ System.out.println("example 1: read temperature from LM75A");
+ System.out.println("--------------------------------------------------");
+ requestFrame = new ModbusFrameReadInputRegisters(2, 0x30, 1);
+ mt.sendModbusFrame(requestFrame);
+ mt.readResponse();
+ System.out.println("");
+
+ System.out.println("\nModbus function code 0x04 (Read input register)");
+ System.out.println("example 2: read version string");
+ System.out.println("--------------------------------------------------");
+ requestFrame = new ModbusFrameReadInputRegisters(2, 0xfff0, 16);
+ mt.sendModbusFrame(requestFrame);
+ mt.readResponse();
+ System.out.println("response as string: " + mt.getLastResponseString(3));
+ System.out.println("");
+
+ System.out.println("\nModbus function code 0x05 (Write single coil)");
+ System.out.println("-----------------------------------------------");
+ requestFrame = new ModbusFrameWriteSingleCoil(2, 2, true);
+ mt.sendModbusFrame(requestFrame);
+ mt.readResponse();
+ System.out.println("");
+
+ System.out.println("\nModbus function code 0x06 (Write single (hold) register)");
+ System.out.println("----------------------------------------------------------");
+ requestFrame = new ModbusFrameWriteRegister(2, 2, 10);
+ mt.sendModbusFrame(requestFrame);
+ mt.readResponse();
+ System.out.println("");
+
+ System.out.println("\nModbus function code 0x0f (Write multiple coils)");
+ System.out.println("--------------------------------------------------");
+ boolean[] data = new boolean[4];
+ data[0] = false;
+ data[1] = true;
+ data[2] = true;
+ data[3] = false;
+ requestFrame = new ModbusFrameWriteMultipleCoils(2, 0, data);
+ mt.sendModbusFrame(requestFrame);
+ mt.readResponse();
+ System.out.println("");
+
+ System.out.println("\nModbus function code 0x10 (Write multiple (hold) registers)");
+ System.out.println("-------------------------------------------------------------");
+ int[] regValues = new int[2];
+ regValues[0] = 0xffff;
+ regValues[1] = 0xffff;
+ requestFrame = new ModbusFrameWriteMultipleRegisters(2, 1, regValues);
+ mt.sendModbusFrame(requestFrame);
+ mt.readResponse();
+ System.out.println("");
+ } finally {
+ mt.close();
+ }
+ }
+ }
}