From e1f3896f2434f829d284161552758e392cb21835 Mon Sep 17 00:00:00 2001 From: whyclxw <810412026@qq,com> Date: 星期四, 10 九月 2020 18:11:15 +0800 Subject: [PATCH] 修改 --- WaterSystem_MonitorServer/src/com/modbus/data/MyModbusUtils.java | 128 ++++++++++++++++++++++++++++++++---------- 1 files changed, 97 insertions(+), 31 deletions(-) diff --git a/WaterSystem_MonitorServer/src/com/modbus/data/MyModbusUtils.java b/WaterSystem_MonitorServer/src/com/modbus/data/MyModbusUtils.java index 6cdf707..e967877 100644 --- a/WaterSystem_MonitorServer/src/com/modbus/data/MyModbusUtils.java +++ b/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,33 @@ // 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); + try { + return master.getMaster().send(batch); + } catch (ModbusTransportException | ErrorResponseException e) { + e.printStackTrace(); + } + return null; + } + + /** * 批量读取使用方法 * @@ -134,7 +176,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 +224,7 @@ master.addErrorCount(); return false; } else { + master.clearError(); return true; } } @@ -194,6 +257,7 @@ master.addErrorCount(); return false; } else { + master.clearError(); return true; } @@ -223,6 +287,7 @@ master.addErrorCount(); return false; } else { + master.clearError(); return true; } @@ -258,6 +323,7 @@ master.addErrorCount(); return false; } else { + master.clearError(); return true; } } @@ -277,42 +343,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类型 -- Gitblit v1.9.1