暚光科技定位系统数据解析

暚光科技定位系统数据解析

暚光科技定位系统数据解析-java

package com.ygkj.test;

import java.io.DataInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.Socket;

import org.apache.commons.codec.binary.Hex;

/*******************************************************************************************************
 * Copyright: vc1.0 2018. All rights reserved.                    <br>
 * The whole package including this class is licensed under        <br>
 *                                                                 <br>
 * @ClassName:                                        <br>
 * @Directory:                                        <br>
 * @version:         v1.0.0                                        <br>
 * @date:                                                <br>
 * @Description:                                                 <br>
 *       1、                                        <br>
 *       2、                                        <br>
 * @Others: 暂无说明                                                <br>
 * @Modification History:                                        <br>
 *       1、                                            <br>
 *       Date:                              <br>
 *       Author:                                        <br>
 *       Modification:                                     <br>
 *                                                                 <br>
 *       2、                                                        <br>
 *       Date:                                                  <br>
 *       Author:                                                   <br>
 *       Modification:                                          <br>
 *
 * @Statement: If you are using the package or parts of it in any commercial way, a commercial license is required. <br>
 *   Visit <a href='http://www.bim-times.com'>http://www.bim-times.com</a> for more information.<br>
 *
*********************************************************************************************************/
public class YGDataReceive  extends Thread{

    private static Socket socket = null;
    private static String serverHost = "192.168.2.200";
    private static final int PORT = 9000;
    byte[] b = new byte[1024];
    public YGDataReceive(){
        try {
            socket = new Socket(serverHost,PORT);//建立socket连接
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public void run(){
        while(true){
            //不停的读取从服务器端发来的消息
            InputStream ois =null;
            DataInputStream dis = null;
            try{
                ois =socket.getInputStream();
                dis = new DataInputStream(ois);
                dis.read(b);
                //String serverToClient = new String(hexEncode(b));  
                System.out.println("服务端返回到客户端的信息:"+printHexString(b));
            }catch(Exception e){
                e.printStackTrace();
            }
        }
    }

    public static void main(String[] args){
        YGDataReceive  msg = new YGDataReceive();
        msg.start();
    }

    /**
     * Hex解码
     * @param input
     * @return
     */
    public static byte[] hexDecode(String input){
        try{
            return Hex.decodeHex(input.toCharArray());
        }catch(Exception e){
            e.printStackTrace();
        }
        return null;
    }

    /**
     * Hex编码
     * @param input
     * @return
     */
    public static String hexEncode(byte[] input){
        return Hex.encodeHexString(input);
    }


    //将指定byte数组以16进制的形式打印到控制台  13911750029
        public static String printHexString( byte[] b) {
            String sd="";
           for (int i = 0; i < b.length; i++) {
             String hex = Integer.toHexString(b[i] & 0xFF);
             if (hex.length() == 1) {
               hex = '0' + hex;
             }
             System.out.print(hex.toUpperCase() +"  ");
             sd=sd+hex.toUpperCase();
           }
           System.out.println("sd:"+sd);
           String str =getPosition(sd);
           return str;
        }

        public static String getPosition(String str){
            String position_msg ="";
            if(str!=null){
                System.out.println("---str.length="+str.length());
                //if(str.length()==40){
                String strID = str.substring(12, 16);
                String strX = str.substring(16, 24);
                String strY = str.substring(24, 32);

                String strZ = str.substring(32, 40);
                System.out.println("-----------------------------------");
                System.out.println(strID);
                System.out.println(strX);
                System.out.println(strY);
                System.out.println(strZ);

                String endStr="FFFF";

                if(!strID.endsWith(endStr)&&!strX.endsWith(endStr)&&!strY.endsWith(endStr)&&!strZ.endsWith(endStr))
                {
                    //System.out.println("ID:"+getInt(getDivLine(str.substring(12, 16)))+"  x:"+getDouble(getDivLine(str.substring(16, 24)))+"  y:"+getDouble(getDivLine(str.substring(24, 32)))+"  z:"+getDouble(getDivLine(str.substring(32, 40))));
                    System.out.println("ID:"+getInt(getDivLine(str.substring(12, 16)))+"  x:"+getDouble(getDivLine(strX))+"  y:"+getDouble(getDivLine(strY))+"  z:"+getDouble(getDivLine(strZ)));
                    position_msg = "ID:"+getInt(getDivLine(str.substring(12, 16)))+"  x:"+getDouble(getDivLine(str.substring(16, 24)))+"  y:"+getDouble(getDivLine(str.substring(24, 32)))+"  z:"+getDouble(getDivLine(str.substring(32, 40)));
                    return position_msg;
                }

                //}
            }
            return null;
        }

        public static String getDivLine(String str){
            String result ="";
            if(str.length()==4){
                result = str.substring(2, 4)+str.substring(0, 2);
            }else if(str.length()==8){
                result = str.substring(4, 8)+str.substring(2, 4)+str.substring(0, 2);
            }else{
                result ="0000";
            }

            return result;
        }

        public static double getDouble(String str){
            Double d = Double.valueOf(Integer.parseInt(str,16));
            return d;
        }


        public static int getInt(String str){
            return Integer.parseInt(str,16);
        }


}
01-02 22:21