whyclj
2020-08-11 3b4031f3a802401d2f6cc6ecbd1e5e0ff3784782
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
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);
        ModbusMaster master = modbusFactory.createTcpMaster(params, false);// TCP Ð­Òé
        try {
            master.setTimeout(2000);
            master.init();
        } catch (ModbusInitException e) {
            e.printStackTrace();
        }
        return master;
    }
}