whyclj
2020-09-24 2b64a566f77f90fe47f62863b7a185b6e1c514b7
WaterSystem_MonitorServer/src/com/modbus/data/MyModbusUtils.java
@@ -1,9 +1,12 @@
package com.modbus.data;
import java.util.Date;
import com.base.ComBase;
import com.serotonin.modbus4j.BatchRead;
import com.serotonin.modbus4j.BatchResults;
import com.serotonin.modbus4j.ModbusMaster;
import com.serotonin.modbus4j.code.DataType;
import com.serotonin.modbus4j.exception.ErrorResponseException;
import com.serotonin.modbus4j.exception.ModbusTransportException;
import com.serotonin.modbus4j.locator.BaseLocator;
@@ -30,12 +33,19 @@
   public static Boolean readCoilStatus(int offset,MyModbusMaster master){
      // 01 Coil Status
      BaseLocator<Boolean> loc = BaseLocator.coilStatus(master.getSlaveId(), offset);
      Boolean value = null;;
      Boolean value = null;
      boolean isSuccess = true;
      try {
         value = master.getMaster().getValue(loc);
      } catch (ModbusTransportException | ErrorResponseException e) {
         master.addErrorCount();
         //e.printStackTrace();
         isSuccess = false;
      } finally {
         if(isSuccess) {
            master.clearError();
         }else {
            master.addErrorCount();
         }
      }
      return value;
   }
@@ -51,11 +61,18 @@
      // 02 Input Status
      BaseLocator<Boolean> loc = BaseLocator.inputStatus(master.getSlaveId(), offset);
      Boolean value = null;;
      boolean isSuccess = true;
      try {
         value = master.getMaster().getValue(loc);
      } catch (ModbusTransportException | ErrorResponseException e) {
         master.addErrorCount();
         //e.printStackTrace();
         isSuccess = false;
      } finally {
         if(isSuccess) {
            master.clearError();
         }else {
            master.addErrorCount();
         }
      }
      return value;
   }
@@ -75,11 +92,18 @@
      // 03 Holding Register类型数据读取
      BaseLocator<Number> loc = BaseLocator.holdingRegister(master.getSlaveId(), offset, dataType);
      Number value = null;
      boolean isSuccess = true;
      try {
         value = master.getMaster().getValue(loc);
      } catch (ModbusTransportException | ErrorResponseException e) {
         //e.printStackTrace();
         master.addErrorCount();
         isSuccess = false;
      }  finally {
         if(isSuccess) {
            master.clearError();
         }else {
            master.addErrorCount();
         }
      }
      return value;
   }
@@ -105,15 +129,41 @@
      // 04 Input Registers类型数据读取
      BaseLocator<Number> loc = BaseLocator.inputRegister(master.getSlaveId(), offset, dataType);
      Number value = null;
      boolean isSuccess = true;
      try {
         value = master.getMaster().getValue(loc);
      } catch (ModbusTransportException | ErrorResponseException e) {
         //e.printStackTrace();
         master.addErrorCount();
         isSuccess = false;
      } finally {
         if(isSuccess) {
            master.clearError();
         }else {
            master.addErrorCount();
         }
      }
      return value;
   }
 
   public static BatchResults<Integer> readMutilRegisters(BatchRead<Integer> batch,MyModbusMaster master){
      batch.setContiguousRequests(false);
      boolean isSuccess = true;
      try {
         return master.getMaster().send(batch);
      } catch (ModbusTransportException | ErrorResponseException e) {
         //e.printStackTrace();
         isSuccess = false;
      } finally {
         if(isSuccess) {
            master.clearError();
         }else {
            master.addErrorCount();
         }
      }
      return null;
   }
   /**
    * 批量读取使用方法
    * 
@@ -134,7 +184,27 @@
//      System.out.println(results.getValue(0));
//      System.out.println(results.getValue(1));
   }
   /**
    *    构造线圈读取节点
    * @param offset
    * @param master
    * @return
    */
   public static BaseLocator<?> createBaseLocator(int offset,MyModbusMaster master) {
      return BaseLocator.coilStatus(master.getSlaveId(), offset);
   }
   /**
    *    构造读取节点
    * @param offset
    * @param master
    * @return
    */
   public static BaseLocator<?> createBaseLocator(int offset,int datatype,MyModbusMaster master) {
      return BaseLocator.holdingRegister(master.getSlaveId(), offset, datatype);
   }
   /**
    * 写 [01 Coil Status(0x)]写一个 function ID = 5
    * 
@@ -162,6 +232,7 @@
         master.addErrorCount();
         return false;
      } else {
         master.clearError();
         return true;
      }
   }
@@ -194,6 +265,7 @@
         master.addErrorCount();
         return false;
      } else {
         master.clearError();
         return true;
      }
 
@@ -223,6 +295,7 @@
         master.addErrorCount();
         return false;
      } else {
         master.clearError();
         return true;
      }
 
@@ -258,6 +331,7 @@
         master.addErrorCount();
         return false;
      } else {
         master.clearError();
         return true;
      }
   }
@@ -277,42 +351,42 @@
      ModbusMaster tcpMaster = master.getMaster();
      // 类型
      BaseLocator<Number> locator = BaseLocator.holdingRegister(master.getSlaveId(), offset, dataType);
      boolean isSuccess = true;
      try {
         tcpMaster.setValue(locator, value);
      } catch (ModbusTransportException | ErrorResponseException e) {
         e.printStackTrace();
         //e.printStackTrace();
         isSuccess = true;
      } finally {
         if(isSuccess) {
            master.clearError();
         }else {
            master.addErrorCount();
         }
      }
   }
   public static BatchResults<Integer> readMutilRegisters(BatchRead<Integer> batch,MyModbusMaster master){
      batch.setContiguousRequests(false);
   public static void main(String[] args) {
      BatchRead<Integer> batch = new BatchRead<Integer>();
      batch.addLocator(0, BaseLocator.holdingRegister(1, 1, DataType.TWO_BYTE_INT_SIGNED));
        batch.addLocator(1, BaseLocator.coilStatus(1, 1));
        batch.setContiguousRequests(true);
      MyModbusMaster m = new MyModbusMaster("192.168.10.221", 2);
      ModbusMaster master = m.getMaster();
      BatchResults<Integer> results = new BatchResults<>();
      try {
         return master.getMaster().send(batch);
         results = master.send(batch);
      } catch (ModbusTransportException | ErrorResponseException e) {
         e.printStackTrace();
      }
      return null;
   }
   /**
    *    构造线圈读取节点
    * @param offset
    * @param master
    * @return
    */
   public static BaseLocator<?> createBaseLocator(int offset,MyModbusMaster master) {
      return BaseLocator.coilStatus(master.getSlaveId(), offset);
      System.out.println(results.getValue(0));
      //System.out.println(results.getValue(1));
   }
   
   /**
    *    构造读取节点
    * @param offset
    * @param master
    * @return
    */
   public static BaseLocator<?> createBaseLocator(int offset,int datatype,MyModbusMaster master) {
      return BaseLocator.holdingRegister(master.getSlaveId(), offset, datatype);
   }
   /**
    *    读取float类型数据
    * @param obj   Short类型