From dec31d5d19346c24412e8b51783455a1f4bec7b1 Mon Sep 17 00:00:00 2001
From: whyclj <1525436766@qq.com>
Date: 星期三, 26 八月 2020 21:29:18 +0800
Subject: [PATCH] 提高读取速度

---
 Motor_MonitorServer/src/com/modbus/data/MyModbusUtils.java |  147 ++++++++++++++++++++++++++++++++++++++++++++++--
 1 files changed, 139 insertions(+), 8 deletions(-)

diff --git a/Motor_MonitorServer/src/com/modbus/data/MyModbusUtils.java b/Motor_MonitorServer/src/com/modbus/data/MyModbusUtils.java
index f16410b..fb37f50 100644
--- a/Motor_MonitorServer/src/com/modbus/data/MyModbusUtils.java
+++ b/Motor_MonitorServer/src/com/modbus/data/MyModbusUtils.java
@@ -1,6 +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;
@@ -27,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;
 	}
@@ -48,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;
 	}
@@ -72,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;
 	}
@@ -102,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;
+	}
+	
+	
 	/**
 	 * 批量读取使用方法
 	 * 
@@ -131,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
 	 * 
@@ -159,6 +224,7 @@
 			master.addErrorCount();
 			return false;
 		} else {
+			master.clearError();
 			return true;
 		}
 	}
@@ -191,6 +257,7 @@
 			master.addErrorCount();
 			return false;
 		} else {
+			master.clearError();
 			return true;
 		}
  
@@ -220,6 +287,7 @@
 			master.addErrorCount();
 			return false;
 		} else {
+			master.clearError();
 			return true;
 		}
  
@@ -255,6 +323,7 @@
 			master.addErrorCount();
 			return false;
 		} else {
+			master.clearError();
 			return true;
 		}
 	}
@@ -274,13 +343,75 @@
 		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 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 {
+			results = master.send(batch);
+		} catch (ModbusTransportException | ErrorResponseException e) {
+			e.printStackTrace();
+		}
+		System.out.println(results.getValue(0));
+		//System.out.println(results.getValue(1));
+	}
 	
+	/**
+	 * 	读取float类型数据
+	 * @param obj	Short类型
+	 * @return
+	 */
+	public static float readShortToFloat(Object obj) {
+		return (float)ComBase.changeShortToInt((Short)obj);
+	}
+	
+	/**
+	 * 	读取float类型数据
+	 * @param obj	Integer类型
+	 * @return
+	 */
+	public static float readIntegerToFloat(Object obj) {
+		return (float)((int)obj);
+	}
+	
+	/**
+	 * 	读取int类型数据
+	 * @param obj	Short类型
+	 * @return
+	 */
+	public static int readShortToInt(Object obj) {
+		return ComBase.changeShortToInt((Short)obj);
+	}
+	
+	/**
+	 * 	读取Boolean类型数据
+	 * @param obj	Boolean类型
+	 * @return
+	 */
+	public static int readBooleanToInt(Object obj) {
+		return ((Boolean)obj?1:0);
+	}
 }

--
Gitblit v1.9.1