浏览代码

通讯库修改

master
pry 1年前
父节点
当前提交
9840670853
共有 4 个文件被更改,包括 487 次插入316 次删除
  1. +44
    -40
      app/src/main/java/com/example/bpa/app/DeviceData.java
  2. +313
    -197
      app/src/main/java/com/example/bpa/app/ModbusTcpServer.java
  3. +29
    -28
      app/src/main/java/com/example/bpa/helper/ThreadManager.java
  4. +101
    -51
      app/src/main/java/com/example/bpa/view/from/lcsz_activity.java

+ 44
- 40
app/src/main/java/com/example/bpa/app/DeviceData.java 查看文件

@@ -8,11 +8,17 @@ import com.example.bpa.Model.IWriteCallBack;
import com.example.bpa.helper.MessageLog;
import com.example.bpa.helper.RTrig;
import com.example.bpa.helper.ThreadManager;
import com.licheedev.modbus4android.ModbusCallback;
import com.licheedev.modbus4android.ModbusRespException;
import com.serotonin.modbus4j.exception.ModbusInitException;
import com.serotonin.modbus4j.exception.ModbusTransportException;
import com.serotonin.modbus4j.msg.ReadHoldingRegistersResponse;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentLinkedQueue;
import java.util.concurrent.ExecutionException;

public class DeviceData {
private static volatile DeviceData _instance;
@@ -59,61 +65,59 @@ public class DeviceData {
ThreadManager.Get().StartLong("Plc设备数据监听", true, new IThread() {
@Override
public void Run() throws InterruptedException {

//获取校准值
ModbusTcpServer.get().ReadShort("VW100", 10, new IReadCallBack<short[]>() {
@Override
public void onSuccess(short[] shorts) {
for (int i = 0; i < shorts.length; i++) {
CalibrationValue.put(i + 1, shorts[i]);
}
ModbusTcpServer.get().ReadShort("VW100", 10, (shorts) -> {
for (int i = 0; i < shorts.length; i++) {
CalibrationValue.put(i + 1, shorts[i]);
}
});

//获取称当前重量
ModbusTcpServer.get().ReadFloat("VD260", 9, new IReadCallBack<float[]>() {
@Override
public void onSuccess(float[] floats) {
for (int i = 0; i < floats.length; i++) {
CallCurrentWeight.put(i + 1, floats[i]);
}
}
});
// ModbusTcpServer.get().ReadInt("VD260", 9, new IReadCallBack<int[]>() {
// @Override
// public void onSuccess(int[] ints) {
// for (int i = 0; i < ints.length; i++) {
// CallCurrentWeight.put(i + 1, ints[i]);
// }
// }
// });

//获取校准基准时间
ModbusTcpServer.get().ReadFloat("VD124", 1, new IReadCallBack<float[]>() {
@Override
public void onSuccess(float[] floats) {
CalibrationReferenceTime = floats[0];
}
});
// ModbusTcpServer.get().ReadInt("VD124", 1, new IReadCallBack<int[]>() {
// @Override
// public void onSuccess(int[] ints) {
// CalibrationReferenceTime = ints[0];
// }
// });

//获取清洗参数
ModbusTcpServer.get().ReadShort("VW140", 4, new IReadCallBack<short[]>() {
@Override
public void onSuccess(short[] shorts) {
DrainageTime = shorts[0];
AddCleaningAgentTime = shorts[1];
InletTime = shorts[2];
CyclicCleaningTime = shorts[3];
}
});
// ModbusTcpServer.get().ReadShort("VW140", 4, new IReadCallBack<short[]>() {
// @Override
// public void onSuccess(short[] shorts) {
// DrainageTime = shorts[0];
// AddCleaningAgentTime = shorts[1];
// InletTime = shorts[2];
// CyclicCleaningTime = shorts[3];
// }
// });

//获取清洗参数
ModbusTcpServer.get().ReadBool("M6.5", 1, new IReadCallBack<boolean[]>() {
@Override
public void onSuccess(boolean[] booleans) {
WeightCalibrationMode = booleans[0];
}
});
// ModbusTcpServer.get().ReadBool("M6.5", 1, new IReadCallBack<boolean[]>() {
// @Override
// public void onSuccess(boolean[] booleans) {
// WeightCalibrationMode = booleans[0];
// }
// });

//配料完成 M0.3
CompleteListen("M0.3", "配料完成", OnChargeMixtureCompleteNotify);
// CompleteListen("M0.3", "配料完成", OnChargeMixtureCompleteNotify);

//清洗完成 M0.6
CompleteListen("M0.6", "清洗完成", OnCleaningCompleteNotify);
// CompleteListen("M0.6", "清洗完成", OnCleaningCompleteNotify);

//去皮完成 M1.3
CompleteListen("M1.3", "去皮完成", OnPeelingCompleteNotify);
// CompleteListen("M1.3", "去皮完成", OnPeelingCompleteNotify);

Thread.sleep(10);
}
@@ -449,7 +453,7 @@ public class DeviceData {
/**
* 称当前重量
*/
ConcurrentHashMap<Integer, Float> CallCurrentWeight = new ConcurrentHashMap<Integer, Float>();
ConcurrentHashMap<Integer, Integer> CallCurrentWeight = new ConcurrentHashMap<Integer, Integer>();

/**
* 获取指定称的当前重量
@@ -478,7 +482,7 @@ public class DeviceData {
/**
* 校准基准时间
*/
float CalibrationReferenceTime;
int CalibrationReferenceTime;

/**
* 获取校准基准时间


+ 313
- 197
app/src/main/java/com/example/bpa/app/ModbusTcpServer.java 查看文件

@@ -7,8 +7,11 @@ import com.example.bpa.config.DataBus;
import com.example.bpa.helper.MessageLog;
import com.licheedev.modbus4android.ModbusCallback;
import com.licheedev.modbus4android.ModbusParam;
import com.licheedev.modbus4android.ModbusRespException;
import com.licheedev.modbus4android.param.TcpParam;
import com.serotonin.modbus4j.ModbusMaster;
import com.serotonin.modbus4j.exception.ModbusInitException;
import com.serotonin.modbus4j.exception.ModbusTransportException;
import com.serotonin.modbus4j.msg.ReadCoilsResponse;
import com.serotonin.modbus4j.msg.ReadHoldingRegistersResponse;
import com.serotonin.modbus4j.msg.WriteCoilResponse;
@@ -16,6 +19,7 @@ import com.serotonin.modbus4j.msg.WriteRegistersResponse;

import java.nio.ByteBuffer;
import java.util.Arrays;
import java.util.concurrent.ExecutionException;


public class ModbusTcpServer {
@@ -117,7 +121,7 @@ public class ModbusTcpServer {
@Override
public void onSuccess(ModbusMaster modbusMaster) {
MessageLog.ShowInfo("设备 " + host + " 连接成功");
DataBus.getInstance().PlcIsConnect=true;
DataBus.getInstance().PlcIsConnect = true;
//1.数据中心
DeviceData.Get().Init();
//2.业务线程
@@ -126,7 +130,7 @@ public class ModbusTcpServer {

@Override
public void onFailure(Throwable tr) {
DataBus.getInstance().PlcIsConnect=false;
DataBus.getInstance().PlcIsConnect = false;
MessageLog.ShowError("设备 " + host + " 连接失败:" + tr.getMessage());
}

@@ -137,6 +141,91 @@ public class ModbusTcpServer {
});
}

private Float BytesToFloat(byte[] buffers, DataFormat df) {
if (buffers.length == 4) {
byte[] bytes = new byte[4];
if (df == DataFormat.ABCD) {
bytes[0] = buffers[3];
bytes[1] = buffers[2];
bytes[2] = buffers[1];
bytes[3] = buffers[0];
} else if (df == DataFormat.CDAB) {
bytes[0] = buffers[1];
bytes[1] = buffers[0];
bytes[2] = buffers[3];
bytes[3] = buffers[2];
} else if (df == DataFormat.BADC) {
bytes[0] = buffers[2];
bytes[1] = buffers[3];
bytes[2] = buffers[0];
bytes[3] = buffers[1];
} else if (df == DataFormat.DCBA) {
bytes[0] = buffers[0];
bytes[1] = buffers[1];
bytes[2] = buffers[2];
bytes[3] = buffers[3];
}
return ByteBuffer.wrap(bytes).getFloat();
}
return 0.0f;
}

private Integer BytesToInt(byte[] buffers, DataFormat df) {
if (buffers.length == 4) {
byte[] bytes = new byte[4];
if (df == DataFormat.ABCD) {
bytes[0] = buffers[3];
bytes[1] = buffers[2];
bytes[2] = buffers[1];
bytes[3] = buffers[0];
} else if (df == DataFormat.CDAB) {
bytes[0] = buffers[1];
bytes[1] = buffers[0];
bytes[2] = buffers[3];
bytes[3] = buffers[2];
} else if (df == DataFormat.BADC) {
bytes[0] = buffers[2];
bytes[1] = buffers[3];
bytes[2] = buffers[0];
bytes[3] = buffers[1];
} else if (df == DataFormat.DCBA) {
bytes[0] = buffers[0];
bytes[1] = buffers[1];
bytes[2] = buffers[2];
bytes[3] = buffers[3];
}
return ByteBuffer.wrap(bytes).getInt();
}
return 0;
}

private byte[] IntToByte(int number) {
int temp = number;
byte[] b = new byte[4];
for (int i = 0; i < b.length; i++) {
b[i] = new Integer(temp & 0xff).byteValue();// 将最低位保存在最低位
temp = temp >> 8; // 向右移8位
}
return b;
}

private short[] IntToShorts(int value) {
short[] res = new short[2];
int temp = value;
byte[] b = new byte[4];
for (int i = 0; i < b.length; i++) {
b[i] = new Integer(temp & 0xff).byteValue();// 将最低位保存在最低位
temp = temp >> 8; // 向右移8位
}
for (int i = 0; i < res.length; i++) {
short s0 = (short) (b[i * 2] & 0xff);// 最低位
short s1 = (short) (b[i * 2 + 1] & 0xff);
s1 <<= 8;
res[i] = (short) (s0 | s1);
}
return res;
}

/***
*读取整形数据
* @param Address the address
@@ -147,78 +236,102 @@ public class ModbusTcpServer {
public void ReadShort(String Address, int length, IReadCallBack<short[]> callback) {
int add = GetAddress(Address);
if (add < 0) return;
ModbusTcpHelper.get().readHoldingRegisters(1, add, length, new ModbusCallback<ReadHoldingRegistersResponse>() {
@Override
public void onSuccess(ReadHoldingRegistersResponse readHoldingRegistersResponse) {
short[] data = readHoldingRegistersResponse.getShortData();
if (data.length == length)
callback.onSuccess(data);
}

@Override
public void onFailure(Throwable tr) {
MessageLog.ShowError("ReadShort onFailure,Address=" + Address + ",length=" + length + ",msg:" + tr.toString());
}

@Override
public void onFinally() {
}
});
try {
ReadHoldingRegistersResponse res = ModbusTcpHelper.get().syncReadHoldingRegisters(1, add, length);
short[] data = res.getShortData();
if (data.length == length)
callback.onSuccess(data);
} catch (InterruptedException e) {
MessageLog.ShowError("ReadShort onFailure,Address=" + Address + ",length=" + length + ",msg:" + e.toString());
} catch (ExecutionException e) {
MessageLog.ShowError("ReadShort onFailure,Address=" + Address + ",length=" + length + ",msg:" + e.toString());
} catch (ModbusTransportException e) {
MessageLog.ShowError("ReadShort onFailure,Address=" + Address + ",length=" + length + ",msg:" + e.toString());
} catch (ModbusInitException e) {
MessageLog.ShowError("ReadShort onFailure,Address=" + Address + ",length=" + length + ",msg:" + e.toString());
} catch (ModbusRespException e) {
MessageLog.ShowError("ReadShort onFailure,Address=" + Address + ",length=" + length + ",msg:" + e.toString());
}
}

public void ReadBool(String Address, int length, IReadCallBack<boolean[]> callback) {
int add = GetAddress(Address);
if (add < 0) return;
ModbusTcpHelper.get().readCoil(1, add, length, new ModbusCallback<ReadCoilsResponse>() {
@Override
public void onSuccess(ReadCoilsResponse readCoilsResponse) {
boolean[] data = readCoilsResponse.getBooleanData();
boolean[] result = Arrays.copyOfRange(data, 0, length);
if (result.length == length)
callback.onSuccess(result);
}

@Override
public void onFailure(Throwable tr) {
MessageLog.ShowError("ReadBool onFailure,Address=" + Address + ",length=" + length + ",msg:" + tr.toString());
}

@Override
public void onFinally() {
}
});
try {
ReadCoilsResponse res = ModbusTcpHelper.get().syncReadCoil(1, add, length);
boolean[] data = res.getBooleanData();
boolean[] result = Arrays.copyOfRange(data, 0, length);
if (result.length == length)
callback.onSuccess(result);
} catch (InterruptedException e) {
MessageLog.ShowError("ReadBool onFailure,Address=" + Address + ",length=" + length + ",msg:" + e.toString());
} catch (ExecutionException e) {
MessageLog.ShowError("ReadBool onFailure,Address=" + Address + ",length=" + length + ",msg:" + e.toString());
} catch (ModbusTransportException e) {
MessageLog.ShowError("ReadBool onFailure,Address=" + Address + ",length=" + length + ",msg:" + e.toString());
} catch (ModbusInitException e) {
MessageLog.ShowError("ReadBool onFailure,Address=" + Address + ",length=" + length + ",msg:" + e.toString());
} catch (ModbusRespException e) {
MessageLog.ShowError("ReadBool onFailure,Address=" + Address + ",length=" + length + ",msg:" + e.toString());
}
}

public void ReadFloat(String Address, int length, IReadCallBack<float[]> callback) {
int add = GetAddress(Address);
if (add < 0) return;
ModbusTcpHelper.get().readHoldingRegisters(1, add, length * 2, new ModbusCallback<ReadHoldingRegistersResponse>() {
@Override
public void onSuccess(ReadHoldingRegistersResponse readHoldingRegistersResponse) {
byte[] data = readHoldingRegistersResponse.getData();
float[] tempValues = new float[length];
for (int i = 0; i < length; i++) {
byte[] tempData = new byte[4];
for (int m = 0; m < 4; m++) {
tempData[m] = data[i * 4 + m];
}
tempValues[i] = ByteBuffer.wrap(tempData).getFloat();
try {
ReadHoldingRegistersResponse res = ModbusTcpHelper.get().syncReadHoldingRegisters(1, add, length);
byte[] data = res.getData();
float[] tempValues = new float[length];
for (int i = 0; i < length; i++) {
byte[] tempData = new byte[4];
for (int m = 0; m < 4; m++) {
tempData[m] = data[i * 4 + m];
}
if (tempValues.length == length)
callback.onSuccess(tempValues);
}

@Override
public void onFailure(Throwable tr) {
MessageLog.ShowError("ReadFloat onFailure,Address=" + Address + ",length=" + length + ",msg:" + tr.toString());
}

@Override
public void onFinally() {

}
});
tempValues[i] = BytesToFloat(tempData, DataFormat.ABCD);
}
if (tempValues.length == length)
callback.onSuccess(tempValues);
} catch (InterruptedException e) {
MessageLog.ShowError("ReadFloat onFailure,Address=" + Address + ",length=" + length + ",msg:" + e.toString());
} catch (ExecutionException e) {
MessageLog.ShowError("ReadFloat onFailure,Address=" + Address + ",length=" + length + ",msg:" + e.toString());
} catch (ModbusTransportException e) {
MessageLog.ShowError("ReadFloat onFailure,Address=" + Address + ",length=" + length + ",msg:" + e.toString());
} catch (ModbusInitException e) {
MessageLog.ShowError("ReadFloat onFailure,Address=" + Address + ",length=" + length + ",msg:" + e.toString());
} catch (ModbusRespException e) {
MessageLog.ShowError("ReadFloat onFailure,Address=" + Address + ",length=" + length + ",msg:" + e.toString());
}
}

public void ReadInt(String Address, int length, IReadCallBack<int[]> callback) {
int add = GetAddress(Address);
if (add < 0) return;
try {
ReadHoldingRegistersResponse res = ModbusTcpHelper.get().syncReadHoldingRegisters(1, add, length);
byte[] data = res.getData();
int[] tempValues = new int[length];
for (int i = 0; i < length; i++) {
byte[] tempData = new byte[4];
for (int m = 0; m < 4; m++) {
tempData[m] = data[i * 4 + m];
}
tempValues[i] = BytesToInt(tempData, DataFormat.ABCD);
}
if (tempValues.length == length)
callback.onSuccess(tempValues);
} catch (InterruptedException e) {
MessageLog.ShowError("ReadInt onFailure,Address=" + Address + ",length=" + length + ",msg:" + e.toString());
} catch (ExecutionException e) {
MessageLog.ShowError("ReadInt onFailure,Address=" + Address + ",length=" + length + ",msg:" + e.toString());
} catch (ModbusTransportException e) {
MessageLog.ShowError("ReadInt onFailure,Address=" + Address + ",length=" + length + ",msg:" + e.toString());
} catch (ModbusInitException e) {
MessageLog.ShowError("ReadInt onFailure,Address=" + Address + ",length=" + length + ",msg:" + e.toString());
} catch (ModbusRespException e) {
MessageLog.ShowError("ReadInt onFailure,Address=" + Address + ",length=" + length + ",msg:" + e.toString());
}
}

public void WriteShort(String Address, short Value, IWriteCallBack callback) {
@@ -226,59 +339,49 @@ public class ModbusTcpServer {
if (add < 0) return;
short[] send = new short[1];
send[0] = Value;
ModbusTcpHelper.get().writeRegisters(1, add, send, new ModbusCallback<WriteRegistersResponse>() {
@Override
public void onSuccess(WriteRegistersResponse writeRegistersResponse) {
MessageLog.ShowInfo("WriteShort onSuccess,Address=" + Address + ",Value=" + Value);
callback.onSuccess();
}

@Override
public void onFailure(Throwable tr) {
MessageLog.ShowError("WriteShort onFailure,Address=" + Address + ",Value=" + Value + ",msg:" + tr.toString());
callback.onFailure(tr.toString());
try {
Thread.sleep(3000);
WriteShort(Address, Value, callback);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}

@Override
public void onFinally() {

}
});
try {
ModbusTcpHelper.get().syncWriteRegisters(1, add, send);
callback.onSuccess();
} catch (InterruptedException e) {
MessageLog.ShowError("WriteShort onFailure,Address=" + Address + ",length=" + Value + ",msg:" + e.toString());
callback.onFailure(e.toString());
} catch (ExecutionException e) {
MessageLog.ShowError("WriteShort onFailure,Address=" + Address + ",length=" + Value + ",msg:" + e.toString());
callback.onFailure(e.toString());
} catch (ModbusTransportException e) {
MessageLog.ShowError("WriteShort onFailure,Address=" + Address + ",length=" + Value + ",msg:" + e.toString());
callback.onFailure(e.toString());
} catch (ModbusInitException e) {
MessageLog.ShowError("WriteShort onFailure,Address=" + Address + ",length=" + Value + ",msg:" + e.toString());
callback.onFailure(e.toString());
} catch (ModbusRespException e) {
MessageLog.ShowError("WriteShort onFailure,Address=" + Address + ",length=" + Value + ",msg:" + e.toString());
callback.onFailure(e.toString());
}
}

public void WriteBool(String Address, boolean Value, IWriteCallBack callback) {
int add = GetAddress(Address);
if (add < 0) return;
ModbusTcpHelper.get().writeCoil(1, add, Value, new ModbusCallback<WriteCoilResponse>() {
@Override
public void onSuccess(WriteCoilResponse writeCoilResponse) {
MessageLog.ShowInfo("WriteBool onSuccess,Address=" + Address + ",Value=" + Value);
callback.onSuccess();
}

@Override
public void onFailure(Throwable tr) {
MessageLog.ShowError("WriteBool onFailure,Address=" + Address + ",Value=" + Value + ",msg:" + tr.toString());
callback.onFailure(tr.toString());
try {
Thread.sleep(3000);
WriteBool(Address, Value, callback);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}

@Override
public void onFinally() {

}
});
try {
ModbusTcpHelper.get().syncWriteCoil(1, add, Value);
callback.onSuccess();
} catch (InterruptedException e) {
MessageLog.ShowError("WriteBool onFailure,Address=" + Address + ",length=" + Value + ",msg:" + e.toString());
callback.onFailure(e.toString());
} catch (ExecutionException e) {
MessageLog.ShowError("WriteBool onFailure,Address=" + Address + ",length=" + Value + ",msg:" + e.toString());
callback.onFailure(e.toString());
} catch (ModbusTransportException e) {
MessageLog.ShowError("WriteBool onFailure,Address=" + Address + ",length=" + Value + ",msg:" + e.toString());
callback.onFailure(e.toString());
} catch (ModbusInitException e) {
MessageLog.ShowError("WriteBool onFailure,Address=" + Address + ",length=" + Value + ",msg:" + e.toString());
callback.onFailure(e.toString());
} catch (ModbusRespException e) {
MessageLog.ShowError("WriteBool onFailure,Address=" + Address + ",length=" + Value + ",msg:" + e.toString());
callback.onFailure(e.toString());
}
}

public void WriteFloat(String Address, float Value, IWriteCallBack callback) {
@@ -286,30 +389,50 @@ public class ModbusTcpServer {
if (add < 0) return;
int intBits = Float.floatToRawIntBits(Value);
short[] send = new short[]{(short) ((intBits >> 16) & 0xffff), (short) (intBits & 0xffff)};
ModbusTcpHelper.get().writeRegisters(1, add, send, new ModbusCallback<WriteRegistersResponse>() {
@Override
public void onSuccess(WriteRegistersResponse writeRegistersResponse) {
MessageLog.ShowInfo("WriteFloat onSuccess,Address=" + Address + ",Value=" + Value);
callback.onSuccess();
}

@Override
public void onFailure(Throwable tr) {
MessageLog.ShowError("WriteFloat onFailure,Address=" + Address + ",Value=" + Value + ",msg:" + tr.toString());
callback.onFailure(tr.toString());
try {
Thread.sleep(3000);
WriteFloat(Address, Value, callback);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}

@Override
public void onFinally() {
try {
ModbusTcpHelper.get().syncWriteRegisters(1, add, send);
callback.onSuccess();
} catch (InterruptedException e) {
MessageLog.ShowError("WriteFloat onFailure,Address=" + Address + ",length=" + Value + ",msg:" + e.toString());
callback.onFailure(e.toString());
} catch (ExecutionException e) {
MessageLog.ShowError("WriteFloat onFailure,Address=" + Address + ",length=" + Value + ",msg:" + e.toString());
callback.onFailure(e.toString());
} catch (ModbusTransportException e) {
MessageLog.ShowError("WriteFloat onFailure,Address=" + Address + ",length=" + Value + ",msg:" + e.toString());
callback.onFailure(e.toString());
} catch (ModbusInitException e) {
MessageLog.ShowError("WriteFloat onFailure,Address=" + Address + ",length=" + Value + ",msg:" + e.toString());
callback.onFailure(e.toString());
} catch (ModbusRespException e) {
MessageLog.ShowError("WriteFloat onFailure,Address=" + Address + ",length=" + Value + ",msg:" + e.toString());
callback.onFailure(e.toString());
}
}

}
});
public void WriteInt(String Address, int Value, IWriteCallBack callback) {
int add = GetAddress(Address);
if (add < 0) return;
short[] send = IntToShorts(Value);
try {
ModbusTcpHelper.get().syncWriteRegisters(1, add, send);
callback.onSuccess();
} catch (InterruptedException e) {
MessageLog.ShowError("WriteInt onFailure,Address=" + Address + ",length=" + Value + ",msg:" + e.toString());
callback.onFailure(e.toString());
} catch (ExecutionException e) {
MessageLog.ShowError("WriteInt onFailure,Address=" + Address + ",length=" + Value + ",msg:" + e.toString());
callback.onFailure(e.toString());
} catch (ModbusTransportException e) {
MessageLog.ShowError("WriteInt onFailure,Address=" + Address + ",length=" + Value + ",msg:" + e.toString());
callback.onFailure(e.toString());
} catch (ModbusInitException e) {
MessageLog.ShowError("WriteInt onFailure,Address=" + Address + ",length=" + Value + ",msg:" + e.toString());
callback.onFailure(e.toString());
} catch (ModbusRespException e) {
MessageLog.ShowError("WriteInt onFailure,Address=" + Address + ",length=" + Value + ",msg:" + e.toString());
callback.onFailure(e.toString());
}
}

public void WriteShort(String Address, short Value) {
@@ -317,55 +440,38 @@ public class ModbusTcpServer {
if (add < 0) return;
short[] send = new short[1];
send[0] = Value;
ModbusTcpHelper.get().writeRegisters(1, add, send, new ModbusCallback<WriteRegistersResponse>() {
@Override
public void onSuccess(WriteRegistersResponse writeRegistersResponse) {
MessageLog.ShowInfo("WriteShort onSuccess,Address=" + Address + ",Value=" + Value);
}

@Override
public void onFailure(Throwable tr) {
MessageLog.ShowError("WriteShort onFailure,Address=" + Address + ",Value=" + Value + ",msg:" + tr.toString());
try {
Thread.sleep(3000);
WriteShort(Address, Value);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}

@Override
public void onFinally() {

}
});
try {
ModbusTcpHelper.get().syncWriteRegisters(1, add, send);
} catch (InterruptedException e) {
MessageLog.ShowError("WriteShort onFailure,Address=" + Address + ",length=" + Value + ",msg:" + e.toString());
} catch (ExecutionException e) {
MessageLog.ShowError("WriteShort onFailure,Address=" + Address + ",length=" + Value + ",msg:" + e.toString());
} catch (ModbusTransportException e) {
MessageLog.ShowError("WriteShort onFailure,Address=" + Address + ",length=" + Value + ",msg:" + e.toString());
} catch (ModbusInitException e) {
MessageLog.ShowError("WriteShort onFailure,Address=" + Address + ",length=" + Value + ",msg:" + e.toString());
} catch (ModbusRespException e) {
MessageLog.ShowError("WriteShort onFailure,Address=" + Address + ",length=" + Value + ",msg:" + e.toString());
}
}

public void WriteBool(String Address, boolean Value) {
int add = GetAddress(Address);
if (add < 0) return;
ModbusTcpHelper.get().writeCoil(1, add, Value, new ModbusCallback<WriteCoilResponse>() {
@Override
public void onSuccess(WriteCoilResponse writeCoilResponse) {
MessageLog.ShowInfo("WriteBool onSuccess,Address=" + Address + ",Value=" + Value);
}

@Override
public void onFailure(Throwable tr) {
MessageLog.ShowError("WriteBool onFailure,Address=" + Address + ",Value=" + Value + ",msg:" + tr.toString());
try {
Thread.sleep(3000);
WriteBool(Address, Value);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}

@Override
public void onFinally() {

}
});
try {
ModbusTcpHelper.get().syncWriteCoil(1, add, Value);
} catch (InterruptedException e) {
MessageLog.ShowError("WriteBool onFailure,Address=" + Address + ",length=" + Value + ",msg:" + e.toString());
} catch (ExecutionException e) {
MessageLog.ShowError("WriteBool onFailure,Address=" + Address + ",length=" + Value + ",msg:" + e.toString());
} catch (ModbusTransportException e) {
MessageLog.ShowError("WriteBool onFailure,Address=" + Address + ",length=" + Value + ",msg:" + e.toString());
} catch (ModbusInitException e) {
MessageLog.ShowError("WriteBool onFailure,Address=" + Address + ",length=" + Value + ",msg:" + e.toString());
} catch (ModbusRespException e) {
MessageLog.ShowError("WriteBool onFailure,Address=" + Address + ",length=" + Value + ",msg:" + e.toString());
}
}

public void WriteFloat(String Address, float Value) {
@@ -373,28 +479,38 @@ public class ModbusTcpServer {
if (add < 0) return;
int intBits = Float.floatToRawIntBits(Value);
short[] send = new short[]{(short) ((intBits >> 16) & 0xffff), (short) (intBits & 0xffff)};
ModbusTcpHelper.get().writeRegisters(1, add, send, new ModbusCallback<WriteRegistersResponse>() {
@Override
public void onSuccess(WriteRegistersResponse writeRegistersResponse) {
MessageLog.ShowInfo("WriteFloat onSuccess,Address=" + Address + ",Value=" + Value);
}

@Override
public void onFailure(Throwable tr) {
MessageLog.ShowError("WriteFloat onFailure,Address=" + Address + ",Value=" + Value + ",msg:" + tr.toString());
try {
Thread.sleep(3000);
WriteFloat(Address, Value);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}

@Override
public void onFinally() {
try {
ModbusTcpHelper.get().syncWriteRegisters(1, add, send);
} catch (InterruptedException e) {
MessageLog.ShowError("WriteFloat onFailure,Address=" + Address + ",length=" + Value + ",msg:" + e.toString());
} catch (ExecutionException e) {
MessageLog.ShowError("WriteFloat onFailure,Address=" + Address + ",length=" + Value + ",msg:" + e.toString());
} catch (ModbusTransportException e) {
MessageLog.ShowError("WriteFloat onFailure,Address=" + Address + ",length=" + Value + ",msg:" + e.toString());
} catch (ModbusInitException e) {
MessageLog.ShowError("WriteFloat onFailure,Address=" + Address + ",length=" + Value + ",msg:" + e.toString());
} catch (ModbusRespException e) {
MessageLog.ShowError("WriteFloat onFailure,Address=" + Address + ",length=" + Value + ",msg:" + e.toString());
}
}

}
});
public void WriteInt(String Address, int Value) {
int add = GetAddress(Address);
if (add < 0) return;
short[] send = IntToShorts(Value);
try {
ModbusTcpHelper.get().syncWriteRegisters(1, add, send);
} catch (InterruptedException e) {
MessageLog.ShowError("WriteInt onFailure,Address=" + Address + ",length=" + Value + ",msg:" + e.toString());
} catch (ExecutionException e) {
MessageLog.ShowError("WriteInt onFailure,Address=" + Address + ",length=" + Value + ",msg:" + e.toString());
} catch (ModbusTransportException e) {
MessageLog.ShowError("WriteInt onFailure,Address=" + Address + ",length=" + Value + ",msg:" + e.toString());
} catch (ModbusInitException e) {
MessageLog.ShowError("WriteInt onFailure,Address=" + Address + ",length=" + Value + ",msg:" + e.toString());
} catch (ModbusRespException e) {
MessageLog.ShowError("WriteInt onFailure,Address=" + Address + ",length=" + Value + ",msg:" + e.toString());
}
}

}

+ 29
- 28
app/src/main/java/com/example/bpa/helper/ThreadManager.java 查看文件

@@ -21,42 +21,43 @@ public class ThreadManager {
public long RestartInterval = 2000;
ConcurrentHashMap<String, ThreadModel> ts = new ConcurrentHashMap<>();

private void Sleep(long millis) {
try {
Thread.sleep(millis);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}

public void StartLong(String Key, boolean IsRestart, IThread _thread) {
if (!ts.containsKey(Key)) {
ts.put(Key, new ThreadModel());
ts.get(Key).RunThread = _thread;
ts.get(Key).ThreadObj = new Thread(new Runnable() {
@Override
public void run() {
MessageLog.ShowInfo("启动线程:" + Key);
while (!ts.get(Key).IsCancel) {
if (IsRestart) {
try {
ts.get(Key).RunThread.Run();
} catch (Exception ex) {
MessageLog.ShowError("多线程:" + Key + "运行发生异常,已重启,errorMsg:" + ex.toString());
try {
Thread.sleep(RestartInterval);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}
} else {
try {
ts.get(Key).RunThread.Run();
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
ts.get(Key).ThreadObj = new Thread(() -> {
MessageLog.ShowInfo("启动线程:" + Key);
while (!ts.get(Key).IsCancel) {
if (IsRestart) {
try {
ts.get(Key).RunThread.Run();
} catch (Exception ex) {
MessageLog.ShowError("多线程:" + Key + "运行发生异常,已重启,errorMsg:" + ex.toString());
Sleep(RestartInterval);
}
} else {
try {
ts.get(Key).RunThread.Run();
} catch (InterruptedException e) {
MessageLog.ShowError("多线程:" + Key + "运行发生异常,已退出,errorMsg:" + e.toString());
}
}
}
try {
ts.get(Key).RunThread.RunComplete();
MessageLog.ShowInfo("线程:[" + Key + "]--执行完成");
try {
ts.get(Key).RunThread.RunComplete();
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
ts.remove(Key);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
ts.remove(Key);
});
ts.get(Key).ThreadObj.setName(Key);
ts.get(Key).ThreadObj.start();


+ 101
- 51
app/src/main/java/com/example/bpa/view/from/lcsz_activity.java 查看文件

@@ -135,9 +135,9 @@ public class lcsz_activity extends AppCompatActivity implements View.OnClickList
//加载listview 数据
bpa_materials = QueryDB.GetMaterialALL();

BPA_MATERIAL wsz=new BPA_MATERIAL();
wsz.name="未设置";
wsz.id="";
BPA_MATERIAL wsz = new BPA_MATERIAL();
wsz.name = "未设置";
wsz.id = "";
bpa_materials.add(wsz);
selectitem_adapter selectadapter = new selectitem_adapter(lcsz_activity.this, R.layout.selectitem_item, bpa_materials);
materiallist.setAdapter(selectadapter);
@@ -181,15 +181,15 @@ public class lcsz_activity extends AppCompatActivity implements View.OnClickList
material_parent.setVisibility(View.GONE);//隐藏父控件
if (currentSilo != null) {
BPA_SILOSANDMATERIAL item = new BPA_SILOSANDMATERIAL();
BPA_MATERIAL material= ((selectitem_adapter) viewHolder).getItem(position);
BPA_MATERIAL material = ((selectitem_adapter) viewHolder).getItem(position);
item.silosID = currentSilo.id;
item.materialID = material.id;
//更新
if (QueryDB.UpdateSilosAndMaterial(item)) {
currentSilo.materialName.setText(material.name);
T.show(lcsz_activity.this, "更新成功");
datas.get(currentSilo.getNum()-1).materialId = item.materialID;
datas.get(currentSilo.getNum()-1).materialName =material.name;
datas.get(currentSilo.getNum() - 1).materialId = item.materialID;
datas.get(currentSilo.getNum() - 1).materialName = material.name;
currentSilo.startFlick();
}
}
@@ -230,57 +230,107 @@ public class lcsz_activity extends AppCompatActivity implements View.OnClickList
* 实时显示线程
*/
public void Run() {
new Thread(new Runnable() {

ThreadManager.Get().StartLong("更新料仓数据", true, new IThread() {
@Override
public void run() {
while (DataBus.getInstance().PlcIsConnect) {
try {
lcsz_activity.this.runOnUiThread(new Runnable() {
@Override
public void run() {

for (int i = 0; i < datas.size(); i++) {
int num = datas.get(i).num;
float val = DeviceData.Get().getCallCurrentWeight(num);
RecyclerView.ViewHolder viewHolder = recyclerView.findViewHolderForAdapterPosition(i);
if (viewHolder != null) {
lc_item_adapter.MyLCViewHolder hold = (lc_item_adapter.MyLCViewHolder) viewHolder;
//hold.silosmargin.setText(new java.text.DecimalFormat("#.00").format(val)+"g");
hold.silosmargin.setText(val + "g");

if (!datas.get(i).materialId.isEmpty()) {
if (val < datas.get(i).warningValue)//补料
{
hold.image_yj.setVisibility(View.VISIBLE);
} else {
hold.image_yj.setVisibility(View.GONE);
FlashHelper.getInstance().stopFlick(hold.image_yj);
}

if (val < datas.get(i).thrsoleValue)//告警
{
hold.image_gj.setVisibility(View.VISIBLE);
} else {
hold.image_gj.setVisibility(View.GONE);
FlashHelper.getInstance().stopFlick(hold.image_gj);
}
}else
{
hold.image_yj.setVisibility(View.GONE);
hold.image_gj.setVisibility(View.GONE);
hold.stopFlick();
}
public void Run() throws InterruptedException {
lcsz_activity.this.runOnUiThread(new Runnable() {
@Override
public void run() {

for (int i = 0; i < datas.size(); i++) {
int num = datas.get(i).num;
float val = DeviceData.Get().getCallCurrentWeight(num);
RecyclerView.ViewHolder viewHolder = recyclerView.findViewHolderForAdapterPosition(i);
if (viewHolder != null) {
lc_item_adapter.MyLCViewHolder hold = (lc_item_adapter.MyLCViewHolder) viewHolder;
//hold.silosmargin.setText(new java.text.DecimalFormat("#.00").format(val)+"g");
hold.silosmargin.setText(val + "g");

if (!datas.get(i).materialId.isEmpty()) {
if (val < datas.get(i).warningValue)//补料
{
hold.image_yj.setVisibility(View.VISIBLE);
} else {
hold.image_yj.setVisibility(View.GONE);
FlashHelper.getInstance().stopFlick(hold.image_yj);
}

if (val < datas.get(i).thrsoleValue)//告警
{
hold.image_gj.setVisibility(View.VISIBLE);
} else {
hold.image_gj.setVisibility(View.GONE);
FlashHelper.getInstance().stopFlick(hold.image_gj);
}
} else {
hold.image_yj.setVisibility(View.GONE);
hold.image_gj.setVisibility(View.GONE);
hold.stopFlick();
}
}
});
Thread.sleep(1000);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}
}
});
Thread.sleep(1000);
}

@Override
public void RunComplete() throws InterruptedException {

}
}).start();
});
// new Thread(new Runnable() {
// @Override
// public void run() {
// while (DataBus.getInstance().PlcIsConnect) {
// try {
// lcsz_activity.this.runOnUiThread(new Runnable() {
// @Override
// public void run() {
//
// for (int i = 0; i < datas.size(); i++) {
// int num = datas.get(i).num;
// float val = DeviceData.Get().getCallCurrentWeight(num);
// RecyclerView.ViewHolder viewHolder = recyclerView.findViewHolderForAdapterPosition(i);
// if (viewHolder != null) {
// lc_item_adapter.MyLCViewHolder hold = (lc_item_adapter.MyLCViewHolder) viewHolder;
// //hold.silosmargin.setText(new java.text.DecimalFormat("#.00").format(val)+"g");
// hold.silosmargin.setText(val + "g");
//
// if (!datas.get(i).materialId.isEmpty()) {
// if (val < datas.get(i).warningValue)//补料
// {
// hold.image_yj.setVisibility(View.VISIBLE);
// } else {
// hold.image_yj.setVisibility(View.GONE);
// FlashHelper.getInstance().stopFlick(hold.image_yj);
// }
//
// if (val < datas.get(i).thrsoleValue)//告警
// {
// hold.image_gj.setVisibility(View.VISIBLE);
// } else {
// hold.image_gj.setVisibility(View.GONE);
// FlashHelper.getInstance().stopFlick(hold.image_gj);
// }
// }else
// {
// hold.image_yj.setVisibility(View.GONE);
// hold.image_gj.setVisibility(View.GONE);
// hold.stopFlick();
// }
// }
// }
// }
// });
// Thread.sleep(1000);
// } catch (InterruptedException e) {
// throw new RuntimeException(e);
// }
// }
// }
// }).start();
}
//endreion
}

正在加载...
取消
保存