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给的值转译成字符串

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值