Browse Source

--rfid-zebra starter ing

k 2 years ago
parent
commit
05879d060e
27 changed files with 302 additions and 1430 deletions
  1. 7 18
      pom.xml
  2. 82 0
      src/main/java/com/jwk/spring/boot/autoconfigure/AutoConfiguration.java
  3. 0 156
      src/main/java/com/jwk/spring/boot/autoconfigure/ModbusAutoConfiguration.java
  4. 0 110
      src/main/java/com/jwk/spring/boot/autoconfigure/ModbusProperties.java
  5. 0 49
      src/main/java/com/jwk/spring/boot/autoconfigure/ModbusRtuMasterTemplate.java
  6. 0 38
      src/main/java/com/jwk/spring/boot/autoconfigure/ModbusTcpMasterTemplate.java
  7. 52 0
      src/main/java/com/jwk/spring/boot/autoconfigure/Properties.java
  8. 37 0
      src/main/java/com/jwk/spring/boot/autoconfigure/RfidReaderTemplate.java
  9. 0 32
      src/main/java/com/jwk/spring/boot/constant/Constant.java
  10. 0 67
      src/main/java/com/jwk/spring/boot/constant/DATA_TYPE_WRAPPER.java
  11. 0 158
      src/main/java/com/jwk/spring/boot/modbus4j/ModbusMasterUtil.java
  12. 0 99
      src/main/java/com/jwk/spring/boot/modbus4j/ModbusRtuMaster.java
  13. 0 100
      src/main/java/com/jwk/spring/boot/modbus4j/ModbusTcpMaster.java
  14. 0 22
      src/main/java/com/jwk/spring/boot/modbus4j/util/rtu/MyProcessImageListener.java
  15. 0 61
      src/main/java/com/jwk/spring/boot/modbus4j/util/rtu/SerialPortUtils.java
  16. 0 178
      src/main/java/com/jwk/spring/boot/modbus4j/util/rtu/SerialPortWrapperImpl.java
  17. 62 0
      src/main/java/com/jwk/spring/boot/rfid/RfidReaderClient.java
  18. 61 0
      src/main/java/com/jwk/spring/boot/rfid/RfidReaderUtil.java
  19. 0 191
      src/main/java/com/jwk/spring/boot/test/ModbusTcpMasterUtil.java
  20. 0 44
      src/main/java/com/jwk/spring/boot/test/RtuMasterTest.java
  21. 0 106
      src/main/java/com/jwk/spring/boot/test/RtuSlaveTest.java
  22. 1 1
      src/main/resources/META-INF/spring.factories
  23. BIN
      src/main/resources/lib/RXTXcomm.jar
  24. BIN
      src/main/resources/lib/jssc-2.8.0.jar
  25. BIN
      src/main/resources/lib/modbus4j-3.0.5.jar
  26. BIN
      src/main/resources/lib/rxtxParallel.dll
  27. BIN
      src/main/resources/lib/rxtxSerial.dll

+ 7 - 18
pom.xml

@@ -5,10 +5,10 @@
   <modelVersion>4.0.0</modelVersion>
 
   <groupId>com.jwk</groupId>
-  <artifactId>modbus-spring-boot-starter</artifactId>
+  <artifactId>rfid-zebra-spring-boot-starter</artifactId>
   <version>1.0</version>
 
-  <name>modbus-spring-boot-starter</name>
+  <name>rfid-zebra-spring-boot-starter</name>
 
   <properties>
     <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
@@ -66,24 +66,13 @@
       <version>3.9</version>
     </dependency>
 
-
-    <!--modbus4j start-->
-    <dependency>
-      <groupId>com.jwk</groupId>
-      <artifactId>RXTXcomm</artifactId>
-      <version>3.0.5</version>
-    </dependency>
+    <!-- rfid-zebra-->
     <dependency>
-      <groupId>com.jwk</groupId>
-      <artifactId>modbus4j</artifactId>
-      <version>3.0.5</version>
+      <groupId>com.warewms</groupId>
+      <artifactId>rfid</artifactId>
+      <version>1.0.0</version>
     </dependency>
-    <dependency>
-      <groupId>com.jwk</groupId>
-      <artifactId>jssc</artifactId>
-      <version>2.8.0</version>
-    </dependency>
-    <!--modbus4j end-->
+
   </dependencies>
 
   <build>

+ 82 - 0
src/main/java/com/jwk/spring/boot/autoconfigure/AutoConfiguration.java

@@ -0,0 +1,82 @@
+package com.jwk.spring.boot.autoconfigure;
+
+import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
+import org.springframework.boot.context.properties.EnableConfigurationProperties;
+import org.springframework.context.annotation.Bean;
+import org.springframework.context.annotation.Configuration;
+
+/**
+ * @author JWK
+ * @version 1.0
+ * @date 2022/12/2 14:46
+ */
+@EnableConfigurationProperties(Properties.class)
+@Configuration
+public class AutoConfiguration {
+
+
+    /**
+     * first
+     *
+     * @param properties
+     * @return
+     */
+    @Bean
+    @ConditionalOnProperty(prefix = "rfid.first", name = "open", havingValue = "true")
+    public RfidReaderTemplate rfidReaderTemplateFirst(Properties properties) {
+        Properties.SubProperties subProperties = properties.getFirst();
+        return new RfidReaderTemplate(subProperties);
+    }
+
+    /**
+     * second
+     *
+     * @param properties
+     * @return
+     */
+    @Bean
+    @ConditionalOnProperty(prefix = "rfid.second", name = "open", havingValue = "true")
+    public RfidReaderTemplate rfidReaderTemplateSecond(Properties properties) {
+        Properties.SubProperties subProperties = properties.getSecond();
+        return new RfidReaderTemplate(subProperties);
+    }
+
+    /**
+     * third
+     *
+     * @param properties
+     * @return
+     */
+    @Bean
+    @ConditionalOnProperty(prefix = "rfid.third", name = "open", havingValue = "true")
+    public RfidReaderTemplate rfidReaderTemplateThird(Properties properties) {
+        Properties.SubProperties subProperties = properties.getThird();
+        return new RfidReaderTemplate(subProperties);
+    }
+
+    /**
+     * fourth
+     *
+     * @param properties
+     * @return
+     */
+    @Bean
+    @ConditionalOnProperty(prefix = "rfid.fourth", name = "open", havingValue = "true")
+    public RfidReaderTemplate rfidReaderTemplateFourth(Properties properties) {
+        Properties.SubProperties subProperties = properties.getFourth();
+        return new RfidReaderTemplate(subProperties);
+    }
+
+    /**
+     * fifth
+     *
+     * @param properties
+     * @return
+     */
+    @Bean
+    @ConditionalOnProperty(prefix = "rfid.fifth", name = "open", havingValue = "true")
+    public RfidReaderTemplate rfidReaderTemplateFifth(Properties properties) {
+        Properties.SubProperties subProperties = properties.getFifth();
+        return new RfidReaderTemplate(subProperties);
+    }
+}

+ 0 - 156
src/main/java/com/jwk/spring/boot/autoconfigure/ModbusAutoConfiguration.java

@@ -1,156 +0,0 @@
-package com.jwk.spring.boot.autoconfigure;
-
-import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
-import org.springframework.boot.context.properties.EnableConfigurationProperties;
-import org.springframework.context.annotation.Bean;
-import org.springframework.context.annotation.Configuration;
-
-/**
- * @author JWK
- * @version 1.0
- * @date 2022/12/2 14:46
- */
-@EnableConfigurationProperties(ModbusProperties.class)
-@Configuration
-public class ModbusAutoConfiguration {
-
-
-    /**
-     * RTU-Master first
-     * 注入方式:
-     * '@Autowired(required = true)'
-     * private ModbusRtuMasterTemplate modbusRtuMasterTemplateFirst;
-     * 没有open=true的bean千万不要注入,@Autowired(required = false)也不行.
-     *
-     * @param modbusProperties
-     * @return
-     */
-    @Bean
-    @ConditionalOnProperty(prefix = "modbus.rtu-master.first", name = "open", havingValue = "true")
-    public ModbusRtuMasterTemplate modbusRtuMasterTemplateFirst(ModbusProperties modbusProperties) {
-        ModbusProperties.RtuMaster rtu = modbusProperties.getRtuMaster().getFirst();
-        return new ModbusRtuMasterTemplate(rtu.getPort(), rtu.getBaudRate(), rtu.getDataBits()
-                , rtu.getStopBits(), rtu.getParity(), rtu.getFlowControlIn(), rtu.getFlowControlOut());
-    }
-
-    /**
-     * RTU-Master second
-     *
-     * @param modbusProperties
-     * @return
-     */
-    @Bean
-    @ConditionalOnProperty(prefix = "modbus.rtu-master.second", name = "open", havingValue = "true")
-    public ModbusRtuMasterTemplate modbusRtuMasterTemplateSecond(ModbusProperties modbusProperties) {
-        ModbusProperties.RtuMaster rtu = modbusProperties.getRtuMaster().getSecond();
-        return new ModbusRtuMasterTemplate(rtu.getPort(), rtu.getBaudRate(), rtu.getDataBits()
-                , rtu.getStopBits(), rtu.getParity(), rtu.getFlowControlIn(), rtu.getFlowControlOut());
-    }
-
-    /**
-     * RTU-Master third
-     *
-     * @param modbusProperties
-     * @return
-     */
-    @Bean
-    @ConditionalOnProperty(prefix = "modbus.rtu-master.third", name = "open", havingValue = "true")
-    public ModbusRtuMasterTemplate modbusRtuMasterTemplateThird(ModbusProperties modbusProperties) {
-        ModbusProperties.RtuMaster rtu = modbusProperties.getRtuMaster().getThird();
-        return new ModbusRtuMasterTemplate(rtu.getPort(), rtu.getBaudRate(), rtu.getDataBits()
-                , rtu.getStopBits(), rtu.getParity(), rtu.getFlowControlIn(), rtu.getFlowControlOut());
-    }
-
-    /**
-     * RTU-Master fourth
-     *
-     * @param modbusProperties
-     * @return
-     */
-    @Bean
-    @ConditionalOnProperty(prefix = "modbus.rtu-master.fourth", name = "open", havingValue = "true")
-    public ModbusRtuMasterTemplate modbusRtuMasterTemplateFourth(ModbusProperties modbusProperties) {
-        ModbusProperties.RtuMaster rtu = modbusProperties.getRtuMaster().getFourth();
-        return new ModbusRtuMasterTemplate(rtu.getPort(), rtu.getBaudRate(), rtu.getDataBits()
-                , rtu.getStopBits(), rtu.getParity(), rtu.getFlowControlIn(), rtu.getFlowControlOut());
-    }
-
-    /**
-     * RTU-Master fifth
-     *
-     * @param modbusProperties
-     * @return
-     */
-    @Bean
-    @ConditionalOnProperty(prefix = "modbus.rtu-master.fifth", name = "open", havingValue = "true")
-    public ModbusRtuMasterTemplate modbusRtuMasterTemplateFifth(ModbusProperties modbusProperties) {
-        ModbusProperties.RtuMaster rtu = modbusProperties.getRtuMaster().getFifth();
-        return new ModbusRtuMasterTemplate(rtu.getPort(), rtu.getBaudRate(), rtu.getDataBits()
-                , rtu.getStopBits(), rtu.getParity(), rtu.getFlowControlIn(), rtu.getFlowControlOut());
-    }
-
-    /**
-     * TCP-Master first
-     *
-     * @param modbusProperties
-     * @return
-     */
-    @Bean
-    @ConditionalOnProperty(prefix = "modbus.tcp-master.first", name = "open", havingValue = "true")
-    public ModbusTcpMasterTemplate modbusTcpMasterTemplateFirst(ModbusProperties modbusProperties) {
-        ModbusProperties.TcpMaster tcp = modbusProperties.getTcpMaster().getFirst();
-        return new ModbusTcpMasterTemplate(tcp.getHost(), tcp.getPort());
-    }
-
-    /**
-     * TCP-Master second
-     *
-     * @param modbusProperties
-     * @return
-     */
-    @Bean
-    @ConditionalOnProperty(prefix = "modbus.tcp-master.second", name = "open", havingValue = "true")
-    public ModbusTcpMasterTemplate modbusTcpMasterTemplateSecond(ModbusProperties modbusProperties) {
-        ModbusProperties.TcpMaster tcp = modbusProperties.getTcpMaster().getSecond();
-        return new ModbusTcpMasterTemplate(tcp.getHost(), tcp.getPort());
-    }
-
-    /**
-     * TCP-Master third
-     *
-     * @param modbusProperties
-     * @return
-     */
-    @Bean
-    @ConditionalOnProperty(prefix = "modbus.tcp-master.third", name = "open", havingValue = "true")
-    public ModbusTcpMasterTemplate modbusTcpMasterTemplateThird(ModbusProperties modbusProperties) {
-        ModbusProperties.TcpMaster tcp = modbusProperties.getTcpMaster().getThird();
-        return new ModbusTcpMasterTemplate(tcp.getHost(), tcp.getPort());
-    }
-
-    /**
-     * TCP-Master fourth
-     *
-     * @param modbusProperties
-     * @return
-     */
-    @Bean
-    @ConditionalOnProperty(prefix = "modbus.tcp-master.fourth", name = "open", havingValue = "true")
-    public ModbusTcpMasterTemplate modbusTcpMasterTemplateFourth(ModbusProperties modbusProperties) {
-        ModbusProperties.TcpMaster tcp = modbusProperties.getTcpMaster().getFourth();
-        return new ModbusTcpMasterTemplate(tcp.getHost(), tcp.getPort());
-    }
-
-    /**
-     * TCP-Master fifth
-     *
-     * @param modbusProperties
-     * @return
-     */
-    @Bean
-    @ConditionalOnProperty(prefix = "modbus.tcp-master.fifth", name = "open", havingValue = "true")
-    public ModbusTcpMasterTemplate modbusTcpMasterTemplateFifth(ModbusProperties modbusProperties) {
-        ModbusProperties.TcpMaster tcp = modbusProperties.getTcpMaster().getFifth();
-        return new ModbusTcpMasterTemplate(tcp.getHost(), tcp.getPort());
-    }
-}

+ 0 - 110
src/main/java/com/jwk/spring/boot/autoconfigure/ModbusProperties.java

@@ -1,110 +0,0 @@
-package com.jwk.spring.boot.autoconfigure;
-
-import lombok.Data;
-import org.springframework.boot.context.properties.ConfigurationProperties;
-
-/**
- * @author JWK
- * @version 1.0
- * @date 2022/12/2 15:40
- */
-@Data
-@ConfigurationProperties(prefix = ModbusProperties.DEMO_PREFIX)
-public class ModbusProperties {
-
-    public static final String DEMO_PREFIX = "modbus";
-
-    /**
-     * 注入方式:
-     * '@Autowired(required = true)'
-     * private ModbusRtuMasterTemplate modbusRtuMasterTemplateFirst;
-     * 没有open=true的bean千万不要注入,@Autowired(required = false)也不行.
-     */
-    private RtuMasterOrder rtuMaster;
-    /**
-     * 注入方式:
-     * '@Autowired(required = true)'
-     * private ModbusTcpMasterTemplate modbusTcpMasterTemplateFirst;
-     * 没有open=true的bean千万不要注入,@Autowired(required = false)也不行.
-     */
-    private TcpMasterOrder tcpMaster;
-
-    @Data
-    public static class RtuMasterOrder {
-        private RtuMaster first;
-        private RtuMaster second;
-        private RtuMaster third;
-        private RtuMaster fourth;
-        private RtuMaster fifth;
-    }
-
-    @Data
-    public static class TcpMasterOrder {
-        private TcpMaster first;
-        private TcpMaster second;
-        private TcpMaster third;
-        private TcpMaster fourth;
-        private TcpMaster fifth;
-    }
-
-    @Data
-    public static class RtuMaster {
-        /**
-         * 开启串口
-         */
-        private boolean open = false;
-        /**
-         * 串口
-         */
-        private String port = "COM1";
-
-        /**
-         * 波特率
-         */
-        private Integer baudRate = 9600;
-
-        /**
-         * 数据位的位数,RTU是8位,ASCII是7位
-         */
-        private Integer dataBits = 8;
-
-        /**
-         * 停止位的位数,如果无奇偶校验为2,有奇偶校验为1
-         */
-        private Integer stopBits = 1;
-
-        /**
-         * 奇偶校验位,无校验是0,奇校验是1,偶校验是2
-         */
-        private Integer parity = 0;
-
-        /**
-         * 硬件之间输入流应答控制
-         */
-        private Integer flowControlIn = 0;
-
-        /**
-         * 硬件之间输出流应答控制
-         */
-        private Integer flowControlOut = 0;
-
-    }
-
-    @Data
-    public static class TcpMaster {
-        /**
-         * 开启TCP连接
-         */
-        private boolean open = false;
-
-        /**
-         * ip地址
-         */
-        private String host = "127.0.0.1";
-
-        /**
-         * 端口
-         */
-        private Integer port = 502;
-    }
-}

+ 0 - 49
src/main/java/com/jwk/spring/boot/autoconfigure/ModbusRtuMasterTemplate.java

@@ -1,49 +0,0 @@
-package com.jwk.spring.boot.autoconfigure;
-
-import com.jwk.spring.boot.modbus4j.ModbusMasterUtil;
-import com.jwk.spring.boot.modbus4j.ModbusRtuMaster;
-import com.serotonin.modbus4j.exception.ModbusInitException;
-
-
-/**
- * 注入方式:
- * '@Autowired(required = true)'
- * private ModbusRtuMasterTemplate modbusRtuMasterTemplateFirst;
- * 没有open=true的bean千万不要注入,@Autowired(required = false)也不行.
- *
- * @author JWK
- * @version 1.0
- * @date 2022/9/7 17:00
- */
-public class ModbusRtuMasterTemplate {
-
-
-    private ModbusRtuMaster modbusRtuMaster;
-    private ModbusMasterUtil modbusMasterUtil;
-
-
-    /**
-     * 初始化串口连接
-     *
-     * @param port
-     * @param baudRate
-     * @param dataBits
-     * @param stopBits
-     * @param parity
-     * @param flowControlIn
-     * @param flowControlOut
-     * @throws ModbusInitException
-     */
-    public ModbusRtuMasterTemplate(String port, int baudRate, int dataBits, int stopBits, int parity
-            , int flowControlIn, int flowControlOut) {
-        modbusRtuMaster = new ModbusRtuMaster(port, baudRate, dataBits, stopBits, parity
-                , flowControlIn, flowControlOut);
-        modbusRtuMaster.connectAsync();
-    }
-
-    public ModbusMasterUtil getModbusMasterUtil() {
-        modbusMasterUtil = modbusRtuMaster.getModbusMasterUtil();
-        return modbusMasterUtil;
-    }
-
-}

+ 0 - 38
src/main/java/com/jwk/spring/boot/autoconfigure/ModbusTcpMasterTemplate.java

@@ -1,38 +0,0 @@
-package com.jwk.spring.boot.autoconfigure;
-
-import com.jwk.spring.boot.modbus4j.ModbusMasterUtil;
-import com.jwk.spring.boot.modbus4j.ModbusTcpMaster;
-
-
-/**
- * 注入方式:
- * '@Autowired(required = true)'
- * private ModbusTcpMasterTemplate modbusTcpMasterTemplateFirst;
- * 没有open=true的bean千万不要注入,@Autowired(required = false)也不行.
- *
- * @author JWK
- * @version 1.0
- * @date 2022/9/7 17:00
- */
-public class ModbusTcpMasterTemplate {
-
-
-    private ModbusTcpMaster modbusTcpMaster;
-    private ModbusMasterUtil modbusMasterUtil;
-
-    /**
-     * 初始化连接
-     *
-     * @param host
-     * @param port
-     */
-    public ModbusTcpMasterTemplate(String host, Integer port) {
-        modbusTcpMaster = new ModbusTcpMaster(host, port);
-        modbusTcpMaster.connectAsync();
-    }
-
-    public ModbusMasterUtil getModbusMasterUtil() {
-        modbusMasterUtil = modbusTcpMaster.getModbusMasterUtil();
-        return modbusMasterUtil;
-    }
-}

+ 52 - 0
src/main/java/com/jwk/spring/boot/autoconfigure/Properties.java

@@ -0,0 +1,52 @@
+package com.jwk.spring.boot.autoconfigure;
+
+import lombok.Data;
+import org.springframework.boot.context.properties.ConfigurationProperties;
+
+/**
+ * @author JWK
+ * @version 1.0
+ * @date 2022/12/2 15:40
+ */
+@Data
+@ConfigurationProperties(prefix = Properties.DEMO_PREFIX)
+public class Properties {
+
+    public static final String DEMO_PREFIX = "rfid";
+
+    private SubProperties first;
+    private SubProperties second;
+    private SubProperties third;
+    private SubProperties fourth;
+    private SubProperties fifth;
+
+
+    @Data
+    public static class SubProperties {
+        /**
+         * 开启连接
+         */
+        private boolean open = false;
+
+        /**
+         * ip地址
+         */
+        private String host = "127.0.0.1";
+
+        /**
+         * 端口
+         */
+        private Integer port = 5084;
+
+        /**
+         * 连接超时时间
+         */
+        private Integer timeoutMilliseconds = 0;
+
+        /**
+         * 读取标签数量
+         */
+        private Integer numberOfTags = 10;
+
+    }
+}

+ 37 - 0
src/main/java/com/jwk/spring/boot/autoconfigure/RfidReaderTemplate.java

@@ -0,0 +1,37 @@
+package com.jwk.spring.boot.autoconfigure;
+
+import com.jwk.spring.boot.rfid.RfidReaderUtil;
+import com.jwk.spring.boot.rfid.RfidReaderClient;
+
+
+/**
+ * 注入方式:
+ * '@Autowired(required = true)'
+ * private RfidReaderTemplate rfidReaderTemplateFirst;
+ * 没有open=true的bean千万不要注入,@Autowired(required = false)也不行.
+ *
+ * @author JWK
+ * @version 1.0
+ * @date 2022/9/7 17:00
+ */
+public class RfidReaderTemplate {
+
+
+    private RfidReaderClient rfidReaderClient;
+    private RfidReaderUtil rfidReaderUtil;
+
+    /**
+     * 初始化连接
+     *
+     * @param properties
+     */
+    public RfidReaderTemplate(Properties.SubProperties properties) {
+        rfidReaderClient = new RfidReaderClient(properties);
+        rfidReaderClient.connectAsync();
+    }
+
+    public RfidReaderUtil getRfidReaderUtil() {
+        rfidReaderUtil = rfidReaderClient.getRfidReaderUtil();
+        return rfidReaderUtil;
+    }
+}

+ 0 - 32
src/main/java/com/jwk/spring/boot/constant/Constant.java

@@ -8,36 +8,4 @@ package com.jwk.spring.boot.constant;
 public class Constant {
 
 
-    /**
-     * 功能码
-     */
-    public enum FUNCTION_CODE {
-        /***
-         * 读线圈寄存器
-         */
-        READ_01H(0x01),
-        /***
-         * 读保持寄存器
-         */
-        READ_03H(0x03),
-        /***
-         * 写单个线圈寄存器
-         */
-        WRITE_05H(0x05),
-        /***
-         * 写单个保持寄存器
-         */
-        WRITE_06H(0x06);
-
-        private Integer value;
-
-        FUNCTION_CODE(Integer value) {
-            this.value = value;
-        }
-
-        public Integer getValue() {
-            return value;
-        }
-    }
-
 }

+ 0 - 67
src/main/java/com/jwk/spring/boot/constant/DATA_TYPE_WRAPPER.java

@@ -1,67 +0,0 @@
-package com.jwk.spring.boot.constant;
-
-import com.serotonin.modbus4j.code.DataType;
-
-/**
- * 包装DataType
- *
- * @author JWK
- * @version 1.0
- * @date 2022/12/12 22:45
- * {@link com.serotonin.modbus4j.code.DataType}
- */
-public enum DATA_TYPE_WRAPPER {
-    BINARY(DataType.BINARY),
-    TWO_BYTE_INT_UNSIGNED(DataType.TWO_BYTE_INT_UNSIGNED),
-    TWO_BYTE_INT_SIGNED(DataType.TWO_BYTE_INT_SIGNED),
-    TWO_BYTE_INT_UNSIGNED_SWAPPED(DataType.TWO_BYTE_INT_UNSIGNED_SWAPPED),
-    TWO_BYTE_INT_SIGNED_SWAPPED(DataType.TWO_BYTE_INT_SIGNED_SWAPPED),
-    FOUR_BYTE_INT_UNSIGNED(DataType.FOUR_BYTE_INT_UNSIGNED),
-    FOUR_BYTE_INT_SIGNED(DataType.FOUR_BYTE_INT_SIGNED),
-    FOUR_BYTE_INT_UNSIGNED_SWAPPED(DataType.FOUR_BYTE_INT_UNSIGNED_SWAPPED),
-    FOUR_BYTE_INT_SIGNED_SWAPPED(DataType.FOUR_BYTE_INT_SIGNED_SWAPPED),
-    FOUR_BYTE_INT_UNSIGNED_SWAPPED_SWAPPED(DataType.FOUR_BYTE_INT_UNSIGNED_SWAPPED_SWAPPED),
-    FOUR_BYTE_INT_SIGNED_SWAPPED_SWAPPED(DataType.FOUR_BYTE_INT_SIGNED_SWAPPED_SWAPPED),
-    FOUR_BYTE_FLOAT(DataType.FOUR_BYTE_FLOAT),
-    FOUR_BYTE_FLOAT_SWAPPED(DataType.FOUR_BYTE_FLOAT_SWAPPED),
-    FOUR_BYTE_FLOAT_SWAPPED_INVERTED(DataType.FOUR_BYTE_FLOAT_SWAPPED_INVERTED),
-    EIGHT_BYTE_INT_UNSIGNED(DataType.EIGHT_BYTE_INT_UNSIGNED),
-    EIGHT_BYTE_INT_SIGNED(DataType.EIGHT_BYTE_INT_SIGNED),
-    EIGHT_BYTE_INT_UNSIGNED_SWAPPED(DataType.EIGHT_BYTE_INT_UNSIGNED_SWAPPED),
-    EIGHT_BYTE_INT_SIGNED_SWAPPED(DataType.EIGHT_BYTE_INT_SIGNED_SWAPPED),
-    EIGHT_BYTE_FLOAT(DataType.EIGHT_BYTE_FLOAT),
-    EIGHT_BYTE_FLOAT_SWAPPED(DataType.EIGHT_BYTE_FLOAT_SWAPPED),
-    TWO_BYTE_BCD(DataType.TWO_BYTE_BCD),
-    FOUR_BYTE_BCD(DataType.FOUR_BYTE_BCD),
-    FOUR_BYTE_BCD_SWAPPED(DataType.FOUR_BYTE_BCD_SWAPPED),
-    CHAR(DataType.CHAR),
-    VARCHAR(DataType.VARCHAR),
-    FOUR_BYTE_MOD_10K(DataType.FOUR_BYTE_MOD_10K),
-    SIX_BYTE_MOD_10K(DataType.SIX_BYTE_MOD_10K),
-    EIGHT_BYTE_MOD_10K(DataType.EIGHT_BYTE_MOD_10K),
-    FOUR_BYTE_MOD_10K_SWAPPED(DataType.FOUR_BYTE_MOD_10K_SWAPPED),
-    SIX_BYTE_MOD_10K_SWAPPED(DataType.SIX_BYTE_MOD_10K_SWAPPED),
-    EIGHT_BYTE_MOD_10K_SWAPPED(DataType.EIGHT_BYTE_MOD_10K_SWAPPED),
-    ONE_BYTE_INT_UNSIGNED_LOWER(DataType.ONE_BYTE_INT_UNSIGNED_LOWER),
-    ONE_BYTE_INT_UNSIGNED_UPPER(DataType.ONE_BYTE_INT_UNSIGNED_UPPER);
-    private int value;
-
-    DATA_TYPE_WRAPPER(int value) {
-        this.value = value;
-    }
-
-    public static int getRegisterCount(int id) {
-        return DataType.getRegisterCount(id);
-    }
-
-    public static Class<?> getJavaType(int id) {
-        return DataType.getJavaType(id);
-    }
-
-
-    public int getValue() {
-        return value;
-    }
-
-}
-

+ 0 - 158
src/main/java/com/jwk/spring/boot/modbus4j/ModbusMasterUtil.java

@@ -1,158 +0,0 @@
-package com.jwk.spring.boot.modbus4j;
-
-import com.jwk.spring.boot.constant.DATA_TYPE_WRAPPER;
-import com.serotonin.modbus4j.ModbusMaster;
-import com.serotonin.modbus4j.locator.BaseLocator;
-import com.serotonin.modbus4j.msg.*;
-import lombok.extern.slf4j.Slf4j;
-
-/**
- * @author JWK
- * @version 1.0
- * @date 2022/12/12 22:10
- */
-@Slf4j
-public class ModbusMasterUtil {
-
-    private ModbusMaster master;
-
-    public ModbusMasterUtil(ModbusMaster master) {
-        this.master = master;
-    }
-
-    /**
-     * 读单个线圈寄存器 01
-     *
-     * @param slaveId
-     * @param offset
-     * @return 读取值
-     */
-    public Boolean readCoil(int slaveId, int offset) {
-        Boolean value = null;
-        BaseLocator<Boolean> loc = BaseLocator.coilStatus(slaveId, offset);
-        try {
-            value = master.getValue(loc);
-        } catch (Exception e) {
-            log.error("读单个线圈寄存器失败:" + e.getMessage(), e);
-        }
-        return value;
-    }
-
-    /**
-     * 读线圈寄存器 01
-     *
-     * @param slaveId
-     * @param offset
-     * @param len
-     */
-    public ReadResponse readCoils(int slaveId, int offset, int len) {
-        try {
-            ReadCoilsRequest request = new ReadCoilsRequest(slaveId, offset, len);
-            ReadCoilsResponse response = (ReadCoilsResponse) master.send(request);
-            if (response.isException()) {
-                log.error("读线圈寄存器错误:" + response.getExceptionMessage());
-                return null;
-            }
-            return response;
-        } catch (Exception e) {
-            log.error("读线圈寄存器失败:" + e.getMessage(), e);
-        }
-        return null;
-    }
-
-    /**
-     * 写单个线圈寄存器 05
-     *
-     * @param slaveId
-     * @param offset
-     * @param value   0x0000 false | 0xFF00 true
-     */
-    public Boolean writeCoil(int slaveId, int offset, int value) {
-        Boolean con = value == 0x0000 ? false : value == 0xFF00 ? true : null;
-        return writeCoil(slaveId, offset, con);
-    }
-
-    /**
-     * 写单个线圈寄存器 05
-     *
-     * @param slaveId
-     * @param offset
-     * @param value
-     */
-    public Boolean writeCoil(int slaveId, int offset, boolean value) {
-        try {
-            WriteCoilRequest request = new WriteCoilRequest(slaveId, offset, value);
-            WriteCoilResponse response = (WriteCoilResponse) master.send(request);
-            if (response.isException()) {
-                log.error("写单个线圈寄存器错误:" + response.getExceptionMessage());
-                return false;
-            }
-            return true;
-        } catch (Exception e) {
-            log.error("写单个线圈寄存器失败:" + e.getMessage(), e);
-        }
-        return false;
-    }
-
-    /**
-     * 读取单个保持寄存器 03
-     *
-     * @param slaveId  从节点
-     * @param offset   位置
-     * @param dataType 数据类型 {@link com.serotonin.modbus4j.code.DataType}{@link DATA_TYPE_WRAPPER}
-     */
-    public Number readHoldingRegister(int slaveId, int offset, DATA_TYPE_WRAPPER dataType) {
-        BaseLocator<Number> loc = BaseLocator.holdingRegister(slaveId, offset, dataType.getValue());
-        Number value = null;
-        try {
-            value = master.getValue(loc);
-        } catch (Exception e) {
-            log.error("读取单个保持寄存器失败::" + e.getMessage(), e);
-        }
-        return value;
-    }
-
-    /**
-     * 读取保持寄存器 03
-     *
-     * @param slaveId
-     * @param offset
-     * @param len
-     */
-    public ReadResponse readHoldingRegisters(int slaveId, int offset, int len) {
-        try {
-            ReadHoldingRegistersRequest request = new ReadHoldingRegistersRequest(slaveId, offset, len);
-            ReadHoldingRegistersResponse response = (ReadHoldingRegistersResponse) master.send(request);
-            if (response.isException()) {
-                log.error("读取保持寄存器错误:" + response.getExceptionMessage());
-                return null;
-            }
-            return response;
-        } catch (Exception e) {
-            log.error("读取保持寄存器失败:" + e.getMessage(), e);
-        }
-        return null;
-    }
-
-    /**
-     * 写单个保持寄存器 06
-     *
-     * @param slaveId
-     * @param offset
-     * @param value
-     */
-    public Boolean writeHoldingRegisters(int slaveId, int offset, int value) {
-        try {
-            WriteRegisterRequest request = new WriteRegisterRequest(slaveId, offset, value);
-            WriteRegisterResponse response = (WriteRegisterResponse) master.send(request);
-            if (response.isException()) {
-                log.error("写保持寄存器错误:" + response.getExceptionMessage());
-                return false;
-            }
-            return true;
-        } catch (Exception e) {
-            log.error("写保持寄存器失败:" + e.getMessage(), e);
-        }
-        return false;
-    }
-}

+ 0 - 99
src/main/java/com/jwk/spring/boot/modbus4j/ModbusRtuMaster.java

@@ -1,99 +0,0 @@
-package com.jwk.spring.boot.modbus4j;
-
-import com.jwk.spring.boot.modbus4j.util.rtu.SerialPortWrapperImpl;
-import com.serotonin.modbus4j.ModbusFactory;
-import com.serotonin.modbus4j.ModbusMaster;
-import com.serotonin.modbus4j.exception.ModbusInitException;
-import lombok.Data;
-import lombok.extern.slf4j.Slf4j;
-
-import java.util.concurrent.Executors;
-import java.util.concurrent.locks.Lock;
-import java.util.concurrent.locks.ReentrantLock;
-
-
-/**
- * @author JWK
- * @version 1.0
- * @date 2022/9/7 17:00
- */
-@Slf4j
-@Data
-public class ModbusRtuMaster extends SerialPortWrapperImpl {
-
-    private ModbusMaster master = null;
-    private ModbusFactory modbusFactory = null;
-    private ModbusMasterUtil modbusMasterUtil = null;
-    private Lock lock = new ReentrantLock();
-
-    /**
-     * 初始化串口连接
-     *
-     * @param port
-     * @param baudRate
-     * @param dataBits
-     * @param stopBits
-     * @param parity
-     * @param flowControlIn
-     * @param flowControlOut
-     * @throws ModbusInitException
-     */
-    public ModbusRtuMaster(String port, int baudRate, int dataBits, int stopBits, int parity,
-                           int flowControlIn, int flowControlOut) {
-        super(port, baudRate, dataBits, stopBits, parity, flowControlIn, flowControlOut);
-    }
-
-    /**
-     * 打开串口 异步
-     *
-     * @throws ModbusInitException
-     */
-    public void connectAsync() {
-        Executors.newSingleThreadExecutor().execute(() -> this.connect());
-    }
-
-    /**
-     * 打开串口
-     *
-     * @throws ModbusInitException
-     */
-    public void connect() {
-        for (; ; ) {
-            try {
-                if (!lock.tryLock()) {
-                    break;
-                }
-                if (modbusFactory == null) {
-                    modbusFactory = new ModbusFactory();
-                }
-                master = modbusFactory.createRtuMaster(this);
-                master.init();
-                modbusMasterUtil = new ModbusMasterUtil(master);
-                log.info("当前串口:{},打开成功!", getPort());
-                break;
-            } catch (Exception e) {
-                log.error("当前串口:{},打开失败!", getPort(), e);
-                try {
-                    Thread.sleep(3000);
-                } catch (InterruptedException ee) {
-
-                }
-            } finally {
-                lock.unlock();
-            }
-        }
-    }
-
-    /**
-     * 重新连接串口
-     */
-    public void reConnect() {
-        try {
-            this.close();
-            Thread.sleep(1000);
-        } catch (Exception e) {
-
-        }
-        connect();
-    }
-}

+ 0 - 100
src/main/java/com/jwk/spring/boot/modbus4j/ModbusTcpMaster.java

@@ -1,100 +0,0 @@
-package com.jwk.spring.boot.modbus4j;
-
-import com.serotonin.modbus4j.ModbusFactory;
-import com.serotonin.modbus4j.ModbusMaster;
-import com.serotonin.modbus4j.exception.ModbusInitException;
-import com.serotonin.modbus4j.ip.IpParameters;
-import lombok.Data;
-import lombok.extern.slf4j.Slf4j;
-
-import java.util.concurrent.Executors;
-import java.util.concurrent.locks.Lock;
-import java.util.concurrent.locks.ReentrantLock;
-
-
-/**
- * @author JWK
- * @version 1.0
- * @date 2022/9/7 17:00
- */
-@Slf4j
-@Data
-public class ModbusTcpMaster {
-
-    private ModbusMaster master = null;
-    private ModbusFactory modbusFactory = null;
-    private ModbusMasterUtil modbusMasterUtil = null;
-    private Lock lock = new ReentrantLock();
-
-    private String host;
-    private Integer port;
-
-    /**
-     * 初始化
-     *
-     * @param host
-     * @param port
-     * @throws ModbusInitException
-     */
-    public ModbusTcpMaster(String host, Integer port) {
-        this.host = host;
-        this.port = port;
-        modbusFactory = new ModbusFactory();
-
-    }
-
-    /**
-     * 打开串口 异步
-     *
-     * @throws ModbusInitException
-     */
-    public void connectAsync() {
-        Executors.newSingleThreadExecutor().execute(() -> this.connect());
-    }
-
-    /**
-     * 连接
-     *
-     * @throws ModbusInitException
-     */
-    public void connect() {
-        for (; ; ) {
-            try {
-                if (!lock.tryLock()) {
-                    break;
-                }
-                IpParameters params = new IpParameters();
-                params.setHost(host);
-                params.setPort(port);
-                master = modbusFactory.createTcpMaster(params, true);
-                master.init();
-                modbusMasterUtil = new ModbusMasterUtil(master);
-                log.info("ModbusTcp:{}:{},连接成功!", getHost(), getPort());
-                break;
-            } catch (Exception e) {
-                log.error("ModbusTcp:{}:{},连接失败!", getHost(), getPort(), e);
-                try {
-                    Thread.sleep(3000);
-                } catch (InterruptedException ee) {
-
-                }
-            } finally {
-                lock.unlock();
-            }
-        }
-    }
-
-    /**
-     * 重新连接串口
-     */
-    public void reConnect() {
-        try {
-            master = null;
-            connect();
-            Thread.sleep(1000);
-        } catch (Exception e) {
-
-        }
-        connect();
-    }
-}

+ 0 - 22
src/main/java/com/jwk/spring/boot/modbus4j/util/rtu/MyProcessImageListener.java

@@ -1,22 +0,0 @@
-package com.jwk.spring.boot.modbus4j.util.rtu;
-
-import com.serotonin.modbus4j.ProcessImageListener;
-
-/**
- * 用于从站监听
- *
- * @author jwk
- * @date 2020/12/26
- */
-public class MyProcessImageListener implements ProcessImageListener {
-
-    @Override
-    public void coilWrite(int offset, boolean oldValue, boolean newValue) {
-        System.out.println("线圈状态地址=" + offset + ",旧值=" + oldValue + ",新值=" + newValue);
-    }
-
-    @Override
-    public void holdingRegisterWrite(int offset, short oldValue, short newValue) {
-        System.out.println("保持寄存器地址=" + offset + ",旧值=" + oldValue + ",新值=" + newValue);
-    }
-}

+ 0 - 61
src/main/java/com/jwk/spring/boot/modbus4j/util/rtu/SerialPortUtils.java

@@ -1,61 +0,0 @@
-package com.jwk.spring.boot.modbus4j.util.rtu;
-
-import gnu.io.CommPort;
-import gnu.io.CommPortIdentifier;
-import gnu.io.SerialPort;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
-
-/**
- * 串口工具类
- * @author wusq
- * @date 2021/1/1
- */
-public class SerialPortUtils {
-
-    private static Logger log = LoggerFactory.getLogger(SerialPortUtils.class);
-
-    /**
-     * 打卡串口
-     * @param portName 串口名
-     * @param baudRate 波特率
-     * @param dataBits 数据位
-     * @param stopBits 停止位
-     * @param parity 校验位
-     * @return 串口对象
-     */
-    public static SerialPort open(String portName, Integer baudRate, Integer dataBits,
-                                  Integer stopBits, Integer parity) {
-        SerialPort result = null;
-        try {
-            // 通过端口名识别端口
-            CommPortIdentifier identifier = CommPortIdentifier.getPortIdentifier(portName);
-            // 打开端口,并给端口名字和一个timeout(打开操作的超时时间)
-            CommPort commPort = identifier.open(portName, 2000);
-            // 判断是不是串口
-            if (commPort instanceof SerialPort) {
-                result = (SerialPort) commPort;
-                // 设置一下串口的波特率等参数
-                result.setSerialPortParams(baudRate, dataBits, stopBits, parity);
-                log.info("打开串口{}成功", portName);
-            }else{
-                log.info("{}不是串口", portName);
-            }
-        } catch (Exception e) {
-            log.error("打开串口{}错误", portName, e);
-        }
-        return result;
-    }
-
-    /**
-     * 关闭串口
-     * @param serialPort
-     */
-    public static void close(SerialPort serialPort) {
-        if (serialPort != null) {
-            serialPort.close();
-            log.warn("串口{}关闭", serialPort.getName());
-        }
-    }
-
-}

+ 0 - 178
src/main/java/com/jwk/spring/boot/modbus4j/util/rtu/SerialPortWrapperImpl.java

@@ -1,178 +0,0 @@
-package com.jwk.spring.boot.modbus4j.util.rtu;
-
-import com.serotonin.modbus4j.serial.SerialPortWrapper;
-import gnu.io.SerialPort;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
-
-import java.io.IOException;
-import java.io.InputStream;
-import java.io.OutputStream;
-
-/**
- * 自定义串口封装
- * @author jwk
- * @date 2021/1/3
- */
-public class SerialPortWrapperImpl implements SerialPortWrapper {
-
-    private final Logger log = LoggerFactory.getLogger(this.getClass());
-
-    /**
-     * 串口对象
-     */
-    private SerialPort serialPort;
-
-    /**
-     * 串口
-     */
-    private String port;
-
-    /**
-     * 波特率
-     */
-    private Integer baudRate;
-
-    /**
-     * 数据位的位数,RTU是8位,ASCII是7位
-     */
-    private Integer dataBits;
-
-    /**
-     * 停止位的位数,如果无奇偶校验为2,有奇偶校验为1
-     */
-    private Integer stopBits;
-
-    /**
-     * 奇偶校验位,无校验是0,奇校验是1,偶校验是2
-     */
-    private Integer parity;
-
-    /**
-     * 硬件之间输入流应答控制
-     */
-    private Integer flowControlIn;
-
-    /**
-     * 硬件之间输出流应答控制
-     */
-    private Integer flowControlOut;
-
-    public SerialPortWrapperImpl() {
-        super();
-    }
-
-    public SerialPortWrapperImpl(String port, int baudRate, int dataBits, int stopBits, int parity,
-            int flowControlIn, int flowControlOut) {
-        this.port = port;
-        this.baudRate = baudRate;
-        this.dataBits = dataBits;
-        this.stopBits = stopBits;
-        this.parity = parity;
-        this.flowControlIn = flowControlIn;
-        this.flowControlOut = flowControlOut;
-    }
-
-    @Override
-    public void close() throws Exception {
-        SerialPortUtils.close(serialPort);
-    }
-
-    @Override
-    public void open() throws Exception {
-        serialPort = SerialPortUtils.open(port, baudRate, dataBits, stopBits, parity);
-    }
-
-    @Override
-    public InputStream getInputStream() {
-        InputStream in = null;
-        try {
-            in = serialPort.getInputStream();
-        } catch (IOException e) {
-            log.error("获取串口输入流错误", e);
-        }
-
-        return in;
-    }
-
-    @Override
-    public OutputStream getOutputStream() {
-        OutputStream out = null;
-        try {
-            out = serialPort.getOutputStream();
-        } catch (IOException e) {
-            log.error("获取串口输出流错误", e);
-        }
-
-        return out;
-    }
-
-    @Override
-    public int getBaudRate() {
-        return this.baudRate;
-    }
-
-    @Override
-    public int getDataBits() {
-        return this.dataBits;
-    }
-
-    @Override
-    public int getStopBits() {
-        return this.stopBits;
-    }
-
-    @Override
-    public int getParity() {
-        return this.parity;
-    }
-
-    public int getFlowControlIn() {
-        return this.flowControlIn;
-    }
-
-
-    public int getFlowControlOut() {
-        return this.flowControlOut;
-    }
-
-    public SerialPort getSerialPort() {
-        return serialPort;
-    }
-
-    public void setSerialPort(SerialPort serialPort) {
-        this.serialPort = serialPort;
-    }
-
-    public String getPort() {
-        return port;
-    }
-
-    public void setPort(String port) {
-        this.port = port;
-    }
-
-    public void setBaudRate(Integer baudRate) {
-        this.baudRate = baudRate;
-    }
-
-    public void setDataBits(Integer dataBits) {
-        this.dataBits = dataBits;
-    }
-
-    public void setStopBits(Integer stopBits) {
-        this.stopBits = stopBits;
-    }
-
-    public void setParity(Integer parity) {
-        this.parity = parity;
-    }
-
-    public void setFlowControlIn(Integer flowControlIn) {
-        this.flowControlIn = flowControlIn;
-    }
-
-    public void setFlowControlOut(Integer flowControlOut) {
-        this.flowControlOut = flowControlOut;
-    }
-}

+ 62 - 0
src/main/java/com/jwk/spring/boot/rfid/RfidReaderClient.java

@@ -0,0 +1,62 @@
+package com.jwk.spring.boot.rfid;
+
+import com.jwk.spring.boot.autoconfigure.Properties;
+import com.mot.rfid.api3.RFIDReader;
+import lombok.Data;
+import lombok.extern.slf4j.Slf4j;
+
+import java.util.concurrent.Executors;
+
+
+/**
+ * @author JWK
+ * @version 1.0
+ * @date 2022/9/7 17:00
+ */
+@Slf4j
+@Data
+public class RfidReaderClient {
+
+    private RFIDReader reader = null;
+    private RfidReaderUtil rfidReaderUtil = null;
+    private Properties.SubProperties properties;
+
+    /**
+     * 初始化
+     *
+     * @param properties
+     */
+    public RfidReaderClient(Properties.SubProperties properties) {
+        this.properties = properties;
+    }
+
+    /**
+     * 打开串口 异步
+     */
+    public void connectAsync() {
+        Executors.newSingleThreadExecutor().execute(() -> this.connect());
+    }
+
+    /**
+     * 连接
+     */
+    public void connect() {
+        for (; ; ) {
+            try {
+                reader = new RFIDReader(properties.getHost(), properties.getPort()
+                        , properties.getTimeoutMilliseconds());
+                reader.connect();
+                rfidReaderUtil = new RfidReaderUtil(reader, properties);
+                log.info("RfidReader:{}:{},连接成功!", properties.getHost(), properties.getPort());
+                break;
+            } catch (Exception e) {
+                log.error("RfidReader:{}:{},连接失败!", properties.getHost(), properties.getPort(), e);
+                try {
+                    Thread.sleep(3000);
+                } catch (InterruptedException ee) {
+                    ee.printStackTrace();
+                }
+            }
+        }
+    }
+}

+ 61 - 0
src/main/java/com/jwk/spring/boot/rfid/RfidReaderUtil.java

@@ -0,0 +1,61 @@
+package com.jwk.spring.boot.rfid;
+
+import com.jwk.spring.boot.autoconfigure.Properties;
+import com.mot.rfid.api3.InvalidUsageException;
+import com.mot.rfid.api3.OperationFailureException;
+import com.mot.rfid.api3.RFIDReader;
+import com.mot.rfid.api3.TagData;
+import lombok.extern.slf4j.Slf4j;
+
+/**
+ * @author JWK
+ * @version 1.0
+ * @date 2022/12/12 22:10
+ */
+@Slf4j
+public class RfidReaderUtil {
+
+    private RFIDReader reader;
+    private Properties.SubProperties properties;
+
+    public RfidReaderUtil(RFIDReader reader, Properties.SubProperties properties) {
+        this.reader = reader;
+        this.properties = properties;
+    }
+
+    /**
+     * 读取标签
+     *
+     * @return
+     * @throws Exception
+     */
+    public TagData[] readTags() {
+        TagData[] readTags = null;
+        try {
+            // 清空标签
+            reader.Actions.purgeTags();
+
+            reader.Actions.Inventory.perform();
+
+            Thread.sleep(1000);
+
+            reader.Actions.Inventory.stop();
+
+            readTags = reader.Actions.getReadTags(properties.getNumberOfTags());
+            if (readTags == null) {
+                throw new Exception("读取标签为空!");
+            }
+        } catch (Exception e) {
+            log.error("RfidReader读取异常!", e);
+            reader = new RFIDReader(properties.getHost(), properties.getPort(), properties.getTimeoutMilliseconds());
+            try {
+                reader.connect();
+            } catch (InvalidUsageException ex) {
+                ex.printStackTrace();
+            } catch (OperationFailureException ex) {
+                ex.printStackTrace();
+            }
+        }
+        return readTags;
+    }
+}

+ 0 - 191
src/main/java/com/jwk/spring/boot/test/ModbusTcpMasterUtil.java

@@ -1,191 +0,0 @@
-package com.jwk.spring.boot.test;
-
-import com.serotonin.modbus4j.BatchRead;
-import com.serotonin.modbus4j.BatchResults;
-import com.serotonin.modbus4j.ModbusFactory;
-import com.serotonin.modbus4j.ModbusMaster;
-import com.serotonin.modbus4j.code.DataType;
-import com.serotonin.modbus4j.exception.ErrorResponseException;
-import com.serotonin.modbus4j.exception.ModbusInitException;
-import com.serotonin.modbus4j.exception.ModbusTransportException;
-import com.serotonin.modbus4j.ip.IpParameters;
-import com.serotonin.modbus4j.locator.BaseLocator;
-
-/**
- *
- * todo 这是另一种读法 先保存在这里
- * modbus通讯工具类,采用modbus4j实现
- *
- * @author lxq
- * @dependencies modbus4j-3.0.3.jar
- * @website https://github.com/infiniteautomation/modbus4j
- */
-public class ModbusTcpMasterUtil {
-    /**
-     * 工厂。
-     */
-    static ModbusFactory modbusFactory;
-
-    static {
-        if (modbusFactory == null) {
-            modbusFactory = new ModbusFactory();
-        }
-    }
-
-    /**
-     * 获取master
-     *
-     * @return
-     * @throws ModbusInitException
-     */
-    public static ModbusMaster getMaster() throws ModbusInitException {
-        IpParameters params = new IpParameters();
-        params.setHost("localhost");
-        params.setPort(502);
-        //
-        // modbusFactory.createRtuMaster(wapper); //RTU 协议
-        // modbusFactory.createUdpMaster(params);//UDP 协议
-        // modbusFactory.createAsciiMaster(wrapper);//ASCII 协议
-        ModbusMaster master = modbusFactory.createTcpMaster(params, false);// TCP 协议
-        master.init();
-
-        return master;
-    }
-
-    /**
-     * 读取[01 Coil Status 0x]类型 开关数据
-     *
-     * @param slaveId slaveId
-     * @param offset  位置
-     * @return 读取值
-     * @throws ModbusTransportException 异常
-     * @throws ErrorResponseException   异常
-     * @throws ModbusInitException      异常
-     */
-    public static Boolean readCoilStatus(int slaveId, int offset)
-            throws ModbusTransportException, ErrorResponseException, ModbusInitException {
-        // 01 Coil Status
-        BaseLocator<Boolean> loc = BaseLocator.coilStatus(slaveId, offset);
-        Boolean value = getMaster().getValue(loc);
-        return value;
-    }
-
-    /**
-     * 读取[02 Input Status 1x]类型 开关数据
-     *
-     * @param slaveId
-     * @param offset
-     * @return
-     * @throws ModbusTransportException
-     * @throws ErrorResponseException
-     * @throws ModbusInitException
-     */
-    public static Boolean readInputStatus(int slaveId, int offset)
-            throws ModbusTransportException, ErrorResponseException, ModbusInitException {
-        // 02 Input Status
-        BaseLocator<Boolean> loc = BaseLocator.inputStatus(slaveId, offset);
-        Boolean value = getMaster().getValue(loc);
-        return value;
-    }
-
-    /**
-     * 读取[03 Holding Register类型 2x]模拟量数据
-     *
-     * @param slaveId  slave Id
-     * @param offset   位置
-     * @param dataType 数据类型,来自com.serotonin.modbus4j.code.DataType
-     * @return
-     * @throws ModbusTransportException 异常
-     * @throws ErrorResponseException   异常
-     * @throws ModbusInitException      异常
-     */
-    public static Number readHoldingRegister(int slaveId, int offset, int dataType)
-            throws ModbusTransportException, ErrorResponseException, ModbusInitException {
-        // 03 Holding Register类型数据读取
-        BaseLocator<Number> loc = BaseLocator.holdingRegister(slaveId, offset, dataType);
-        Number value = getMaster().getValue(loc);
-        return value;
-    }
-
-    /**
-     * 读取[04 Input Registers 3x]类型 模拟量数据
-     *
-     * @param slaveId  slaveId
-     * @param offset   位置
-     * @param dataType 数据类型,来自com.serotonin.modbus4j.code.DataType
-     * @return 返回结果
-     * @throws ModbusTransportException 异常
-     * @throws ErrorResponseException   异常
-     * @throws ModbusInitException      异常
-     */
-    public static Number readInputRegisters(int slaveId, int offset, int dataType)
-            throws ModbusTransportException, ErrorResponseException, ModbusInitException {
-        // 04 Input Registers类型数据读取
-        BaseLocator<Number> loc = BaseLocator.inputRegister(slaveId, offset, dataType);
-        Number value = getMaster().getValue(loc);
-        return value;
-    }
-
-    /**
-     * 批量读取使用方法
-     *
-     * @throws ModbusTransportException
-     * @throws ErrorResponseException
-     * @throws ModbusInitException
-     */
-    public static void batchRead() throws ModbusTransportException, ErrorResponseException, ModbusInitException {
-
-        BatchRead<Integer> batch = new BatchRead<Integer>();
-
-        batch.addLocator(0, BaseLocator.holdingRegister(1, 1, DataType.FOUR_BYTE_FLOAT));
-        batch.addLocator(1, BaseLocator.inputStatus(1, 0));
-
-        ModbusMaster master = getMaster();
-
-        batch.setContiguousRequests(false);
-        BatchResults<Integer> results = master.send(batch);
-        System.out.println(results.getValue(0));
-        System.out.println(results.getValue(1));
-    }
-
-    /**
-     * 测试
-     *
-     * @param args
-     */
-    public static void main(String[] args) {
-        try {
-            // 01测试
-            Boolean v011 = readCoilStatus(1, 0);
-            Boolean v012 = readCoilStatus(1, 1);
-            Boolean v013 = readCoilStatus(1, 6);
-            System.out.println("v011:" + v011);
-            System.out.println("v012:" + v012);
-            System.out.println("v013:" + v013);
-            // 02测试
-            Boolean v021 = readInputStatus(1, 0);
-            Boolean v022 = readInputStatus(1, 1);
-            Boolean v023 = readInputStatus(1, 2);
-            System.out.println("v021:" + v021);
-            System.out.println("v022:" + v022);
-            System.out.println("v023:" + v023);
-
-            // 03测试
-            Number v031 = readHoldingRegister(1, 1, DataType.FOUR_BYTE_FLOAT);// 注意,float
-            Number v032 = readHoldingRegister(1, 3, DataType.FOUR_BYTE_FLOAT);// 同上
-            System.out.println("v031:" + v031);
-            System.out.println("v032:" + v032);
-
-            // 04测试
-            Number v041 = readInputRegisters(1, 1, DataType.FOUR_BYTE_FLOAT);//
-            Number v042 = readInputRegisters(1, 3, DataType.FOUR_BYTE_FLOAT);//
-            System.out.println("v041:" + v041);
-            System.out.println("v042:" + v042);
-            // 批量读取
-            batchRead();
-
-        } catch (Exception e) {
-            e.printStackTrace();
-        }
-    }
-}

+ 0 - 44
src/main/java/com/jwk/spring/boot/test/RtuMasterTest.java

@@ -1,44 +0,0 @@
-package com.jwk.spring.boot.test;
-
-import com.jwk.spring.boot.constant.Constant;
-import com.jwk.spring.boot.modbus4j.ModbusRtuMaster;
-import gnu.io.SerialPort;
-import lombok.extern.slf4j.Slf4j;
-
-import java.util.Arrays;
-import java.util.Random;
-
-/**
- * 模拟主站设备
- *
- * @author wusq
- * @date 2021/1/3
- */
-@Slf4j
-public class RtuMasterTest {
-
-    public static void main(String[] args) throws Exception {
-        createRtuMaster();
-    }
-
-    private static void createRtuMaster() throws Exception {
-        // 从站设备ID是1
-        int slaveId = 1;
-        ModbusRtuMaster rtuMaster = new ModbusRtuMaster("COM1", 9600
-                , SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE
-                , 0, 0);
-        rtuMaster.connect();
-        for (; ; ) {
-            try {
-                short[] shorts = rtuMaster
-                        .getModbusMasterUtil().readCoils( 0, 3, slaveId).getShortData();
-                System.out.println(Arrays.toString(shorts));
-                boolean b = rtuMaster.getModbusMasterUtil().writeCoil( 1, new Random().nextInt(100), slaveId);
-                Thread.sleep(2000);
-            } catch (Exception e) {
-                log.error("", e);
-                rtuMaster.reConnect();
-            }
-        }
-    }
-}

+ 0 - 106
src/main/java/com/jwk/spring/boot/test/RtuSlaveTest.java

@@ -1,106 +0,0 @@
-package com.jwk.spring.boot.test;
-
-import com.jwk.spring.boot.modbus4j.util.rtu.MyProcessImageListener;
-import com.jwk.spring.boot.modbus4j.util.rtu.SerialPortWrapperImpl;
-import com.serotonin.modbus4j.BasicProcessImage;
-import com.serotonin.modbus4j.ModbusFactory;
-import com.serotonin.modbus4j.ModbusSlaveSet;
-import com.serotonin.modbus4j.ProcessImage;
-import com.serotonin.modbus4j.exception.ModbusInitException;
-import gnu.io.SerialPort;
-
-import java.util.Random;
-
-/**
- * 模拟从站设备
- * @author wusq
- * @date 2021/1/3
- */
-public class RtuSlaveTest {
-
-    public static void main(String[] args) {
-        createRtuSlave();
-    }
-
-    public static void createRtuSlave(){
-        // 设置串口参数,串口是COM2,波特率是9600
-        SerialPortWrapperImpl wrapper = new SerialPortWrapperImpl("COM2", 9600,
-                SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE, 0, 0);
-
-        // Modbus工厂,可以创建RTU、TCP等不同类型的Master和Slave
-        ModbusFactory modbusFactory = new ModbusFactory();
-
-        final ModbusSlaveSet slave = modbusFactory.createRtuSlave(wrapper);
-
-        // 这玩意网上有人叫做过程影像区,其实就是寄存器
-        // 寄存器里可以设置线圈状态、离散输入状态、保持寄存器和输入寄存器
-        // 这里设置了从站设备ID是1
-        BasicProcessImage processImage = new BasicProcessImage(1);
-        processImage.setInvalidAddressValue(Short.MIN_VALUE);
-        slave.addProcessImage(processImage);
-
-        // 添加监听器,监听slave线圈状态和保持寄存器的写入
-        processImage.addListener(new MyProcessImageListener());
-
-        setCoil(processImage);
-        setInput(processImage);
-        setHoldingRegister(processImage);
-        setInputRegister(processImage);
-
-        // 开启线程启动从站设备
-        new Thread(new Runnable() {
-            @Override
-            public void run() {
-                try {
-                    slave.start();
-                }
-                catch (ModbusInitException e) {
-                    e.printStackTrace();
-                }
-            }
-        }).start();
-
-        /*new Timer().schedule(new TimerTask() {
-            @Override
-            public void run() {
-                // 间隔1秒修改从站设备1的保持寄存器数据
-                updateHoldingRegister(slave.getProcessImage(1));
-            }
-        }, 1000, 1000);*/
-    }
-
-    private static void setCoil(ProcessImage processImage){
-        // 模拟线圈状态
-        processImage.setCoil(0, true);
-        processImage.setCoil(1, false);
-        processImage.setCoil(2, true);
-    }
-
-    private static void setInput(ProcessImage processImage){
-        // 模拟离散输入状态
-        processImage.setInput(0, false);
-        processImage.setInput(1, true);
-        processImage.setInput(2, false);
-    }
-
-    private static void setHoldingRegister(ProcessImage processImage){
-        // 模拟保持寄存器的值
-        processImage.setHoldingRegister(0,(short) 11);
-        processImage.setHoldingRegister(1,(short) 22);
-        processImage.setHoldingRegister(2,(short) 33);
-    }
-
-    private static void updateHoldingRegister(ProcessImage processImage){
-        // 模拟修改保持寄存器的值
-        processImage.setHoldingRegister(0, (short) new Random().nextInt(100));
-        processImage.setHoldingRegister(1,(short) new Random().nextInt(100));
-        processImage.setHoldingRegister(2,(short) new Random().nextInt(100));
-    }
-
-    private static void setInputRegister(ProcessImage processImage){
-        // 模拟输入寄存器的值
-        processImage.setInputRegister(0,(short) 44);
-        processImage.setInputRegister(1,(short) 55);
-        processImage.setInputRegister(2,(short) 66);
-    }
-}

+ 1 - 1
src/main/resources/META-INF/spring.factories

@@ -1,3 +1,3 @@
 # Auto Configure
 org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
-com.jwk.spring.boot.autoconfigure.ModbusAutoConfiguration
+com.jwk.spring.boot.autoconfigure.AutoConfiguration

BIN
src/main/resources/lib/RXTXcomm.jar


BIN
src/main/resources/lib/jssc-2.8.0.jar


BIN
src/main/resources/lib/modbus4j-3.0.5.jar


BIN
src/main/resources/lib/rxtxParallel.dll


BIN
src/main/resources/lib/rxtxSerial.dll