diff --git a/.idea/gradle.xml b/.idea/gradle.xml index a2d7c213..56d6814d 100644 --- a/.idea/gradle.xml +++ b/.idea/gradle.xml @@ -7,6 +7,7 @@ + diff --git a/app/build.gradle b/app/build.gradle index e1d21f56..875581ed 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -1,13 +1,12 @@ apply plugin: 'com.android.application' android { - compileSdkVersion rootProject.ext.compileSdkVersion - buildToolsVersion rootProject.ext.buildToolsVersion + compileSdk rootProject.ext.compileSdkVersion defaultConfig { applicationId "com.bonait.bnframework" - minSdkVersion rootProject.ext.minSdkVersion - targetSdkVersion rootProject.ext.targetSdkVersion + minSdk rootProject.ext.minSdkVersion + targetSdk rootProject.ext.targetSdkVersion versionCode rootProject.ext.versionCode versionName rootProject.ext.versionName testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" @@ -29,6 +28,7 @@ android { sourceCompatibility JavaVersion.VERSION_1_8 targetCompatibility JavaVersion.VERSION_1_8 } + buildToolsVersion rootProject.ext.buildToolsVersion } dependencies { @@ -97,9 +97,9 @@ dependencies { implementation files('libs\\org.eclipse.paho.client.mqttv3-1.2.5.jar') //Modbus - // implementation 'com.github.licheedev:Modbus4Android:2.0.2' + implementation 'com.github.licheedev:Modbus4Android:2.0.2' -// //下载进度条 - // implementation 'com.github.Ccapton:Android-ColorfulProgressBar:1.0.5' + //下载进度条 + implementation 'com.github.Ccapton:Android-ColorfulProgressBar:1.0.5' } diff --git a/app/libs/release_2.0.2_modbus4j-2.0.2.jar b/app/libs/release_2.0.2_modbus4j-2.0.2.jar deleted file mode 100644 index 4aca47b9..00000000 Binary files a/app/libs/release_2.0.2_modbus4j-2.0.2.jar and /dev/null differ diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index 570a1962..ba51d460 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -8,7 +8,7 @@ - + diff --git a/app/src/main/java/com/bonait/bnframework/MainApplication.java b/app/src/main/java/com/bonait/bnframework/MainApplication.java index aea7a0e3..7cc7c587 100644 --- a/app/src/main/java/com/bonait/bnframework/MainApplication.java +++ b/app/src/main/java/com/bonait/bnframework/MainApplication.java @@ -4,7 +4,19 @@ import android.annotation.SuppressLint; import android.app.Activity; import android.app.Application; import android.content.Context; - +import android.util.Log; + +import com.bonait.bnframework.business.ConfigData; +import com.bonait.bnframework.common.constant.ConfigName; +import com.bonait.bnframework.common.db.QueryDB; +import com.bonait.bnframework.common.db.mode.BPA_ALERTLOG; +import com.bonait.bnframework.common.db.mode.BPA_LOG; +import com.bonait.bnframework.common.db.res.AlertLogEnum; +import com.bonait.bnframework.common.db.res.UserLogEnum; +import com.bonait.bnframework.common.helper.CrashHandler; +import com.bonait.bnframework.common.helper.I.IMessageLogNotify; +import com.bonait.bnframework.common.helper.MessageLog; +import com.bonait.bnframework.common.helper.SdCart; import com.bonait.bnframework.manager.ActivityLifecycleManager; import com.bonait.bnframework.common.constant.Constants; import com.bonait.bnframework.common.notification.MainNotification; @@ -45,16 +57,17 @@ public class MainApplication extends Application { super.onCreate(); context = getApplicationContext(); //0.全局异常捕获,保存为SD卡本级目录 crash -// CrashHandler.getInstance().init(this); -// -// //1.设置程序active,初始化Main函数进程,初始化消息日志 -// InitMsgLog();ConfigName.getInstance().dishesCon = this; -// -// //2.初始化SD卡,数据库DB -// SdCart.getInstance().initSD(); -// -// //4.初始化Main -// ConfigData.getInstance(); + CrashHandler.getInstance().init(this); + + //1.设置程序active,初始化Main函数进程,初始化消息日志 + InitMsgLog(); + ConfigName.getInstance().dishesCon = this; + + //2.初始化SD卡,数据库DB + SdCart.getInstance().initSD(); + + //4.初始化Main + ConfigData.getInstance(); // activity生命周期管理 ActivityLifecycleManager.get().init(this); @@ -84,7 +97,64 @@ public class MainApplication extends Application { } //========================================================================// + /** + * 初始化消息日志接收打印 + */ + public void InitMsgLog(){ + MessageLog.MsgNotify = new IMessageLogNotify() { + @Override + public void ErrorMsg(String msg) { + Log.e("Error", msg); + } + @Override + public void InfoMsg(String msg) { + Log.i("Info", msg); + } + @Override + public void WarnMsg(String msg) { + Log.w("Warn", msg); + } + @Override + public void UserMsg(UserLogEnum type, String msg) { + BPA_LOG log = new BPA_LOG(); + log.userID = ConfigName.getInstance().user.userID; + switch (type.toString()) + { + case "登录日志":log.type=1; + break; + case "角色操作日志":log.type=2; + break; + case "数据接收":log.type=3; + break; + case "上传日志":log.type=4; + break; + case "订单处理日志":log.type=5; + break; + } + log.text = msg; + QueryDB.Addlog(log); + } + @Override + public void AlertMsg(AlertLogEnum type, String msg) { + BPA_ALERTLOG log = new BPA_ALERTLOG(); + log.userID = ConfigName.getInstance().user.userID; + switch (type.toString()) + { + case "异常订单未制作日志":log.type=1; + break; + case "料仓缺料日志":log.type=2; + break; + case "传感器异常日志":log.type=3; + break; + case "其他":log.type=4; + break; + } + log.text = msg; + QueryDB.AddAlertlog(log); + } + }; + } /** * 内存泄漏检测,根据flag来判断要不要初始化 */ diff --git a/app/src/main/java/com/bonait/bnframework/business/DeviceData.java b/app/src/main/java/com/bonait/bnframework/business/DeviceData.java new file mode 100644 index 00000000..587f2d5f --- /dev/null +++ b/app/src/main/java/com/bonait/bnframework/business/DeviceData.java @@ -0,0 +1,920 @@ +package com.bonait.bnframework.business; +import com.bonait.bnframework.common.helper.ByteHelper; +import com.bonait.bnframework.common.helper.I.IRun; +import com.bonait.bnframework.common.helper.I.IRunT; +import com.bonait.bnframework.common.helper.I.IThread; +import com.bonait.bnframework.common.helper.I.IWriteCallBack; +import com.bonait.bnframework.common.helper.MessageLog; +import com.bonait.bnframework.common.helper.RTrig; +import com.bonait.bnframework.common.helper.ThreadManager; +import com.bonait.bnframework.common.modbus.ModbusTcpServer; +import com.bonait.bnframework.common.helper.ByteHelper; +import com.bonait.bnframework.common.helper.MessageLog; +import com.bonait.bnframework.common.helper.RTrig; +import com.bonait.bnframework.common.helper.ThreadManager; + +import java.util.concurrent.ConcurrentHashMap; +import java.util.concurrent.ConcurrentLinkedQueue; + +public class DeviceData { + private static volatile DeviceData _instance; + + public static DeviceData Get() { + if (_instance == null) + _instance = new DeviceData(); + return _instance; + + } + + + //----------------------------------------------------------------Start 模拟测试 ---------------------------------------------------------------- + ConcurrentLinkedQueue testComms = new ConcurrentLinkedQueue<>(); + + public void testAddComm(int value) { + testComms.offer(value); + } + + public void TestInit() { + ThreadManager.Get().StartLong("TestInit", true, new IThread() { + @Override + public void Run() throws InterruptedException { + while (testComms.size() > 0) { + Thread.sleep(5000); + if (OnChargeMixtureComNotPar != null) { + OnChargeMixtureComNotPar.Run(testComms.poll()); + } + } + Thread.sleep(100); + } + + @Override + public void RunComplete() throws InterruptedException { + + } + }); + ; + } + //----------------------------------------------------------------End 模拟测试 ---------------------------------------------------------------- + + //region 初始化 + public void Init() { + ThreadManager.Get().StartLong("Plc设备数据监听", true, new IThread() { + @Override + public void Run() throws InterruptedException { + + //获取校准值-增加两个果糖通道 + ModbusTcpServer.get().ReadShort("VW100", 12, (shorts) -> { + for (int i = 0; i < shorts.length; i++) { + CalibrationValue.put(i + 1, shorts[i]); + } + }); + + //获取称当前重量 + ModbusTcpServer.get().ReadInt("VD260", 9, ints -> { + for (int i = 0; i < ints.length; i++) { + CallCurrentWeight.put(i + 1, ints[i]); + } + }); + + //获取校准基准时间 + ModbusTcpServer.get().ReadInt("VD124", 1, ints -> { + CalibrationReferenceTime = ints[0]; + }); + + //获取温度参数 + ModbusTcpServer.get().ReadInt("VD310", 3, ints -> { + FructoseTemperature1=ints[0]; + FructoseTemperature2=ints[1]; + WashTheTankTemperature=ints[2]; + }); + + //获取温度上下限 + ModbusTcpServer.get().ReadInt("VD388", 6, ints -> { + FructoseTemperature1_up=ints[0]; + FructoseTemperature1_down=ints[1]; + FructoseTemperature2_up=ints[2]; + FructoseTemperature2_down=ints[3]; + WashTheTankTemperature_up=ints[4]; + WashTheTankTemperature_down=ints[5]; + }); + + //获取清洗参数 + ModbusTcpServer.get().ReadShort("VW140", 5, shorts -> { + DrainageTime = shorts[0]; + AddCleaningAgentTime = shorts[1]; + InletTime = shorts[2]; + CyclicCleaningTime = shorts[3]; + CleaningAndHeatingTime = shorts[4]; + }); + + //获取清洗参数 + ModbusTcpServer.get().ReadBool("M6.5", 1, booleans -> { + 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]; + }); + + //读取2号果糖机按钮状态 + ModbusTcpServer.get().ReadBool("M52.0", 1, booleans -> { + ReheatSwitch2 = booleans[0]; + }); + + //读取当前扫码信息 + ModbusTcpServer.get().ReadString("VW342", 20, (data) -> { + + if(!Code.equals(data)) + { + if(!data.isEmpty() && OnScanTheCodeInformationT!=null) + { + MessageLog.ShowInfo("当前扫码信息:"+data); + OnScanTheCodeInformationT.Run(data); + } + Code=data; + } + //1.查询订单信息 + //2.展示订单界面 + //3.制作订单信息 + //4.回馈订单信息 + }); + + //读取实时状态 + ModbusTcpServer.get().ReadStatus("VW382", 3, data -> { + for (int i = 0; i < data.length; i++) { + byte status=data[i];//0x92 + IoStatus.put(i*8+0, ByteHelper.getBit(status,0)==1); + IoStatus.put(i*8+1, ByteHelper.getBit(status,1)==1); + IoStatus.put(i*8+2, ByteHelper.getBit(status,2)==1); + IoStatus.put(i*8+3, ByteHelper.getBit(status,3)==1); + IoStatus.put(i*8+4, ByteHelper.getBit(status,4)==1); + IoStatus.put(i*8+5, ByteHelper.getBit(status,5)==1); + IoStatus.put(i*8+6, ByteHelper.getBit(status,6)==1); + IoStatus.put(i*8+7, ByteHelper.getBit(status,7)==1); + } + }); + + //配料完成 M0.3 + CompleteListen("M0.3", "配料完成", OnChargeMixtureCompleteNotify); + + //清洗完成 M0.6 + CompleteListen("M0.6", "清洗完成", OnCleaningCompleteNotify); + + //去皮完成 M1.3 + CompleteListen("M1.3", "去皮完成", OnPeelingCompleteNotify); + + //扫码信号 M1.5 + //CompleteListen("M1.5", "扫码信号", OnScanTheCodeInformation); + + Thread.sleep(10); + } + + @Override + public void RunComplete() throws InterruptedException { + + } + }); + } + //endregion + /** + * 扫码信息 + */ + public String Code=""; + //region 完成信号监听 + + /** + * 完成信号监听 + * + * @param add 监听地址 + * @param name 监听 KEY + * @param callback 执行委托 + */ + private void CompleteListen(String add, String name, IRun callback) { + ModbusTcpServer.get().ReadBool(add, 1, booleans -> { + RTrig.get(name).Start(booleans[0], () -> { + if (callback != null) + callback.Run(); + }); + }); + } + //endregion + + // region 通知接口 + /** + * 配料完成通知 + */ + public IRun OnChargeMixtureCompleteNotify; + + /** + * 通道配料完成通知 + */ + public IRunT OnChargeMixtureComNotPar; + + /** + * 清洗完成通知 + */ + public IRun OnCleaningCompleteNotify; + + /** + * 去皮完成 + */ + public IRun OnPeelingCompleteNotify; + + /** + * 接收到扫码信息 + */ + public IRun OnScanTheCodeInformation; + /** + * 接收到扫码信息,返回 + */ + public IRunT OnScanTheCodeInformationT; + //endregion + + /** + * 校准值写入 + * PLC 地址VW100--VW118 + * + * @param value 需要写入的值 + * @param ch 需要写入的通道编号,1--10 + */ + public void setCalibrationValue(short value, int ch, IWriteCallBack callback) { + if (ch >= 1 && ch <= 12) { + String add = "VW" + (98 + ch * 2); + 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 + * + * @param value 需要写入的值 + * @param ch 需要写入的通道编号,1--10 + */ + 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, callback); + }else if(ch>=11 && ch<=12)//果糖需求 + { + String add = "VW" + (198 +20+ ch * 2); + ModbusTcpServer.get().WriteShort(add, value, callback); + } + } + + /** + * 设置通道校准开关 + * ,PLC 地址 M2.0--M3.1 + * + * @param value 需要写入的值 + * @param ch 需要写入的通道编号,1--10 + */ + public void setChCalibrationSwitch(boolean value, int ch, IWriteCallBack callback) { + if (ch >= 1 && ch <= 12) { + String add=""; + if(ch==9) + { + add="M03.0"; + }else if(ch==10) + { + add="M03.1"; + }else if(ch==11)//果糖1 + { + add="M03.2"; + }else if(ch==12)//果糖2 + { + add="M03.3"; + }else + { + add = ModbusTcpServer.get().getBitSingleAdd("M", 2, ch); + } + + if (add.length() > 0) + ModbusTcpServer.get().WriteBool(add, value, callback); + } + } + + //region 手动控制 + /** + * 手动控制设备 + * @param value + * @param ch + * @param callback + */ + public void setSwitch(boolean value, String ch, IWriteCallBack callback) { + if(ch.isEmpty()) + { + return; + } + String add=ch; + if (add.length() > 0) + ModbusTcpServer.get().WriteBool(add, value, callback); + } + //endregion + + + /** + * 称去皮校准 + * + * @param value + * @param ch 设置称的通道号,ch1-ch8 + */ + 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, callback); + } + } + + /** + * 砝码值写入 + * + * @param value + * @param ch 设置称的砝码通道号,ch1-ch8 + */ + 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, callback); + } + } + + /** + * 重量清零 + */ + public void setWeightClear(IWriteCallBack callback) { + ModbusTcpServer.get().WriteBool("M6.0", true, callback); + } + + /** + * 关闭写保护 + */ + public void setCloseWriteProtect(IWriteCallBack callback) { + ModbusTcpServer.get().WriteBool("M6.1", true, callback); + } + + /** + * 打开写保护 + */ + public void setOpenWriteProtect(IWriteCallBack callback) { + ModbusTcpServer.get().WriteBool("M6.4", true, callback); + } + + /** + * 零点校准 + */ + public void setZeroPointCalibration(IWriteCallBack callback) { + ModbusTcpServer.get().WriteBool("M6.2", true, callback); + } + + /** + * 砝码值写入 + */ + public void setWeightValueWrite(IWriteCallBack callback) { + ModbusTcpServer.get().WriteBool("M6.3", true, callback); + } + + /** + * 砝码校准模式 + */ + boolean WeightCalibrationMode; + + /** + * 砝码校准模式 + */ + public void setWeightCalibrationMode(Boolean value, IWriteCallBack callback) { + ModbusTcpServer.get().WriteBool("M6.5", value, callback); + } + + /** + * 获取砝码校准模式 + * + * @return + */ + public boolean getWeightCalibrationMode() { + return WeightCalibrationMode; + } + + /** + * 启动通道校准 + */ + public void setChStartCalibration(IWriteCallBack callback) { + ModbusTcpServer.get().WriteBool("M0.0", true, callback); + } + + /** + * 启动配料 + */ + public void setChargeMixtureStart(IWriteCallBack callback) { + ModbusTcpServer.get().WriteBool("M0.1", true, callback); + } + + /** + * 手自动切换 + * + * @param value true:自动模式,flase:手动模式 + */ + public void setHandOrAutoSwitch(boolean value, IWriteCallBack callback) { + ModbusTcpServer.get().WriteBool("M0.2", value, callback); + } + + /** + * 手自动模式 + */ + boolean HandOrAutoMode; + + /** + * 获取手自动模式的状态 + * + * @return true:自动模式,flase:手动模式 + */ + public boolean getHandOrAutoSwitch() { + return HandOrAutoMode; + } + + /** + * 设置清洗模式 + * + * @param value true:清洗模式,false:配料模式 + */ + public void setCleaningMode(boolean value, IWriteCallBack callback) { + ModbusTcpServer.get().WriteBool("M0.4", value, callback); + } + + /** + * 清洗模式 + */ + boolean CleaningMode; + + /** + * 获取清洗模式 + * + * @return true:清洗模式,false:配料模式 + */ + public boolean getCleaningMode() { + return CleaningMode; + } + + /** + * 设置开始清洗 + */ + public void setStartCleaning(IWriteCallBack callback) { + ModbusTcpServer.get().WriteBool("M0.5", true, callback); + } + + /** + * 设置一键去皮 + */ + public void setOneClickPeeling(IWriteCallBack callback) { + ModbusTcpServer.get().WriteBool("M1.2", true, callback); + } + + /** + * 一键去皮完成状态 + */ + boolean OneClickPeelingComplete; + + /** + * 设置一键去皮完成状态 + * + * @param value + */ + public void setOneClickPeelingComplete(boolean value) { + OneClickPeelingComplete = value; + } + + /** + * 清洗完成 + */ + boolean CleaningComplete; + + /** + * 获取清洗完成的状态 + * + * @return + */ + public boolean getCleaningComplete() { + return CleaningComplete; + } + + //region 获取或设置-校准模式 + + /** + * 设置校准模式 + * + * @param value true:零点校准模式,false:清零去皮模式 + */ + public void setCalibrationMode(boolean value, IWriteCallBack callback) { + ModbusTcpServer.get().WriteBool("M1.4", value, callback); + } + + /** + * 校准模式 + */ + boolean CalibrationMode; + + /** + * 获取校准模式 + * + * @return true:零点校准模式,false:清零去皮模式 + */ + public boolean getCalibrationMode() { + return CalibrationMode; + } + + //endregion + + + + /** + * 称当前重量 + */ + ConcurrentHashMap CallCurrentWeight = new ConcurrentHashMap(); + + /** + * 获取指定称的当前重量 + * + * @param ch 需要获取的通道号,称1--称9 + * @return + */ + public int getCallCurrentWeight(int ch) { + if (!CallCurrentWeight.containsKey(ch)) { + return 0; + } + return CallCurrentWeight.get(ch); + } + + //region 获取或设置-校准基准时间 + + /** + * 设置校准基准时间 + * + * @param value 需要写入的值 + */ + public void setCalibrationReferenceTime(int value, IWriteCallBack callback) { + ModbusTcpServer.get().WriteInt("VD124", value, callback); + } + + /** + * 校准基准时间 + */ + int CalibrationReferenceTime=0; + + /** + * 获取校准基准时间 + * + * @return + */ + public int getCalibrationReferenceTime() { + return CalibrationReferenceTime; + } + + //endregion + + //region 获取或设置-排水时间 + + /** + * 设置排水时间 + * + * @param value + */ + public void setDrainageTime(short value, IWriteCallBack callback) { + ModbusTcpServer.get().WriteShort("VW140", value, callback); + } + + /** + * 排水时间 + */ + short DrainageTime; + + /** + * 获取排水时间 + * + * @return + */ + public short getDrainageTime() { + return DrainageTime; + } + + //endregion + + //region 获取或设置-清洗剂添加时间 + + /** + * 设置清洗剂添加时间 + * + * @param value + */ + public void setAddCleaningAgentTime(short value, IWriteCallBack callback) { + ModbusTcpServer.get().WriteShort("VW142", value, callback); + } + + /** + * 清洗剂添加时间 + */ + short AddCleaningAgentTime; + + /** + * 获取清洗剂添加时间 + * + * @return + */ + public short getAddCleaningAgentTime() { + return AddCleaningAgentTime; + } + + //endregion + + //region 获取或设置-进水时间 + + /** + * 设置进水时间 + * + * @param value + */ + public void setInletTime(short value, IWriteCallBack callback) { + ModbusTcpServer.get().WriteShort("VW144", value, callback); + } + + /** + * 进水时间 + */ + short InletTime; + + /** + * 获取进水时间 + * + * @return + */ + public short getInletTime() { + return InletTime; + } + + //endregion + + //region 获取或设置-循环清洗时间 + + /** + * 设置循环清洗时间 + * + * @param value + */ + public void setCyclicCleaningTime(short value, IWriteCallBack callback) { + ModbusTcpServer.get().WriteShort("VW146", value, callback); + } + + /** + * 循环清洗时间 + */ + short CyclicCleaningTime; + + /** + * 获取循环清洗时间 + * + * @return + */ + public short getCyclicCleaningTime() { + return CyclicCleaningTime; + } + //endregion + + //region 获取或设置-清洗加热时间 + + /** + * 设置清洗加热时间 + * + * @param value + */ + public void setCleaningAndHeatingTime(short value, IWriteCallBack callback) { + ModbusTcpServer.get().WriteShort("VW148", value, callback); + } + + /** + * 清洗加热时间 + */ + short CleaningAndHeatingTime; + + /** + * 获取清洗加热时间 + * + * @return + */ + public short getCleaningAndHeatingTime() { + return CleaningAndHeatingTime; + } + //endregion + + //region 获取温度信息 + /** + * 果糖1实际温度 + */ + Boolean ReheatSwitch1=false; + int FructoseTemperature1=0; + int FructoseTemperature1_up=0; + int FructoseTemperature1_down=0; + /** + * 设置果糖1号加热启动停止 + * + * @param value true:启动,flase:停止 + */ + public void setReheatSwitch1(boolean value, IWriteCallBack callback) { + ModbusTcpServer.get().WriteBool("M51.7", value, callback); + } + public boolean getReheatSwitch1() { + return ReheatSwitch1; + } + /** + * 设置获取果糖1上下线 + * @param value_up 上限 + * @param value_down 下限 + * @param callback + */ + public void setFructoseTemperature1(int value_up,int value_down, IWriteCallBack callback) { + ModbusTcpServer.get().WriteInt("VD388", value_up, callback); + ModbusTcpServer.get().WriteInt("VD392", value_down, callback); + } + /** + * 获取果糖1实际温度 + * + * @return + */ + public int getFructoseTemperature1() { + return FructoseTemperature1; + } + /** + * 获取果糖1上限 + * + * @return + */ + public int getFructoseTemperature1_up() { + return FructoseTemperature1_up; + } + /** + * 获取果糖1下限 + * + * @return + */ + public int getFructoseTemperature1_down() { + return FructoseTemperature1_down; + } + /** + * 果糖2实际温度 + */ + Boolean ReheatSwitch2=false; + int FructoseTemperature2=0; + int FructoseTemperature2_up=0; + int FructoseTemperature2_down=0; + /** + * 设置果糖2号加热启动停止 + * + * @param value true:启动,flase:停止 + */ + public void setReheatSwitch2(boolean value, IWriteCallBack callback) { + ModbusTcpServer.get().WriteBool("M52.0", value, callback); + } + public boolean getReheatSwitch2() { + return ReheatSwitch2; + } + /** + * 设置获取果糖2上下线 + * @param value_up 上限 + * @param value_down 下限 + * @param callback + */ + public void setFructoseTemperature2(int value_up,int value_down, IWriteCallBack callback) { + ModbusTcpServer.get().WriteInt("VD396", value_up, callback); + ModbusTcpServer.get().WriteInt("VD400", value_down, callback); + } + /** + * 获取果糖2实际温度 + * + * @return + */ + public int getFructoseTemperature2() { + return FructoseTemperature2; + } + /** + * 获取果糖2上限 + * + * @return + */ + public int getFructoseTemperature2_up() { + return FructoseTemperature2_up; + } + /** + * 获取果糖2下限 + * + * @return + */ + public int getFructoseTemperature2_down() { + return FructoseTemperature2_down; + } + /** + * 清洗池实际温度 + */ + int WashTheTankTemperature=0; + int WashTheTankTemperature_up=0; + int WashTheTankTemperature_down=0; + /** + * 设置清洗池实际温度上下线 + * @param value_up 上限 + * @param value_down 下限 + * @param callback + */ + public void setWashTheTankTemperature(int value_up,int value_down, IWriteCallBack callback) { + ModbusTcpServer.get().WriteInt("VD404", value_up, callback); + ModbusTcpServer.get().WriteInt("VD408", value_down, callback); + } + /** + * 清洗池实际温度 + * + * @return + */ + public int getWashTheTankTemperature() { + return WashTheTankTemperature; + } + /** + * 获取清洗池实际温度上限 + * + * @return + */ + public int getWashTheTankTemperature_up() { + return WashTheTankTemperature_up; + } + /** + * 获取清洗池实际温度下限 + * + * @return + */ + public int getWashTheTankTemperature_down() { + return WashTheTankTemperature_down; + } + //endregion + + //region 实时状态 + /** + * 实时状态 + */ + 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路称校准砝码重量 + * + * @param value + */ + public void setCalibrationWeight(short value, IWriteCallBack callback) { + ModbusTcpServer.get().WriteShort("VW250", value, callback); + } + + /** + * 设置称重9校准砝码重量 + * + * @param value + */ + public void setCalibrationWeight9(short value, IWriteCallBack callback) { + 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/bonait/bnframework/common/modbus/ModbusTcpHelper.java b/app/src/main/java/com/bonait/bnframework/common/modbus/ModbusTcpHelper.java new file mode 100644 index 00000000..47e0bea5 --- /dev/null +++ b/app/src/main/java/com/bonait/bnframework/common/modbus/ModbusTcpHelper.java @@ -0,0 +1,44 @@ +package com.bonait.bnframework.common.modbus; + +import com.licheedev.modbus4android.ModbusWorker; +import com.serotonin.modbus4j.ModbusMaster; + + +public class ModbusTcpHelper extends ModbusWorker { + private static volatile ModbusTcpHelper instance = null; + + public static ModbusTcpHelper get() { + ModbusTcpHelper manager = instance; + if (manager == null) { + synchronized (ModbusTcpHelper.class) { + manager = instance; + if (manager == null) { + manager = new ModbusTcpHelper(); + instance = manager; + } + } + } + return manager; + } + + private ModbusTcpHelper() { + } + + /** + * 释放整个ModbusManager,单例会被置null + */ + public synchronized void release() { + super.release(); + instance = null; + } + + /** + * 获取 + * @return + */ + public synchronized ModbusMaster getModbusMaster() + { + return super.getModbusMaster(); + } + +} diff --git a/app/src/main/java/com/bonait/bnframework/common/modbus/ModbusTcpServer.java b/app/src/main/java/com/bonait/bnframework/common/modbus/ModbusTcpServer.java new file mode 100644 index 00000000..17be14c2 --- /dev/null +++ b/app/src/main/java/com/bonait/bnframework/common/modbus/ModbusTcpServer.java @@ -0,0 +1,781 @@ +package com.bonait.bnframework.common.modbus; + + +import com.bonait.bnframework.common.constant.ConfigName; +import com.bonait.bnframework.common.constant.DataBus; +import com.bonait.bnframework.common.helper.DataFormat; +import com.bonait.bnframework.common.helper.I.IReadCallBack; +import com.bonait.bnframework.common.helper.I.IWriteCallBack; +import com.bonait.bnframework.common.helper.MessageLog; +import com.licheedev.modbus4android.ModbusCallback; +import com.licheedev.modbus4android.ModbusParam; +import com.licheedev.modbus4android.ModbusRespException; +import com.licheedev.modbus4android.param.TcpParam; +import com.serotonin.modbus4j.ModbusMaster; +import com.serotonin.modbus4j.exception.ModbusInitException; +import com.serotonin.modbus4j.exception.ModbusTransportException; +import com.serotonin.modbus4j.msg.ReadCoilsResponse; +import com.serotonin.modbus4j.msg.ReadHoldingRegistersResponse; + +import java.io.IOException; +import java.io.InputStreamReader; +import java.io.LineNumberReader; +import java.io.UnsupportedEncodingException; +import java.nio.ByteBuffer; +import java.util.Arrays; +import java.util.concurrent.ExecutionException; + + +public class ModbusTcpServer { + private static volatile ModbusTcpServer instance = null; + + public static ModbusTcpServer get() { + ModbusTcpServer manager = instance; + if (manager == null) { + synchronized (ModbusTcpServer.class) { + manager = instance; + if (manager == null) { + manager = new ModbusTcpServer(); + instance = manager; + } + } + } + return manager; + } + + private ModbusTcpServer() { + } + + ModbusParam param; + + private int GetAddress(String address) { + if (address == null) return -1; + if (address.length() > 0) { + address = address.trim(); + if (address.toUpperCase().contains("M") && address.length() >= 4) { + String[] res = address.substring(1).split("[.]"); + if (res != null && res.length == 2) { + try { + int firstAdd = Integer.parseInt(res[0]); + int endAdd = Integer.parseInt(res[1]); + if (endAdd >= 0 && endAdd <= 7) { + return (firstAdd * 8) + 320 + endAdd; + } + } catch (NumberFormatException e) { + return -1; + } + } + } else if (address.toUpperCase().contains("I") && address.length() >= 4) { + String[] res = address.substring(1).split("[.]"); + if (res != null && res.length == 2) { + try { + int firstAdd = Integer.parseInt(res[0]); + int endAdd = Integer.parseInt(res[1]); + if (endAdd >= 0 && endAdd <= 7) { + return (firstAdd * 8) + endAdd; + } + } catch (NumberFormatException e) { + return -1; + } + } + } else if ((address.toUpperCase().contains("VW") || address.toUpperCase().contains("VD")) && address.length() >= 3) { + String res = address.substring(2); + if (res != null) { + try { + int tempAdd = Integer.parseInt(res); + return (tempAdd / 2) + 100; + } catch (NumberFormatException e) { + return -1; + } + } + } else { + try { + return Integer.parseInt(address); + } catch (NumberFormatException e) { + return -1; + } + } + } + return -1; + } + + /** + * 获取布尔位地址信息 + * 列:M2.5 = getBitSingleAdd("M",2,5); + * + * @param Prefix 地址标头 + * @param startAdd 起始地址编号 + * @param num 要获取的第几位数量 + * @return + */ + public String getBitSingleAdd(String Prefix, int startAdd, int num) { + if (num > 0) { + int FirstAdd = num / 8; + int EndAdd = num % 8; + if (EndAdd == 0) { + FirstAdd--; + EndAdd = 7; + } else { + EndAdd--; + } + return Prefix + FirstAdd + startAdd + "." + EndAdd; + } + return ""; + } + + /** + * Ping PLC地址是否通畅 + * @param address + * @param pingTimes ping的次数 + * @param timeOut 超时时间 10 + * @return + */ + public static boolean ping2(String address, int pingTimes, int timeOut) { + Process process = null; + try { + process = Runtime.getRuntime().exec( "ping " + "-c " + pingTimes + " -w " + timeOut+ " "+address); + InputStreamReader r = new InputStreamReader(process.getInputStream()); + + LineNumberReader returnData = new LineNumberReader(r); + + String returnMsg=""; + + String line = ""; + + while ((line = returnData.readLine()) != null) { + + System.out.println(line); + + returnMsg += line; + + } + + if(returnMsg.indexOf("100% packet loss")!=-1){ + System.out.println("与 " +address +" 连接不畅通."); + return false; + } else{ + + System.out.println("与 " +address +" 连接畅通."); + return true; + } + } catch (IOException e) { + e.printStackTrace(); + } + return false; + + } + + + /** + * 连接 + */ + public void Connect() throws InterruptedException { + boolean status = false; + while (!status) { + try { + //status为0则代表通,为1则代表不通。 + status =ping2(ConfigName.getInstance().Address,1,1); + Thread.sleep(1000); + } catch (InterruptedException e) { + MessageLog.ShowInfo("设备 " + ConfigName.getInstance().Address + " 网络验证失败"); + } catch (Exception e) { + MessageLog.ShowInfo("设备 " + ConfigName.getInstance().Address + " 网络验证失败"); + } + } + MessageLog.ShowInfo("设备 " + ConfigName.getInstance().Address + " PLC通讯正常,准备连接!"); + String host=ConfigName.getInstance().Address; + int port=ConfigName.getInstance().Post; + param = TcpParam.create(host, port) + .setTimeout(1000) + .setRetries(0) + .setEncapsulated(false) + .setKeepAlive(true); + while (DataBus.getInstance().PlcIsConnect==false) + { + ConnectPLC(); + Thread.sleep(5000); + + + } + } + + /** + * 连接PLC + */ + public void ConnectPLC() + { + ModbusTcpHelper.get().init(param, new ModbusCallback() { + @Override + public void onSuccess(ModbusMaster modbusMaster) { + MessageLog.ShowInfo("设备 " + ConfigName.getInstance().Address + " 连接成功"); + DataBus.getInstance().PlcIsConnect = true; + //1.数据中心 +// DeviceData.Get().Init(); +// //2.业务线程 +// BusinessServer.Get().Init(); +// //3.设置自动模式 +// DeviceData.Get().setHandOrAutoSwitch(true, null); +// DeviceData.Get().setCleaningMode(false, null);//关闭自动清洗模式 +// //4.启动设备服务 +// DeviceServer.Get().Init(); +// //5.启动日志服务 +// LogServer.Get().Init(); +// //6.云订单服务 +// OrderServer.Get().Init(); +// //7.加热服务 +// ReheatServer.Get().Init(); +// //8.扫码服务 +// ScanCodeServer.Get().Init(); + } + + @Override + public void onFailure(Throwable tr) { + DataBus.getInstance().PlcIsConnect = false; + MessageLog.ShowError("设备 " + ConfigName.getInstance().Address + " 连接失败:" + tr.getMessage()); + } + + @Override + public void onFinally() { + + } + }); + } + +// /** +// * 重新连接Modbus +// */ +// public void ReconnectModbus() +// { +// try { +// +// ThreadManager.Get().StartLong("PLC断线重连线程", true, new IThread() { +// @Override +// public void Run() throws InterruptedException { +// try { +// boolean status =ping2(ConfigName.getInstance().Address,1,1); +// if(status) +// { +// ModbusMaster master= ModbusTcpHelper.get().getModbusMaster(); +// if (master==null || master.isConnected() == false) { +// ConnectPLC(); +// } +// Thread.sleep(20000); +// } +// } catch (Exception e) { +// Log.i("PLC", "PLC重连接失败!"+e.getMessage()); +// } +// } +// @Override +// public void RunComplete() throws InterruptedException { +// } +// }); +// }catch (Exception e) { +// MessageLog.ShowInfo("重新连接Modbus异常," +e.getMessage()); +// } +// } + + private Float BytesToFloat(byte[] buffers, DataFormat df) { + if (buffers.length == 4) { + byte[] bytes = new byte[4]; + if (df == DataFormat.ABCD) { + bytes[0] = buffers[3]; + bytes[1] = buffers[2]; + bytes[2] = buffers[1]; + bytes[3] = buffers[0]; + } else if (df == DataFormat.CDAB) { + bytes[0] = buffers[1]; + bytes[1] = buffers[0]; + bytes[2] = buffers[3]; + bytes[3] = buffers[2]; + } else if (df == DataFormat.BADC) { + bytes[0] = buffers[2]; + bytes[1] = buffers[3]; + bytes[2] = buffers[0]; + bytes[3] = buffers[1]; + } else if (df == DataFormat.DCBA) { + bytes[0] = buffers[0]; + bytes[1] = buffers[1]; + bytes[2] = buffers[2]; + bytes[3] = buffers[3]; + } + return ByteBuffer.wrap(bytes).getFloat(); + } + return 0.0f; + } + + private Integer BytesToInt(byte[] buffers, DataFormat df) { + if (buffers.length == 4) { + byte[] bytes = new byte[4]; + if (df == DataFormat.ABCD) { + bytes[0] = buffers[3]; + bytes[1] = buffers[2]; + bytes[2] = buffers[1]; + bytes[3] = buffers[0]; + } else if (df == DataFormat.CDAB) { + bytes[0] = buffers[1]; + bytes[1] = buffers[0]; + bytes[2] = buffers[3]; + bytes[3] = buffers[2]; + } else if (df == DataFormat.BADC) { + bytes[0] = buffers[2]; + bytes[1] = buffers[3]; + bytes[2] = buffers[0]; + bytes[3] = buffers[1]; + } else if (df == DataFormat.DCBA) { + bytes[0] = buffers[0]; + bytes[1] = buffers[1]; + bytes[2] = buffers[2]; + bytes[3] = buffers[3]; + } + return ByteBuffer.wrap(bytes).getInt(); + } + return 0; + } + + private byte[] IntToByte(int number) { + int temp = number; + byte[] b = new byte[4]; + for (int i = 0; i < b.length; i++) { + b[i] = new Integer(temp & 0xff).byteValue();// 将最低位保存在最低位 + temp = temp >> 8; // 向右移8位 + } + return b; + } + + private short[] IntToShorts(int value) { + short[] res = new short[2]; + int temp = value; + byte[] b = new byte[4]; + for (int i = 0; i < b.length; i++) { + b[i] = new Integer(temp & 0xff).byteValue();// 将最低位保存在最低位 + temp = temp >> 8; // 向右移8位 + } + for (int i = 0; i < res.length; i++) { + short s0 = (short) (b[i * 2] & 0xff);// 最低位 + short s1 = (short) (b[i * 2 + 1] & 0xff); + s1 <<= 8; + res[i] = (short) (s0 | s1); + } + return res; + } + + public static String GetString(short[] src, int start, int len) throws UnsupportedEncodingException { + short[] temp = new short[len]; + for (int i = 0; i < len; i++) { + temp[i] = src[i + start]; + } + byte[] bytesTemp = shorts2Bytes(temp); + for (int i = 0; i < bytesTemp.length; i++) { + byte b = bytesTemp[i]; + } + String str = new String(bytesTemp, "UTF-8"); + return str; + } + + public static byte[] shorts2Bytes(short[] data) { + byte[] byteValue = new byte[data.length * 2]; + for (int i = 0; i < data.length; i++) { + byteValue[i * 2] = (byte) (data[i] & 0xff); + byteValue[i * 2 + 1] = (byte) ((data[i] & 0xff00) >> 8); + } + return byteValue; + } + + /*** + *读取实时状态 + * @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 读取成功的回调 + */ + public void ReadString(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[] data1 = Arrays.copyOfRange(data, 0, 18); + //36 GUID 18 Number + String id = ""; + try { + byte[] tempdata = new byte[18]; + for (int i = 0; i < data1.length; i++) { + if (i % 2 == 0) { + + tempdata[i + 1] = data1[i]; + } else { + tempdata[i - 1] = data1[i]; + } + } + id = new String(tempdata, "UTF-8").trim(); + } catch (UnsupportedEncodingException ex) { + } + if (callback != null) callback.onSuccess(id); + } 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()); + } + } + + 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; + try { + ReadHoldingRegistersResponse res = ModbusTcpHelper.get().syncReadHoldingRegisters(1, add, length); + short[] data = res.getShortData(); + if (data.length == length) { + if (callback != null) callback.onSuccess(data); + } + } 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()); + } + } + + public void ReadBool(String Address, int length, IReadCallBack callback) { + int add = GetAddress(Address); + if (add < 0) return; + try { + ReadCoilsResponse res = ModbusTcpHelper.get().syncReadCoil(1, add, length); + boolean[] data = res.getBooleanData(); + boolean[] result = Arrays.copyOfRange(data, 0, length); + if (result.length == length) { + if (callback != null) callback.onSuccess(result); + } + } catch (InterruptedException e) { + MessageLog.ShowError("ReadBool onFailure,Address=" + Address + ",length=" + length + ",msg:" + e.toString()); + } catch (ExecutionException e) { + MessageLog.ShowError("ReadBool onFailure,Address=" + Address + ",length=" + length + ",msg:" + e.toString()); + } catch (ModbusTransportException e) { + MessageLog.ShowError("ReadBool onFailure,Address=" + Address + ",length=" + length + ",msg:" + e.toString()); + } catch (ModbusInitException e) { + MessageLog.ShowError("ReadBool onFailure,Address=" + Address + ",length=" + length + ",msg:" + e.toString()); + } catch (ModbusRespException e) { + MessageLog.ShowError("ReadBool onFailure,Address=" + Address + ",length=" + length + ",msg:" + e.toString()); + } + } + + public void ReadFloat(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(); + float[] tempValues = new float[length]; + for (int i = 0; i < length; i++) { + byte[] tempData = new byte[4]; + for (int m = 0; m < 4; m++) { + tempData[m] = data[i * 4 + m]; + } + tempValues[i] = BytesToFloat(tempData, DataFormat.ABCD); + } + if (tempValues.length == length) { + if (callback != null) callback.onSuccess(tempValues); + } + } catch (InterruptedException e) { + MessageLog.ShowError("ReadFloat onFailure,Address=" + Address + ",length=" + length + ",msg:" + e.toString()); + } catch (ExecutionException e) { + MessageLog.ShowError("ReadFloat onFailure,Address=" + Address + ",length=" + length + ",msg:" + e.toString()); + } catch (ModbusTransportException e) { + MessageLog.ShowError("ReadFloat onFailure,Address=" + Address + ",length=" + length + ",msg:" + e.toString()); + } catch (ModbusInitException e) { + MessageLog.ShowError("ReadFloat onFailure,Address=" + Address + ",length=" + length + ",msg:" + e.toString()); + } catch (ModbusRespException e) { + MessageLog.ShowError("ReadFloat onFailure,Address=" + Address + ",length=" + length + ",msg:" + e.toString()); + } + } + + public void ReadInt(String Address, int length, IReadCallBack callback) { + int add = GetAddress(Address); + if (add < 0) return; + try { + ReadHoldingRegistersResponse res = ModbusTcpHelper.get().syncReadHoldingRegisters(1, add, length * 2); + byte[] data = res.getData(); + int[] tempValues = new int[length]; + for (int i = 0; i < length; i++) { + byte[] tempData = new byte[4]; + for (int m = 0; m < 4; m++) { + tempData[m] = data[i * 4 + m]; + } +// Integer a1 = tempValues[i] = BytesToInt(tempData, DataFormat.ABCD); +// Integer a2 = tempValues[i] = BytesToInt(tempData, DataFormat.BADC); +// Integer a3 = tempValues[i] = BytesToInt(tempData, DataFormat.CDAB); +// Integer a4 = tempValues[i] = BytesToInt(tempData, DataFormat.DCBA); + + tempValues[i] = BytesToInt(tempData, DataFormat.BADC); + + } + if (tempValues.length == length) { + if (callback != null) callback.onSuccess(tempValues); + } + } catch (InterruptedException e) { + MessageLog.ShowError("ReadInt onFailure,Address=" + Address + ",length=" + length + ",msg:" + e.toString()); + } catch (ExecutionException e) { + MessageLog.ShowError("ReadInt onFailure,Address=" + Address + ",length=" + length + ",msg:" + e.toString()); + } catch (ModbusTransportException e) { + MessageLog.ShowError("ReadInt onFailure,Address=" + Address + ",length=" + length + ",msg:" + e.toString()); + } catch (ModbusInitException e) { + MessageLog.ShowError("ReadInt onFailure,Address=" + Address + ",length=" + length + ",msg:" + e.toString()); + } catch (ModbusRespException e) { + MessageLog.ShowError("ReadInt onFailure,Address=" + Address + ",length=" + length + ",msg:" + e.toString()); + } + } + + 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; + try { + ModbusTcpHelper.get().syncWriteRegisters(1, add, send); + if (callback != null) + callback.onSuccess(); + } catch (InterruptedException e) { + MessageLog.ShowError("WriteShort onFailure,Address=" + Address + ",length=" + Value + ",msg:" + e.toString()); + if (callback != null) callback.onFailure(e.toString()); + } catch (ExecutionException e) { + MessageLog.ShowError("WriteShort onFailure,Address=" + Address + ",length=" + Value + ",msg:" + e.toString()); + if (callback != null) callback.onFailure(e.toString()); + } catch (ModbusTransportException e) { + MessageLog.ShowError("WriteShort onFailure,Address=" + Address + ",length=" + Value + ",msg:" + e.toString()); + if (callback != null) callback.onFailure(e.toString()); + } catch (ModbusInitException e) { + MessageLog.ShowError("WriteShort onFailure,Address=" + Address + ",length=" + Value + ",msg:" + e.toString()); + if (callback != null) callback.onFailure(e.toString()); + } catch (ModbusRespException e) { + MessageLog.ShowError("WriteShort onFailure,Address=" + Address + ",length=" + Value + ",msg:" + e.toString()); + if (callback != null) callback.onFailure(e.toString()); + } + } + + public void WriteBool(String Address, boolean Value, IWriteCallBack callback) { + int add = GetAddress(Address); + if (add < 0) return; + try { + ModbusTcpHelper.get().syncWriteCoil(1, add, Value); + if (callback != null) + callback.onSuccess(); + } catch (InterruptedException e) { + MessageLog.ShowError("WriteBool onFailure,Address=" + Address + ",length=" + Value + ",msg:" + e.toString()); + if (callback != null) callback.onFailure(e.toString()); + } catch (ExecutionException e) { + MessageLog.ShowError("WriteBool onFailure,Address=" + Address + ",length=" + Value + ",msg:" + e.toString()); + if (callback != null) callback.onFailure(e.toString()); + } catch (ModbusTransportException e) { + MessageLog.ShowError("WriteBool onFailure,Address=" + Address + ",length=" + Value + ",msg:" + e.toString()); + if (callback != null) callback.onFailure(e.toString()); + } catch (ModbusInitException e) { + MessageLog.ShowError("WriteBool onFailure,Address=" + Address + ",length=" + Value + ",msg:" + e.toString()); + if (callback != null) callback.onFailure(e.toString()); + } catch (ModbusRespException e) { + MessageLog.ShowError("WriteBool onFailure,Address=" + Address + ",length=" + Value + ",msg:" + e.toString()); + if (callback != null) callback.onFailure(e.toString()); + } + } + + 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)}; + try { + ModbusTcpHelper.get().syncWriteRegisters(1, add, send); + + if (callback != null) callback.onSuccess(); + } catch (InterruptedException e) { + MessageLog.ShowError("WriteFloat onFailure,Address=" + Address + ",length=" + Value + ",msg:" + e.toString()); + if (callback != null) callback.onFailure(e.toString()); + } catch (ExecutionException e) { + MessageLog.ShowError("WriteFloat onFailure,Address=" + Address + ",length=" + Value + ",msg:" + e.toString()); + if (callback != null) callback.onFailure(e.toString()); + } catch (ModbusTransportException e) { + MessageLog.ShowError("WriteFloat onFailure,Address=" + Address + ",length=" + Value + ",msg:" + e.toString()); + if (callback != null) callback.onFailure(e.toString()); + } catch (ModbusInitException e) { + MessageLog.ShowError("WriteFloat onFailure,Address=" + Address + ",length=" + Value + ",msg:" + e.toString()); + if (callback != null) callback.onFailure(e.toString()); + } catch (ModbusRespException e) { + MessageLog.ShowError("WriteFloat onFailure,Address=" + Address + ",length=" + Value + ",msg:" + e.toString()); + if (callback != null) callback.onFailure(e.toString()); + } + } + + public void WriteInt(String Address, int Value, IWriteCallBack callback) { + int add = GetAddress(Address); + if (add < 0) return; + short[] send = IntToShorts(Value); + try { + ModbusTcpHelper.get().syncWriteRegisters(1, add, send); + if (callback != null) callback.onSuccess(); + } catch (InterruptedException e) { + MessageLog.ShowError("WriteInt onFailure,Address=" + Address + ",length=" + Value + ",msg:" + e.toString()); + if (callback != null) callback.onFailure(e.toString()); + } catch (ExecutionException e) { + MessageLog.ShowError("WriteInt onFailure,Address=" + Address + ",length=" + Value + ",msg:" + e.toString()); + if (callback != null) callback.onFailure(e.toString()); + } catch (ModbusTransportException e) { + MessageLog.ShowError("WriteInt onFailure,Address=" + Address + ",length=" + Value + ",msg:" + e.toString()); + if (callback != null) callback.onFailure(e.toString()); + } catch (ModbusInitException e) { + MessageLog.ShowError("WriteInt onFailure,Address=" + Address + ",length=" + Value + ",msg:" + e.toString()); + if (callback != null) callback.onFailure(e.toString()); + } catch (ModbusRespException e) { + MessageLog.ShowError("WriteInt onFailure,Address=" + Address + ",length=" + Value + ",msg:" + e.toString()); + if (callback != null) callback.onFailure(e.toString()); + } + } + + public void WriteShort(String Address, short Value) { + int add = GetAddress(Address); + if (add < 0) return; + short[] send = new short[1]; + send[0] = Value; + try { + ModbusTcpHelper.get().syncWriteRegisters(1, add, send); + } catch (InterruptedException e) { + MessageLog.ShowError("WriteShort onFailure,Address=" + Address + ",length=" + Value + ",msg:" + e.toString()); + } catch (ExecutionException e) { + MessageLog.ShowError("WriteShort onFailure,Address=" + Address + ",length=" + Value + ",msg:" + e.toString()); + } catch (ModbusTransportException e) { + MessageLog.ShowError("WriteShort onFailure,Address=" + Address + ",length=" + Value + ",msg:" + e.toString()); + } catch (ModbusInitException e) { + MessageLog.ShowError("WriteShort onFailure,Address=" + Address + ",length=" + Value + ",msg:" + e.toString()); + } catch (ModbusRespException e) { + MessageLog.ShowError("WriteShort onFailure,Address=" + Address + ",length=" + Value + ",msg:" + e.toString()); + } + } + + public void WriteBool(String Address, boolean Value) { + int add = GetAddress(Address); + if (add < 0) return; + + try { + ModbusTcpHelper.get().syncWriteCoil(1, add, Value); + } catch (InterruptedException e) { + MessageLog.ShowError("WriteBool onFailure,Address=" + Address + ",length=" + Value + ",msg:" + e.toString()); + } catch (ExecutionException e) { + MessageLog.ShowError("WriteBool onFailure,Address=" + Address + ",length=" + Value + ",msg:" + e.toString()); + } catch (ModbusTransportException e) { + MessageLog.ShowError("WriteBool onFailure,Address=" + Address + ",length=" + Value + ",msg:" + e.toString()); + } catch (ModbusInitException e) { + MessageLog.ShowError("WriteBool onFailure,Address=" + Address + ",length=" + Value + ",msg:" + e.toString()); + } catch (ModbusRespException e) { + MessageLog.ShowError("WriteBool onFailure,Address=" + Address + ",length=" + Value + ",msg:" + e.toString()); + } + } + + public void WriteFloat(String Address, float Value) { + int add = GetAddress(Address); + if (add < 0) return; + int intBits = Float.floatToRawIntBits(Value); + short[] send = new short[]{(short) ((intBits >> 16) & 0xffff), (short) (intBits & 0xffff)}; + try { + ModbusTcpHelper.get().syncWriteRegisters(1, add, send); + } catch (InterruptedException e) { + MessageLog.ShowError("WriteFloat onFailure,Address=" + Address + ",length=" + Value + ",msg:" + e.toString()); + } catch (ExecutionException e) { + MessageLog.ShowError("WriteFloat onFailure,Address=" + Address + ",length=" + Value + ",msg:" + e.toString()); + } catch (ModbusTransportException e) { + MessageLog.ShowError("WriteFloat onFailure,Address=" + Address + ",length=" + Value + ",msg:" + e.toString()); + } catch (ModbusInitException e) { + MessageLog.ShowError("WriteFloat onFailure,Address=" + Address + ",length=" + Value + ",msg:" + e.toString()); + } catch (ModbusRespException e) { + MessageLog.ShowError("WriteFloat onFailure,Address=" + Address + ",length=" + Value + ",msg:" + e.toString()); + } + } + + public void WriteInt(String Address, int Value) { + int add = GetAddress(Address); + if (add < 0) return; + short[] send = IntToShorts(Value); + try { + ModbusTcpHelper.get().syncWriteRegisters(1, add, send); + } catch (InterruptedException e) { + MessageLog.ShowError("WriteInt onFailure,Address=" + Address + ",length=" + Value + ",msg:" + e.toString()); + } catch (ExecutionException e) { + MessageLog.ShowError("WriteInt onFailure,Address=" + Address + ",length=" + Value + ",msg:" + e.toString()); + } catch (ModbusTransportException e) { + MessageLog.ShowError("WriteInt onFailure,Address=" + Address + ",length=" + Value + ",msg:" + e.toString()); + } catch (ModbusInitException e) { + MessageLog.ShowError("WriteInt onFailure,Address=" + Address + ",length=" + Value + ",msg:" + e.toString()); + } catch (ModbusRespException e) { + MessageLog.ShowError("WriteInt onFailure,Address=" + Address + ",length=" + Value + ",msg:" + e.toString()); + } + } + +} diff --git a/build.gradle b/build.gradle index b42a71ec..8637a107 100644 --- a/build.gradle +++ b/build.gradle @@ -1,26 +1,26 @@ // Top-level build file where you can add configuration options common to all sub-projects/modules. + buildscript { repositories { google() jcenter() - + } dependencies { - classpath 'com.android.tools.build:gradle:3.4.1' - + classpath 'com.android.tools.build:gradle:7.0.0' // NOTE: Do not place your application dependencies here; they belong // in the individual module build.gradle files } + } allprojects { repositories { google() jcenter() - maven { url "https://jitpack.io" } - + } } diff --git a/gradle/wrapper/gradle-wrapper.properties b/gradle/wrapper/gradle-wrapper.properties index 65322165..9f4d4db4 100644 --- a/gradle/wrapper/gradle-wrapper.properties +++ b/gradle/wrapper/gradle-wrapper.properties @@ -1,6 +1,6 @@ -#Thu Apr 18 09:23:33 CST 2019 +#Mon Jun 26 17:24:18 CST 2023 distributionBase=GRADLE_USER_HOME +distributionUrl=https\://services.gradle.org/distributions/gradle-7.5-bin.zip distributionPath=wrapper/dists -zipStoreBase=GRADLE_USER_HOME zipStorePath=wrapper/dists -distributionUrl=https\://services.gradle.org/distributions/gradle-5.1.1-all.zip +zipStoreBase=GRADLE_USER_HOME