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        MessageLength
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:

0d0a2a4b5700   //head of the message
4400  //length of the message =0x0044
0200   //command ID=0x0002
00000000   // IP of client software
0000   // port client software
2a4b572c4e523039473030303031 __ 2c3031352c3038303735362c2300000000000000 __ 00000000000000000000000000000000//command string 50byte(*KW,NR09G00001,015,080756,#)  
0d0a//end of the message
BUt I am getting0D0A2A4B5700440000022A4B572C4E5230394730353633372C3031352C3038303735362C230D0A

I creating the packet like this

 

try{
  
StringBuilder sb =newStringBuilder();
  
ByteArrayOutputStream bytearrypacket =newByteArrayOutputStream();
  
DataOutputStream dateoutputpacket =newDataOutputStream(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 =newDatagramPacket(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();
  
}

FacebookTwitterLinkedin
Pin It
Joomla Tutorials for Beginners