From: Manfred Steiner Date: Thu, 1 Oct 2020 15:37:33 +0000 (+0200) Subject: java_modbusmaster: change to JSerialComm, folder released with jar file added X-Git-Url: https://git.htl-mechatronik.at/public/?a=commitdiff_plain;h=7138bd9c35b810a747de6c8dd6c0f648332df8b3;p=sx%2Fmodbus-sure.git java_modbusmaster: change to JSerialComm, folder released with jar file added --- diff --git a/java_modbusmaster/nbproject/build-impl.xml b/java_modbusmaster/nbproject/build-impl.xml index c3a5098..cf9698d 100644 --- a/java_modbusmaster/nbproject/build-impl.xml +++ b/java_modbusmaster/nbproject/build-impl.xml @@ -154,18 +154,6 @@ is divided into following sections: - - - - - - - - - - - - @@ -1193,13 +1181,27 @@ is divided into following sections: - + + + + + + + + + + + + + + + To run this application from the command line without Ant, try: java -jar "${dist.jar.resolved}" - + diff --git a/java_modbusmaster/nbproject/genfiles.properties b/java_modbusmaster/nbproject/genfiles.properties index 17f9a58..15c72fd 100644 --- a/java_modbusmaster/nbproject/genfiles.properties +++ b/java_modbusmaster/nbproject/genfiles.properties @@ -4,5 +4,5 @@ build.xml.stylesheet.CRC32=f85dc8f2@1.89.1.48 # This file is used by a NetBeans-based IDE to track changes in generated files such as build-impl.xml. # Do not edit this file. You may delete it but then the IDE will never regenerate such files for you. nbproject/build-impl.xml.data.CRC32=1872cd09 -nbproject/build-impl.xml.script.CRC32=306010fb -nbproject/build-impl.xml.stylesheet.CRC32=3a2fa800@1.89.1.48 +nbproject/build-impl.xml.script.CRC32=4e2f058b +nbproject/build-impl.xml.stylesheet.CRC32=f89f7d21@1.95.0.48 diff --git a/java_modbusmaster/nbproject/project.properties b/java_modbusmaster/nbproject/project.properties index 8eb24e2..fb79f10 100644 --- a/java_modbusmaster/nbproject/project.properties +++ b/java_modbusmaster/nbproject/project.properties @@ -1,9 +1,10 @@ annotation.processing.enabled=true annotation.processing.enabled.in.editor=false -annotation.processing.processor.options= annotation.processing.processors.list= annotation.processing.run.all.processors=true annotation.processing.source.output=${build.generated.sources.dir}/ap-source-output +application.title=java_modbusmaster +application.vendor=steiner build.classes.dir=${build.dir}/classes build.classes.excludes=**/*.java,**/*.form # This directory is removed when the project is cleaned: @@ -28,19 +29,22 @@ debug.test.modulepath=\ dist.dir=dist dist.jar=${dist.dir}/java_modbusmaster.jar dist.javadoc.dir=${dist.dir}/javadoc +endorsed.classpath= excludes= includes=** jar.compress=false -javac.classpath= +javac.classpath=\ + ${libs.JSerialComm.classpath} # Space-separated list of extra javac options javac.compilerargs= javac.deprecation=false +javac.external.vm=false javac.modulepath= javac.processormodulepath= javac.processorpath=\ ${javac.classpath} -javac.source=1.7 -javac.target=1.7 +javac.source=1.8 +javac.target=1.8 javac.test.classpath=\ ${javac.classpath}:\ ${build.classes.dir} @@ -51,6 +55,7 @@ javac.test.processorpath=\ javadoc.additionalparam= javadoc.author=false javadoc.encoding=${source.encoding} +javadoc.html5=false javadoc.noindex=false javadoc.nonavbar=false javadoc.notree=false @@ -59,6 +64,8 @@ javadoc.splitindex=true javadoc.use=true javadoc.version=false javadoc.windowtitle= +jlink.launcher=false +jlink.launcher.name=java_modbusmaster main.class=modbus.ModbusTest manifest.file=manifest.mf meta.inf.dir=${src.dir}/META-INF diff --git a/java_modbusmaster/released/README.TXT b/java_modbusmaster/released/README.TXT new file mode 100644 index 0000000..5de1d92 --- /dev/null +++ b/java_modbusmaster/released/README.TXT @@ -0,0 +1,32 @@ +======================== +BUILD OUTPUT DESCRIPTION +======================== + +When you build an Java application project that has a main class, the IDE +automatically copies all of the JAR +files on the projects classpath to your projects dist/lib folder. The IDE +also adds each of the JAR files to the Class-Path element in the application +JAR files manifest file (MANIFEST.MF). + +To run the project from the command line, go to the dist folder and +type the following: + +java -jar "java_modbusmaster.jar" + +To distribute this project, zip up the dist folder (including the lib folder) +and distribute the ZIP file. + +Notes: + +* If two JAR files on the project classpath have the same name, only the first +JAR file is copied to the lib folder. +* Only JAR files are copied to the lib folder. +If the classpath contains other types of files or folders, these files (folders) +are not copied. +* If a library on the projects classpath also has a Class-Path element +specified in the manifest,the content of the Class-Path element has to be on +the projects runtime path. +* To set a main class in a standard Java project, right-click the project node +in the Projects window and choose Properties. Then click Run and enter the +class name in the Main Class field. Alternatively, you can manually type the +class name in the manifest Main-Class element. diff --git a/java_modbusmaster/released/java_modbusmaster.jar b/java_modbusmaster/released/java_modbusmaster.jar new file mode 100644 index 0000000..29c9e3a Binary files /dev/null and b/java_modbusmaster/released/java_modbusmaster.jar differ diff --git a/java_modbusmaster/released/lib/jserialcomm.jar b/java_modbusmaster/released/lib/jserialcomm.jar new file mode 100644 index 0000000..c987259 Binary files /dev/null and b/java_modbusmaster/released/lib/jserialcomm.jar differ diff --git a/java_modbusmaster/src/modbus/ModbusTest.java b/java_modbusmaster/src/modbus/ModbusTest.java index d3ae87a..e606cff 100644 --- a/java_modbusmaster/src/modbus/ModbusTest.java +++ b/java_modbusmaster/src/modbus/ModbusTest.java @@ -1,14 +1,12 @@ - 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; @@ -18,178 +16,185 @@ import modbus.frames.ModbusFrameWriteMultipleCoils; 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=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 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(); + } + } + } }