【问题标题】:Send messsage from server to GPS device从服务器发送消息到 GPS 设备
【发布时间】:2014-12-28 07:43:16
【问题描述】:

我一直在使用 TK06A GPS 跟踪器,我正在开发我自己的服务器 java 文件,我已经按照 TK06A 手册收到了初始登录消息包,现在我需要将 ack 发送回设备以获取 GPS消息包。

我没有收到保存坐标的 GPS 消息包。我在下面添加代码。我确信在获得 LOC 中的 IMEI 号码是正确的之前,我在输出流/发送 Ak 时遇到问题。我完全被这里打动了。我不知道我哪里错了。

请帮忙!

public void run() {
DataInputStream inputS = null;
DataOutputStream dos = null;
try {
    inputS = new DataInputStream(socket.getInputStream());
    if (inputS.available() > 0) {
        byte[] bb = getBytesFromInputStream(inputS);
        ChannelBuffer buf = toByteBuffer(bb);
        String imei = readImei(buf);
        System.out.println("IMEI::::: " + imei);
        buf.skipBytes(5); // End

        OutputStream os = socket.getOutputStream();
        dos = new DataOutputStream(os);
        byte[] response = parseHex();

        dos.write(response);
        Thread.sleep(1000);
        dos.flush();

    }

} catch (Exception e) {
    e.printStackTrace();
}

finally {
    try {
        inputS.close();
        if (dos != null)
            dos.close();
        socket.close();
    } catch (IOException e) {

    }
}

}

public byte[] parseHex() {
    String  hexACKlogin = "787805010001D9DC0D0A"; // String in HEX format
    int len = hexACKlogin.length();
    byte[] data = new byte[len / 2];
    for (int i = 0; i < len; i += 2) {
        data[i / 2] = (byte) ((Character.digit(hexACKlogin.charAt(i), 16) << 4)
                             + Character.digit(hexACKlogin.charAt(i+1), 16));
    }  
     return data;
}

【问题讨论】:

  • 请告诉我们您正在实施什么协议。否则,我们将无法为您提供帮助。
  • 基本上你从一个数据套接字读取的方式也是你写入这个数据套接字的方式,只是使用从类*Writer而不是read和*Reader的write方法。
  • 为什么要标记 GPS??
  • 也许,这可以提供一些提示:security.stackexchange.com/questions/55365/…

标签: java tcp gps serversocket tracker


【解决方案1】:

我正在为 GPS 跟踪设备 (TK06A) 实现逻辑。客户端是我用来读取从跟踪器发送的数据的 GPS 跟踪器和服务器。

我收到一个 18 字节的数据,我发现它是一个不包含坐标/纬度和经度的登录消息包。

据说我需要向客户端发送一个ACK响应[我卡在这里]。

在我的情况下,我需要将字节作为响应发送给客户端 Gps 跟踪器 字节[]响应= {0x78 0x78 0x05 0x01 0x00 0x01 0xD9 0xDC 0x0D 0x0A};

我已经使用 portpeeker 发送了 ack,它返回了一些 36 字节的响应。

PS:我正在使用 TCP 进行数据传输

try {
        inputS = new DataInputStream(socket.getInputStream());
        oos = new ObjectOutputStream(socket.getOutputStream());
        if (inputS.available() > 0) {
            System.out.println("Bytes Cap::: "+inputS.available());
            byte[] bb = getBytesFromInputStream(inputS);
            ChannelBuffer buf = toByteBuffer(bb);
            buf.skipBytes(4);
            String imei = readImei(buf);
            System.out.println("IMEI::::: " + imei);
            buf.skipBytes(5); // End

            byte[] response = sendOutputMessage();
            oos.write(response);
            oos.flush();
        }

    } catch (Exception e) {
        e.printStackTrace();
    }finally{
        try {
            inputS.close();
            oos.close();
            socket.close();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

 public byte[] sendOutputMessage() {
    byte buf[] = new byte[10];
    // Start of the body of setOutput
    // Message type  0x78 0x78 0x05 0x01 0x00 0x01 0xD9 0xDC 0x0D 0x0A
    buf[0] = (byte) 0x78;
    buf[1] = (byte) 0x78;
    buf[2] = (byte) 0x05;
    buf[3] = (byte) 0x01;
    buf[4] = (byte) 0x00;
    buf[5] = (byte) 0x01;
    buf[6] = (byte) 0xD9;
    buf[7] = (byte) 0xDC;
    buf[8] = (byte) 0x0D; // end f1
    buf[9] = (byte) 0x0A;
    return buf;
}

【讨论】:

  • 当您发送此响应时,您会收到什么错误?
猜你喜欢
  • 1970-01-01
  • 2012-12-09
  • 1970-01-01
  • 1970-01-01
  • 2016-09-17
  • 2017-02-20
  • 1970-01-01
  • 1970-01-01
  • 2016-02-14
相关资源
最近更新 更多