whyclxw
2020-09-10 e1f3896f2434f829d284161552758e392cb21835
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
package com.modbus.data;
 
import java.text.NumberFormat;
 
import com.base.ComBase;
import com.serotonin.modbus4j.ModbusMaster;
import com.serotonin.modbus4j.ip.IpParameters;
 
public class MyModbusMaster {
    public final static int SLAVEID_DEFAULT = 2;
    
    public final static int MAX_ERROR_COUNT = 2;        //×î´óÁ¬Ðø´íÎó¼ÆÊý
    private ModbusMaster master;
    private int errcount;                                //´íÎó¼ÆÊý    ´íÎóÁ¬Ðø³¬¹ýÈý¸öÊ±ÖØÐ»ñÈ¡master
    private int totalerr;                                //×ܵĴíÎó¼ÆÊý            //ÓÃÓÚÅбðÉ豸ÊÇ·ñµôÏß
    private String target_ip;                            //Ä¿±êip
    private int slaveId;                                //É豸ID
    
    public MyModbusMaster(String target_ip,int slaveId) {
        this.target_ip = target_ip;
        this.slaveId = slaveId;
        IpParameters params = new IpParameters();
        params.setHost(target_ip);
        this.master = MyModbusFactory.getMaster(params);
    }
    
    public void addErrorCount() {
        this.errcount++;
        if(this.errcount > MAX_ERROR_COUNT) {
            reConnect();
        }
        if(this.totalerr > 99999999) {
            this.totalerr = 5;
        }
        this.totalerr ++;
    }
    
    //Çå¿Õ´íÎó¼ÆÊý
    public void clearError() {
        errcount = 0;
        totalerr = 0;
    }
    
    public void reConnect() {
        IpParameters params = new IpParameters();
        params.setHost(target_ip);
        if(this.master != null) {
            //Çå¿ÕÉÏÒ»¸ömaster
            this.master.destroy();
        }
        this.errcount = 0;
        this.master = MyModbusFactory.getMaster(params);
    }
    
    /**
     *     ÅжϻñÈ¡µ½µÄÖµÊÇ·ñΪ¿Õ
     * @param target
     * @param source
     * @return
     */
    public static Object checkNullData(Object target,Object source) {
        if(null == target) {
            return source;
        }
        try {
            if(target instanceof Number) {
                if(source instanceof Integer) {
                    return ((Number) target).intValue();
                }else if(source instanceof Float) {
                    return ((Number) target).floatValue();
                }
            }
            if(target instanceof Boolean) {
                //ÅжÏBooleanÀàÐÍÊý¾Ý
                if((Boolean)target) {
                    target = 1;
                }else {
                    target = 0;
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return target;
        
    }
    
    public int getTotalerr() {
        return totalerr;
    }
 
    public void setTotalerr(int totalerr) {
        this.totalerr = totalerr;
    }
 
    public ModbusMaster getMaster() {
        return master;
    }
    public int getErrcount() {
        return errcount;
    }
    public void setMaster(ModbusMaster master) {
        this.master = master;
    }
    public void setErrcount(int errcount) {
        this.errcount = errcount;
    }
 
    public String getTarget_ip() {
        return target_ip;
    }
 
    public void setTarget_ip(String target_ip) {
        this.target_ip = target_ip;
    }
 
    public int getSlaveId() {
        return slaveId;
    }
 
    public void setSlaveId(int slaveId) {
        this.slaveId = slaveId;
    }
 
    @Override
    public String toString() {
        return "MyModbusMaster [SLAVEID_DEFAULT=" + SLAVEID_DEFAULT + ", master=" + master + ", errcount=" + errcount
                + ", target_ip=" + target_ip + ", slaveId=" + slaveId + "]";
    }
}