whyclxw
2021-05-13 366b7c2daeac609853befcc05ddbac2058425c52
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
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;                //·þÎñ¶Ë¿Ú
    
    public static final int ModBus_TCP         = 1;            //ModBus-TcpЭÒé
    public static final int ModBus_RTU         = 2;            //ModBus-RtuЭÒé
    public static final int ModBus_UDP         = 3;            //ModBus-UdpЭÒé
    public static final int ModBus_ASCII     = 4;            //ModBus-AsciiЭÒé
    
    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);
        //false:¶ÌÁ¬½Ó      true:³¤Á¬½Ó
        ModbusMaster master = modbusFactory.createTcpMaster(params, true);// TCP Ð­Òé
        try {
             //ÉèÖó¬Ê±Ê±¼ä
            master.setTimeout(2000);
            //ÉèÖÃÖØÁ¬´ÎÊý
            master.setRetries(3);
            //³õʼ»¯
            master.init();
        } catch (ModbusInitException e) {
            //e.printStackTrace();
        }
        return master;
    }
    /**
     * »ñÈ¡master
     * 
     * @return
     * @throws ModbusInitException
     */
    public static ModbusMaster getMaster(MyIpParameters params,int Modbus_Type){
        // modbusFactory.createRtuMaster(wapper);         //RTU Ð­Òé
        // modbusFactory.createUdpMaster(params);        //UDP Ð­Òé
        // modbusFactory.createAsciiMaster(wrapper);    //ASCII Ð­Òé
        ModbusMaster master = null;
        switch (Modbus_Type) {
        case ModBus_TCP:{
            params.getParam().setPort(SERVER_PORT);
            master = modbusFactory.createTcpMaster(params.getParam(), true);// TCP Ð­Òé
        }break;
        case ModBus_RTU:{
            //master = modbusFactory.createRtuMaster(params.getWrapper());
            master = modbusFactory.createRtuMaster(params.getWrapper());
        }break;
        case ModBus_UDP:{
            master = modbusFactory.createUdpMaster(params.getParam());
        }break;
        case ModBus_ASCII:{
            //modbusFactory.createAsciiMaster(wrapper);
        }
        default:
            break;
        }
        try {
            master.setTimeout(2000);
            master.init();
        } catch (ModbusInitException e) {
            //e.printStackTrace();
        }
        return master;
    }
}