From 025f0510d5bae8213fa3da84a3ff068c34a73d91 Mon Sep 17 00:00:00 2001 From: Nah <15196688790@163.com> Date: Fri, 8 Dec 2023 13:32:12 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9PLC=E8=AF=BB=E5=8F=96?= =?UTF-8?q?=E7=9A=84=E6=96=B9=E6=B3=95=E3=80=82=E5=AE=8C=E5=96=84=E6=89=8B?= =?UTF-8?q?=E5=8A=A8=E5=86=99=E5=85=A5=E5=80=BC=E7=95=8C=E9=9D=A2=E7=9A=84?= =?UTF-8?q?=E5=8A=9F=E8=83=BD=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../business/ExecuteTheRecipe.java | 35 ++++++++---- .../business/deviceControl/PLCControl.java | 56 ++++++++++++------- .../deviceControl/PLCControllerTest.java | 9 +++ 3 files changed, 71 insertions(+), 29 deletions(-) diff --git a/app/src/main/java/com/bonait/bnframework/business/ExecuteTheRecipe.java b/app/src/main/java/com/bonait/bnframework/business/ExecuteTheRecipe.java index c6cb7a03..2dcfb55a 100644 --- a/app/src/main/java/com/bonait/bnframework/business/ExecuteTheRecipe.java +++ b/app/src/main/java/com/bonait/bnframework/business/ExecuteTheRecipe.java @@ -237,16 +237,28 @@ public class ExecuteTheRecipe { public void Run() throws InterruptedException { try { //region 临时屏蔽实时数据 -// for (String item : ConfigName.getInstance().PLC_Address.keySet()) { -// String key = item; -// BPA_PLCADDRESS plcaddress = ConfigName.getInstance().PLC_Address.get(item); -// if (plcaddress.isread == 1) { -// Object val = ReadPLC(key); -// ListeningValue.put(key, val); -// } -// } + for (String item : ConfigName.getInstance().PLC_Address.keySet()) { + String key = item; + BPA_PLCADDRESS plcaddress = ConfigName.getInstance().PLC_Address.get(item); + if (plcaddress.isread == 1) { + //Object val = ReadPLC(key); + Object val=null; + if (plcaddress.address.contains("M")){ + val=PLCControl.get().readPLC(key,Boolean.class); + }else if(plcaddress.address.contains("D")){ + val=PLCControl.get().readPLC(key,Short.class); + } + if (val!=null){ + ListeningValue.put(key, val); + } + } + } //endregion - + isTimeOut=PLCControl.get().isTimeOut(); + if (isTimeOut){ + ModbusMaster.get().IsConnected=false; + } + PLCControl.get().clrHeartbeat(null); if(!IsMakeGood) { //1.读取扫码数据 @@ -276,9 +288,12 @@ public class ExecuteTheRecipe { // OutletWeigh=zl==null?0:(int)zl; OutletWeigh=ScaleDevice.get().getCurrentWeight(); + Object bz= ExecuteTheRecipe.ReadPLC("出料口检测"); IsCup=bz==null?false:(boolean) bz; - Object obj1= ExecuteTheRecipe.ReadPLC("水池温度"); +// Object obj1= ExecuteTheRecipe.ReadPLC("水池温度"); +// WaterTemp=obj1==null?0:(int)obj1; + Object obj1= PLCControl.get().readPLC("清洗温度", Short.class); WaterTemp=obj1==null?0:(int)obj1; if (ConfigName.getInstance().versionSelectionEnum.equals("奶茶机")) diff --git a/app/src/main/java/com/bonait/bnframework/business/deviceControl/PLCControl.java b/app/src/main/java/com/bonait/bnframework/business/deviceControl/PLCControl.java index f2cced57..c6f849c7 100644 --- a/app/src/main/java/com/bonait/bnframework/business/deviceControl/PLCControl.java +++ b/app/src/main/java/com/bonait/bnframework/business/deviceControl/PLCControl.java @@ -5,7 +5,6 @@ import android.os.Looper; import com.bonait.bnframework.common.constant.ConfigName; import com.bonait.bnframework.common.db.mode.BPA_PLCADDRESS; -import com.bonait.bnframework.common.helper.I.IReadCallBack; import com.bonait.bnframework.common.helper.I.IWriteCallBack; import com.bonait.bnframework.common.modbus.ModbusMaster; import com.bonait.bnframework.common.utils.ToastUtils; @@ -45,25 +44,44 @@ public class PLCControl{ } - public TDataType readPLC(String address,Class dataType, IReadCallBack callBack) { - if (Boolean.class.equals(dataType)) { - - return dataType.cast(true); - } else if (Short.class.equals(dataType)) { - - - return dataType.cast((short) 0); - } else if (Integer.class.equals(dataType)) { - - - return dataType.cast(0); - } else if (Float.class.equals(dataType)) { - - return dataType.cast(0.0f); - } else { - // 其他类型的处理逻辑 - return null; // 默认返回 null + /**读取PLC + * @param address 变量的tag,如心跳时间、通道1开关。 + * @param dataType 数据类型的值就行,如读取bool就true或者false。读取浮点数就是0.0f等 + * */ + public Object readPLC(String address,Class dataType) { + final Object[] results= {null}; + if (ModbusMaster.get().IsConnected && ConfigName.PLC_Address.containsKey(address)) { + String addr = ConfigName.PLC_Address.get(address).address; + if (!addr.isEmpty() && getModAddr(addr) != -1) { + try { + String startAddr = String.valueOf(getModAddr(addr)); + if (Boolean.class.equals(dataType)) { + ModbusMaster.get().ReadBool(startAddr, 1, slaveId, val -> { + results[0] = val[0]; + }); + } else if (Short.class.equals(dataType)) { + ModbusMaster.get().ReadShort(startAddr, 1, slaveId, val -> { + results[0] = val[0]; + }); + } else if (Integer.class.equals(dataType)) { + ModbusMaster.get().ReadInt(startAddr, 1, slaveId, val -> { + results[0] = val[0]; + }); + } else if (Float.class.equals(dataType)) { + ModbusMaster.get().ReadFloat(startAddr, 1, slaveId, val -> { + results[0] = val[0]; + }); + return dataType.cast(0.0f); + } else { + // 其他类型的处理逻辑 + return null; // 默认返回 null + } + } catch (Exception ex) { + ToastUtils.error("ReadPLC PLC error:"+ex.getMessage()); + } + } } + return results[0]; } diff --git a/app/src/main/java/com/bonait/bnframework/business/deviceControl/PLCControllerTest.java b/app/src/main/java/com/bonait/bnframework/business/deviceControl/PLCControllerTest.java index 4674c625..68b79c92 100644 --- a/app/src/main/java/com/bonait/bnframework/business/deviceControl/PLCControllerTest.java +++ b/app/src/main/java/com/bonait/bnframework/business/deviceControl/PLCControllerTest.java @@ -1,5 +1,7 @@ package com.bonait.bnframework.business.deviceControl; +import android.util.Log; + class PLCControllerTest { @org.junit.jupiter.api.Test @@ -7,4 +9,11 @@ class PLCControllerTest { //PLCController controller=new PLCController(); //controller.WritePLC("300",122.2f,null); } + + @org.junit.jupiter.api.Test + void readPLC() { + Object result= PLCControl.get() + .readPLC("心跳时间",Short.class); + Log.i("TestOutput", "readPLC: "+result); + } } \ No newline at end of file