使用java创建数据包并获取确认

时间:2014-04-28 06:28:21

标签: java sockets gps packet

您好我从GPS设备获取数据,我想通过创建数据包发送响应回Gps设备我的数据包是这样的。一旦我发送此数据包,我想检查数据包是否已交付或不。

No. Field   TYPE    Length  description
1   sMark    Char   6      Flag of message (\r\n*KW\0)
2   packetLe short  2      Message Length
3   CMD     Short   2          0x8200
4   cErrorCodeChar  1      0x00OK, 0x01invalid deviceID
5   sEnd     Char   2      message end "\r\n"

我创建了如下所示的数据包,我正在将数据包发送到gps设备,但我没有收到任何回复或确认。

try {
               StringBuilder sb = new StringBuilder();
    ByteArrayOutputStream bytearrypacket = new ByteArrayOutputStream();
    DataOutputStream dateoutputpacket = new DataOutputStream(bytearrypacket);
    dateoutputpacket.writeBytes("\r\n*KW\0");
    dateoutputpacket.writeShort(0x4400);
    dateoutputpacket.writeShort(0x0200);
    dateoutputpacket.writeBytes("*KW,NR09G05618,015,080756,#");
    dateoutputpacket.writeBytes("\r\n");
   dateoutputpacket.flush();
    byte[] res = bytearrypacket.toByteArray();
 for (byte b : res) {
        sb.append(String.format("%02X ", b));
   }
                DatagramPacket responsepacket = new DatagramPacket(res, res.length, packet.getSocketAddress());
                dsocket.send(responsepacket);
            } catch (IOException ex) {
                ex.printStackTrace();
            } catch (SecurityException se) {
                se.printStackTrace();
            } catch (IllegalBlockingModeException il) {
                il.printStackTrace();
            } catch (IllegalArgumentException ilg) {
                ilg.printStackTrace();
            }

更新

实际上是我想要的Hex格式:

   0d 0a 2a 4b 57 00   //head of the message
        44 00               //length of the message =0x0044
        02 00               //command ID=0x0002
        00 00 00 00         // IP of client software
        00 00               // port client software
        2a 4b 57 2c 4e 52 30 39 47 30 30 30 30 31 __ 2c 30 31 35 2c 30 38 30 37 35 
        36 2c 23 00 00 00 00 00 00 00 __ 00 00 00 00 00 00 00 00 00 00 
        00 00 00 00 00 00 //command string 50byte(*KW,NR09G00001,015,080756,#)

        0d 0a //end of the message

BUt我正在

0D 0A 2A 4B 57 00 44 00 00 02 2A 4B 57 2C 4E 52 30 39 47 30 35 36 33 37 2C 30 31 35 2C 30 38 30 37 35 36 2C 23 0D 0A

0 个答案:

没有答案