From 47f9d4a231a116f528f9d18d738420dba7f753bd Mon Sep 17 00:00:00 2001
From: 严智鑫 <test>
Date: 星期三, 26 三月 2025 23:43:15 +0800
Subject: [PATCH] 成功使用ModbusTcp,S7连接设备并且存储到数据库

---
 nglib/src/ng/devices/ModbusService.java |   37 ++++++++++++++++++++++++++++++-------
 1 files changed, 30 insertions(+), 7 deletions(-)

diff --git a/nglib/src/ng/devices/ModbusService.java b/nglib/src/ng/devices/ModbusService.java
index c8d3fba..897a45a 100644
--- a/nglib/src/ng/devices/ModbusService.java
+++ b/nglib/src/ng/devices/ModbusService.java
@@ -2,6 +2,8 @@
 
 import java.sql.SQLException;
 
+import builder.S7control;
+import com.github.xingshuangs.iot.protocol.s7.enums.EPlcType;
 import ng.db.DBHelper;
 import ng.db.DBSession;
 import ng.db.DBSession.StdCallResult;
@@ -49,12 +51,19 @@
     	this.readOffset=ReadOffset;
     	this.readSize=ReadSize;
 		this.functionType= FunctionType;
-    	this.client=new ModbusClient();
-    	//鍚姩绾跨▼
-    	this.client.setConnectionParam(MachineID,ip,port,(byte)state,timeout);
 
-    	thread=new java.lang.Thread(this);
-    	thread.start();
+    	//鍚姩绾跨▼
+		if(this.port==102){
+			thread=new java.lang.Thread(this);
+			thread.start();
+		}else{
+			this.client=new ModbusClient();
+			this.client.setConnectionParam(MachineID,ip,port,(byte)state,timeout);
+			thread=new java.lang.Thread(this);
+			thread.start();
+		}
+
+
     }
     
     
@@ -110,10 +119,23 @@
 	void proc(){
 		running=true;
 		while(running){
-			if(client.IsEnable()){
+			if(this.port==102||client.IsEnable()){
 				try {
 					java.lang.Thread.sleep(UpadateInterval);
-					String back=once();
+					if(this.port==102){
+						if(this.machineID==78){
+							S7control s7=new S7control(EPlcType.S1500, ip, port, 0, 0);
+							System.out.println("S7-S1500:"+s7.readWord("DB34.122",  1));
+							s7.writeWord("DB34.122",26);
+							System.out.println("S7-S1500:"+s7.readWord("DB34.122",  1));
+						}else{
+							S7control s7=new S7control(EPlcType.S1200, ip, port, 0, 0);
+							System.out.println("S7-S1200:"+s7.readWord("DB20.0",  1));
+						}
+					}else{
+						String back=once();
+					}
+
 				} catch (InterruptedException e) {
 					// TODO Auto-generated catch block
 				}
@@ -141,6 +163,7 @@
 	@Override
 	public void run() {
 		// TODO Auto-generated method stub
+
 		proc();
 	}
 	public class ModbusDataPackage{

--
Gitblit v1.8.0