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 e044727..986a153 100644 --- a/app/src/main/java/com/example/bpa/helper/ModbusTcpServer.java +++ b/app/src/main/java/com/example/bpa/helper/ModbusTcpServer.java @@ -295,10 +295,11 @@ public class ModbusTcpServer { try { ReadHoldingRegistersResponse res = ModbusTcpHelper.get().syncReadHoldingRegisters(1, add, length); byte[] data = res.getData(); - + byte[] data1= Arrays.copyOfRange(data, 0, 36); + //36 GUID 18 Number String id=""; try { - id = new String(data, "UTF-8"); + id = new String(data1, "UTF-8").trim(); } catch (UnsupportedEncodingException ex) { } if(callback!=null) callback.onSuccess(id); @@ -314,6 +315,36 @@ public class ModbusTcpServer { MessageLog.ShowError("ReadShort onFailure,Address=" + Address + ",length=" + length + ",msg:" + e.toString()); } } + + public void WriteString(String Address, int length, IWriteCallBack callback) { + int add = GetAddress(Address); + if (add < 0) return; + short[] send = new short[length]; + for (short item:send) + { + item=0; + } + try { + ModbusTcpHelper.get().syncWriteRegisters(1, add, send); + if(callback!=null) + callback.onSuccess(); + } catch (InterruptedException e) { + MessageLog.ShowError("WriteShort onFailure,Address=" + Address + ",length=" + length + ",msg:" + e.toString()); + if(callback!=null)callback.onFailure(e.toString()); + } catch (ExecutionException e) { + MessageLog.ShowError("WriteShort onFailure,Address=" + Address + ",length=" + length + ",msg:" + e.toString()); + if(callback!=null)callback.onFailure(e.toString()); + } catch (ModbusTransportException e) { + MessageLog.ShowError("WriteShort onFailure,Address=" + Address + ",length=" + length + ",msg:" + e.toString()); + if(callback!=null)callback.onFailure(e.toString()); + } catch (ModbusInitException e) { + MessageLog.ShowError("WriteShort onFailure,Address=" + Address + ",length=" + length + ",msg:" + e.toString()); + if(callback!=null)callback.onFailure(e.toString()); + } catch (ModbusRespException e) { + MessageLog.ShowError("WriteShort onFailure,Address=" + Address + ",length=" + length + ",msg:" + e.toString()); + if(callback!=null)callback.onFailure(e.toString()); + } + } public void ReadShort(String Address, int length, IReadCallBack callback) { int add = GetAddress(Address); if (add < 0) return; 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 c728673..e850da6 100644 --- a/app/src/main/java/com/example/bpa/service/DeviceData.java +++ b/app/src/main/java/com/example/bpa/service/DeviceData.java @@ -120,14 +120,15 @@ public class DeviceData { //读取当前扫码信息 ModbusTcpServer.get().ReadString("VW342", 20, (data) -> { - MessageLog.ShowInfo("当前扫码信息:"+data); + if(!Code.equals(data)) { - Code=data; - if(OnScanTheCodeInformationT!=null) + if(!data.isEmpty() && OnScanTheCodeInformationT!=null) { - OnScanTheCodeInformationT.Run(Code); + MessageLog.ShowInfo("当前扫码信息:"+data); + OnScanTheCodeInformationT.Run(data); } + Code=data; } //1.查询订单信息 //2.展示订单界面 @@ -875,5 +876,10 @@ public class DeviceData { ModbusTcpServer.get().WriteShort("VW254", value, callback); } - + /** + * 扫码清零 + */ + public void setCodeClear(IWriteCallBack callback) { + ModbusTcpServer.get().WriteString("VW342", 20, callback); + } } diff --git a/app/src/main/java/com/example/bpa/service/ScanCodeServer.java b/app/src/main/java/com/example/bpa/service/ScanCodeServer.java index 53b81a3..5bf62ab 100644 --- a/app/src/main/java/com/example/bpa/service/ScanCodeServer.java +++ b/app/src/main/java/com/example/bpa/service/ScanCodeServer.java @@ -3,6 +3,7 @@ package com.example.bpa.service; 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.app.Main; import com.example.bpa.helper.MessageLog; import com.example.bpa.helper.ModbusTcpServer; @@ -46,6 +47,15 @@ public class ScanCodeServer { //4.回馈订单信息 Code=s; Main.getInstance().GetOrder(Code); + DeviceData.Get().setCodeClear(new IWriteCallBack() { + @Override + public void onSuccess() { + } + @Override + public void onFailure(String ErrorMsg) { + } + }); + } }; }