XK3168仪表通讯说明
Java对接仪表
这边用的串口服务,将串口转成TCP通讯,但是解析方式是一样的。
while (ips.read(bytes) != -1) { try { Thread.sleep(300); int length = ips.read(bytes); byte[] readBuffer = new byte[length]; System.arraycopy(bytes, 0, readBuffer, 0, length); byte valueToFind = (byte) 0xff; // 要查找的值 boolean found = findValue(readBuffer, valueToFind); if (found) { LogUtils.info("仪表", this.config.getIp(), "地磅数据 " + Arrays.toString(readBuffer)); // 0xff 出现的位置 byte[] bytes1 = new byte[5]; int index = lastIndexOf(readBuffer,valueToFind); //LogUtils.info("仪表", this.config.getIp(), "地磅数据FF的最后一次出现位置 " + index); int indexed = indexOf(readBuffer,valueToFind); //LogUtils.info("仪表", this.config.getIp(), "地磅数据FF的第一次出现位置 " + indexed); if (index > -1){ int len= readBuffer.length - index; //LogUtils.info("仪表", this.config.getIp(), "地磅数据FF的最后一次出现时是否够截取的长度 " + len); if (len>=5) { System.arraycopy(readBuffer, index, bytes1, 0, 5); LogUtils.info("仪表", this.config.getIp(), "要解析的数据 = " + Arrays.toString(bytes1)); int weight = MeterDadaParser.TF5ParseData(bytes1); sendWeightData(weight); }else { if (indexed > -1) { int len1 = readBuffer.length - indexed; //LogUtils.info("仪表", this.config.getIp(), "地磅数据FF的第一次出现时是否够截取的长度 " + len1 ); if (len1 >= 5) { System.arraycopy(readBuffer, indexed, bytes1, 0, 5); LogUtils.info("仪表", this.config.getIp(), "要解析的数据 = " + Arrays.toString(bytes1)); int weight = MeterDadaParser.TF5ParseData(bytes1); sendWeightData(weight); } } } } } } }
public static int TF5ParseData(byte[] data) { int d5 = (data[1] >> 5) & 0x01; //符号 byte GE = (byte) (data[2] & 0x0F); //个位 int intGE= Math.abs(GE); byte SHI = (byte) (data[2] >> 4); //十位 int intSHI = Math.abs(SHI)* 10; byte BAI = (byte) (data[3] & 0x0F); //百位 int intBAI = Math.abs(BAI) * 100; byte QIAN = (byte) (data[3] >> 4); //千位 int intQIAN = Math.abs(QIAN) * 1000; byte WAN = (byte) (data[4] & 0x0F); //万位 int intWAN = Math.abs(WAN) * 10000; byte SHIWAN = (byte) (data[4] >> 4); //十万位 int intSHIWAN = Math.abs(SHIWAN) * 100000; // int number = intGE + intSHI + intBAI + intQIAN + intWAN + intSHIWAN; String sign = "+", // 截取符号位 reading = String.valueOf(number), // 截取数据位 fraction = ""; // 截取小数位 if (Cache.isDebug) { LogUtils.warn(LogUtils.DEVICE_TYPE_YB, StrUtil.format("数据位:{},符号位:{},读数为:{},小数位:{}\r\n", data, sign, reading, fraction)); LogUtils.warn(LogUtils.DEVICE_TYPE_YB, d5 + " 符号"); LogUtils.warn(LogUtils.DEVICE_TYPE_YB, intGE + " 个位"); LogUtils.warn(LogUtils.DEVICE_TYPE_YB, intSHI + " 十位"); LogUtils.warn(LogUtils.DEVICE_TYPE_YB, intBAI + " 百位"); LogUtils.warn(LogUtils.DEVICE_TYPE_YB, intQIAN + " 千位"); LogUtils.warn(LogUtils.DEVICE_TYPE_YB, intWAN + " 万位"); LogUtils.warn(LogUtils.DEVICE_TYPE_YB, intSHIWAN + " 十万位"); LogUtils.warn(LogUtils.DEVICE_TYPE_YB, number + " KG"); } return Integer.parseInt(MeterDataUtil.packageData(sign, reading)); }
文章版权声明:除非注明,否则均为白茶清欢的个人技术分享原创文章,转载或复制请以超链接形式并注明出处。
还没有评论,来说两句吧...