| | |
| | | package com.modbus.data;
|
| | |
|
| | | import java.text.NumberFormat;
|
| | |
|
| | | import com.base.ComBase;
|
| | | import com.serotonin.modbus4j.ModbusMaster;
|
| | | import com.serotonin.modbus4j.ip.IpParameters;
|
| | |
|
| | |
| | | public final static int SLAVEID_DEFAULT = 2;
|
| | |
|
| | | public final static int MAX_ERROR_COUNT = 2; //最大连续错误计数
|
| | | public final static int LOSE_CONNECT_COUNT = 20; //通讯中断最大计数
|
| | | |
| | | private ModbusMaster master;
|
| | | private int errcount; //错误计数 错误连续超过三个时重新获取master
|
| | | private int totalerr = 31; //总的错误计数[默认失去连接] 用于判别设备是否掉线
|
| | | private String target_ip; //目标ip
|
| | | private int slaveId; //设备ID
|
| | |
|
| | |
| | | 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() {
|
| | | //System.out.println(target_ip);
|
| | | IpParameters params = new IpParameters();
|
| | | params.setHost(target_ip);
|
| | | if(this.master != null) {
|
| | |
| | |
|
| | | }
|
| | |
|
| | | public int getConnectState() {
|
| | | if(this.totalerr < LOSE_CONNECT_COUNT) {
|
| | | return 1;
|
| | | }
|
| | | return 0;
|
| | | }
|
| | | |
| | | public int getTotalerr() {
|
| | | return totalerr;
|
| | | }
|
| | |
|
| | | public void setTotalerr(int totalerr) {
|
| | | this.totalerr = totalerr;
|
| | | }
|
| | |
|
| | | public ModbusMaster getMaster() {
|
| | | return master;
|
| | | }
|