从服务器发送消息到 GPS 设备

问题描述 投票:0回答:2

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

我没有收到保存坐标的 GPS 消息包。我正在添加下面的代码。我确信在获取 LOC 中的 IMEI 号码之前是正确的,并且我在输出流/发送 ACk 时遇到问题。我在这里彻底震惊了。我不知道我哪里错了。

请帮忙!

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;
}
java tcp gps serversocket tracker
2个回答
0
投票

我正在实现 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;
}

0
投票

@Gerald Amalraj,您在尝试中得到任何解决方案吗?我遇到了同样的问题,但使用其他 GPS 跟踪设备。就我而言,我尝试过普通的 ServerSocket 和 ServerSocketChannel (NIO) 这两种方法,但似乎都没有帮助。即使我多次发送相同的响应包,我也只会继续使用登录协议接收相同的数据(字节数组 0x01 中的第三个索引)。如果您找到解决方案,请告知;谢谢!

© www.soinside.com 2019 - 2024. All rights reserved.