From 949b58ebbae96cae3d57fe15f8b679167789e4c4 Mon Sep 17 00:00:00 2001 From: Nah <15196688790@163.com> Date: Mon, 4 Dec 2023 14:14:42 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E4=B8=8B=E4=BD=8D=E8=AE=BE?= =?UTF-8?q?=E5=A4=87=E6=93=8D=E4=BD=9C=E7=9A=84=E7=B1=BB=EF=BC=8C=E4=BB=A5?= =?UTF-8?q?=E5=8F=8A=E4=B8=AD=E8=8B=B1=E6=96=87=E5=AD=97=E7=AC=A6=E4=B8=B2?= =?UTF-8?q?=E6=96=87=E4=BB=B6=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .idea/compiler.xml | 6 ++ .idea/gradle.xml | 20 ++++++ .idea/misc.xml | 4 ++ .../shelved.patch | 71 +++++++++++++++++++ ..._Checkout_at_2023_12_4_14_14__Changes_.xml | 4 ++ .../bonait/bnframework/business/MainInit.java | 13 +--- .../common/modbus/ModbusRTUServer.java | 2 +- 7 files changed, 108 insertions(+), 12 deletions(-) create mode 100644 .idea/compiler.xml create mode 100644 .idea/gradle.xml create mode 100644 .idea/misc.xml create mode 100644 .idea/shelf/Uncommitted_changes_before_Checkout_at_2023_12_4_14_14_[Changes]/shelved.patch create mode 100644 .idea/shelf/Uncommitted_changes_before_Checkout_at_2023_12_4_14_14__Changes_.xml diff --git a/.idea/compiler.xml b/.idea/compiler.xml new file mode 100644 index 00000000..fb7f4a8a --- /dev/null +++ b/.idea/compiler.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/.idea/gradle.xml b/.idea/gradle.xml new file mode 100644 index 00000000..ce86a83a --- /dev/null +++ b/.idea/gradle.xml @@ -0,0 +1,20 @@ + + + + + + + \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 00000000..e9a58ca7 --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,4 @@ + + + + \ No newline at end of file diff --git a/.idea/shelf/Uncommitted_changes_before_Checkout_at_2023_12_4_14_14_[Changes]/shelved.patch b/.idea/shelf/Uncommitted_changes_before_Checkout_at_2023_12_4_14_14_[Changes]/shelved.patch new file mode 100644 index 00000000..d21148e1 --- /dev/null +++ b/.idea/shelf/Uncommitted_changes_before_Checkout_at_2023_12_4_14_14_[Changes]/shelved.patch @@ -0,0 +1,71 @@ +Index: app/src/main/java/com/bonait/bnframework/business/MainInit.java +IDEA additional info: +Subsystem: com.intellij.openapi.diff.impl.patch.BaseRevisionTextPatchEP +<+>package com.bonait.bnframework.business;\r\n\r\nimport android.app.Activity;\r\nimport android.app.Application;\r\nimport android.util.Log;\r\n\r\nimport com.bonait.bnframework.BuildConfig;\r\nimport com.bonait.bnframework.common.constant.ConfigName;\r\nimport com.bonait.bnframework.common.constant.Constants;\r\nimport com.bonait.bnframework.common.constant.DataBus;\r\nimport com.bonait.bnframework.common.db.QueryDB;\r\nimport com.bonait.bnframework.common.db.file.DBHelper;\r\nimport com.bonait.bnframework.common.db.mode.BPA_ALERTLOG;\r\nimport com.bonait.bnframework.common.db.mode.BPA_CLOUDDATA;\r\nimport com.bonait.bnframework.common.db.mode.BPA_GOODS;\r\nimport com.bonait.bnframework.common.db.mode.BPA_GOODSRECIPE;\r\nimport com.bonait.bnframework.common.db.mode.BPA_LOG;\r\nimport com.bonait.bnframework.common.db.mode.BPA_PLCADDRESS;\r\nimport com.bonait.bnframework.common.db.mode.BPA_SILOS;\r\nimport com.bonait.bnframework.common.db.mode.BPA_SILOSANDMATERIAL;\r\nimport com.bonait.bnframework.common.db.mode.BPA_USER;\r\nimport com.bonait.bnframework.common.db.res.AlertLogEnum;\r\nimport com.bonait.bnframework.common.db.res.ResGoodsRecipe;\r\nimport com.bonait.bnframework.common.db.res.UserLogEnum;\r\nimport com.bonait.bnframework.common.helper.CrashHandler;\r\nimport com.bonait.bnframework.common.helper.I.IMessageLogNotify;\r\nimport com.bonait.bnframework.common.helper.MessageLog;\r\nimport com.bonait.bnframework.common.helper.SdCart;\r\nimport com.bonait.bnframework.common.modbus.ModbusTcpHelper;\r\nimport com.bonait.bnframework.common.notification.MainNotification;\r\nimport com.bonait.bnframework.common.utils.AppUtils;\r\nimport com.bonait.bnframework.common.utils.NetworkUtils;\r\nimport com.bonait.bnframework.common.utils.PreferenceUtils;\r\nimport com.bonait.bnframework.common.utils.ToastUtils;\r\nimport com.bonait.bnframework.common.utils.WifiInterceptor;\r\nimport com.bonait.bnframework.manager.ActivityLifecycleManager;\r\nimport com.lzy.okgo.OkGo;\r\nimport com.lzy.okgo.cache.CacheEntity;\r\nimport com.lzy.okgo.cache.CacheMode;\r\nimport com.lzy.okgo.interceptor.HttpLoggingInterceptor;\r\nimport com.orhanobut.logger.AndroidLogAdapter;\r\nimport com.orhanobut.logger.FormatStrategy;\r\nimport com.orhanobut.logger.Logger;\r\nimport com.orhanobut.logger.PrettyFormatStrategy;\r\nimport com.qmuiteam.qmui.arch.QMUISwipeBackActivityManager;\r\n\r\nimport org.litepal.LitePal;\r\n\r\nimport java.util.ArrayList;\r\nimport java.util.concurrent.TimeUnit;\r\nimport java.util.logging.Level;\r\n\r\nimport okhttp3.OkHttpClient;\r\n\r\npublic class MainInit {\r\n /**\r\n * 初始化\r\n * @param app\r\n */\r\n public static void Init(Application app)\r\n {\r\n //0.全局异常捕获,保存为SD卡本级目录 crash\r\n CrashHandler.getInstance().init(app);\r\n\r\n //1.设置程序active,初始化Main函数进程,初始化消息日志\r\n InitMsgLog();\r\n\r\n //2.初始化SD卡,数据库DB\r\n SdCart.getInstance().initSD();\r\n\r\n //4.初始化Main,并且增加本地图片库\r\n ConfigData.getInstance().AddImage(app);\r\n\r\n // activity生命周期管理\r\n ActivityLifecycleManager.get().init(app);\r\n\r\n // QMUI 框架初始化\r\n QMUISwipeBackActivityManager.init(app);\r\n\r\n // LitePal 数据库初始化\r\n LitePal.initialize(app);\r\n\r\n // 全局配置OkGo\r\n initOkGo(app);\r\n\r\n // 配置sharedPreferences\r\n PreferenceUtils.initPreference(app, AppUtils.getAppName(app), Activity.MODE_PRIVATE);\r\n\r\n // 初始化通知栏消息渠道\r\n MainNotification.initNotificationChannel(app);\r\n\r\n // 内存泄漏检测\r\n initLeakCanary(false);\r\n\r\n // Log日志打印框架\r\n initLogCat();\r\n\r\n // SmartShow Toast框架,暂时不用,先使用ToastUtils工具类的\r\n //SmartShow.init(this);\r\n\r\n //初始化DB数据\r\n InitDBdata();\r\n //ConfigData.getInstance().LoadingCloud();//加载云端数据\r\n\r\n DataBus.getInstance().GetTodayNoMake();\r\n\r\n //设置连接到有线网络\r\n // NetworkUtils.connetEnternet(app);\r\n }\r\n\r\n\r\n //========================================================================//\r\n public static void text()\r\n {\r\n String id=\"38f0a737-2476-40d8-a7d8-28ff7796e9d2\";\r\n BPA_GOODS gd=QueryDB.GetGoodsId(id);\r\n ArrayList PF=QueryDB.GetGoodsSrecipeID(id);\r\n ArrayList goods= QueryDB.GetGoodsALL();\r\n for (BPA_GOODS good:goods)\r\n {\r\n if(!good.id.equals(id))\r\n {\r\n for (BPA_GOODSRECIPE m:PF)\r\n {\r\n m.id=java.util.UUID.randomUUID().toString();\r\n m.goodsID=good.id;\r\n QueryDB.AddGoodsSrecipe(m);\r\n }\r\n }\r\n }\r\n\r\n\r\n\r\n\r\n\r\n }\r\n\r\n /**\r\n * 根据第一个配方重新写配方\r\n */\r\n public static void cx_cp()\r\n {\r\n ArrayList goods= QueryDB.GetGoodsALL();\r\n //38f0a737-2476-40d8-a7d8-28ff7796e9d2 鱼香肉丝\r\n //b414cef3-a315-4841-954a-abee17af02d6 重游发财\r\n\r\n\r\n String ydrs=\"dc2136ad-ee32-48c1-8077-afbdb80af9a3\";\r\n ArrayList ydrs_pf=QueryDB.GetGoodsSrecipeID(ydrs);\r\n\r\n\r\n String hc=\"b414cef3-a315-4841-954a-abee17af02d6\";\r\n ArrayList hc_pf=QueryDB.GetGoodsSrecipeID(hc);\r\n\r\n ArrayList rec= QueryDB.GetGoodsSrecipeALL();\r\n for (BPA_GOODSRECIPE pf:rec)\r\n {\r\n QueryDB.DeleteGoodsSrecipe(pf);\r\n }\r\n for (BPA_GOODS good:goods)\r\n {\r\n if(good.sort<=30)\r\n {\r\n for (BPA_GOODSRECIPE m:ydrs_pf)\r\n {\r\n m.id=java.util.UUID.randomUUID().toString();\r\n m.goodsID=good.id;\r\n QueryDB.AddGoodsSrecipe(m);\r\n }\r\n }else\r\n {\r\n for (BPA_GOODSRECIPE m:hc_pf)\r\n {\r\n m.id=java.util.UUID.randomUUID().toString();\r\n m.goodsID=good.id;\r\n QueryDB.AddGoodsSrecipe(m);\r\n }\r\n }\r\n }\r\n }\r\n /**\r\n * 初始化数据库数据(默认数据)\r\n */\r\n public static void InitDBdata() {\r\n //1.没有用户数据 默认写一个admin\r\n if (QueryDB.GetUserALL().size() <= 0) {\r\n BPA_USER user = new BPA_USER();\r\n user.id = \"20230415-fyf\";\r\n user.name = \"管理员\";\r\n user.account = \"admin\";\r\n user.pass = \"123456\";\r\n user.deviceID = ConfigName.getInstance().DeviceId;\r\n user.userID = \"20230415-fyf\";\r\n user.exp = \"管理员\";\r\n QueryDB.AddUser(user);\r\n\r\n ConfigData.getInstance().RevertPLCProcess();\r\n }\r\n }\r\n /**\r\n * 删除表-重新生成新表\r\n */\r\n public static void DBdelete()\r\n {\r\n// DBHelper.getInstance(this).DeleteCreateTables(BPA_CLOUDDATA.class,null);\r\n// DBHelper.getInstance(this).DeleteCreateTables(BPA_PLCADDRESS.class,null);\r\n }\r\n\r\n /**\r\n * 初始化消息日志接收打印\r\n */\r\n public static void InitMsgLog(){\r\n MessageLog.MsgNotify = new IMessageLogNotify() {\r\n @Override\r\n public void ErrorMsg(String msg) {\r\n if(msg.contains(\"msg:com.serotonin.modbus4j.exception\"))\r\n {\r\n ModbusTcpHelper.get().release();//释放modbus\r\n ConfigName.getInstance().PlcIsConnect=false;\r\n\r\n BPA_ALERTLOG log = new BPA_ALERTLOG();\r\n log.userID = ConfigName.getInstance().user.userID;\r\n log.type=4;\r\n log.text = msg+\"程序断开PLC,准备重连机制...\";\r\n QueryDB.AddAlertlog(log);\r\n }\r\n }\r\n @Override\r\n public void InfoMsg(String msg) {\r\n Log.i(\"Info\", msg);\r\n\r\n }\r\n @Override\r\n public void WarnMsg(String msg) {\r\n Log.w(\"Warn\", msg);\r\n }\r\n @Override\r\n public void UserMsg(UserLogEnum type, String msg) {\r\n BPA_LOG log = new BPA_LOG();\r\n log.userID = ConfigName.getInstance().user.userID;\r\n ToastUtils.info(msg);\r\n switch (type.toString())\r\n {\r\n case \"登录日志\":log.type=1;\r\n break;\r\n case \"角色操作日志\":log.type=2;\r\n break;\r\n case \"数据接收\":log.type=3;\r\n break;\r\n case \"上传日志\":log.type=4;\r\n break;\r\n case \"订单处理日志\":log.type=5;\r\n break;\r\n }\r\n log.text = msg;\r\n QueryDB.Addlog(log);\r\n }\r\n\r\n @Override\r\n public void AlertMsg(AlertLogEnum type, String msg) {\r\n BPA_ALERTLOG log = new BPA_ALERTLOG();\r\n log.userID = ConfigName.getInstance().user.userID;\r\n switch (type.toString())\r\n {\r\n case \"异常订单未制作日志\":log.type=1;\r\n break;\r\n case \"料仓缺料日志\":log.type=2;\r\n break;\r\n case \"传感器异常日志\":log.type=3;\r\n break;\r\n case \"其他\":log.type=4;\r\n break;\r\n }\r\n log.text = msg;\r\n\r\n QueryDB.AddAlertlog(log);\r\n }\r\n };\r\n }\r\n /**\r\n * 内存泄漏检测,根据flag来判断要不要初始化\r\n */\r\n private static void initLeakCanary(boolean flag) {\r\n if (flag) {\r\n // leak 内存检测注册\r\n// if (LeakCanary.isInAnalyzerProcess(this)) {\r\n// return;\r\n// }\r\n// LeakCanary.install(this);\r\n }\r\n }\r\n\r\n /**\r\n * 初始化log日志框架\r\n */\r\n private static void initLogCat() {\r\n FormatStrategy formatStrategy = PrettyFormatStrategy.newBuilder()\r\n .showThreadInfo(false) // (可选)是否显示线程信息。 默认值为true\r\n .methodCount(2) // (可选)要显示的方法行数。 默认2\r\n .build();\r\n Logger.addLogAdapter(new AndroidLogAdapter(formatStrategy) {\r\n @Override\r\n public boolean isLoggable(int priority, String tag) {\r\n return BuildConfig.DEBUG;\r\n }\r\n });\r\n }\r\n\r\n /**\r\n * 配置OkGo\r\n */\r\n private static void initOkGo(Application app) {\r\n\r\n //---------这里给出的是示例代码,告诉你可以这么传,实际使用的时候,根据需要传,不需要就不传-------------//\r\n /*HttpHeaders headers = new HttpHeaders();\r\n headers.put(\"commonHeaderKey1\", \"commonHeaderValue1\"); //header不支持中文,不允许有特殊字符\r\n headers.put(\"commonHeaderKey2\", \"commonHeaderValue2\");\r\n HttpParams params = new HttpParams();\r\n params.put(\"commonParamsKey1\", \"commonParamsValue1\"); //param支持中文,直接传,不要自己编码\r\n params.put(\"commonParamsKey2\", \"这里支持中文参数\");*/\r\n //----------------------------------------------------------------------------------------//\r\n\r\n OkHttpClient.Builder builder = new OkHttpClient.Builder();\r\n\r\n //log相关\r\n HttpLoggingInterceptor loggingInterceptor = new HttpLoggingInterceptor(\"OkGo\");\r\n //log打印级别,决定了log显示的详细程度\r\n loggingInterceptor.setPrintLevel(HttpLoggingInterceptor.Level.BODY);\r\n //log颜色级别,决定了log在控制台显示的颜色\r\n loggingInterceptor.setColorLevel(Level.INFO);\r\n //添加OkGo默认debug日志\r\n builder.addInterceptor(loggingInterceptor);\r\n //builder.addInterceptor(new WifiInterceptor(app));\r\n //-------------------------配置超时时间,默认60000ms,60s------------------------------//\r\n //OkGo.DEFAULT_MILLISECONDS\r\n //全局的连接超时时间\r\n builder.connectTimeout(Constants.CONNECT_TIME_OUT, TimeUnit.MILLISECONDS);\r\n //全局的读取超时时间\r\n builder.readTimeout(Constants.CONNECT_TIME_OUT, TimeUnit.MILLISECONDS);\r\n //全局的写入超时时间\r\n builder.writeTimeout(OkGo.DEFAULT_MILLISECONDS, TimeUnit.MILLISECONDS);\r\n OkGo.getInstance().init(app) //必须调用初始化\r\n .setOkHttpClient(builder.build()) //建议设置OkHttpClient,不设置将使用默认的\r\n .setCacheMode(CacheMode.NO_CACHE) //全局统一缓存模式,默认不使用缓存,可以不传\r\n .setCacheTime(CacheEntity.CACHE_NEVER_EXPIRE) //全局统一缓存时间,默认永不过期,可以不传\r\n .setRetryCount(0); //全局统一超时重连次数,默认为三次,那么最差的情况会请求4次(一次原始请求,三次重连请求),不需要可以设置为0\r\n //.addCommonParams(params); //全局公共参数\r\n\r\n }\r\n}\r\n +Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP +<+>UTF-8 +=================================================================== +diff --git a/app/src/main/java/com/bonait/bnframework/business/MainInit.java b/app/src/main/java/com/bonait/bnframework/business/MainInit.java +--- a/app/src/main/java/com/bonait/bnframework/business/MainInit.java (revision 1e67707b12981e019b49f3fb3f1b772d4a8d1662) ++++ b/app/src/main/java/com/bonait/bnframework/business/MainInit.java (date 1701246282625) +@@ -9,30 +9,21 @@ + import com.bonait.bnframework.common.constant.Constants; + import com.bonait.bnframework.common.constant.DataBus; + import com.bonait.bnframework.common.db.QueryDB; +-import com.bonait.bnframework.common.db.file.DBHelper; + import com.bonait.bnframework.common.db.mode.BPA_ALERTLOG; +-import com.bonait.bnframework.common.db.mode.BPA_CLOUDDATA; + import com.bonait.bnframework.common.db.mode.BPA_GOODS; + import com.bonait.bnframework.common.db.mode.BPA_GOODSRECIPE; + import com.bonait.bnframework.common.db.mode.BPA_LOG; +-import com.bonait.bnframework.common.db.mode.BPA_PLCADDRESS; +-import com.bonait.bnframework.common.db.mode.BPA_SILOS; +-import com.bonait.bnframework.common.db.mode.BPA_SILOSANDMATERIAL; + import com.bonait.bnframework.common.db.mode.BPA_USER; + import com.bonait.bnframework.common.db.res.AlertLogEnum; +-import com.bonait.bnframework.common.db.res.ResGoodsRecipe; + 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.common.modbus.ModbusTcpHelper; + import com.bonait.bnframework.common.notification.MainNotification; + import com.bonait.bnframework.common.utils.AppUtils; +-import com.bonait.bnframework.common.utils.NetworkUtils; + import com.bonait.bnframework.common.utils.PreferenceUtils; + import com.bonait.bnframework.common.utils.ToastUtils; +-import com.bonait.bnframework.common.utils.WifiInterceptor; + import com.bonait.bnframework.manager.ActivityLifecycleManager; + import com.lzy.okgo.OkGo; + import com.lzy.okgo.cache.CacheEntity; +@@ -215,8 +206,8 @@ + public void ErrorMsg(String msg) { + if(msg.contains("msg:com.serotonin.modbus4j.exception")) + { +- ModbusTcpHelper.get().release();//释放modbus +- ConfigName.getInstance().PlcIsConnect=false; ++ //ModbusTcpHelper.get().release();//释放modbus ++ //ConfigName.getInstance().PlcIsConnect=false; + + BPA_ALERTLOG log = new BPA_ALERTLOG(); + log.userID = ConfigName.getInstance().user.userID; +Index: app/src/main/java/com/bonait/bnframework/common/modbus/ModbusRTUServer.java +IDEA additional info: +Subsystem: com.intellij.openapi.diff.impl.patch.BaseRevisionTextPatchEP +<+>package com.bonait.bnframework.common.modbus;\r\n\r\nimport com.bonait.bnframework.business.ConfigData;\r\nimport com.bonait.bnframework.common.constant.ConfigName;\r\nimport com.bonait.bnframework.common.helper.DataFormat;\r\nimport com.bonait.bnframework.common.helper.I.IReadCallBack;\r\nimport com.bonait.bnframework.common.helper.I.IWriteCallBack;\r\nimport com.bonait.bnframework.common.helper.MessageLog;\r\nimport com.licheedev.modbus4android.ModbusCallback;\r\nimport com.licheedev.modbus4android.ModbusParam;\r\nimport com.licheedev.modbus4android.ModbusRespException;\r\nimport com.licheedev.modbus4android.param.SerialParam;\r\nimport com.serotonin.modbus4j.ModbusMaster;\r\nimport com.serotonin.modbus4j.exception.ModbusInitException;\r\nimport com.serotonin.modbus4j.exception.ModbusTransportException;\r\nimport com.serotonin.modbus4j.msg.ReadCoilsResponse;\r\nimport com.serotonin.modbus4j.msg.ReadHoldingRegistersResponse;\r\n\r\nimport java.io.UnsupportedEncodingException;\r\nimport java.nio.ByteBuffer;\r\nimport java.util.Arrays;\r\nimport java.util.concurrent.ExecutionException;\r\n\r\npublic class ModbusRTUServer {\r\n //region 单例模式\r\n private static volatile ModbusRTUServer instance = null;\r\n\r\n public static ModbusRTUServer get() {\r\n ModbusRTUServer manager = instance;\r\n if (manager == null) {\r\n synchronized (ModbusRTUServer.class) {\r\n manager = instance;\r\n if (manager == null) {\r\n manager = new ModbusRTUServer();\r\n instance = manager;\r\n }\r\n }\r\n }\r\n return manager;\r\n }\r\n private ModbusRTUServer() {\r\n }\r\n //endregion\r\n\r\n\r\n static ModbusParam param;\r\n\r\n /**适用于三菱PLC,只有X,Y,M,D区*/\r\n public static int GetAddress(String address){\r\n if (address == null) return -1;\r\n address = address.toUpperCase().trim();\r\n\r\n try {\r\n if (address.length() < 2) return Integer.parseInt(address);\r\n\r\n String res = address.substring(1);\r\n if (res == null) return -1;\r\n\r\n int startAddress=Integer.parseInt(res) ;\r\n //如果是M区地址。\r\n if (address.contains(\"M\")) {\r\n return startAddress + 4000;\r\n } else if (address.contains(\"D\")) {\r\n return startAddress;\r\n }else if(address.contains(\"X\")){\r\n return Integer.parseInt(res,8);\r\n }else if(address.contains(\"Y\")){\r\n return Integer.parseInt(res,8)+300;\r\n }else{\r\n return Integer.parseInt(address);\r\n }\r\n } catch (Exception e) {\r\n return -1;\r\n }\r\n }\r\n\r\n\r\n /** 连接PLC*/\r\n public void ConnectPLC()\r\n {\r\n int baudRate=ConfigName.getInstance().SerialBaudRate;\r\n int dataBits=ConfigName.getInstance().SerialDataBits;\r\n int parity=ConfigName.getInstance().SerialParity;\r\n int stopBits=ConfigName.getInstance().SerialStopBits;\r\n //String com=\"COM0\";\r\n\r\n param = SerialParam.create(\"/dev/ttyCOM1\", baudRate) // 串口地址和波特率\r\n .setDataBits(dataBits) // 数据位\r\n .setParity(parity) // 校验位\r\n .setStopBits(stopBits) // 停止位\r\n .setTimeout(3000).setRetries(0); // 不重试\r\n\r\n ModbusTcpHelper.get().init(param, new ModbusCallback() {\r\n @Override\r\n public void onSuccess(ModbusMaster modbusMaster) {\r\n //ToastUtils.info(\"串口 [\"+com+\"] 连接成功\");\r\n\r\n //MessageLog.ShowInfo(\"串口 [\"+com+\"] 连接成功\");\r\n ConfigName.getInstance().SerialIsConnect = true;\r\n ConfigData.getInstance().PLC_Init();\r\n }\r\n\r\n @Override\r\n public void onFailure(Throwable tr) {\r\n ConfigName.getInstance().SerialIsConnect = false;\r\n //ToastUtils.info(\"串口 [\"+com+\"] 连接失败:\" + tr.getMessage());\r\n //MessageLog.ShowError(\"串口 [\"+com+\"] 连接失败:\" + tr.getMessage());\r\n }\r\n @Override\r\n public void onFinally() {\r\n }\r\n });\r\n }\r\n\r\n\r\n //region 数据类型转换\r\n private Float BytesToFloat(byte[] buffers, DataFormat df) {\r\n if (buffers.length == 4) {\r\n byte[] bytes = new byte[4];\r\n if (df == DataFormat.ABCD) {\r\n bytes[0] = buffers[3];\r\n bytes[1] = buffers[2];\r\n bytes[2] = buffers[1];\r\n bytes[3] = buffers[0];\r\n } else if (df == DataFormat.CDAB) {\r\n bytes[0] = buffers[1];\r\n bytes[1] = buffers[0];\r\n bytes[2] = buffers[3];\r\n bytes[3] = buffers[2];\r\n } else if (df == DataFormat.BADC) {\r\n bytes[0] = buffers[2];\r\n bytes[1] = buffers[3];\r\n bytes[2] = buffers[0];\r\n bytes[3] = buffers[1];\r\n } else if (df == DataFormat.DCBA) {\r\n bytes[0] = buffers[0];\r\n bytes[1] = buffers[1];\r\n bytes[2] = buffers[2];\r\n bytes[3] = buffers[3];\r\n }\r\n return ByteBuffer.wrap(bytes).getFloat();\r\n }\r\n return 0.0f;\r\n }\r\n\r\n private Integer BytesToInt(byte[] buffers, DataFormat df) {\r\n if (buffers.length == 4) {\r\n byte[] bytes = new byte[4];\r\n if (df == DataFormat.ABCD) {\r\n bytes[0] = buffers[3];\r\n bytes[1] = buffers[2];\r\n bytes[2] = buffers[1];\r\n bytes[3] = buffers[0];\r\n } else if (df == DataFormat.CDAB) {\r\n bytes[0] = buffers[1];\r\n bytes[1] = buffers[0];\r\n bytes[2] = buffers[3];\r\n bytes[3] = buffers[2];\r\n } else if (df == DataFormat.BADC) {\r\n bytes[0] = buffers[2];\r\n bytes[1] = buffers[3];\r\n bytes[2] = buffers[0];\r\n bytes[3] = buffers[1];\r\n } else if (df == DataFormat.DCBA) {\r\n bytes[0] = buffers[0];\r\n bytes[1] = buffers[1];\r\n bytes[2] = buffers[2];\r\n bytes[3] = buffers[3];\r\n }\r\n return ByteBuffer.wrap(bytes).getInt();\r\n }\r\n return 0;\r\n }\r\n\r\n private byte[] IntToByte(int number) {\r\n int temp = number;\r\n byte[] b = new byte[4];\r\n for (int i = 0; i < b.length; i++) {\r\n b[i] = new Integer(temp & 0xff).byteValue();// 将最低位保存在最低位\r\n temp = temp >> 8; // 向右移8位\r\n }\r\n return b;\r\n }\r\n\r\n private short[] IntToShorts(int value) {\r\n short[] res = new short[2];\r\n int temp = value;\r\n byte[] b = new byte[4];\r\n for (int i = 0; i < b.length; i++) {\r\n b[i] = new Integer(temp & 0xff).byteValue();// 将最低位保存在最低位\r\n temp = temp >> 8; // 向右移8位\r\n }\r\n for (int i = 0; i < res.length; i++) {\r\n short s0 = (short) (b[i * 2] & 0xff);// 最低位\r\n short s1 = (short) (b[i * 2 + 1] & 0xff);\r\n s1 <<= 8;\r\n res[i] = (short) (s0 | s1);\r\n }\r\n return res;\r\n }\r\n\r\n public static String GetString(short[] src, int start, int len) throws UnsupportedEncodingException {\r\n short[] temp = new short[len];\r\n for (int i = 0; i < len; i++) {\r\n temp[i] = src[i + start];\r\n }\r\n byte[] bytesTemp = shorts2Bytes(temp);\r\n for (int i = 0; i < bytesTemp.length; i++) {\r\n byte b = bytesTemp[i];\r\n }\r\n String str = new String(bytesTemp, \"UTF-8\");\r\n return str;\r\n }\r\n\r\n public static byte[] shorts2Bytes(short[] data) {\r\n byte[] byteValue = new byte[data.length * 2];\r\n for (int i = 0; i < data.length; i++) {\r\n byteValue[i * 2] = (byte) (data[i] & 0xff);\r\n byteValue[i * 2 + 1] = (byte) ((data[i] & 0xff00) >> 8);\r\n }\r\n return byteValue;\r\n }\r\n //endregion\r\n\r\n //region 读取及写入方法\r\n\r\n /**写入字符串*/\r\n public void WriteString(int slaveAddress,String Address, int length, IWriteCallBack callback) {\r\n int add = GetAddress(Address);\r\n if (add < 0) return;\r\n short[] send = new short[length];\r\n for (short item : send) {\r\n item = 0;\r\n }\r\n try {\r\n ModbusTcpHelper.get().syncWriteRegisters(slaveAddress, add, send);\r\n if (callback != null)\r\n callback.onSuccess();\r\n } catch (InterruptedException e) {\r\n MessageLog.ShowError(\"WriteShort onFailure,Address=\" + Address + \",length=\" + length + \",msg:\" + e.toString());\r\n if (callback != null) callback.onFailure(e.toString());\r\n } catch (ExecutionException e) {\r\n MessageLog.ShowError(\"WriteShort onFailure,Address=\" + Address + \",length=\" + length + \",msg:\" + e.toString());\r\n if (callback != null) callback.onFailure(e.toString());\r\n } catch (ModbusTransportException e) {\r\n MessageLog.ShowError(\"WriteShort onFailure,Address=\" + Address + \",length=\" + length + \",msg:\" + e.toString());\r\n if (callback != null) callback.onFailure(e.toString());\r\n } catch (ModbusInitException e) {\r\n MessageLog.ShowError(\"WriteShort onFailure,Address=\" + Address + \",length=\" + length + \",msg:\" + e.toString());\r\n if (callback != null) callback.onFailure(e.toString());\r\n } catch (ModbusRespException e) {\r\n MessageLog.ShowError(\"WriteShort onFailure,Address=\" + Address + \",length=\" + length + \",msg:\" + e.toString());\r\n if (callback != null) callback.onFailure(e.toString());\r\n }\r\n }\r\n\r\n /**写入字符串*/\r\n public void WriteString(int slaveAddress,int Address, short[] values, IWriteCallBack callback) {\r\n if (slaveAddress<0) return;\r\n if (Address < 0) return;\r\n if (values ==null || values.length==0) return;\r\n try {\r\n ModbusTcpHelper.get().syncWriteRegisters(slaveAddress, Address, values);\r\n if (callback != null)\r\n callback.onSuccess();\r\n } catch (InterruptedException e) {\r\n MessageLog.ShowError(\"WriteString onFailure,Address=\" + Address + \",msg:\" + e.toString());\r\n if (callback != null) callback.onFailure(e.toString());\r\n } catch (ExecutionException e) {\r\n MessageLog.ShowError(\"WriteString onFailure,Address=\" + Address + \",msg:\" + e.toString());\r\n if (callback != null) callback.onFailure(e.toString());\r\n } catch (ModbusTransportException e) {\r\n MessageLog.ShowError(\"WriteString onFailure,Address=\" + Address + \",msg:\" + e.toString());\r\n if (callback != null) callback.onFailure(e.toString());\r\n } catch (ModbusInitException e) {\r\n MessageLog.ShowError(\"WriteString onFailure,Address=\" + Address + \",msg:\" + e.toString());\r\n if (callback != null) callback.onFailure(e.toString());\r\n } catch (ModbusRespException e) {\r\n MessageLog.ShowError(\"WriteString onFailure,Address=\" + Address + \",msg:\" + e.toString());\r\n if (callback != null) callback.onFailure(e.toString());\r\n }\r\n }\r\n\r\n\r\n /**根据地址批量读取16位寄存器*/\r\n public void ReadShorts(int slaveAddress,int Address, int length, IReadCallBack callback) {\r\n //int add = GetAddress(Address);\r\n if (Address < 0) return;\r\n try {\r\n ReadHoldingRegistersResponse res = ModbusTcpHelper.get()\r\n .syncReadHoldingRegisters(slaveAddress, Address, length);\r\n short[] data = res.getShortData();\r\n if (data.length == length) {\r\n if (callback != null) callback.onSuccess(data);\r\n }\r\n } catch (InterruptedException e) {\r\n MessageLog.ShowError(\"ReadShort onFailure,Address=\" + Address + \",length=\" + length + \",msg:\" + e.toString());\r\n } catch (ExecutionException e) {\r\n MessageLog.ShowError(\"ReadShort onFailure,Address=\" + Address + \",length=\" + length + \",msg:\" + e.toString());\r\n } catch (ModbusTransportException e) {\r\n MessageLog.ShowError(\"ReadShort onFailure,Address=\" + Address + \",length=\" + length + \",msg:\" + e.toString());\r\n } catch (ModbusInitException e) {\r\n MessageLog.ShowError(\"ReadShort onFailure,Address=\" + Address + \",length=\" + length + \",msg:\" + e.toString());\r\n } catch (ModbusRespException e) {\r\n MessageLog.ShowError(\"ReadShort onFailure,Address=\" + Address + \",length=\" + length + \",msg:\" + e.toString());\r\n }\r\n }\r\n\r\n public void ReadBools(int slaveAddress,String Address, int length, IReadCallBack callback) {\r\n int add = GetAddress(Address);\r\n if (add < 0) return;\r\n try {\r\n ReadCoilsResponse res = ModbusTcpHelper.get()\r\n .syncReadCoil(slaveAddress, add, length);\r\n boolean[] data = res.getBooleanData();\r\n boolean[] result = Arrays.copyOfRange(data, 0, length);\r\n if (result.length == length) {\r\n if (callback != null) callback.onSuccess(result);\r\n }\r\n } catch (InterruptedException e) {\r\n MessageLog.ShowError(\"ReadBool onFailure,Address=\" + Address + \",length=\" + length + \",msg:\" + e.toString());\r\n } catch (ExecutionException e) {\r\n MessageLog.ShowError(\"ReadBool onFailure,Address=\" + Address + \",length=\" + length + \",msg:\" + e.toString());\r\n } catch (ModbusTransportException e) {\r\n MessageLog.ShowError(\"ReadBool onFailure,Address=\" + Address + \",length=\" + length + \",msg:\" + e.toString());\r\n } catch (ModbusInitException e) {\r\n MessageLog.ShowError(\"ReadBool onFailure,Address=\" + Address + \",length=\" + length + \",msg:\" + e.toString());\r\n } catch (ModbusRespException e) {\r\n MessageLog.ShowError(\"ReadBool onFailure,Address=\" + Address + \",length=\" + length + \",msg:\" + e.toString());\r\n }catch (Exception e){\r\n MessageLog.ShowError(\"ReadBool onFailure,Address=\" + Address + \",length=\" + length + \",msg:\" + e.toString());\r\n }\r\n }\r\n\r\n public void ReadFloats(int slaveAddress,int Address, int length, IReadCallBack callback) {\r\n //int add = GetAddress(Address);\r\n if (Address < 0) return;\r\n try {\r\n ReadHoldingRegistersResponse res = ModbusTcpHelper.get().syncReadHoldingRegisters(slaveAddress, Address, length*2);\r\n byte[] data = res.getData();\r\n float[] tempValues = new float[length];\r\n for (int i = 0; i < length; i++) {\r\n byte[] tempData = new byte[4];\r\n for (int m = 0; m < 4; m++) {\r\n tempData[m] = data[i * 4 + m];\r\n }\r\n tempValues[i] = BytesToFloat(tempData, DataFormat.BADC);\r\n }\r\n if (tempValues.length == length) {\r\n if (callback != null) callback.onSuccess(tempValues);\r\n }\r\n } catch (InterruptedException e) {\r\n MessageLog.ShowError(\"ReadFloat onFailure,Address=\" + Address + \",length=\" + length + \",msg:\" + e.toString());\r\n } catch (ExecutionException e) {\r\n MessageLog.ShowError(\"ReadFloat onFailure,Address=\" + Address + \",length=\" + length + \",msg:\" + e.toString());\r\n } catch (ModbusTransportException e) {\r\n MessageLog.ShowError(\"ReadFloat onFailure,Address=\" + Address + \",length=\" + length + \",msg:\" + e.toString());\r\n } catch (ModbusInitException e) {\r\n MessageLog.ShowError(\"ReadFloat onFailure,Address=\" + Address + \",length=\" + length + \",msg:\" + e.toString());\r\n } catch (ModbusRespException e) {\r\n MessageLog.ShowError(\"ReadFloat onFailure,Address=\" + Address + \",length=\" + length + \",msg:\" + e.toString());\r\n }\r\n }\r\n\r\n public void ReadInts(int slaveAddress,int Address, int length,DataFormat dataFormat, IReadCallBack callback) {\r\n //int add = GetAddress(Address);\r\n if (Address < 0) return;\r\n try {\r\n ReadHoldingRegistersResponse res = ModbusTcpHelper.get().syncReadHoldingRegisters(slaveAddress, Address, length * 2);\r\n byte[] data = res.getData();\r\n int[] tempValues = new int[length];\r\n if(data.length>=4)\r\n {\r\n for (int i = 0; i < length; i++) {\r\n byte[] tempData = new byte[4];\r\n for (int m = 0; m < 4; m++) {\r\n tempData[m] = data[i * 4 + m];\r\n }\r\n tempValues[i] = BytesToInt(tempData, dataFormat);\r\n }\r\n }else\r\n {\r\n MessageLog.ShowError(\"ReadInt onFailure,Address=\" );\r\n\r\n }\r\n if (callback != null) callback.onSuccess(tempValues);\r\n } catch (InterruptedException e) {\r\n MessageLog.ShowError(\"ReadInt onFailure,Address=\" + Address + \",length=\" + length + \",msg:\" + e.toString());\r\n } catch (ExecutionException e) {\r\n MessageLog.ShowError(\"ReadInt onFailure,Address=\" + Address + \",length=\" + length + \",msg:\" + e.toString());\r\n } catch (ModbusTransportException e) {\r\n MessageLog.ShowError(\"ReadInt onFailure,Address=\" + Address + \",length=\" + length + \",msg:\" + e.toString());\r\n } catch (ModbusInitException e) {\r\n MessageLog.ShowError(\"ReadInt onFailure,Address=\" + Address + \",length=\" + length + \",msg:\" + e.toString());\r\n } catch (ModbusRespException e) {\r\n MessageLog.ShowError(\"ReadInt onFailure,Address=\" + Address + \",length=\" + length + \",msg:\" + e.toString());\r\n }\r\n }\r\n\r\n public void WriteShort(int slaveAddress,int Address, short Value, IWriteCallBack callback) {\r\n //int add = GetAddress(Address);\r\n if (Address < 0) return;\r\n short[] send = new short[1];\r\n send[0] = Value;\r\n try {\r\n ModbusTcpHelper.get().syncWriteRegisters(slaveAddress, Address, send);\r\n if (callback != null)\r\n callback.onSuccess();\r\n } catch (InterruptedException e) {\r\n MessageLog.ShowError(\"WriteShort onFailure,Address=\" + Address + \",length=\" + Value + \",msg:\" + e.toString());\r\n if (callback != null) callback.onFailure(e.toString());\r\n } catch (ExecutionException e) {\r\n MessageLog.ShowError(\"WriteShort onFailure,Address=\" + Address + \",length=\" + Value + \",msg:\" + e.toString());\r\n if (callback != null) callback.onFailure(e.toString());\r\n } catch (ModbusTransportException e) {\r\n MessageLog.ShowError(\"WriteShort onFailure,Address=\" + Address + \",length=\" + Value + \",msg:\" + e.toString());\r\n if (callback != null) callback.onFailure(e.toString());\r\n } catch (ModbusInitException e) {\r\n MessageLog.ShowError(\"WriteShort onFailure,Address=\" + Address + \",length=\" + Value + \",msg:\" + e.toString());\r\n if (callback != null) callback.onFailure(e.toString());\r\n } catch (ModbusRespException e) {\r\n MessageLog.ShowError(\"WriteShort onFailure,Address=\" + Address + \",length=\" + Value + \",msg:\" + e.toString());\r\n if (callback != null) callback.onFailure(e.toString());\r\n }\r\n }\r\n\r\n public void WriteShorts(int slaveAddress,int Address, short[] Value, IWriteCallBack callback) {\r\n //int add = GetAddress(Address);\r\n if (Address < 0) return;\r\n try {\r\n ModbusTcpHelper.get().syncWriteRegisters(slaveAddress, Address, Value);\r\n if (callback != null)\r\n callback.onSuccess();\r\n } catch (InterruptedException e) {\r\n MessageLog.ShowError(\"WriteShort onFailure,Address=\" + Address + \",length=\" + Value + \",msg:\" + e.toString());\r\n if (callback != null) callback.onFailure(e.toString());\r\n } catch (ExecutionException e) {\r\n MessageLog.ShowError(\"WriteShort onFailure,Address=\" + Address + \",length=\" + Value + \",msg:\" + e.toString());\r\n if (callback != null) callback.onFailure(e.toString());\r\n } catch (ModbusTransportException e) {\r\n MessageLog.ShowError(\"WriteShort onFailure,Address=\" + Address + \",length=\" + Value + \",msg:\" + e.toString());\r\n if (callback != null) callback.onFailure(e.toString());\r\n } catch (ModbusInitException e) {\r\n MessageLog.ShowError(\"WriteShort onFailure,Address=\" + Address + \",length=\" + Value + \",msg:\" + e.toString());\r\n if (callback != null) callback.onFailure(e.toString());\r\n } catch (ModbusRespException e) {\r\n MessageLog.ShowError(\"WriteShort onFailure,Address=\" + Address + \",length=\" + Value + \",msg:\" + e.toString());\r\n if (callback != null) callback.onFailure(e.toString());\r\n }\r\n }\r\n\r\n public void WriteBool(int slaveAddress,int Address, boolean Value, IWriteCallBack callback) {\r\n //int add = GetAddress(Address);\r\n if (Address < 0) return;\r\n try {\r\n ModbusTcpHelper.get().syncWriteCoil(slaveAddress, Address, Value);\r\n if (callback != null)\r\n callback.onSuccess();\r\n } catch (InterruptedException e) {\r\n MessageLog.ShowError(\"WriteBool onFailure,Address=\" + Address + \",length=\" + Value + \",msg:\" + e.toString());\r\n if (callback != null) callback.onFailure(e.toString());\r\n } catch (ExecutionException e) {\r\n MessageLog.ShowError(\"WriteBool onFailure,Address=\" + Address + \",length=\" + Value + \",msg:\" + e.toString());\r\n if (callback != null) callback.onFailure(e.toString());\r\n } catch (ModbusTransportException e) {\r\n MessageLog.ShowError(\"WriteBool onFailure,Address=\" + Address + \",length=\" + Value + \",msg:\" + e.toString());\r\n if (callback != null) callback.onFailure(e.toString());\r\n } catch (ModbusInitException e) {\r\n MessageLog.ShowError(\"WriteBool onFailure,Address=\" + Address + \",length=\" + Value + \",msg:\" + e.toString());\r\n if (callback != null) callback.onFailure(e.toString());\r\n } catch (ModbusRespException e) {\r\n MessageLog.ShowError(\"WriteBool onFailure,Address=\" + Address + \",length=\" + Value + \",msg:\" + e.toString());\r\n if (callback != null) callback.onFailure(e.toString());\r\n }\r\n }\r\n\r\n public void WriteBools(int slaveAddress,int Address, boolean[] Value, IWriteCallBack callback) {\r\n //int add = GetAddress(Address);\r\n if (Address < 0) return;\r\n try {\r\n ModbusTcpHelper.get().syncWriteCoils(slaveAddress, Address, Value);\r\n if (callback != null)\r\n callback.onSuccess();\r\n } catch (InterruptedException e) {\r\n MessageLog.ShowError(\"WriteBool onFailure,Address=\" + Address + \",length=\" + Value + \",msg:\" + e.toString());\r\n if (callback != null) callback.onFailure(e.toString());\r\n } catch (ExecutionException e) {\r\n MessageLog.ShowError(\"WriteBool onFailure,Address=\" + Address + \",length=\" + Value + \",msg:\" + e.toString());\r\n if (callback != null) callback.onFailure(e.toString());\r\n } catch (ModbusTransportException e) {\r\n MessageLog.ShowError(\"WriteBool onFailure,Address=\" + Address + \",length=\" + Value + \",msg:\" + e.toString());\r\n if (callback != null) callback.onFailure(e.toString());\r\n } catch (ModbusInitException e) {\r\n MessageLog.ShowError(\"WriteBool onFailure,Address=\" + Address + \",length=\" + Value + \",msg:\" + e.toString());\r\n if (callback != null) callback.onFailure(e.toString());\r\n } catch (ModbusRespException e) {\r\n MessageLog.ShowError(\"WriteBool onFailure,Address=\" + Address + \",length=\" + Value + \",msg:\" + e.toString());\r\n if (callback != null) callback.onFailure(e.toString());\r\n }\r\n }\r\n public void WriteFloat(int slaveAddress,int Address, float Value, IWriteCallBack callback) {\r\n //int add = GetAddress(Address);\r\n if (Address < 0) return;\r\n int intBits = Float.floatToRawIntBits(Value);\r\n short[] send = IntToShorts(intBits);\r\n try {\r\n ModbusTcpHelper.get().syncWriteRegisters(slaveAddress, Address, send);\r\n\r\n if (callback != null) callback.onSuccess();\r\n } catch (InterruptedException e) {\r\n MessageLog.ShowError(\"WriteFloat onFailure,Address=\" + Address + \",length=\" + Value + \",msg:\" + e.toString());\r\n if (callback != null) callback.onFailure(e.toString());\r\n } catch (ExecutionException e) {\r\n MessageLog.ShowError(\"WriteFloat onFailure,Address=\" + Address + \",length=\" + Value + \",msg:\" + e.toString());\r\n if (callback != null) callback.onFailure(e.toString());\r\n } catch (ModbusTransportException e) {\r\n MessageLog.ShowError(\"WriteFloat onFailure,Address=\" + Address + \",length=\" + Value + \",msg:\" + e.toString());\r\n if (callback != null) callback.onFailure(e.toString());\r\n } catch (ModbusInitException e) {\r\n MessageLog.ShowError(\"WriteFloat onFailure,Address=\" + Address + \",length=\" + Value + \",msg:\" + e.toString());\r\n if (callback != null) callback.onFailure(e.toString());\r\n } catch (ModbusRespException e) {\r\n MessageLog.ShowError(\"WriteFloat onFailure,Address=\" + Address + \",length=\" + Value + \",msg:\" + e.toString());\r\n if (callback != null) callback.onFailure(e.toString());\r\n }\r\n }\r\n\r\n public void WriteInt(int slaveAddress,int Address, int Value, IWriteCallBack callback) {\r\n //int add = GetAddress(Address);\r\n if (Address < 0) return;\r\n short[] send = IntToShorts(Value);\r\n try {\r\n ModbusTcpHelper.get().syncWriteRegisters(slaveAddress, Address, send);\r\n if (callback != null) callback.onSuccess();\r\n } catch (InterruptedException e) {\r\n MessageLog.ShowError(\"WriteInt onFailure,Address=\" + Address + \",length=\" + Value + \",msg:\" + e.toString());\r\n if (callback != null) callback.onFailure(e.toString());\r\n } catch (ExecutionException e) {\r\n MessageLog.ShowError(\"WriteInt onFailure,Address=\" + Address + \",length=\" + Value + \",msg:\" + e.toString());\r\n if (callback != null) callback.onFailure(e.toString());\r\n } catch (ModbusTransportException e) {\r\n MessageLog.ShowError(\"WriteInt onFailure,Address=\" + Address + \",length=\" + Value + \",msg:\" + e.toString());\r\n if (callback != null) callback.onFailure(e.toString());\r\n } catch (ModbusInitException e) {\r\n MessageLog.ShowError(\"WriteInt onFailure,Address=\" + Address + \",length=\" + Value + \",msg:\" + e.toString());\r\n if (callback != null) callback.onFailure(e.toString());\r\n } catch (ModbusRespException e) {\r\n MessageLog.ShowError(\"WriteInt onFailure,Address=\" + Address + \",length=\" + Value + \",msg:\" + e.toString());\r\n if (callback != null) callback.onFailure(e.toString());\r\n }\r\n }\r\n\r\n public void WriteShort(int slaveAddress,int Address, short Value) {\r\n //int add = GetAddress(Address);\r\n if (Address < 0) return;\r\n short[] send = new short[1];\r\n send[0] = Value;\r\n try {\r\n ModbusTcpHelper.get().syncWriteRegisters(slaveAddress, Address, send);\r\n } catch (InterruptedException e) {\r\n MessageLog.ShowError(\"WriteShort onFailure,Address=\" + Address + \",length=\" + Value + \",msg:\" + e.toString());\r\n } catch (ExecutionException e) {\r\n MessageLog.ShowError(\"WriteShort onFailure,Address=\" + Address + \",length=\" + Value + \",msg:\" + e.toString());\r\n } catch (ModbusTransportException e) {\r\n MessageLog.ShowError(\"WriteShort onFailure,Address=\" + Address + \",length=\" + Value + \",msg:\" + e.toString());\r\n } catch (ModbusInitException e) {\r\n MessageLog.ShowError(\"WriteShort onFailure,Address=\" + Address + \",length=\" + Value + \",msg:\" + e.toString());\r\n } catch (ModbusRespException e) {\r\n MessageLog.ShowError(\"WriteShort onFailure,Address=\" + Address + \",length=\" + Value + \",msg:\" + e.toString());\r\n }\r\n }\r\n\r\n public void WriteBool(int slaveAddress,int Address, boolean Value) {\r\n //int add = GetAddress(Address);\r\n if (Address < 0) return;\r\n\r\n try {\r\n ModbusTcpHelper.get().syncWriteCoil(slaveAddress, Address, Value);\r\n } catch (InterruptedException e) {\r\n MessageLog.ShowError(\"WriteBool onFailure,Address=\" + Address + \",length=\" + Value + \",msg:\" + e.toString());\r\n } catch (ExecutionException e) {\r\n MessageLog.ShowError(\"WriteBool onFailure,Address=\" + Address + \",length=\" + Value + \",msg:\" + e.toString());\r\n } catch (ModbusTransportException e) {\r\n MessageLog.ShowError(\"WriteBool onFailure,Address=\" + Address + \",length=\" + Value + \",msg:\" + e.toString());\r\n } catch (ModbusInitException e) {\r\n MessageLog.ShowError(\"WriteBool onFailure,Address=\" + Address + \",length=\" + Value + \",msg:\" + e.toString());\r\n } catch (ModbusRespException e) {\r\n MessageLog.ShowError(\"WriteBool onFailure,Address=\" + Address + \",length=\" + Value + \",msg:\" + e.toString());\r\n }\r\n }\r\n\r\n public void WriteFloat(int slaveAddress,int Address, float Value) {\r\n //int add = GetAddress(Address);\r\n if (Address < 0) return;\r\n int intBits = Float.floatToRawIntBits(Value);\r\n short[] send = new short[]{(short) ((intBits >> 16) & 0xffff), (short) (intBits & 0xffff)};\r\n try {\r\n ModbusTcpHelper.get().syncWriteRegisters(slaveAddress, Address, send);\r\n } catch (InterruptedException e) {\r\n MessageLog.ShowError(\"WriteFloat onFailure,Address=\" + Address + \",length=\" + Value + \",msg:\" + e.toString());\r\n } catch (ExecutionException e) {\r\n MessageLog.ShowError(\"WriteFloat onFailure,Address=\" + Address + \",length=\" + Value + \",msg:\" + e.toString());\r\n } catch (ModbusTransportException e) {\r\n MessageLog.ShowError(\"WriteFloat onFailure,Address=\" + Address + \",length=\" + Value + \",msg:\" + e.toString());\r\n } catch (ModbusInitException e) {\r\n MessageLog.ShowError(\"WriteFloat onFailure,Address=\" + Address + \",length=\" + Value + \",msg:\" + e.toString());\r\n } catch (ModbusRespException e) {\r\n MessageLog.ShowError(\"WriteFloat onFailure,Address=\" + Address + \",length=\" + Value + \",msg:\" + e.toString());\r\n }\r\n }\r\n\r\n public void WriteInt(int slaveAddress,int Address, int Value) {\r\n //int add = GetAddress(Address);\r\n if (Address < 0) return;\r\n short[] send = IntToShorts(Value);\r\n try {\r\n ModbusTcpHelper.get().syncWriteRegisters(slaveAddress, Address, send);\r\n } catch (InterruptedException e) {\r\n MessageLog.ShowError(\"WriteInt onFailure,Address=\" + Address + \",length=\" + Value + \",msg:\" + e.toString());\r\n } catch (ExecutionException e) {\r\n MessageLog.ShowError(\"WriteInt onFailure,Address=\" + Address + \",length=\" + Value + \",msg:\" + e.toString());\r\n } catch (ModbusTransportException e) {\r\n MessageLog.ShowError(\"WriteInt onFailure,Address=\" + Address + \",length=\" + Value + \",msg:\" + e.toString());\r\n } catch (ModbusInitException e) {\r\n MessageLog.ShowError(\"WriteInt onFailure,Address=\" + Address + \",length=\" + Value + \",msg:\" + e.toString());\r\n } catch (ModbusRespException e) {\r\n MessageLog.ShowError(\"WriteInt onFailure,Address=\" + Address + \",length=\" + Value + \",msg:\" + e.toString());\r\n }\r\n }\r\n\r\n //endregion\r\n}\r\n +Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP +<+>UTF-8 +=================================================================== +diff --git a/app/src/main/java/com/bonait/bnframework/common/modbus/ModbusRTUServer.java b/app/src/main/java/com/bonait/bnframework/common/modbus/ModbusRTUServer.java +--- a/app/src/main/java/com/bonait/bnframework/common/modbus/ModbusRTUServer.java (revision 1e67707b12981e019b49f3fb3f1b772d4a8d1662) ++++ b/app/src/main/java/com/bonait/bnframework/common/modbus/ModbusRTUServer.java (date 1701245142553) +@@ -84,7 +84,7 @@ + int stopBits=ConfigName.getInstance().SerialStopBits; + //String com="COM0"; + +- param = SerialParam.create("/dev/ttyCOM1", baudRate) // 串口地址和波特率 ++ param = SerialParam.create("/dev/ttyCOM0", baudRate) // 串口地址和波特率 + .setDataBits(dataBits) // 数据位 + .setParity(parity) // 校验位 + .setStopBits(stopBits) // 停止位 diff --git a/.idea/shelf/Uncommitted_changes_before_Checkout_at_2023_12_4_14_14__Changes_.xml b/.idea/shelf/Uncommitted_changes_before_Checkout_at_2023_12_4_14_14__Changes_.xml new file mode 100644 index 00000000..4dc181f3 --- /dev/null +++ b/.idea/shelf/Uncommitted_changes_before_Checkout_at_2023_12_4_14_14__Changes_.xml @@ -0,0 +1,4 @@ + + \ No newline at end of file diff --git a/app/src/main/java/com/bonait/bnframework/business/MainInit.java b/app/src/main/java/com/bonait/bnframework/business/MainInit.java index 8bc6e4af..56800c03 100644 --- a/app/src/main/java/com/bonait/bnframework/business/MainInit.java +++ b/app/src/main/java/com/bonait/bnframework/business/MainInit.java @@ -9,30 +9,21 @@ import com.bonait.bnframework.common.constant.ConfigName; import com.bonait.bnframework.common.constant.Constants; import com.bonait.bnframework.common.constant.DataBus; import com.bonait.bnframework.common.db.QueryDB; -import com.bonait.bnframework.common.db.file.DBHelper; import com.bonait.bnframework.common.db.mode.BPA_ALERTLOG; -import com.bonait.bnframework.common.db.mode.BPA_CLOUDDATA; import com.bonait.bnframework.common.db.mode.BPA_GOODS; import com.bonait.bnframework.common.db.mode.BPA_GOODSRECIPE; import com.bonait.bnframework.common.db.mode.BPA_LOG; -import com.bonait.bnframework.common.db.mode.BPA_PLCADDRESS; -import com.bonait.bnframework.common.db.mode.BPA_SILOS; -import com.bonait.bnframework.common.db.mode.BPA_SILOSANDMATERIAL; import com.bonait.bnframework.common.db.mode.BPA_USER; import com.bonait.bnframework.common.db.res.AlertLogEnum; -import com.bonait.bnframework.common.db.res.ResGoodsRecipe; 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.common.modbus.ModbusTcpHelper; import com.bonait.bnframework.common.notification.MainNotification; import com.bonait.bnframework.common.utils.AppUtils; -import com.bonait.bnframework.common.utils.NetworkUtils; import com.bonait.bnframework.common.utils.PreferenceUtils; import com.bonait.bnframework.common.utils.ToastUtils; -import com.bonait.bnframework.common.utils.WifiInterceptor; import com.bonait.bnframework.manager.ActivityLifecycleManager; import com.lzy.okgo.OkGo; import com.lzy.okgo.cache.CacheEntity; @@ -215,8 +206,8 @@ public class MainInit { public void ErrorMsg(String msg) { if(msg.contains("msg:com.serotonin.modbus4j.exception")) { - ModbusTcpHelper.get().release();//释放modbus - ConfigName.getInstance().PlcIsConnect=false; + //ModbusTcpHelper.get().release();//释放modbus + //ConfigName.getInstance().PlcIsConnect=false; BPA_ALERTLOG log = new BPA_ALERTLOG(); log.userID = ConfigName.getInstance().user.userID; diff --git a/app/src/main/java/com/bonait/bnframework/common/modbus/ModbusRTUServer.java b/app/src/main/java/com/bonait/bnframework/common/modbus/ModbusRTUServer.java index bd32c37c..377688a5 100644 --- a/app/src/main/java/com/bonait/bnframework/common/modbus/ModbusRTUServer.java +++ b/app/src/main/java/com/bonait/bnframework/common/modbus/ModbusRTUServer.java @@ -84,7 +84,7 @@ public class ModbusRTUServer { int stopBits=ConfigName.getInstance().SerialStopBits; //String com="COM0"; - param = SerialParam.create("/dev/ttyCOM1", baudRate) // 串口地址和波特率 + param = SerialParam.create("/dev/ttyCOM0", baudRate) // 串口地址和波特率 .setDataBits(dataBits) // 数据位 .setParity(parity) // 校验位 .setStopBits(stopBits) // 停止位