车载808协议数据转发丢失问题
因为解析后的808消息结构在特定情况下会生成0x7e或者0x7d,会导致转发出去的数据被截断,解析出来的长度异常导致丢失报文数据。
转义代码
/** * * 发送消息时转义<br> * * <pre> * 0x7e <====> 0x7d02 * </pre> * * @param bs * 要转义的字节数组 * @param start * 起始索引 * @param end * 结束索引 * @return 转义后的字节数组 * @throws Exception */ public byte[] doEscape4Send(byte[] bs, int start, int end) throws Exception { if (start < 0 || end > bs.length) throw new ArrayIndexOutOfBoundsException("doEscape4Send error : index out of bounds(start=" + start + ",end=" + end + ",bytes length=" + bs.length + ")"); ByteArrayOutputStream baos = null; try { baos = new ByteArrayOutputStream(); for (int i = 0; i < start; i++) { baos.write(bs[i]); } for (int i = start; i < end; i++) { if (bs[i] == 0x7e) { baos.write(0x7d); baos.write(0x02); } else if (bs[i] == 0x7d) { baos.write(0x7d); baos.write(0x01); } else { baos.write(bs[i]); } } for (int i = end; i < bs.length; i++) { baos.write(bs[i]); } return baos.toByteArray(); } catch (Exception e) { throw e; } finally { if (baos != null) { baos.close(); baos = null; } } }