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

---
 nglib/src/builder/ThreadHttpServer.java |    5 
 nglib/src/ng/devices/ModbusService.java |   37 ++
 nglib/src/builder/HttpHandlerDemo.java  |    1 
 nglib/src/builder/S7control.java        |  487 ++++++++++++++++++++++++++++++++
 nglib/src/ng/devices/MysqlService.java  |   25 
 .idea/vcs.xml                           |    6 
 .idea/workspace.xml                     |   77 +++++
 nglib/.idea/encodings.xml               |    4 
 gmms/WebContent/login/run.jsp           |    4 
 nglib/nglib.iml                         |   43 +-
 nglib/.idea/uiDesigner.xml              |  124 ++++++++
 nglib/src/builder/Manager.java          |   61 ++-
 nglib/src/builder/MachineManager.java   |    4 
 13 files changed, 813 insertions(+), 65 deletions(-)

diff --git a/.idea/vcs.xml b/.idea/vcs.xml
new file mode 100644
index 0000000..35eb1dd
--- /dev/null
+++ b/.idea/vcs.xml
@@ -0,0 +1,6 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<project version="4">
+  <component name="VcsDirectoryMappings">
+    <mapping directory="" vcs="Git" />
+  </component>
+</project>
\ No newline at end of file
diff --git a/.idea/workspace.xml b/.idea/workspace.xml
new file mode 100644
index 0000000..1aca4db
--- /dev/null
+++ b/.idea/workspace.xml
@@ -0,0 +1,77 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<project version="4">
+  <component name="AutoImportSettings">
+    <option name="autoReloadType" value="SELECTIVE" />
+  </component>
+  <component name="ChangeListManager">
+    <list default="true" id="8fb06979-163e-47f4-9dfa-cdbddc7489f0" name="鏇存敼" comment="" />
+    <option name="SHOW_DIALOG" value="false" />
+    <option name="HIGHLIGHT_CONFLICTS" value="true" />
+    <option name="HIGHLIGHT_NON_ACTIVE_CHANGELIST" value="false" />
+    <option name="LAST_RESOLUTION" value="IGNORE" />
+  </component>
+  <component name="Git.Settings">
+    <option name="RECENT_GIT_ROOT_PATH" value="$PROJECT_DIR$" />
+  </component>
+  <component name="ProjectColorInfo">{
+  &quot;associatedIndex&quot;: 2
+}</component>
+  <component name="ProjectId" id="2uHpKN6b8NaF21shwcxgtYDcJ0z" />
+  <component name="ProjectViewState">
+    <option name="hideEmptyMiddlePackages" value="true" />
+    <option name="showLibraryContents" value="true" />
+  </component>
+  <component name="PropertiesComponent">{
+  &quot;keyToString&quot;: {
+    &quot;RunOnceActivity.ShowReadmeOnStart&quot;: &quot;true&quot;,
+    &quot;git-widget-placeholder&quot;: &quot;master&quot;,
+    &quot;kotlin-language-version-configured&quot;: &quot;true&quot;,
+    &quot;last_opened_file_path&quot;: &quot;D:/workfile/Haier_MES_II&quot;,
+    &quot;node.js.detected.package.eslint&quot;: &quot;true&quot;,
+    &quot;node.js.detected.package.tslint&quot;: &quot;true&quot;,
+    &quot;node.js.selected.package.eslint&quot;: &quot;(autodetect)&quot;,
+    &quot;node.js.selected.package.tslint&quot;: &quot;(autodetect)&quot;,
+    &quot;nodejs_package_manager_path&quot;: &quot;npm&quot;,
+    &quot;vue.rearranger.settings.migration&quot;: &quot;true&quot;
+  }
+}</component>
+  <component name="SharedIndexes">
+    <attachedChunks>
+      <set>
+        <option value="bundled-jdk-9f38398b9061-18abd8497189-intellij.indexing.shared.core-IU-241.14494.240" />
+        <option value="bundled-js-predefined-1d06a55b98c1-74d2a5396914-JavaScript-IU-241.14494.240" />
+      </set>
+    </attachedChunks>
+  </component>
+  <component name="SpellCheckerSettings" RuntimeDictionaries="0" Folders="0" CustomDictionaries="0" DefaultDictionary="搴旂敤绋嬪簭绾�" UseSingleDictionary="true" transferred="true" />
+  <component name="TaskManager">
+    <task active="true" id="Default" summary="榛樿浠诲姟">
+      <changelist id="8fb06979-163e-47f4-9dfa-cdbddc7489f0" name="鏇存敼" comment="" />
+      <created>1741915288398</created>
+      <option name="number" value="Default" />
+      <option name="presentableId" value="Default" />
+      <updated>1741915288398</updated>
+      <workItem from="1741915289710" duration="1265000" />
+      <workItem from="1742172726815" duration="696000" />
+    </task>
+    <servers />
+  </component>
+  <component name="TypeScriptGeneratedFilesManager">
+    <option name="version" value="3" />
+  </component>
+  <component name="Vcs.Log.Tabs.Properties">
+    <option name="TAB_STATES">
+      <map>
+        <entry key="MAIN">
+          <value>
+            <State />
+          </value>
+        </entry>
+      </map>
+    </option>
+  </component>
+  <component name="VcsManagerConfiguration">
+    <MESSAGE value="鍒嗘壒鎻愪氦" />
+    <option name="LAST_COMMIT_MESSAGE" value="" />
+  </component>
+</project>
\ No newline at end of file
diff --git a/gmms/WebContent/login/run.jsp b/gmms/WebContent/login/run.jsp
index f84d3f8..1dfd45d 100644
--- a/gmms/WebContent/login/run.jsp
+++ b/gmms/WebContent/login/run.jsp
@@ -3,9 +3,9 @@
 <%@page import="toTcp.TcpMain"%>
 <%@include file="../mysqlInsert/mysql_connect.jsp"%>
 <%
-ThreadHttpServer.mains();
+//ThreadHttpServer.mains();
 //TcpMain.getElectric();
-//Manager.appStart(new String[]{url,user,password });
+Manager.appStart(new String[]{url,user,password });
 
 //response.sendRedirect("bdg.jsp");
 %>
\ No newline at end of file
diff --git a/nglib/.idea/encodings.xml b/nglib/.idea/encodings.xml
index fdeccc0..d60cdc1 100644
--- a/nglib/.idea/encodings.xml
+++ b/nglib/.idea/encodings.xml
@@ -1,9 +1,13 @@
 <?xml version="1.0" encoding="UTF-8"?>
 <project version="4">
   <component name="Encoding">
+    <file url="file://$PROJECT_DIR$/bin/S7/S7control.java" charset="UTF-8" />
     <file url="file://$PROJECT_DIR$/src/builder/HttpHandlerDemo.java" charset="GBK" />
     <file url="file://$PROJECT_DIR$/src/builder/MachineManager.java" charset="GBK" />
     <file url="file://$PROJECT_DIR$/src/builder/Manager.java" charset="GBK" />
+    <file url="file://$PROJECT_DIR$/src/builder/S7control.java" charset="UTF-8" />
+    <file url="file://$PROJECT_DIR$/src/builder/S7control.java" charset="UTF-8" />
+    <file url="file://$PROJECT_DIR$/src/builder/S7control.java" charset="UTF-8" />
     <file url="file://$PROJECT_DIR$/src/builder/ThreadHttpServer.java" charset="GBK" />
     <file url="file://$PROJECT_DIR$/src/ng/db/DBSession.java" charset="UTF-8" />
     <file url="file://$PROJECT_DIR$/src/toTcp/TCPClient.java" charset="GBK" />
diff --git a/nglib/.idea/uiDesigner.xml b/nglib/.idea/uiDesigner.xml
new file mode 100644
index 0000000..2b63946
--- /dev/null
+++ b/nglib/.idea/uiDesigner.xml
@@ -0,0 +1,124 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<project version="4">
+  <component name="Palette2">
+    <group name="Swing">
+      <item class="com.intellij.uiDesigner.HSpacer" tooltip-text="Horizontal Spacer" icon="/com/intellij/uiDesigner/icons/hspacer.svg" removable="false" auto-create-binding="false" can-attach-label="false">
+        <default-constraints vsize-policy="1" hsize-policy="6" anchor="0" fill="1" />
+      </item>
+      <item class="com.intellij.uiDesigner.VSpacer" tooltip-text="Vertical Spacer" icon="/com/intellij/uiDesigner/icons/vspacer.svg" removable="false" auto-create-binding="false" can-attach-label="false">
+        <default-constraints vsize-policy="6" hsize-policy="1" anchor="0" fill="2" />
+      </item>
+      <item class="javax.swing.JPanel" icon="/com/intellij/uiDesigner/icons/panel.svg" removable="false" auto-create-binding="false" can-attach-label="false">
+        <default-constraints vsize-policy="3" hsize-policy="3" anchor="0" fill="3" />
+      </item>
+      <item class="javax.swing.JScrollPane" icon="/com/intellij/uiDesigner/icons/scrollPane.svg" removable="false" auto-create-binding="false" can-attach-label="true">
+        <default-constraints vsize-policy="7" hsize-policy="7" anchor="0" fill="3" />
+      </item>
+      <item class="javax.swing.JButton" icon="/com/intellij/uiDesigner/icons/button.svg" removable="false" auto-create-binding="true" can-attach-label="false">
+        <default-constraints vsize-policy="0" hsize-policy="3" anchor="0" fill="1" />
+        <initial-values>
+          <property name="text" value="Button" />
+        </initial-values>
+      </item>
+      <item class="javax.swing.JRadioButton" icon="/com/intellij/uiDesigner/icons/radioButton.svg" removable="false" auto-create-binding="true" can-attach-label="false">
+        <default-constraints vsize-policy="0" hsize-policy="3" anchor="8" fill="0" />
+        <initial-values>
+          <property name="text" value="RadioButton" />
+        </initial-values>
+      </item>
+      <item class="javax.swing.JCheckBox" icon="/com/intellij/uiDesigner/icons/checkBox.svg" removable="false" auto-create-binding="true" can-attach-label="false">
+        <default-constraints vsize-policy="0" hsize-policy="3" anchor="8" fill="0" />
+        <initial-values>
+          <property name="text" value="CheckBox" />
+        </initial-values>
+      </item>
+      <item class="javax.swing.JLabel" icon="/com/intellij/uiDesigner/icons/label.svg" removable="false" auto-create-binding="false" can-attach-label="false">
+        <default-constraints vsize-policy="0" hsize-policy="0" anchor="8" fill="0" />
+        <initial-values>
+          <property name="text" value="Label" />
+        </initial-values>
+      </item>
+      <item class="javax.swing.JTextField" icon="/com/intellij/uiDesigner/icons/textField.svg" removable="false" auto-create-binding="true" can-attach-label="true">
+        <default-constraints vsize-policy="0" hsize-policy="6" anchor="8" fill="1">
+          <preferred-size width="150" height="-1" />
+        </default-constraints>
+      </item>
+      <item class="javax.swing.JPasswordField" icon="/com/intellij/uiDesigner/icons/passwordField.svg" removable="false" auto-create-binding="true" can-attach-label="true">
+        <default-constraints vsize-policy="0" hsize-policy="6" anchor="8" fill="1">
+          <preferred-size width="150" height="-1" />
+        </default-constraints>
+      </item>
+      <item class="javax.swing.JFormattedTextField" icon="/com/intellij/uiDesigner/icons/formattedTextField.svg" removable="false" auto-create-binding="true" can-attach-label="true">
+        <default-constraints vsize-policy="0" hsize-policy="6" anchor="8" fill="1">
+          <preferred-size width="150" height="-1" />
+        </default-constraints>
+      </item>
+      <item class="javax.swing.JTextArea" icon="/com/intellij/uiDesigner/icons/textArea.svg" removable="false" auto-create-binding="true" can-attach-label="true">
+        <default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3">
+          <preferred-size width="150" height="50" />
+        </default-constraints>
+      </item>
+      <item class="javax.swing.JTextPane" icon="/com/intellij/uiDesigner/icons/textPane.svg" removable="false" auto-create-binding="true" can-attach-label="true">
+        <default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3">
+          <preferred-size width="150" height="50" />
+        </default-constraints>
+      </item>
+      <item class="javax.swing.JEditorPane" icon="/com/intellij/uiDesigner/icons/editorPane.svg" removable="false" auto-create-binding="true" can-attach-label="true">
+        <default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3">
+          <preferred-size width="150" height="50" />
+        </default-constraints>
+      </item>
+      <item class="javax.swing.JComboBox" icon="/com/intellij/uiDesigner/icons/comboBox.svg" removable="false" auto-create-binding="true" can-attach-label="true">
+        <default-constraints vsize-policy="0" hsize-policy="2" anchor="8" fill="1" />
+      </item>
+      <item class="javax.swing.JTable" icon="/com/intellij/uiDesigner/icons/table.svg" removable="false" auto-create-binding="true" can-attach-label="false">
+        <default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3">
+          <preferred-size width="150" height="50" />
+        </default-constraints>
+      </item>
+      <item class="javax.swing.JList" icon="/com/intellij/uiDesigner/icons/list.svg" removable="false" auto-create-binding="true" can-attach-label="false">
+        <default-constraints vsize-policy="6" hsize-policy="2" anchor="0" fill="3">
+          <preferred-size width="150" height="50" />
+        </default-constraints>
+      </item>
+      <item class="javax.swing.JTree" icon="/com/intellij/uiDesigner/icons/tree.svg" removable="false" auto-create-binding="true" can-attach-label="false">
+        <default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3">
+          <preferred-size width="150" height="50" />
+        </default-constraints>
+      </item>
+      <item class="javax.swing.JTabbedPane" icon="/com/intellij/uiDesigner/icons/tabbedPane.svg" removable="false" auto-create-binding="true" can-attach-label="false">
+        <default-constraints vsize-policy="3" hsize-policy="3" anchor="0" fill="3">
+          <preferred-size width="200" height="200" />
+        </default-constraints>
+      </item>
+      <item class="javax.swing.JSplitPane" icon="/com/intellij/uiDesigner/icons/splitPane.svg" removable="false" auto-create-binding="false" can-attach-label="false">
+        <default-constraints vsize-policy="3" hsize-policy="3" anchor="0" fill="3">
+          <preferred-size width="200" height="200" />
+        </default-constraints>
+      </item>
+      <item class="javax.swing.JSpinner" icon="/com/intellij/uiDesigner/icons/spinner.svg" removable="false" auto-create-binding="true" can-attach-label="true">
+        <default-constraints vsize-policy="0" hsize-policy="6" anchor="8" fill="1" />
+      </item>
+      <item class="javax.swing.JSlider" icon="/com/intellij/uiDesigner/icons/slider.svg" removable="false" auto-create-binding="true" can-attach-label="false">
+        <default-constraints vsize-policy="0" hsize-policy="6" anchor="8" fill="1" />
+      </item>
+      <item class="javax.swing.JSeparator" icon="/com/intellij/uiDesigner/icons/separator.svg" removable="false" auto-create-binding="false" can-attach-label="false">
+        <default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3" />
+      </item>
+      <item class="javax.swing.JProgressBar" icon="/com/intellij/uiDesigner/icons/progressbar.svg" removable="false" auto-create-binding="true" can-attach-label="false">
+        <default-constraints vsize-policy="0" hsize-policy="6" anchor="0" fill="1" />
+      </item>
+      <item class="javax.swing.JToolBar" icon="/com/intellij/uiDesigner/icons/toolbar.svg" removable="false" auto-create-binding="false" can-attach-label="false">
+        <default-constraints vsize-policy="0" hsize-policy="6" anchor="0" fill="1">
+          <preferred-size width="-1" height="20" />
+        </default-constraints>
+      </item>
+      <item class="javax.swing.JToolBar$Separator" icon="/com/intellij/uiDesigner/icons/toolbarSeparator.svg" removable="false" auto-create-binding="false" can-attach-label="false">
+        <default-constraints vsize-policy="0" hsize-policy="0" anchor="0" fill="1" />
+      </item>
+      <item class="javax.swing.JScrollBar" icon="/com/intellij/uiDesigner/icons/scrollbar.svg" removable="false" auto-create-binding="true" can-attach-label="false">
+        <default-constraints vsize-policy="6" hsize-policy="0" anchor="0" fill="2" />
+      </item>
+    </group>
+  </component>
+</project>
\ No newline at end of file
diff --git a/nglib/nglib.iml b/nglib/nglib.iml
index 747da8d..fcfffa8 100644
--- a/nglib/nglib.iml
+++ b/nglib/nglib.iml
@@ -16,16 +16,16 @@
     </content>
     <orderEntry type="jdk" jdkName="JavaSE-1.8" jdkType="JavaSDK" />
     <orderEntry type="sourceFolder" forTests="false" />
-    <orderEntry type="module-library" exported="">
+    <orderEntry type="module-library">
       <library>
         <CLASSES>
-          <root url="jar://$MODULE_DIR$/../json-20230618.jar!/" />
+          <root url="jar://$MODULE_DIR$/../json.jar!/" />
         </CLASSES>
         <JAVADOC />
         <SOURCES />
       </library>
     </orderEntry>
-    <orderEntry type="module-library" exported="">
+    <orderEntry type="module-library">
       <library>
         <CLASSES>
           <root url="jar://$MODULE_DIR$/../mysql-connector-java-8.0.20.jar!/" />
@@ -34,7 +34,7 @@
         <SOURCES />
       </library>
     </orderEntry>
-    <orderEntry type="module-library" exported="">
+    <orderEntry type="module-library">
       <library>
         <CLASSES>
           <root url="jar://$MODULE_DIR$/../jacob-1.18.jar!/" />
@@ -43,7 +43,7 @@
         <SOURCES />
       </library>
     </orderEntry>
-    <orderEntry type="module-library" exported="">
+    <orderEntry type="module-library">
       <library>
         <CLASSES>
           <root url="jar://$MODULE_DIR$/../fastjson-1.2.2.jar!/" />
@@ -52,7 +52,7 @@
         <SOURCES />
       </library>
     </orderEntry>
-    <orderEntry type="module-library" exported="">
+    <orderEntry type="module-library">
       <library>
         <CLASSES>
           <root url="jar://E:/HuaweiMoveData/Users/SNG-010/Desktop/娴峰皵浜屾湡/iot-communication-1.4.2.jar!/" />
@@ -61,7 +61,7 @@
         <SOURCES />
       </library>
     </orderEntry>
-    <orderEntry type="module-library" exported="">
+    <orderEntry type="module-library">
       <library>
         <CLASSES>
           <root url="jar://E:/HuaweiMoveData/Users/SNG-010/Desktop/娴峰皵浜屾湡/jul-to-slf4j-1.7.28.jar!/" />
@@ -70,7 +70,7 @@
         <SOURCES />
       </library>
     </orderEntry>
-    <orderEntry type="module-library" exported="">
+    <orderEntry type="module-library">
       <library>
         <CLASSES>
           <root url="jar://E:/HuaweiMoveData/Users/SNG-010/Desktop/娴峰皵浜屾湡/logback-classic-1.2.3.jar!/" />
@@ -79,7 +79,7 @@
         <SOURCES />
       </library>
     </orderEntry>
-    <orderEntry type="module-library" exported="">
+    <orderEntry type="module-library">
       <library>
         <CLASSES>
           <root url="jar://E:/HuaweiMoveData/Users/SNG-010/Desktop/娴峰皵浜屾湡/logback-core-1.2.3.jar!/" />
@@ -88,7 +88,7 @@
         <SOURCES />
       </library>
     </orderEntry>
-    <orderEntry type="module-library" exported="">
+    <orderEntry type="module-library">
       <library>
         <CLASSES>
           <root url="jar://E:/HuaweiMoveData/Users/SNG-010/Desktop/娴峰皵浜屾湡/nglib.jar!/" />
@@ -97,7 +97,7 @@
         <SOURCES />
       </library>
     </orderEntry>
-    <orderEntry type="module-library" exported="">
+    <orderEntry type="module-library">
       <library>
         <CLASSES>
           <root url="jar://E:/HuaweiMoveData/Users/SNG-010/Desktop/娴峰皵浜屾湡/slf4j-api-1.7.28.jar!/" />
@@ -106,7 +106,7 @@
         <SOURCES />
       </library>
     </orderEntry>
-    <orderEntry type="module-library" exported="">
+    <orderEntry type="module-library">
       <library>
         <CLASSES>
           <root url="jar://$MODULE_DIR$/../druid-1.1.12.jar!/" />
@@ -115,7 +115,7 @@
         <SOURCES />
       </library>
     </orderEntry>
-    <orderEntry type="module-library" exported="">
+    <orderEntry type="module-library">
       <library>
         <CLASSES>
           <root url="jar://$MODULE_DIR$/../commons-net-3.9.0.jar!/" />
@@ -124,7 +124,7 @@
         <SOURCES />
       </library>
     </orderEntry>
-    <orderEntry type="module-library" exported="">
+    <orderEntry type="module-library">
       <library>
         <CLASSES>
           <root url="jar://$MODULE_DIR$/../artemis-http-client-1.1.10.jar!/" />
@@ -133,7 +133,7 @@
         <SOURCES />
       </library>
     </orderEntry>
-    <orderEntry type="module-library" exported="">
+    <orderEntry type="module-library">
       <library>
         <CLASSES>
           <root url="jar://$MODULE_DIR$/../iot-communication-1.4.2.jar!/" />
@@ -142,5 +142,18 @@
         <SOURCES />
       </library>
     </orderEntry>
+    <orderEntry type="module-library" exported="">
+      <library>
+        <CLASSES>
+          <root url="file://$MODULE_DIR$/lib" />
+        </CLASSES>
+        <JAVADOC />
+        <SOURCES>
+          <root url="file://$MODULE_DIR$/lib" />
+        </SOURCES>
+        <jarDirectory url="file://$MODULE_DIR$/lib" recursive="false" />
+        <jarDirectory url="file://$MODULE_DIR$/lib" recursive="false" type="SOURCES" />
+      </library>
+    </orderEntry>
   </component>
 </module>
\ No newline at end of file
diff --git a/nglib/src/builder/HttpHandlerDemo.java b/nglib/src/builder/HttpHandlerDemo.java
index 2c5f029..0314693 100644
--- a/nglib/src/builder/HttpHandlerDemo.java
+++ b/nglib/src/builder/HttpHandlerDemo.java
@@ -77,7 +77,6 @@
 			String result=null;
 
 			try{
-
 				sn=DBHelper.getDBHelper("mes").createSession(false);
 				Connection con= sn.getConnection();
 				CallableStatement sql=con.prepareCall("{call Face_recognition(?,?,?,?)}");
diff --git a/nglib/src/builder/MachineManager.java b/nglib/src/builder/MachineManager.java
index 9f23c90..ec6262f 100644
--- a/nglib/src/builder/MachineManager.java
+++ b/nglib/src/builder/MachineManager.java
@@ -34,7 +34,10 @@
 	List<item> items=new ArrayList<item>();
 	
 	public void RunAll(){
+
+
 	   for(item it:items){
+		   System.out.println(it);
 		   Run(it.Name);
 		 
 	   }
@@ -49,6 +52,7 @@
 		if(itm!=null){
 			if(itm.serv==null){
 				itm.serv=new MysqlService(itm.helper,itm.ID);
+
 				itm.serv.Run(itm.ID,itm.IP,itm.Port, (byte)1,itm.ReadOffset, itm.ReadSize,1000,itm.FunctionType);
 				System.out.println(String.format("设备启动-{%s}(%s)(%s)",itm.Name,itm.IP,itm.FunctionType));
 			}
diff --git a/nglib/src/builder/Manager.java b/nglib/src/builder/Manager.java
index 2ef111f..644db4b 100644
--- a/nglib/src/builder/Manager.java
+++ b/nglib/src/builder/Manager.java
@@ -1,6 +1,5 @@
 package builder;
 
-import S7.S7control;
 import com.github.xingshuangs.iot.protocol.s7.enums.EPlcType;
 import ng.db.DBHelper;
 import ng.db.DBSession;
@@ -12,7 +11,8 @@
 import java.net.Socket;
 import java.sql.ResultSet;
 
-public class Manager {
+
+public class Manager extends Thread {
 	//设备管理器
 	static MachineManager mmgr;
 	//优化排版管理器
@@ -82,35 +82,44 @@
 			//读取设备列表
 			DBHelper db=DBHelper.getDBHelper("mes");
 			sn=DBHelper.getDBHelper("mes").createSession(false);
-			sn.createSql("select id,machine_name,ip_address,port,flag,read_size,read_offset,function_type,flag from machine where id=78");
+			sn.createSql("select id,machine_name,ip_address,port,flag,read_size,read_offset,function_type from machine where id>=80");
 			ResultSet set= sn.query().getCurrentResult();
-			System.out.println("S7链接");
-			S7control s7=new S7control(EPlcType.S1500, "192.168.30.14", 102, 0, 0);
-			System.out.println("S7:"+s7.readByte("DB34.0",  2));
 
-//			//创建所有设备通讯
-//			while(set.next()){
-//			   int id=(int)set.getLong(1);
-//			   String name=set.getString(2);
-//			   String ip=set.getString(3);
-//			   int port=set.getInt(4);
-//			   int flag=set.getInt(5);
-//			   int readSize=set.getInt(6);
-//			   int readOffset=set.getInt(7);
-//			   byte functionType=set.getByte(8);
-//			   int flags=set.getInt(9);
-//			   if(flags==1){
-//				   mmgr.createDevice(db,id,name,ip,port,readOffset,readSize,null,functionType);
-//			   }
+			//创建所有设备通讯
+			while(set.next()){
+			   int id=(int)set.getLong(1);
+			   String name=set.getString(2);
+			   String ip=set.getString(3);
+			   int port=set.getInt(4);
+			   int flag=set.getInt(5);
+			   int readSize=set.getInt(6);
+			   int readOffset=set.getInt(7);
+			   byte functionType=set.getByte(8);
+			   if(flag==1){
+				   mmgr.createDevice(db,id,name,ip,port,readOffset,readSize,null,functionType);
+//				   if(port==102){
+//					   if(id==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{
 //
-//			}
+//				   }
+
+			   }
+
+			}
 		}
 		catch(Exception e){
 			
 		}
 		finally{
 			if(sn!=null){
-
 				sn.close();
 			}
 		}
@@ -211,9 +220,13 @@
 			System.out.println("错误");
 		}
 	}
-	
+	@Override
+	public void start() {
+
+	}
+
 	public static void main(String[] args){
-		//Manager.appStart(new String[]{"jdbc:mysql://10.153.19.150/gmms?serverTimezone=GMT%2B8","root","beibo.123/" });
+		Manager.appStart(new String[]{"jdbc:mysql://localhost/gmms?serverTimezone=GMT%2B8","root","beibo.123/" });
 		while(true){
 			try {
 				Thread.sleep(100);
diff --git a/nglib/src/builder/S7control.java b/nglib/src/builder/S7control.java
new file mode 100644
index 0000000..12d15b5
--- /dev/null
+++ b/nglib/src/builder/S7control.java
@@ -0,0 +1,487 @@
+package builder;
+
+import com.github.xingshuangs.iot.protocol.s7.enums.EPlcType;
+import com.github.xingshuangs.iot.protocol.s7.service.MultiAddressWrite;
+import com.github.xingshuangs.iot.protocol.s7.service.S7PLC;
+
+import java.util.ArrayList;
+import java.util.List;
+
+public class S7control {
+
+    S7PLC s7PLC; // PLC閫氳绫诲疄渚�
+
+    public S7control(EPlcType plcType, String ip, int port, int rack, int slot) {
+        if (s7PLC == null) {
+            s7PLC = new S7PLC(plcType, ip, port, 0, 0);
+        }
+    }
+
+    /**
+     * 鍏抽棴瑗块棬瀛恠7閫氳杩炴帴
+     */
+    public void closeS7client() {
+        if (s7PLC != null) {
+            s7PLC.close();
+        }
+    }
+
+    /**
+     * 閲嶅惎瑗块棬瀛恠7閫氳杩炴帴
+     */
+    public boolean reStartS7client() {
+        if (s7PLC != null) {
+            try {
+                s7PLC.hotRestart();
+                return true;
+            } catch (Exception ex) {
+                return false;
+            }
+        }
+        return false;
+    }
+
+
+    /**
+     * s7閫氳杩炴帴鐘舵��
+     */
+    public boolean checkConnected() {
+        return s7PLC.checkConnected();
+    }
+
+    /**
+     * 鎸夋寚瀹氱殑鍦板潃 鍐欏叆涓�涓獁ord
+     *
+     * @param address 鍦板潃
+     * @param data    word鐨勫��
+     */
+    public boolean writeWord(String address, int data) {
+        if (s7PLC == null) {
+            return false;
+        }
+        boolean result = false;
+        int tryCount = 2;
+        do {
+            try {
+                s7PLC.writeUInt16(address, data);
+                result = true;
+            } catch (Exception ex) {
+                System.out.println("鍚憄lc鍐欏懡浠よ繃绋嬩腑鍙戠敓寮傚父锛屽師鍥犱负锛�" + ex.getMessage());
+                reStartS7client();
+            } finally {
+                tryCount -= 1;
+            }
+        }
+        while (!result && tryCount > 0);
+        return result;
+    }
+
+    /**
+     * 浠庢煇鍦板潃杩炵画 鍐欏叆澶氫釜word
+     *
+     * @param address 鍦板潃
+     * @param datas   word鐨勫��
+     */
+    public boolean writeWord(String address, List<Integer> datas) {
+        if (s7PLC == null) {
+            return false;
+        }
+        boolean result = false;
+        int tryCount = 2;
+        // s7PLC.write(address, data);
+        List<String> addresslist = getAddressList(address, datas.size(), 16);
+        MultiAddressWrite addressWrite = new MultiAddressWrite();
+        for (int i = 0; i < datas.size(); i++) {
+            addressWrite.addUInt16(addresslist.get(i), datas.get(i));
+        }
+        do {
+            try {
+                s7PLC.writeMultiData(addressWrite);
+                result = true;
+            } catch (Exception ex) {
+                reStartS7client();
+            } finally {
+                tryCount -= 1;
+            }
+        }
+        while (!result && tryCount > 0);
+        return result;
+
+    }
+
+    /**
+     * 鎸夋寚瀹氱殑鍦板潃 鍐欏叆澶氫釜word
+     *
+     * @param address 鍦板潃
+     * @param datas   word鐨勫��
+     */
+
+
+    /**
+     * 鎸夋寚瀹氱殑鍦板潃 鍐欏叆涓�涓狟it
+     *
+     * @param address 鍦板潃
+     * @param data    Bit鐨勫��
+     */
+    public boolean writeBit(String address, Boolean data) {
+        if (s7PLC == null) {
+            return false;
+        }
+        boolean result = false;
+        int tryCount = 2;
+        do {
+            try {
+                s7PLC.writeBoolean(address, data);
+                result = true;
+            } catch (Exception ex) {
+                reStartS7client();
+            } finally {
+                tryCount -= 1;
+            }
+        }
+        while (!result && tryCount > 0);
+        return result;
+    }
+
+    /**
+     * 鎸夋寚瀹氱殑鍦板潃 鍐欏叆澶氫釜bit
+     *
+     * @param address 鍦板潃
+     * @param datas   bit鐨勫��
+     */
+    public boolean writeBit(List<String> address, List<Boolean> datas) {
+        if (s7PLC == null) {
+            return false;
+        }
+
+        MultiAddressWrite addressWrite = new MultiAddressWrite();
+        for (int i = 0; i < address.size(); i++) {
+            addressWrite.addBoolean(address.get(i), datas.get(i));
+        }
+        boolean result = false;
+        int tryCount = 2;
+        do {
+            try {
+                s7PLC.writeMultiData(addressWrite);
+                result = true;
+            } catch (Exception ex) {
+                reStartS7client();
+            } finally {
+                tryCount -= 1;
+            }
+        }
+        while (!result && tryCount > 0);
+        return result;
+
+    }
+
+    /**
+     * 浠庢煇鍦板潃杩炵画 鍐欏叆澶氫釜bit
+     *
+     * @param address 鍦板潃
+     * @param datas   word鐨勫��
+     */
+    public boolean writeBit(String address, List<Boolean> datas) {
+        if (s7PLC == null) {
+            return false;
+        }
+
+        // s7PLC.write(address, data);
+        List<String> addresslist = getAddressList(address, datas.size(), 1);
+        MultiAddressWrite addressWrite = new MultiAddressWrite();
+        for (int i = 0; i < datas.size(); i++) {
+            addressWrite.addBoolean(addresslist.get(i), datas.get(i));
+        }
+        boolean result = false;
+        int tryCount = 2;
+        do {
+            try {
+                s7PLC.writeMultiData(addressWrite);
+                result = true;
+            } catch (Exception ex) {
+                reStartS7client();
+            } finally {
+                tryCount -= 1;
+            }
+        }
+        while (!result && tryCount > 0);
+        return result;
+    }
+
+    /**
+     * 鎸夋寚瀹氱殑鍦板潃 鍐欏叆澶氫釜byte
+     *
+     * @param address 鍦板潃
+     * @param datas   byte鐨勫��
+     */
+    public boolean writeByte(String address, byte[] datas) {
+        if (s7PLC == null) {
+            return false;
+        }
+        boolean result = false;
+        int tryCount = 2;
+        do {
+            try {
+                s7PLC.writeByte(address, datas);
+                result = true;
+            } catch (Exception ex) {
+                reStartS7client();
+            } finally {
+                tryCount -= 1;
+            }
+        }
+        while (!result && tryCount > 0);
+        return result;
+    }
+
+    /**
+     * 鎸夋寚瀹氱殑鍦板潃 璇诲彇word缁撴灉闆�
+     *
+     * @param address 鍦板潃
+     * @return 缁撴灉
+     */
+    public List<Integer> readWord(List<String> address) {
+        if (s7PLC == null) {
+            return null;
+        }
+        List<Integer> result = null;
+        try {
+            result = s7PLC.readUInt16(address);
+        } catch (Exception e) {
+            s7PLC.hotRestart();
+            System.out.println("璇诲彇 " + address + " 澶辫触锛�" + e.getMessage());
+
+        } finally {
+            return result;
+        }
+    }
+
+
+    /**
+     * 鎸夋寚瀹氱殑鍦板潃 璇诲彇word缁撴灉闆�
+     *
+     * @param address 鍦板潃
+     * @param count   杩炵画璇诲灏戜釜word
+     * @return 缁撴灉
+     */
+    public List<Integer> readWord(String address, int count) {
+        if (s7PLC == null) {
+            return null;
+        }
+        List<Integer> result = null;
+        List<String> addresslist = getAddressList(address, count, 16);
+        try {
+            result = s7PLC.readUInt16(addresslist);
+        } catch (Exception e) {
+            s7PLC.hotRestart();
+            System.out.println("璇诲彇 " + address + " 澶辫触锛�" + e.getMessage());
+        } finally {
+            return result;
+        }
+    }
+
+    /**
+     * 鎸夋寚瀹氱殑鍦板潃 璇诲彇byte缁撴灉闆�
+     *
+     * @param address 鍦板潃
+     * @param count   杩炵画璇诲灏戜釜byte
+     * @return 缁撴灉
+     */
+    public byte[] readByte(String address, int count) {
+        if (s7PLC == null) {
+            return null;
+        }
+        // List<String> addresslist = GetAddressList(address, count, 16);
+        byte[] bytes = null;
+        try {
+            bytes = s7PLC.readByte(address, count);
+        } catch (Exception e) {
+            // 澶勭悊寮傚父
+            s7PLC.hotRestart();
+            System.out.println("璇诲彇 " + address + " 澶辫触锛�" + e.getMessage());
+        } finally {
+            return bytes;
+        }
+
+    }
+
+    /**
+     * 鎸夋寚瀹氱殑鍦板潃 鎸塨it浣� 0 flase 1 true 璇诲彇缁撴灉
+     *
+     * @param addresslist 鍦板潃闆�
+     * @return Boolean缁撴灉
+     */
+    public List<Boolean> readBits(List<String> addresslist) {
+        if (s7PLC == null) {
+            return null;
+        }
+        List<Boolean> values = new ArrayList<>();
+        try {
+            values = s7PLC.readBoolean(addresslist);
+        } catch (Exception e) {
+            // 澶勭悊寮傚父
+            s7PLC.hotRestart();
+        } finally {
+            return values;
+        }
+    }
+
+   /* //璇诲彇涓嶈繛缁湴鍧�bit
+    public List<Boolean> readBits(List<String> addressList) {
+        if (s7PLC == null || addressList.isEmpty()) {
+            return null;
+        }
+
+        List<Boolean> values = new ArrayList<>();
+        for (String address : addressList) {
+            try {
+                boolean value = s7PLC.readBoolean(address);
+                values.add(value);
+            } catch (Exception e) {
+                // 澶勭悊寮傚父
+                System.out.println("璇诲彇 " + address + " 澶辫触锛�" + e.getMessage());
+            }
+        }
+
+        return values;
+    }*/
+
+
+    //璇诲彇瀛楃涓�
+    public String readString(String address) {
+        if (s7PLC == null) {
+            return null;
+        }
+        String result = null;
+        try {
+            result = s7PLC.readString(address);
+        } catch (Exception e) {
+            s7PLC.hotRestart();
+            System.out.println("璇诲彇 " + address + " 澶辫触锛�" + e.getMessage());
+        } finally {
+            return result;
+        }
+    }
+
+
+    //璇诲彇鏃堕棿
+    public Long readtime(String address) {
+        if (s7PLC == null) {
+            return null;
+        }
+        Long result = null;
+        try {
+            result = s7PLC.readTime(address);
+        } catch (Exception e) {
+            s7PLC.hotRestart();
+            System.out.println("璇诲彇 " + address + " 澶辫触锛�" + e.getMessage());
+        } finally {
+            return result;
+        }
+    }
+
+
+    public boolean writetime(String address, long datas) {
+        if (s7PLC == null) {
+            return false;
+        }
+        boolean result = false;
+        int tryCount = 2;
+        do {
+            try {
+                s7PLC.writeTime(address, datas); // 灏嗘暟鎹啓鍏ュ崟涓湴鍧�
+                result = true;
+            } catch (Exception ex) {
+                reStartS7client();
+            } finally {
+                tryCount -= 1;
+            }
+        }
+        while (!result && tryCount > 0);
+        return result;
+    }
+
+
+    /**
+     * 浠庢寚瀹氱殑鍦板潃寮�濮� 杩炵画鎸塨it浣嶈鍙�
+     *
+     * @param address 鍦板潃
+     * @param count   闀垮害
+     * @return Boolean缁撴灉
+     */
+    public List<Boolean> readBits(String address, int count) {
+        if (s7PLC == null) {
+            return null;
+        }
+        List<Boolean> values = new ArrayList<>();
+        List<String> addresslist = getAddressList(address, count, 1);
+        try {
+            values = s7PLC.readBoolean(addresslist);
+        } catch (Exception e) {
+            s7PLC.hotRestart();
+            System.out.println("璇诲彇 " + address + " 澶辫触锛�" + e.getMessage());
+        } finally {
+            return values;
+        }
+    }
+
+    ;
+
+
+    private List<String> getAddressList(String address, int count, int addedbit) {
+        List<String> addresslist = new ArrayList<String>();
+
+        String[] stringdatas = address.trim().split("\\.");
+        if (stringdatas.length < 2 || !address.startsWith("DB")) {
+            return null;
+        }
+        int dbwindex = 0;
+        int bitindex = 0;
+        if (stringdatas.length == 2) {
+            dbwindex = Integer.parseInt(stringdatas[1]);
+        } else if (stringdatas.length == 3) {
+            dbwindex = Integer.parseInt(stringdatas[1]);
+            bitindex = Integer.parseInt(stringdatas[2]);
+        } else {
+            return null;
+        }
+
+        addresslist.add(address);
+        for (int i = 0; i < count - 1; i++) {
+
+            int bitcurrent = bitindex + addedbit;
+            if (bitcurrent > 7) {
+                dbwindex += bitcurrent / 8;
+                bitindex = 0;
+            } else {
+                bitindex = bitcurrent;
+            }
+
+            String endstr = stringdatas.length == 3 ? "." + bitindex : "";
+            addresslist.add(stringdatas[0] + "." + dbwindex + endstr);
+        }
+        return addresslist;
+    }
+
+    public void writeString(String addr,String data) {
+        s7PLC.writeString(addr,data);
+    }
+
+    public void writeUInt16(String addr,int data) {
+        s7PLC.writeUInt16(addr,data);
+    }
+
+    public void writeUInt32(String addr,int data) {
+        s7PLC.writeUInt32(addr,data);
+    }
+
+    public void writeStringy(String addr,String value) {
+        byte[] bytes = value.getBytes();
+        s7PLC.writeByte(addr,bytes);
+    }
+
+    public String readStrings(String addr) {
+        return s7PLC.readString(addr);
+    }
+
+}
diff --git a/nglib/src/builder/ThreadHttpServer.java b/nglib/src/builder/ThreadHttpServer.java
index 5a3e1f1..dad79d5 100644
--- a/nglib/src/builder/ThreadHttpServer.java
+++ b/nglib/src/builder/ThreadHttpServer.java
@@ -5,8 +5,6 @@
 import java.util.concurrent.ExecutorService;
 import java.util.concurrent.Executors;
 
-import S7.S7control;
-import com.github.xingshuangs.iot.protocol.s7.enums.EPlcType;
 import com.sun.net.httpserver.*;
 
 public class ThreadHttpServer  {
@@ -16,9 +14,6 @@
 	private static final String Httpcontext="/demo";
 	private static final int nThreads=8;
 	public static void mains() {
-		System.out.println("S7链接");
-		S7control s7=new S7control(EPlcType.S1500, "192.168.30.14", 102, 0, 0);
-		System.out.println("S7:"+s7.readByte("DB34.0",  2));
 		HttpServer httpServer;
 		try {
 			httpServer=HttpServer.create(new InetSocketAddress(port),0);
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{
diff --git a/nglib/src/ng/devices/MysqlService.java b/nglib/src/ng/devices/MysqlService.java
index 7785c18..f064719 100644
--- a/nglib/src/ng/devices/MysqlService.java
+++ b/nglib/src/ng/devices/MysqlService.java
@@ -4,6 +4,8 @@
 import java.sql.Connection;
 
 import builder.Manager;
+import builder.S7control;
+import com.github.xingshuangs.iot.protocol.s7.enums.EPlcType;
 import ng.db.DBHelper;
 import ng.db.DBSession;
 import ng.devices.ModbusService.ModbusDataPackage;
@@ -110,21 +112,22 @@
 	    DBSession sn=null;
 	    String result=null;
 	    String flag=null;
-	    
+	    System.out.println(lastGet);
 	    try{
 	    //创建连接
 	    	sn=this.db.createSession(false);
 	    	Connection con= sn.getConnection();
-	    	//掉哦那个存储过程
-	    	 CallableStatement sql=con.prepareCall("{call Total_method(?,?,?,?)}");
-	    	 sql.registerOutParameter(3, java.sql.Types.VARCHAR);
-	      	 sql.registerOutParameter(4, java.sql.Types.VARCHAR);
-	      	 sql.setString(1, s);
-	      	 sql.setLong(2, id);
-	      	 sql.execute();
-	      	 //读取返回参数
-	      	 result= sql.getString(3);
-	      	flag= sql.getString(4);
+	    	//调用那个存储过程
+			CallableStatement sql=con.prepareCall("{call Total_method(?,?,?,?)}");
+			sql.registerOutParameter(3, java.sql.Types.VARCHAR);
+			sql.registerOutParameter(4, java.sql.Types.VARCHAR);
+			sql.setString(1, s);
+			sql.setLong(2, id);
+			sql.execute();
+			//读取返回参数
+			result= sql.getString(3);
+			flag= sql.getString(4);
+
 	    }
 	    catch(Exception e){
 	    	e.printStackTrace();

--
Gitblit v1.8.0