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