严智鑫
2025-04-11 c81475564cc5c29bdda546e51c8dd38b555889f9
nglib/src/ng/devices/ModbusService.java
@@ -1,13 +1,19 @@
package ng.devices;
import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;
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;
public abstract class ModbusService implements Runnable  {
   //modbus客户端
   protected ModbusClient client;
@@ -26,6 +32,7 @@
   protected  String ip;
   //设备端口
   protected int port;
   protected String remarks;
   //线程
   java.lang.Thread thread;
   //缓冲区(服务允许发送超过协议要求的数据,所以缓冲区更大)
@@ -41,7 +48,7 @@
    }
    //运行
    public void Run(int MachineID,String ip,int port,byte state,int ReadOffset,int ReadSize,int timeout,byte FunctionType){
    public void Run(int MachineID,String ip,int port,byte state,int ReadOffset,int ReadSize,int timeout,byte FunctionType,String remarks){
       //设置参数
       this.machineID=MachineID;
       this.ip=ip;
@@ -49,12 +56,20 @@
       this.readOffset=ReadOffset;
       this.readSize=ReadSize;
      this.functionType= FunctionType;
       this.client=new ModbusClient();
       //启动线程
       this.client.setConnectionParam(MachineID,ip,port,(byte)state,timeout);
      this.remarks= remarks;
       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();
      }
    }
    
    
@@ -89,6 +104,94 @@
       }
       return msg;
    }
   //一次s7读取行为
   String onceS7(){
      try {
         DBSession sn=null;
         String result=null;
         String flag=null;
         String messsage="000000000000000000";
         EPlcType ePlcType=null;
         if(this.machineID==4){
            ePlcType=EPlcType.S1500;
         }else if(this.machineID==6){
            ePlcType=EPlcType.S200_SMART;
         }else{
            ePlcType=EPlcType.S1200;
         }
         S7control s7=new S7control(ePlcType, ip, port, 0, 0);
         //List<Integer> word=s7.readWord("DB34.122",  1);
         List<Integer> listWord=s7.readWord(this.remarks,(this.readSize/2));
         if(listWord==null){
            System.out.println("notread");
            return "";
         }
            for(int i=0;i<listWord.size();i++){
            //System.out.println("i  "+listWord.size()+"   "+messsage);
            messsage+=HexUtil.intTo2ByteHex(listWord.get(i));
         }
         //System.out.println(messsage);
         //System.out.println(ip+"  "+s7.readWord("DB34.122",  1));
         //数据库
         DBHelper db=DBHelper.getDBHelper("mes");
         try{
            //创建连接
            sn=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, messsage);
            sql.setLong(2, machineID);
            sql.execute();
            //读取返回参数
            result= sql.getString(3);
            flag= sql.getString(4);
            if (result!=null&&result.length()>0){
               String []results=result.split(",");
               //得到存储过程返回的值,数组第一个为地址,第二个为值
               int resultsLength=results.length;
               int resultsSize=resultsLength/2;
               for(int i=1;i<resultsSize+1;i++){
                  int addressIndex=(i-1)*2;
                  int valueIndex=i*2-1;
                  s7.writeWord(results[addressIndex],Integer.valueOf(results[valueIndex]));//参数组写入
               }
//               if(results.length==2){
//                  s7.writeWord(results[0],Integer.valueOf(results[1]) );
//               }
//               if(results.length==4){
//                  s7.writeWord(results[0],Integer.valueOf(results[1]));//配方
//                  s7.writeWord(results[2],Integer.valueOf(results[3]));//速度
//               }
               List<Integer> listWord2=s7.readWord(this.remarks,(this.readSize/2));
               System.out.println(listWord2.size());
            }
         }
         catch(Exception e){
            e.printStackTrace();
         }
         finally{
            sn.close();
         }
      } catch (Exception e) {
         // TODO Auto-generated catch block
         e.printStackTrace();
      }
      finally{
      }
      return "";
   }
   public void SendPLC(int Address, int count, byte[] buffer, int offset) {
      this.client.write(Address, count, buffer, offset);
@@ -110,10 +213,15 @@
   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){
                  String back=onceS7();
               }else{
                  String back=once();
               }
            } catch (InterruptedException e) {
               // TODO Auto-generated catch block
            }
@@ -141,6 +249,7 @@
   @Override
   public void run() {
      // TODO Auto-generated method stub
      proc();
   }
   public class ModbusDataPackage{