From 974635dcd96c3d8b80e862297c6ecd9f2dba9db3 Mon Sep 17 00:00:00 2001 From: fyf <11621@LAPTOP-04QQU0AO> Date: Tue, 6 Jun 2023 16:05:31 +0800 Subject: [PATCH] =?UTF-8?q?=E7=8E=B0=E5=9C=BA=E8=B0=83=E8=AF=95=E6=94=B9?= =?UTF-8?q?=E5=8A=A81003333?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/example/bpa/helper/ByteHelper.java | 6 +- .../example/bpa/helper/ModbusTcpServer.java | 34 +++++++++- .../com/example/bpa/service/DeviceData.java | 30 ++++++++- .../bpa/view/fragment/SsjkFragment.java | 65 ++++++++++++++++++- 4 files changed, 129 insertions(+), 6 deletions(-) diff --git a/app/src/main/java/com/example/bpa/helper/ByteHelper.java b/app/src/main/java/com/example/bpa/helper/ByteHelper.java index 9e0f967..2b30c08 100644 --- a/app/src/main/java/com/example/bpa/helper/ByteHelper.java +++ b/app/src/main/java/com/example/bpa/helper/ByteHelper.java @@ -127,6 +127,10 @@ public class ByteHelper { } return byteArray; } - + //b为传入的字节,i为第几位(范围0-7),如要获取bit0,则i=0 + public static int getBit(byte b,int i) { + int bit = (int) ((b >> i) & 0x1); + return bit; + } } diff --git a/app/src/main/java/com/example/bpa/helper/ModbusTcpServer.java b/app/src/main/java/com/example/bpa/helper/ModbusTcpServer.java index 986a153..591d959 100644 --- a/app/src/main/java/com/example/bpa/helper/ModbusTcpServer.java +++ b/app/src/main/java/com/example/bpa/helper/ModbusTcpServer.java @@ -284,7 +284,39 @@ public class ModbusTcpServer { } /*** - *读取整形数据 + *读取实时状态 + * @param Address the address + * @param length 读取的长度 3 + * @param callback 读取成功的回调 + */ + public void ReadStatus(String Address, int length, IReadCallBack callback) { + int add = GetAddress(Address); + if (add < 0) return; + try { + ReadHoldingRegistersResponse res = ModbusTcpHelper.get().syncReadHoldingRegisters(1, add, length); + byte[] data = res.getData(); + byte[] tempData=new byte[6]; + tempData[0]=data[1]; + tempData[1]=data[0]; + tempData[2]=data[3]; + tempData[3]=data[2]; + tempData[4]=data[5]; + tempData[5]=data[4]; + if(callback!=null) callback.onSuccess(tempData); + } 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()); + } + } + /*** + *读取扫码数据 * @param Address the address * @param length 读取的长度 * @param callback 读取成功的回调 diff --git a/app/src/main/java/com/example/bpa/service/DeviceData.java b/app/src/main/java/com/example/bpa/service/DeviceData.java index e850da6..0e380d6 100644 --- a/app/src/main/java/com/example/bpa/service/DeviceData.java +++ b/app/src/main/java/com/example/bpa/service/DeviceData.java @@ -4,6 +4,7 @@ import com.example.bpa.Model.IRun; import com.example.bpa.Model.IRunT; import com.example.bpa.Model.IThread; import com.example.bpa.Model.IWriteCallBack; +import com.example.bpa.helper.ByteHelper; import com.example.bpa.helper.MessageLog; import com.example.bpa.helper.ModbusTcpServer; import com.example.bpa.helper.RTrig; @@ -108,6 +109,11 @@ public class DeviceData { WeightCalibrationMode = booleans[0]; }); + ModbusTcpServer.get().ReadBool("M6.5", 1, booleans -> { + WeightCalibrationMode = booleans[0]; + }); + //IoStatus + //读取1号果糖机按钮状态 ModbusTcpServer.get().ReadBool("M51.7", 1, booleans -> { ReheatSwitch1 = booleans[0]; @@ -136,6 +142,17 @@ public class DeviceData { //4.回馈订单信息 }); + //读取实时状态 + ModbusTcpServer.get().ReadStatus("VW200", 3, data -> { + for (int i = 0; i < data.length; i++) { + byte status=data[i];//0x92 + IoStatus.put(i*4+1, ByteHelper.getBit(status,0)==1); + IoStatus.put(i*4+2, ByteHelper.getBit(status,1)==1); + IoStatus.put(i*4+3, ByteHelper.getBit(status,2)==1); + IoStatus.put(i*4+4, ByteHelper.getBit(status,3)==1); + } + }); + //配料完成 M0.3 CompleteListen("M0.3", "配料完成", OnChargeMixtureCompleteNotify); @@ -856,7 +873,18 @@ public class DeviceData { /** * 实时状态 */ - ConcurrentHashMap IoStatus = new ConcurrentHashMap(); + public ConcurrentHashMap IoStatus = new ConcurrentHashMap(); + /** + * 获取实时状态 + * @param ch + * @return + */ + public boolean getConcurrentHash(int ch) { + if (!IoStatus.containsKey(ch)) { + return false; + } + return IoStatus.get(ch); + } //endregion /** * 设置8路称校准砝码重量 diff --git a/app/src/main/java/com/example/bpa/view/fragment/SsjkFragment.java b/app/src/main/java/com/example/bpa/view/fragment/SsjkFragment.java index b070d5c..917e741 100644 --- a/app/src/main/java/com/example/bpa/view/fragment/SsjkFragment.java +++ b/app/src/main/java/com/example/bpa/view/fragment/SsjkFragment.java @@ -9,14 +9,20 @@ import androidx.fragment.app.FragmentTransaction; import androidx.recyclerview.widget.LinearLayoutManager; import androidx.recyclerview.widget.RecyclerView; +import android.app.Activity; +import android.content.Context; +import android.content.ContextWrapper; import android.os.Bundle; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import com.example.bpa.R; +import com.example.bpa.config.ConfigName; import com.example.bpa.config.DataBus; +import com.example.bpa.helper.MessageLog; import com.example.bpa.helper.WrapContentLinearLayoutManager; +import com.example.bpa.service.DeviceData; import com.example.bpa.view.adapter.devstatus_adapter; import com.example.bpa.view.adapter.maingoods_adapter; import com.example.bpa.view.adapter.makegood_adapter; @@ -24,12 +30,14 @@ import com.example.bpa.view.control.MyLayoutManager; import com.example.bpa.view.fragment.setting.mqttparameter; import com.example.bpa.view.fragment.setting.ovarparameter; import com.example.bpa.view.fragment.setting.systemparameter; +import com.example.bpa.view.from.lsjy_activity; import com.example.bpa.view.mode.StatusMode; import java.util.ArrayList; public class SsjkFragment extends Fragment implements View.OnClickListener { + Activity activity=null; View view; RecyclerView recyclerView; @@ -52,9 +60,12 @@ public class SsjkFragment extends Fragment implements View.OnClickListener { recyclerView.setLayoutManager(layout); adapter = new devstatus_adapter(getContext()); recyclerView.setAdapter(adapter); + + activity=findActivity(view.getContext()); } private void initEvent() { + // } /** @@ -64,19 +75,19 @@ public class SsjkFragment extends Fragment implements View.OnClickListener { { try{ DataBus.getInstance().statusModes=new ArrayList<>(); - DataBus.getInstance().statusModes.add(new StatusMode("杯子检测",R.mipmap.gd1,R.mipmap.gd2,true)); + DataBus.getInstance().statusModes.add(new StatusMode("杯子检测",R.mipmap.gd1,R.mipmap.gd2,false)); DataBus.getInstance().statusModes.add(new StatusMode("蠕动泵(一)",R.mipmap.rdl1,R.mipmap.rdl2,false)); DataBus.getInstance().statusModes.add(new StatusMode("蠕动泵(二)",R.mipmap.rdl1,R.mipmap.rdl2,false)); DataBus.getInstance().statusModes.add(new StatusMode("蠕动泵(三)",R.mipmap.rdl1,R.mipmap.rdl2,false)); DataBus.getInstance().statusModes.add(new StatusMode("蠕动泵(四)",R.mipmap.rdl1,R.mipmap.rdl2,false)); DataBus.getInstance().statusModes.add(new StatusMode("蠕动泵(五)",R.mipmap.rdl1,R.mipmap.rdl2,false)); - DataBus.getInstance().statusModes.add(new StatusMode("果糖泵(左)",R.mipmap.rdl1,R.mipmap.rdl2,true)); + DataBus.getInstance().statusModes.add(new StatusMode("果糖泵(左)",R.mipmap.rdl1,R.mipmap.rdl2,false)); DataBus.getInstance().statusModes.add(new StatusMode("果糖泵(右)",R.mipmap.rdl1,R.mipmap.rdl2,false)); DataBus.getInstance().statusModes.add(new StatusMode("离心泵(一)",R.mipmap.lxl1,R.mipmap.lxl2,false)); DataBus.getInstance().statusModes.add(new StatusMode("离心泵(二)",R.mipmap.lxl1,R.mipmap.lxl2,false)); DataBus.getInstance().statusModes.add(new StatusMode("离心泵(三)",R.mipmap.lxl1,R.mipmap.lxl2,false)); - DataBus.getInstance().statusModes.add(new StatusMode("离心泵(四)",R.mipmap.lxl1,R.mipmap.lxl2,true)); + DataBus.getInstance().statusModes.add(new StatusMode("离心泵(四)",R.mipmap.lxl1,R.mipmap.lxl2,false)); DataBus.getInstance().statusModes.add(new StatusMode("离心泵(五)",R.mipmap.lxl1,R.mipmap.lxl2,false)); DataBus.getInstance().statusModes.add(new StatusMode("出水阀",R.mipmap.csf1,R.mipmap.csf2,false)); @@ -97,8 +108,56 @@ public class SsjkFragment extends Fragment implements View.OnClickListener { } } + public void SetStatus(int i,Boolean bool) + { + if(DataBus.getInstance().statusModes.size()>=i) + { + StatusMode mode= DataBus.getInstance().statusModes.get(i); + mode.Status=bool; + DataBus.getInstance().statusModes.set(i,mode); + } + } @Override public void onClick(View v) { } + private Activity findActivity(@NonNull Context context) { + if (context instanceof Activity) { + return (Activity) context; + } else if (context instanceof ContextWrapper) { + return findActivity(((ContextWrapper) context).getBaseContext()); + } else { + return null; + } + } + /** + * 实时显示线程 + */ + public void Run() { + new Thread(new Runnable() { + @Override + public void run() { + while (DataBus.getInstance().PlcIsConnect) { + try { + if(activity!=null) + { + activity.runOnUiThread(new Runnable() { + @Override + public void run() { + try { + // SetStatus(0,DeviceData.getConcurrentHash(1)); + } catch (Exception e) { + MessageLog.ShowInfo("状态显示解析异常!"+e.getMessage()); + } + } + }); + } + Thread.sleep(500); + } catch (InterruptedException e) { + throw new RuntimeException(e); + } + } + } + }).start(); + } } \ No newline at end of file