Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 11 of 11

Thread: Send Hex data in a UDP frame

  1. #1
    Junior Member
    Join Date
    Sep 2013
    Posts
    20
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Send Hex data in a UDP frame

    Hi, Im trying to make a web aplication using servlet with Java Eclipse.

    I need send through a web aplication commands to a GPS, it only accepts hex frames by UDP, I have some code done, but it is sent as text, for these reason its not working for what I need.

    I write a command for example AT (in hex= 4154), and the aplication have to add 00010400+hex_comand: 000104004154

    I hope someone can help with that, here is the code Im using.


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Send Hex data in a UDP frame

    Please edit your post and wrap your code with code tags:
    [code=java]
    YOUR CODE HERE
    [/code]
    to get highlighting and preserve formatting.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Sep 2013
    Posts
    20
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Send Hex data in a UDP frame

    ok, done

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Send Hex data in a UDP frame

    If you want bytes of hex data vs bytes of character data you should build the bytes in a byte array instead of using a StringBuilder. For example is this what you want to send:
    byte[] theBytes = {0x00, 0x01, 0x04, 0x00, 0x41, 0x54 };
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Sep 2013
    Posts
    20
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Send Hex data in a UDP frame

    idk how to it, can help me telling me what I need add to the java code. at this moment i have 0001044154 but idk how change it to {0x00, 0x01, 0x04, 0x00, 0x41, 0x54 }; and send it that the datagram know thats a commands in hex format.

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Send Hex data in a UDP frame

    Can you post the code that shows where you have: 0001044154
    Is it in a String?
    Where did the data come from?

    datagram know thats a commands in hex format.
    Not sure what that means.
    0x41 is a way to represent what is in a byte in a hex format.
    'A' is the same data in character format
    0b01000001 is the same data in binary format
    and 65 in decimal format.

    All three of the above are exactly the same bits in a byte. I've shown 3 different ways to represent the same contents of that byte.
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Junior Member
    Join Date
    Sep 2013
    Posts
    20
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Send Hex data in a UDP frame

    yes i have it in a string, i read command, after that i add 00010400 and in the next step i convert to hex the commands without change the first 8 characters, i save this in a string variable.

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Send Hex data in a UDP frame

    The String class has a method to get a byte array from a String.
    Use the Arrays class's toString() method to print out the content of the byte array to see what is there.
    The values will be printed as decimal vs as hex: 65 = 0x41 and 84 = 0x54
    If you don't understand my answer, don't ignore it, ask a question.

  9. #9
    Junior Member
    Join Date
    Sep 2013
    Posts
    20
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Send Hex data in a UDP frame

    cant get it. remember i just have 2 weeks using java and my english its not good.

    Can tell me more about that

  10. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Send Hex data in a UDP frame

    Create a String
    use the String class's method to get an array of bytes from the String
    Put that byte array into the UDP buffer

    For debug/testing use the Arrays class's toString() method to format the array for printing:
    System.out.println("an ID "+ java.util.Arrays.toString(theArrayName));
    If you don't understand my answer, don't ignore it, ask a question.

  11. The Following User Says Thank You to Norm For This Useful Post:

    weichsel (October 29th, 2013)

  12. #11
    Junior Member
    Join Date
    Sep 2013
    Posts
    20
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Send Hex data in a UDP frame

    I finnaly get it, i used this:

    HexBinaryAdapter adapter = new HexBinaryAdapter();
    byte[] bytes = adapter.unmarshal(data);

    with that i convert the hex data captured in the strin variable and change all to a byte form. Thanks Norm.

Similar Threads

  1. Replies: 2
    Last Post: July 9th, 2013, 06:01 PM
  2. Need Help to acquire data from a connection UDP
    By clavius11 in forum Java Networking
    Replies: 4
    Last Post: May 3rd, 2013, 06:41 AM
  3. how to send a hex file to a serial port in java
    By Himali in forum Java Networking
    Replies: 10
    Last Post: April 1st, 2012, 03:06 PM
  4. Send XML message over UDP?
    By udpkillsme in forum Java Networking
    Replies: 1
    Last Post: June 22nd, 2011, 11:27 AM
  5. Replies: 6
    Last Post: October 23rd, 2009, 03:53 AM