严智鑫
2025-03-26 47f9d4a231a116f528f9d18d738420dba7f753bd
成功使用ModbusTcp,S7连接设备并且存储到数据库
9个文件已修改
4个文件已添加
878 ■■■■■ 已修改文件
.idea/vcs.xml 6 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
.idea/workspace.xml 77 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
gmms/WebContent/login/run.jsp 4 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
nglib/.idea/encodings.xml 4 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
nglib/.idea/uiDesigner.xml 124 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
nglib/nglib.iml 43 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
nglib/src/builder/HttpHandlerDemo.java 1 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
nglib/src/builder/MachineManager.java 4 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
nglib/src/builder/Manager.java 61 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
nglib/src/builder/S7control.java 487 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
nglib/src/builder/ThreadHttpServer.java 5 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
nglib/src/ng/devices/ModbusService.java 37 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
nglib/src/ng/devices/MysqlService.java 25 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
.idea/vcs.xml
New file
@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
  <component name="VcsDirectoryMappings">
    <mapping directory="" vcs="Git" />
  </component>
</project>
.idea/workspace.xml
New file
@@ -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>
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");
%>
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" />
nglib/.idea/uiDesigner.xml
New file
@@ -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>
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>
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(?,?,?,?)}");
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));
            }
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);
nglib/src/builder/S7control.java
New file
@@ -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);
        }
    }
    /**
     * 关闭西门子s7通讯连接
     */
    public void closeS7client() {
        if (s7PLC != null) {
            s7PLC.close();
        }
    }
    /**
     * 重启西门子s7通讯连接
     */
    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();
    }
    /**
     * 按指定的地址 写入一个word
     *
     * @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("向plc写命令过程中发生异常,原因为:" + 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的值
     */
    /**
     * 按指定的地址 写入一个Bit
     *
     * @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;
        }
    }
    /**
     * 按指定的地址 按bit位 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;
    }
    /**
     * 从指定的地址开始 连续按bit位读取
     *
     * @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);
    }
}
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);
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{
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();