严智鑫
2025-04-07 6b8cb1b9d271260b0d7d19334aa9e9ac24d4ae3c
nglib/src/ng/devices/ModbusService.java
@@ -1,6 +1,9 @@
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;
@@ -10,6 +13,7 @@
public abstract class ModbusService implements Runnable  {
   //modbus客户端
   protected ModbusClient client;
@@ -28,6 +32,7 @@
   protected  String ip;
   //设备端口
   protected int port;
   protected String remarks;
   //线程
   java.lang.Thread thread;
   //缓冲区(服务允许发送超过协议要求的数据,所以缓冲区更大)
@@ -43,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;
@@ -51,6 +56,7 @@
       this.readOffset=ReadOffset;
       this.readSize=ReadSize;
      this.functionType= FunctionType;
      this.remarks= remarks;
       //启动线程
      if(this.port==102){
@@ -98,6 +104,92 @@
       }
       return msg;
    }
   //一次s7读取行为
   String onceS7(){
      try {
         DBSession sn=null;
         String result=null;
         String flag=null;
         String messsage="000000000000000000";
         EPlcType ePlcType=null;
         if(this.machineID==78){
            ePlcType=EPlcType.S1500;
         }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);
@@ -123,15 +215,7 @@
            try {
               java.lang.Thread.sleep(UpadateInterval);
               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));
                  }
                  String back=onceS7();
               }else{
                  String back=once();
               }