8 Replies Latest reply: Apr 30, 2014 3:42 AM by 7d842125-81b7-4ac4-968d-3bb4f4b96089 RSS

    Generating the packet in java.

    7d842125-81b7-4ac4-968d-3bb4f4b96089

      Hi,

      Here I need to generate the packet ,This packet send to the gps device my packet format is like this.

      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  cErrorCode Char     1        0x00OK, 0x01invalid deviceID
      5  sEnd       Char     2        message end "\r\n"




      Actually Hex format wht i suppose to get:

      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 I am getting

      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

      I creating the packet like this

       

      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();
        
      }

        • 2. Re: Generating the packet in java.
          jwenting

          you have the 0x0002 reversed into 0x0200 and you're not inserting the ip address and port.

          Fix that first, and then review the rest of your code for what else you're doing wrong.

          • 3. Re: Generating the packet in java.
            7d842125-81b7-4ac4-968d-3bb4f4b96089

            I am getting confusion in the adding port and ip only  becz I am adding the packet.getSocketAddress() while sending packet right.But I need add IP as 00 00 00 00 and port as 00 00 why i am not geting that.according Hexdecimal form wht is there above.


            • 4. Re: Generating the packet in java.
              jwenting

              nobody can be that clueless...

              • 5. Re: Generating the packet in java.
                rp0428
                I am getting confusion in the adding port and ip only  becz I am adding the packet.getSocketAddress() while sending packet right.But I need add IP as 00 00 00 00 and port as 00 00 why i am not geting that.according Hexdecimal form wht is there above.

                 

                Well - you don't have ANY code to add zeroes for IP and port.

                dateoutputpacket.writeShort(0x0200);
                dateoutputpacket
                .writeBytes("*KW,NR09G05618,015,080756,#");

                Your 'zeroes' would go between those two. You won't get them if you don't write them.

                Here I need to generate the packet ,This packet send to the gps device my packet format is like this.

                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  cErrorCode Char     1        0x00OK, 0x01invalid deviceID
                5  sEnd       Char     2        message end "\r\n"

                That doesn't show where the message actually goes. There is a message length but no message.

                 

                Edit your first post and provide the correct packet description and correct the data and code.

                • 6. Re: Generating the packet in java.
                  7d842125-81b7-4ac4-968d-3bb4f4b96089

                  If I create the packet that as generate the hex like this ,This is actual data.Thanks for your replay.


                  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
                  • 7. Re: Generating the packet in java.
                    rp0428

                    That isn't what I ask you to do:

                    That doesn't show where the message actually goes. There is a message length but no message.

                     

                    Edit your first post and provide the correct packet description and correct the data and code.

                     

                    • 8. Re: Generating the packet in java.
                      7d842125-81b7-4ac4-968d-3bb4f4b96089

                      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  cErrorCode Char     1        0x00OK, 0x01invalid deviceID
                      5  sEnd       Char     2        message end "\r\n"



                      (\r\n*KW\0) 6://head of the message that is 6byte

                      2        ://Message Length that in 1 byte and 13 byte length

                      2        ://commend for the device that is ox8200 that is 2 byte

                      1        ://error message 1 byte

                      2   "\r\n"     :////end of the message