栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

Java物联网IOT跟PLC字符互相转换

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Java物联网IOT跟PLC字符互相转换

 @Test
    public void plcTestSystem() throws Exception {
        //字符 转成ascii
       String string = Md5Util.stringToAscii("JM-010-1-ZJ");
       // 分割 PLC 每个地址只能接收2个字符串
       String[] line = string.split(",");
       String ad = "";
       for (String s : line) {
           ad = ad + s;
       }
       // 0-4 是 JM 两个字符
       String jihua1 = ad.substring(0, 4);
       // 获取左位字符
       String zuo = jihua1.substring(0, 2);
       // 获取右位字符
       String you = jihua1.substring(2, 4);
       // 转成int
       Integer zuoInt = Integer.parseInt(zuo);
       Integer youInt = Integer.parseInt(you);
       // PLC 计算方式:zuo位* 256 +右位
       int jihuazhi1 = zuoInt * 256 + youInt;
       opcUaConfig.writeOpcUa("jihuazhifan1", jihuazhi1 + "");
       System.out.println(jihuazhi1);
       String jihua2 = ad.substring(4, 8);
       String zuo2 = jihua2.substring(0, 2);
       String you2 = jihua2.substring(2, 4);
       Integer zuoInt2 = Integer.parseInt(zuo2);
       Integer youInt2 = Integer.parseInt(you2);
       int jihuazhi2 = zuoInt2 * 256 + youInt2;
       opcUaConfig.writeOpcUa("jihuazhifan2", jihuazhi2 + "");
       String jihua3 = ad.substring(8, 12);
       String zuo3 = jihua3.substring(0, 2);
       String you3 = jihua3.substring(2, 4);
       Integer zuoInt3 = Integer.parseInt(zuo3);
       Integer youInt3 = Integer.parseInt(you3);
       int jihuazhi3 = zuoInt3 * 256 + youInt3;
       opcUaConfig.writeOpcUa("jihuazhifan3", jihuazhi3 + "");
       String jihua4 = ad.substring(12, 16);
       String zuo4 = jihua4.substring(0, 2);
       String you4 = jihua4.substring(2, 4);
       Integer zuoInt4 = Integer.parseInt(zuo4);
       Integer youInt4 = Integer.parseInt(you4);
       int jihuazhi4 = zuoInt4 * 256 + youInt4;
       opcUaConfig.writeOpcUa("jihuazhifan4", jihuazhi4 + "");

   }

转译成PLC可以接收的方法

 

        String zhifan = "12333";
        Integer zuoyezhe = Double.valueOf(zhifan).intValue();
        // 第一个
        Integer zuoyezhe1 = zuoyezhe / 256;
        Integer zuoyezhe2 = zuoyezhe % 256;
        String asciiToString = Md5Util.asciiToString(zuoyezhe1.toString());
        String asciiToString1 = Md5Util.asciiToString(zuoyezhe2.toString());
        zhifan = asciiToString + asciiToString1;
        System.out.println(zhifan);

PLC给的值转译成字符串

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/292673.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号