diff --git a/app/src/main/java/com/example/bpa/Model/IReadCallBack.java b/app/src/main/java/com/example/bpa/Model/IReadCallBack.java new file mode 100644 index 0000000..58372df --- /dev/null +++ b/app/src/main/java/com/example/bpa/Model/IReadCallBack.java @@ -0,0 +1,5 @@ +package com.example.bpa.Model; + +public interface IReadCallBack { + void onSuccess(T t); +} diff --git a/app/src/main/java/com/example/bpa/Model/IRun.java b/app/src/main/java/com/example/bpa/Model/IRun.java new file mode 100644 index 0000000..aa686b9 --- /dev/null +++ b/app/src/main/java/com/example/bpa/Model/IRun.java @@ -0,0 +1,5 @@ +package com.example.bpa.Model; + +public interface IRun { + void Run(); +} diff --git a/app/src/main/java/com/example/bpa/Model/IWriteCallBack.java b/app/src/main/java/com/example/bpa/Model/IWriteCallBack.java new file mode 100644 index 0000000..e44cf35 --- /dev/null +++ b/app/src/main/java/com/example/bpa/Model/IWriteCallBack.java @@ -0,0 +1,7 @@ +package com.example.bpa.Model; + +public interface IWriteCallBack { + void onSuccess(); + + void onFailure(String ErrorMsg); +} diff --git a/app/src/main/java/com/example/bpa/app/BusinessServer.java b/app/src/main/java/com/example/bpa/app/BusinessServer.java new file mode 100644 index 0000000..f901a83 --- /dev/null +++ b/app/src/main/java/com/example/bpa/app/BusinessServer.java @@ -0,0 +1,53 @@ +package com.example.bpa.app; + +import com.example.bpa.Model.IThread; +import com.example.bpa.db.QueryDB; +import com.example.bpa.helper.ThreadManager; +import com.example.bpa.view.mode.ResGoodsRecipe; + +import java.util.ArrayList; +import java.util.concurrent.ConcurrentLinkedQueue; + +public class BusinessServer { + private static volatile BusinessServer _instance; + + public static BusinessServer get() { + if (_instance == null) + _instance = new BusinessServer(); + return _instance; + } + + private BusinessServer() { + } + + ConcurrentLinkedQueue Commoditys = new ConcurrentLinkedQueue(); + + /** + * 添加配方 + * + * @param info 配方信息 + */ + public void AddCommodity(String info) { + Commoditys.offer(info); + } + + public void Start() { + ThreadManager.Get().StartLong("配方执行业务流程", true, new IThread() { + @Override + public void Run() throws InterruptedException { + while (Commoditys.size() > 0) { + Commoditys.poll(); + + ArrayList goods = QueryDB.GetGoodsSrecipeList(""); + } + Thread.sleep(1000); + } + + @Override + public void RunComplete() throws InterruptedException { + + } + }); + } + +} diff --git a/app/src/main/java/com/example/bpa/app/DeviceData.java b/app/src/main/java/com/example/bpa/app/DeviceData.java index 0828db1..dc34e4b 100644 --- a/app/src/main/java/com/example/bpa/app/DeviceData.java +++ b/app/src/main/java/com/example/bpa/app/DeviceData.java @@ -1,11 +1,14 @@ package com.example.bpa.app; -import android.renderscript.Sampler; - +import com.example.bpa.Model.IReadCallBack; +import com.example.bpa.Model.IRun; import com.example.bpa.Model.IThread; +import com.example.bpa.Model.IWriteCallBack; +import com.example.bpa.helper.RTrig; import com.example.bpa.helper.ThreadManager; import java.util.concurrent.ConcurrentHashMap; +import java.util.concurrent.ConcurrentLinkedQueue; public class DeviceData { private static volatile DeviceData _instance; @@ -20,7 +23,53 @@ public class DeviceData { ThreadManager.Get().StartLong("Plc设备数据监听", true, new IThread() { @Override public void Run() throws InterruptedException { - + //获取校准值 + ModbusTcpServer.get().ReadShort("VW100", 10, new IReadCallBack() { + @Override + public void onSuccess(short[] shorts) { + for (int i = 0; i < shorts.length; i++) { + CalibrationValue.put(i + 1, shorts[i]); + } + } + }); + + //获取称当前重量 + ModbusTcpServer.get().ReadFloat("VD260", 9, new IReadCallBack() { + @Override + public void onSuccess(float[] floats) { + for (int i = 0; i < floats.length; i++) { + CallCurrentWeight.put(i + 1, floats[i]); + } + } + }); + + //获取校准基准时间 + ModbusTcpServer.get().ReadFloat("VD124", 1, new IReadCallBack() { + @Override + public void onSuccess(float[] floats) { + CalibrationReferenceTime = floats[0]; + } + }); + + //获取清洗参数 + ModbusTcpServer.get().ReadShort("VW140", 4, new IReadCallBack() { + @Override + public void onSuccess(short[] shorts) { + DrainageTime = shorts[0]; + AddCleaningAgentTime = shorts[1]; + InletTime = shorts[2]; + CyclicCleaningTime = shorts[3]; + } + }); + + //配料完成 M0.3 + CompleteListen("M0.3", "配料完成", ChargeMixtureCompleteNotify); + + //清洗完成 M0.6 + CompleteListen("M0.6", "清洗完成", CleaningCompleteNotify); + + //去皮完成 M1.3 + CompleteListen("M1.3", "去皮完成", PeelingCompleteNotify); Thread.sleep(10); } @@ -32,34 +81,35 @@ public class DeviceData { }); } - /** - * 称当前重量 - */ - ConcurrentHashMap CallCurrentWeight = new ConcurrentHashMap(); - /** - * 手自动模式 - */ - boolean HandOrAutoMode; - - /** - * 清洗模式 - */ - boolean CleaningMode; + private void CompleteListen(String add, String name, IRun callback) { + ModbusTcpServer.get().ReadBool(add, 1, new IReadCallBack() { + @Override + public void onSuccess(boolean[] booleans) { + RTrig.get(name).Start(booleans[0], new IRun() { + @Override + public void Run() { + if (callback != null) + callback.Run(); + } + }); + } + }); + } /** - * 清洗完成 + * 配料完成通知 */ - boolean CleaningComplete; + public IRun ChargeMixtureCompleteNotify; /** - * 一键去皮完成状态 + * 清洗完成通知 */ - boolean OneClickPeelingComplete; + public IRun CleaningCompleteNotify; /** - * 校准模式 + * 去皮完成 */ - boolean CalibrationMode; + public IRun PeelingCompleteNotify; /** * 校准值写入 @@ -68,13 +118,30 @@ public class DeviceData { * @param value 需要写入的值 * @param ch 需要写入的通道编号,1--10 */ - public void setCalibrationValue(short value, int ch) { + public void setCalibrationValue(short value, int ch, IWriteCallBack callback) { if (ch >= 1 && ch <= 10) { String add = "VW" + (98 + ch * 2); - ModbusTcpServer.get().WriteShort(add, value); + ModbusTcpServer.get().WriteShort(add, value, callback); } } + /** + * 校准值 + */ + ConcurrentHashMap CalibrationValue = new ConcurrentHashMap<>(); + + /** + * 获取校准值 + * + * @param ch 要获取的通道号 + * @return + */ + public short getCalibrationValue(int ch) { + if (!CalibrationValue.containsKey(ch)) + return 0; + return CalibrationValue.get(ch); + } + /** * 需求值写入 * ,PLC 地址 VW200--VW218 @@ -82,10 +149,10 @@ public class DeviceData { * @param value 需要写入的值 * @param ch 需要写入的通道编号,1--10 */ - public void setDemandValue(short value, int ch) { + public void setDemandValue(short value, int ch, IWriteCallBack callback) { if (ch >= 1 && ch <= 10) { String add = "VW" + (198 + ch * 2); - ModbusTcpServer.get().WriteShort(add, value); + ModbusTcpServer.get().WriteShort(add, value, callback); } } @@ -96,11 +163,11 @@ public class DeviceData { * @param value 需要写入的值 * @param ch 需要写入的通道编号,1--10 */ - public void setChCalibrationSwitch(boolean value, int ch) { + public void setChCalibrationSwitch(boolean value, int ch, IWriteCallBack callback) { if (ch >= 1 && ch <= 10) { String add = ModbusTcpServer.get().getBitSingleAdd("M", 2, ch); if (add.length() > 0) - ModbusTcpServer.get().WriteBool(add, value); + ModbusTcpServer.get().WriteBool(add, value, callback); } } @@ -110,11 +177,11 @@ public class DeviceData { * @param value * @param ch 设置称的通道号,ch1-ch8 */ - public void setRemovePeelCalibration(boolean value, int ch) { + public void setRemovePeelCalibration(boolean value, int ch, IWriteCallBack callback) { if (ch >= 1 && ch <= 8) { String add = ModbusTcpServer.get().getBitSingleAdd("M", 5, ch); if (add.length() > 0) - ModbusTcpServer.get().WriteBool(add, value); + ModbusTcpServer.get().WriteBool(add, value, callback); } } @@ -124,68 +191,68 @@ public class DeviceData { * @param value * @param ch 设置称的砝码通道号,ch1-ch8 */ - public void setWeightValue(boolean value, int ch) { + public void setWeightValue(boolean value, int ch, IWriteCallBack callback) { if (ch >= 1 && ch <= 8) { String add = ModbusTcpServer.get().getBitSingleAdd("M", 15, ch); if (add.length() > 0) - ModbusTcpServer.get().WriteBool(add, value); + ModbusTcpServer.get().WriteBool(add, value, callback); } } /** * 重量清零 */ - public void setWeightClear() { - ModbusTcpServer.get().WriteBool("M6.0", true); + public void setWeightClear(IWriteCallBack callback) { + ModbusTcpServer.get().WriteBool("M6.0", true, callback); } /** * 关闭写保护 */ - public void setCloseWriteProtect() { - ModbusTcpServer.get().WriteBool("M6.1", true); + public void setCloseWriteProtect(IWriteCallBack callback) { + ModbusTcpServer.get().WriteBool("M6.1", true, callback); } /** * 打开写保护 */ - public void setOpenWriteProtect() { - ModbusTcpServer.get().WriteBool("M6.4", true); + public void setOpenWriteProtect(IWriteCallBack callback) { + ModbusTcpServer.get().WriteBool("M6.4", true, callback); } /** * 零点校准 */ - public void setZeroPointCalibration() { - ModbusTcpServer.get().WriteBool("M6.2", true); + public void setZeroPointCalibration(IWriteCallBack callback) { + ModbusTcpServer.get().WriteBool("M6.2", true, callback); } /** * 砝码值写入 */ - public void setWeightValueWrite() { - ModbusTcpServer.get().WriteBool("M6.3", true); + public void setWeightValueWrite(IWriteCallBack callback) { + ModbusTcpServer.get().WriteBool("M6.3", true, callback); } /** * 砝码校准模式 */ - public void setWeightCalibrationMode() { - ModbusTcpServer.get().WriteBool("M6.5", true); + public void setWeightCalibrationMode(IWriteCallBack callback) { + ModbusTcpServer.get().WriteBool("M6.5", true, callback); } /** * 启动通道校准 */ - public void setChStartCalibration() { - ModbusTcpServer.get().WriteBool("M0.0", true); + public void setChStartCalibration(IWriteCallBack callback) { + ModbusTcpServer.get().WriteBool("M0.0", true, callback); } /** * 启动配料 */ - public void setChargeMixtureStart() { - ModbusTcpServer.get().WriteBool("M0.1", true); + public void setChargeMixtureStart(IWriteCallBack callback) { + ModbusTcpServer.get().WriteBool("M0.1", true, callback); } /** @@ -193,10 +260,15 @@ public class DeviceData { * * @param value true:自动模式,flase:手动模式 */ - public void setHandOrAutoSwitch(boolean value) { - ModbusTcpServer.get().WriteBool("M0.2", value); + public void setHandOrAutoSwitch(boolean value, IWriteCallBack callback) { + ModbusTcpServer.get().WriteBool("M0.2", value, callback); } + /** + * 手自动模式 + */ + boolean HandOrAutoMode; + /** * 获取手自动模式的状态 * @@ -211,10 +283,15 @@ public class DeviceData { * * @param value true:清洗模式,false:配料模式 */ - public void setCleaningMode(boolean value) { - ModbusTcpServer.get().WriteBool("M0.4", value); + public void setCleaningMode(boolean value, IWriteCallBack callback) { + ModbusTcpServer.get().WriteBool("M0.4", value, callback); } + /** + * 清洗模式 + */ + boolean CleaningMode; + /** * 获取清洗模式 * @@ -227,17 +304,22 @@ public class DeviceData { /** * 设置开始清洗 */ - public void setStartCleaning() { - ModbusTcpServer.get().WriteBool("M0.5", true); + public void setStartCleaning(IWriteCallBack callback) { + ModbusTcpServer.get().WriteBool("M0.5", true, callback); } /** * 设置一键去皮 */ - public void setOneClickPeeling() { - ModbusTcpServer.get().WriteBool("M1.2", true); + public void setOneClickPeeling(IWriteCallBack callback) { + ModbusTcpServer.get().WriteBool("M1.2", true, callback); } + /** + * 一键去皮完成状态 + */ + boolean OneClickPeelingComplete; + /** * 设置一键去皮完成状态 * @@ -247,6 +329,11 @@ public class DeviceData { OneClickPeelingComplete = value; } + /** + * 清洗完成 + */ + boolean CleaningComplete; + /** * 获取清洗完成的状态 * @@ -261,10 +348,15 @@ public class DeviceData { * * @param value true:零点校准模式,false:清零去皮模式 */ - public void setCalibrationMode(boolean value) { - ModbusTcpServer.get().WriteBool("M1.4", value); + public void setCalibrationMode(boolean value, IWriteCallBack callback) { + ModbusTcpServer.get().WriteBool("M1.4", value, callback); } + /** + * 校准模式 + */ + boolean CalibrationMode; + /** * 获取校准模式 * @@ -274,6 +366,11 @@ public class DeviceData { return CalibrationMode; } + /** + * 称当前重量 + */ + ConcurrentHashMap CallCurrentWeight = new ConcurrentHashMap(); + /** * 获取指定称的当前重量 * @@ -292,8 +389,22 @@ public class DeviceData { * * @param value 需要写入的值 */ - public void setCalibrationReferenceTime(float value) { - ModbusTcpServer.get().WriteFloat("VD124", value); + public void setCalibrationReferenceTime(float value, IWriteCallBack callback) { + ModbusTcpServer.get().WriteFloat("VD124", value, callback); + } + + /** + * 校准基准时间 + */ + float CalibrationReferenceTime; + + /** + * 获取校准基准时间 + * + * @return + */ + public float getCalibrationReferenceTime() { + return CalibrationReferenceTime; } /** @@ -301,8 +412,22 @@ public class DeviceData { * * @param value */ - public void setDrainageTime(short value) { - ModbusTcpServer.get().WriteShort("VW140", value); + public void setDrainageTime(short value, IWriteCallBack callback) { + ModbusTcpServer.get().WriteShort("VW140", value, callback); + } + + /** + * 排水时间 + */ + short DrainageTime; + + /** + * 获取排水时间 + * + * @return + */ + public short getDrainageTime() { + return DrainageTime; } /** @@ -310,8 +435,22 @@ public class DeviceData { * * @param value */ - public void setAddCleaningAgentTime(short value) { - ModbusTcpServer.get().WriteShort("VW142", value); + public void setAddCleaningAgentTime(short value, IWriteCallBack callback) { + ModbusTcpServer.get().WriteShort("VW142", value, callback); + } + + /** + * 清洗剂添加时间 + */ + short AddCleaningAgentTime; + + /** + * 获取清洗剂添加时间 + * + * @return + */ + public short getAddCleaningAgentTime() { + return AddCleaningAgentTime; } /** @@ -319,8 +458,22 @@ public class DeviceData { * * @param value */ - public void setInletTime(short value) { - ModbusTcpServer.get().WriteShort("VW144", value); + public void setInletTime(short value, IWriteCallBack callback) { + ModbusTcpServer.get().WriteShort("VW144", value, callback); + } + + /** + * 进水时间 + */ + short InletTime; + + /** + * 获取进水时间 + * + * @return + */ + public short getInletTime() { + return InletTime; } /** @@ -328,8 +481,22 @@ public class DeviceData { * * @param value */ - public void setCyclicCleaningTime(short value) { - ModbusTcpServer.get().WriteShort("VW146", value); + public void setCyclicCleaningTime(short value, IWriteCallBack callback) { + ModbusTcpServer.get().WriteShort("VW146", value, callback); + } + + /** + * 循环清洗时间 + */ + short CyclicCleaningTime; + + /** + * 获取循环清洗时间 + * + * @return + */ + public short getCyclicCleaningTime() { + return CyclicCleaningTime; } /** @@ -337,8 +504,8 @@ public class DeviceData { * * @param value */ - public void setCalibrationWeight(short value) { - ModbusTcpServer.get().WriteShort("250", value); + public void setCalibrationWeight(short value, IWriteCallBack callback) { + ModbusTcpServer.get().WriteShort("250", value, callback); } /** @@ -346,8 +513,8 @@ public class DeviceData { * * @param value */ - public void setCalibrationWeight9(short value) { - ModbusTcpServer.get().WriteShort("254", value); + public void setCalibrationWeight9(short value, IWriteCallBack callback) { + ModbusTcpServer.get().WriteShort("254", value, callback); } diff --git a/app/src/main/java/com/example/bpa/app/IReadCallBack.java b/app/src/main/java/com/example/bpa/app/IReadCallBack.java deleted file mode 100644 index e196018..0000000 --- a/app/src/main/java/com/example/bpa/app/IReadCallBack.java +++ /dev/null @@ -1,5 +0,0 @@ -package com.example.bpa.app; - -public interface IReadCallBack { - void onSuccess(T t); -} diff --git a/app/src/main/java/com/example/bpa/app/ModbusTcpServer.java b/app/src/main/java/com/example/bpa/app/ModbusTcpServer.java index de5fad4..145885a 100644 --- a/app/src/main/java/com/example/bpa/app/ModbusTcpServer.java +++ b/app/src/main/java/com/example/bpa/app/ModbusTcpServer.java @@ -1,8 +1,8 @@ package com.example.bpa.app; -import android.util.Log; - +import com.example.bpa.Model.IReadCallBack; +import com.example.bpa.Model.IWriteCallBack; import com.example.bpa.helper.MessageLog; import com.licheedev.modbus4android.ModbusCallback; import com.licheedev.modbus4android.ModbusParam; @@ -13,14 +13,8 @@ import com.serotonin.modbus4j.msg.ReadHoldingRegistersResponse; import com.serotonin.modbus4j.msg.WriteCoilResponse; import com.serotonin.modbus4j.msg.WriteRegistersResponse; -import java.lang.reflect.Type; import java.nio.ByteBuffer; -import java.nio.FloatBuffer; -import java.nio.ShortBuffer; import java.util.Arrays; -import java.util.LinkedList; - -import io.reactivex.SingleConverter; public class ModbusTcpServer { @@ -150,12 +144,13 @@ public class ModbusTcpServer { @Override public void onSuccess(ReadHoldingRegistersResponse readHoldingRegistersResponse) { short[] data = readHoldingRegistersResponse.getShortData(); - callback.onSuccess(data); + if (data.length == length) + callback.onSuccess(data); } @Override public void onFailure(Throwable tr) { - MessageLog.ShowError("ReadShort onFailure,Address=" + Address + ",length=" + length + ",msg:" + tr.getMessage()); + MessageLog.ShowError("ReadShort onFailure,Address=" + Address + ",length=" + length + ",msg:" + tr.toString()); } @Override @@ -172,12 +167,13 @@ public class ModbusTcpServer { public void onSuccess(ReadCoilsResponse readCoilsResponse) { boolean[] data = readCoilsResponse.getBooleanData(); boolean[] result = Arrays.copyOfRange(data, 0, length); - callback.onSuccess(result); + if (result.length == length) + callback.onSuccess(result); } @Override public void onFailure(Throwable tr) { - MessageLog.ShowError("ReadBool onFailure,Address=" + Address + ",length=" + length + ",msg:" + tr.getMessage()); + MessageLog.ShowError("ReadBool onFailure,Address=" + Address + ",length=" + length + ",msg:" + tr.toString()); } @Override @@ -201,12 +197,13 @@ public class ModbusTcpServer { } tempValues[i] = ByteBuffer.wrap(tempData).getFloat(); } - callback.onSuccess(tempValues); + if (tempValues.length == length) + callback.onSuccess(tempValues); } @Override public void onFailure(Throwable tr) { - MessageLog.ShowError("ReadFloat onFailure,Address=" + Address + ",length=" + length + ",msg:" + tr.getMessage()); + MessageLog.ShowError("ReadFloat onFailure,Address=" + Address + ",length=" + length + ",msg:" + tr.toString()); } @Override @@ -217,6 +214,97 @@ public class ModbusTcpServer { } + public void WriteShort(String Address, short Value, IWriteCallBack callback) { + int add = GetAddress(Address); + if (add < 0) return; + short[] send = new short[1]; + send[0] = Value; + ModbusTcpHelper.get().writeRegisters(1, add, send, new ModbusCallback() { + @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() { + + } + }); + } + + 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() { + @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() { + + } + }); + } + + public void WriteFloat(String Address, float Value, IWriteCallBack callback) { + int add = GetAddress(Address); + 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() { + @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() { + + } + }); + } + public void WriteShort(String Address, short Value) { int add = GetAddress(Address); if (add < 0) return; @@ -230,7 +318,13 @@ public class ModbusTcpServer { @Override public void onFailure(Throwable tr) { - MessageLog.ShowError("WriteShort onFailure,Address=" + Address + ",Value=" + Value + ",msg:" + tr.getMessage()); + 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 @@ -251,7 +345,13 @@ public class ModbusTcpServer { @Override public void onFailure(Throwable tr) { - MessageLog.ShowError("WriteBool onFailure,Address=" + Address + ",Value=" + Value + ",msg:" + tr.getMessage()); + 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 @@ -274,7 +374,13 @@ public class ModbusTcpServer { @Override public void onFailure(Throwable tr) { - MessageLog.ShowError("WriteFloat onFailure,Address=" + Address + ",Value=" + Value + ",msg:" + tr.getMessage()); + 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 diff --git a/app/src/main/java/com/example/bpa/helper/RTrig.java b/app/src/main/java/com/example/bpa/helper/RTrig.java new file mode 100644 index 0000000..cda3bc3 --- /dev/null +++ b/app/src/main/java/com/example/bpa/helper/RTrig.java @@ -0,0 +1,43 @@ +package com.example.bpa.helper; + +import com.example.bpa.Model.IRun; +import com.example.bpa.Model.IWriteCallBack; + +import java.util.concurrent.ConcurrentHashMap; + +/** + * 上升沿操作 + */ +public class RTrig { + private static volatile ConcurrentHashMap _Instance; + + public static RTrig get(String name) { + if (_Instance == null) + _Instance = new ConcurrentHashMap(); + if (!_Instance.containsKey(name)) + _Instance.put(name, new RTrig()); + return _Instance.get(name); + } + + private RTrig() { + } + + private boolean flag; + private boolean Q; + + public boolean getQ() { + return Q; + } + + private void setIN(boolean falag) { + Q = falag && !flag; + flag = falag; + } + + public void Start(boolean IN, IRun callback) { + setIN(IN); + if (Q) + callback.Run(); + } + +} diff --git a/app/src/main/java/com/example/bpa/view/fragment/SystemSetFragment.java b/app/src/main/java/com/example/bpa/view/fragment/SystemSetFragment.java index 2bda4cc..71acffb 100644 --- a/app/src/main/java/com/example/bpa/view/fragment/SystemSetFragment.java +++ b/app/src/main/java/com/example/bpa/view/fragment/SystemSetFragment.java @@ -12,10 +12,9 @@ import android.view.ViewGroup; import android.widget.Button; import com.example.bpa.Model.IMessageLogNotify; +import com.example.bpa.Model.IReadCallBack; import com.example.bpa.Model.IThread; -import com.example.bpa.Model.ThreadModel; import com.example.bpa.R; -import com.example.bpa.app.IReadCallBack; import com.example.bpa.app.ModbusTcpServer; import com.example.bpa.helper.MessageLog; import com.example.bpa.helper.ThreadManager; @@ -65,7 +64,7 @@ public class SystemSetFragment extends Fragment { new Thread(new Runnable() { @Override public void run() { - ModbusTcpServer.get().Connect("192.168.1.14", 502); + ModbusTcpServer.get().Connect("192.168.1.9", 502); } }).start(); } @@ -78,41 +77,33 @@ public class SystemSetFragment extends Fragment { ThreadManager.Get().StartLong("数据读取监听", true, new IThread() { @Override public void Run() throws InterruptedException { -// ModbusTcpServer.get().ReadShort(0, 3, new IReadCallBack() { -// @Override -// public void onSuccess(short[] shorts) { -// for (int i = 0; i < shorts.length; i++) { -// MessageLog.ShowInfo("Shorts[" + i + "]=" + shorts[i]); -// } -// } -// }); -// -// ModbusTcpServer.get().ReadFloat(10, 3, new IReadCallBack() { -// @Override -// public void onSuccess(float[] floats) { -// for (int i = 0; i < floats.length; i++) { -// MessageLog.ShowInfo("Floats[" + i + "]=" + floats[i]); -// } -// } -// }); -// -// ModbusTcpServer.get().ReadBool(0, 3, new IReadCallBack() { -// @Override -// public void onSuccess(boolean[] booleans) { -// for (int i = 0; i < booleans.length; i++) { -// MessageLog.ShowInfo("Booleans[" + i + "]=" + booleans[i]); -// } -// } -// }); - - MessageLog.ShowInfo("开始执行"); - boolean[] bools = new boolean[10]; - for (int i = 0; i < 20; i++) { - bools[i] = true; - } - + ModbusTcpServer.get().ReadShort("0", 3, new IReadCallBack() { + @Override + public void onSuccess(short[] shorts) { + for (int i = 0; i < shorts.length; i++) { + MessageLog.ShowInfo("Shorts[" + i + "]=" + shorts[i]); + } + } + }); + + ModbusTcpServer.get().ReadFloat("10", 3, new IReadCallBack() { + @Override + public void onSuccess(float[] floats) { + for (int i = 0; i < floats.length; i++) { + MessageLog.ShowInfo("Floats[" + i + "]=" + floats[i]); + } + } + }); + + ModbusTcpServer.get().ReadBool("0", 3, new IReadCallBack() { + @Override + public void onSuccess(boolean[] booleans) { + for (int i = 0; i < booleans.length; i++) { + MessageLog.ShowInfo("Booleans[" + i + "]=" + booleans[i]); + } + } + }); Thread.sleep(3000); - } @Override