为什么我这个不能正常解析数据,求大神指导

好好努力 发布于 2015/08/25 09:48
阅读 155
收藏 0

数据是: AA AA 56 00 01 30 30 31 31 33 31 30 30 30 36 30 39 30 30 30 31 47 31 30 32 4C 32 30 36 31 32 30 32 32 35 00 00 01 D6 07 08 11 05 0F 00 04 0B 23 19 00 2A 03 00 53 00 00 00 0C 23 19 00 2A 03 00 53 00 00 00 1F 23 19 00 2A 03 00 53 00 00 00 20 23 19 00 2A 03 00 53 00 00 00 18 9E EE EE 

public class TmsIoHandler extends IoHandlerAdapter{ 

@Override

 public void messageReceived(IoSession session, Object message) throws Exception { System.out.println("session是"+session); // TODO Auto-generated method stub // String addr=session.getRemoteAddress().toString(); // 示例值为 /127.0.0.1:50641 // System.out.println(addr+"向本机服务发送内容:"+String.valueOf(CommUtils2.toHexString((byte[])message))); // logger.debug("设备"+session.getAttribute("deviceId")+"("+addr+")接收到信息:"+String.valueOf(CommUtils2.toHexString((byte[])message))); // System.out.println("设备"+session.getAttribute("deviceId")+"("+addr+")接收到信息:"+String.valueOf(CommUtils2.toHexString((byte[])message))); TrRecData jkVdRecData = new TrRecData(); System.out.println("jkVdRecData是"+jkVdRecData); // jkVdRecData.setDeviceId(session.getAttribute("deviceId").toString()); jkVdRecData.setDeviceId("DGB_ZX_VD07"); 

 TrReciveTask.this.recService.saveData(jkVdRecData, (byte[])message); 

 session.close(); } 

/**对数据进行处理并进行保存*/ 

public void saveData(TrRecData recEntity,byte[] bytes){

 System.out.println("-----------------进入服务层==========="+new Date()); if(ArrayUtils.isEmpty(bytes)){ logger.error("被解析的数据不能为空" ); return ; } 

 Calendar c=Calendar.getInstance(); 

 Date end=c.getTime(); 

 c.add(Calendar.MINUTE, -5);

 Date start=c.getTime(); 

 byte[] newBytes = Arrays.copyOf(bytes, bytes.length);

 String logMsg=CommUtils2.toHexString(newBytes);//设备状态表中的部分错误信息(因可能超长,应用时可将其置为"") 

 System.out.println("logMsg是"+logMsg); //拆分数组,查询标识符及长度进行拆分

 //注:ArrayUtils.subarray(newBytes, 0,5) 截取下角标从0到4的内容

 int splitFlag=0;

 int[] grade ={1,2,3};//设备类型 

 List recListNear= new ArrayList();//靠近车检器的一半车道 

 List recListNotNear= new ArrayList();//车检器对面的一半车道 

 //获取车检所在方向 String direcionId=dao.findDirection(recEntity.getDeviceId());

 System.out.println("设备deviceId是:"+recEntity.getDeviceId());

 int direcionIdInt=0; try { direcionIdInt=(direcionId==""? 0 :Integer.parseInt(direcionId)); } catch (NumberFormatException el) { 

 // TODO Auto-generated catch block logger.error("设备"+recEntity.getDeviceId()+"方向转换失败,打印出查询方向:"+direcionId); } 

 while(newBytes.length>=86){ 

 try{ splitFlag=ArrayUtils.indexOf(newBytes, (byte)0xAA); 

 System.out.println("splitFlag是:"+splitFlag); 

 if(splitFlag==-1){ return;} 

 //标识符位 if(newBytes[2] == (byte)0xAA && newBytes[3] ==(byte)0xAA){

 newBytes=ArrayUtils.subarray(newBytes, 4,newBytes.length); 

 continue; } 

// int size =newBytes[0] & 0xFF; 

 byte[] type =ArrayUtils.subarray(newBytes, 4, 19); 

 String logtype=CommUtils2.toHexString(type); 

// int laneId=newBytes[42] & 0xFF;laneId=laneId+1;

/车道号 byte laneNumber = newBytes[45]; 

 /** * Convert byte[] to hex string * * @param src * @return hex string */ 

public static String toHexString(byte[] src) { 

 if (src == null || src.length <= 0) {

 throw new IllegalArgumentException(

"[toHexString] this byteArray must not be null or empty");

 }

 StringBuilder stringBuilder = new StringBuilder(""); 

 for (int i = 0; i < src.length; i++) {

 int v = src[i] & 0xFF; String hv = Integer.toHexString(v);

 stringBuilder.append("0x"); if (hv.length() < 2) { stringBuilder.append(0); }

 stringBuilder.append(hv.toUpperCase());

 } return stringBuilder.toString(); }

加载中
0
i
iceleeyo

代码贴得真乱

好好看看报文协议是怎么定义的,每个字段或域字节长度是多少,数据类型是BCD,ASCII,还是BINARY

你得先把打印的16进制报文自己手工分析下,再去检查解析代码

OSCHINA
登录后可查看更多优质内容
返回顶部
顶部