whyclj
2020-10-14 455401679935a41e16f8f71a8b62dd9af47b934a
Motor_MonitorServer/src/com/modbus/data/MyModbusFactory.java
@@ -1,42 +1,42 @@
package com.modbus.data;
import com.serotonin.modbus4j.ModbusFactory;
import com.serotonin.modbus4j.ModbusMaster;
import com.serotonin.modbus4j.exception.ModbusInitException;
import com.serotonin.modbus4j.ip.IpParameters;
public class MyModbusFactory {
   private static final int SERVER_PORT = 502;            //服务端口
   static ModbusFactory modbusFactory;
   static {
      if (modbusFactory == null) {
         modbusFactory = new ModbusFactory();
      }
   }
   /**
    * 获取master
    *
    * @return
    * @throws ModbusInitException
    */
   public static ModbusMaster getMaster(IpParameters params){
      // modbusFactory.createRtuMaster(wapper);       //RTU 协议
      // modbusFactory.createUdpMaster(params);      //UDP 协议
      // modbusFactory.createAsciiMaster(wrapper);   //ASCII 协议
      params.setPort(SERVER_PORT);
      //true:长连接           false:短连接
      ModbusMaster master = modbusFactory.createTcpMaster(params, true);// TCP 协议
      try {
         master.setTimeout(500);
         //设置重连次数
         master.setRetries(3);
         master.init();
      } catch (ModbusInitException e) {
         e.printStackTrace();
      }
      return master;
   }
}
package com.modbus.data;
import com.serotonin.modbus4j.ModbusFactory;
import com.serotonin.modbus4j.ModbusMaster;
import com.serotonin.modbus4j.exception.ModbusInitException;
import com.serotonin.modbus4j.ip.IpParameters;
public class MyModbusFactory {
   private static final int SERVER_PORT = 502;            //服务端口
   static ModbusFactory modbusFactory;
   static {
      if (modbusFactory == null) {
         modbusFactory = new ModbusFactory();
      }
   }
   /**
    * 获取master
    *
    * @return
    * @throws ModbusInitException
    */
   public static ModbusMaster getMaster(IpParameters params){
      // modbusFactory.createRtuMaster(wapper);       //RTU 协议
      // modbusFactory.createUdpMaster(params);      //UDP 协议
      // modbusFactory.createAsciiMaster(wrapper);   //ASCII 协议
      params.setPort(SERVER_PORT);
      //true:长连接           false:短连接
      ModbusMaster master = modbusFactory.createTcpMaster(params, true);// TCP 协议
      try {
         master.setTimeout(2000);
         //设置重连次数
         master.setRetries(3);
         master.init();
      } catch (ModbusInitException e) {
         //e.printStackTrace();
      }
      return master;
   }
}