浏览代码

修改PLC读取的方法。完善手动写入值界面的功能。

NewNaiChaPeiLiaoBaTai
Nah 11 个月前
父节点
当前提交
025f0510d5
共有 3 个文件被更改,包括 71 次插入29 次删除
  1. +25
    -10
      app/src/main/java/com/bonait/bnframework/business/ExecuteTheRecipe.java
  2. +37
    -19
      app/src/main/java/com/bonait/bnframework/business/deviceControl/PLCControl.java
  3. +9
    -0
      app/src/main/java/com/bonait/bnframework/business/deviceControl/PLCControllerTest.java

+ 25
- 10
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("奶茶机"))


+ 37
- 19
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> TDataType readPLC(String address,Class<TDataType> 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 <TDataType> Object readPLC(String address,Class<TDataType> 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];
}




+ 9
- 0
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);
}
}

正在加载...
取消
保存