Shop OBEX P1 Docs P2 Docs Learn Events
Hex Packets for radio configuration. — Parallax Forums

Hex Packets for radio configuration.

billiam2536billiam2536 Posts: 28
edited 2010-03-01 02:59 in Propeller 1
I'm trying to communicate with a DNT900 radio by RFM, and in protocol mode it uses hexadecimal packets, and I really just have no idea where to start. How do I send a packet, such as "0xFB 0x04 0x03 ..." to the radio from the propeller? I am COMPLETELY lost.

▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Working on getting money for autopilot project. Come check out my site if your interested in the project. Maybe go to the bottom of the page wink.gif

Comments

  • Mike GreenMike Green Posts: 23,101
    edited 2010-02-28 19:29
    First of all, "0xFB" is just a notation for binary (hexadecimal) numbers. The equivalent in Spin or assembly is "$FB". Just substitute "$" for "0x".

    How you send a packet depends on what the radio expects. Presumably it's some kind of asynchronous serial stream. You need to provide a manual that describes the Baud, how many bits of data, how many stop bits, whether parity is expected or provided. Typically for this sort of thing, there are 8 bits of data, no parity, and 1 stop bit used. The Baud could be anything.

    Start with the manual for the radio. There are a variety of ways to send asynchronous serial data. Look at Simple_Serial from the Object Exchange. Also look at FullDuplexSerial which comes with the Propeller Tool. There are demo programs that come with each of these and illustrate the basics of how to use them. Also read the comments in each. Library objects are often documented in the comments in the object's source.
  • billiam2536billiam2536 Posts: 28
    edited 2010-02-28 19:35
    The radio manual can be found here . How do I send the series of hex bytes, or each thing preceded by 0x or $? Do I send each one individually or is there a way to group them together to all be sent at once?

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Working on getting money for autopilot project. Come check out my site if your interested in the project. Maybe go to the bottom of the page wink.gif

    Post Edited (billiam2536) : 2/28/2010 10:45:00 PM GMT
  • w8anw8an Posts: 176
    edited 2010-02-28 19:39
    This object will provide some insight into assembling packets
    obex.parallax.com/objects/146/

    Of course you will need to study the documents of your device so you can frame your packets as necessary.

    --Steve
  • billiam2536billiam2536 Posts: 28
    edited 2010-02-28 20:17
    Alright, I've read through the program as best I can and I understand a little better, but not completely. If I'm using PST to communicate with the propeller to communicate with the radio, how should I go about sending the packet $FB $04 $03 $17 $02 $08 to the radio? What pub function should I use? I'm sorry if this is really obvious, but I'm totally new to the programming and packets and stuff.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Working on getting money for autopilot project. Come check out my site if your interested in the project. Maybe go to the bottom of the page wink.gif
  • w8anw8an Posts: 176
    edited 2010-02-28 22:29
    If all you want to do is send a series of bytes via a serial connection, you should probably look at Numbers.spin and FullDuplexSerial.spin. Both should be in your Propeller Tool library folder.

    Note that FB is just a hexadecimal representation for the binary number 11111011 which is also equivalent to decimal 251. The dollar sign indicates it is shown as hexadecimal. The binary would be represented by a preceding percent sign, %11111011

    What you need to do is to first load the two mentioned library files (and initialize appropriately). Then you can convert the human readable string "FB" into its numeric value using the Numbers object. This value (a single byte) would then be sent to your radio via the serial object. Next you would convert and send "04", then "03" and so on. Or you can convert them first then send the group of bytes. Whatever your radio is expecting is important here.
  • billiam2536billiam2536 Posts: 28
    edited 2010-02-28 22:55
    $FB is the start byte, the message protocol info can be read on page 27 of the link above. If somebody will read it, can you tell me if I am understanding what I am reading, and that it really is a packet that I'm wanting here?

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Working on getting money for autopilot project. Come check out my site if your interested in the project. Maybe go to the bottom of the page wink.gif
  • Dr_AculaDr_Acula Posts: 5,484
    edited 2010-03-01 01:23
    Yes I can see what you mean about being confusing. That manual has a huge amount of detail.

    Firstly, hex vs decimal, I tend to use the Windows Calculator a lot. If you set it to scientific mode it can do hex to decimal with one keypress. So $FB is decimal 251.

    Next thing is the simple serial object. I'm a bit confused myself here. The decimal method seems straightforward:
    PUB tx(txbyte)
    
    '' Send byte (may wait for room in buffer)
    
      repeat until (tx_tail <> (tx_head + 1) & $F)
      tx_buffer[noparse][[/noparse]tx_head] := txbyte
      tx_head := (tx_head + 1) & $F
    
      if rxtx_mode & %1000
        rx
    
    



    I think you just send it 251, or a variable whose value is 251.

    But the hex method is confusing;
    PUB hex(value, digits)
    
    '' Print a hexadecimal number
    
      value <<= (8 - digits) << 2
      repeat digits
        tx(lookupz((value <-= 4) & $F : "0".."9", "A".."F"))
    
    



    For a two byte value I think it is sending two bytes rather than one, as it has 'repeat digits' So if you send it $FB I think it is going to send decimal 70 then decimal 66. Maybe I'm wrong there, but until someone clarifies perhaps stick to decimal numbers and do the conversion in a calculator.

    Also, I'm sure there is a way of converting hex to decimal in spin. In basic it is
    a=val("&HFB")

    (which muddies the waters more as there are at least 4 different hex prefixes/suffixes I can think of - &H at the beginning, H at the end, 0x at the beginning and $ at the beginning).

    Serial is hard to do if you can't see what you are doing, especially with values that don't display on a terminal. Decimal 65 (hex 0x41) is fine as it prints A but decimal 01 may not print anything. I've found it helps to have some simple code to output bytes from a PC - eg a few lines of vb.net, plus a serial line monitor program so you can see bytes going out and bytes coming back. Even debugging the propeller it helps to have a serial monitor program - eg program the propeller to output a byte every few seconds (eg A) then run a terminal program on a PC and a serial port monitor program for good measure and watch the bytes come through. The propeller terminal program I think can do the heavy lifting there.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    www.smarthome.viviti.com/propeller
  • SciNemoSciNemo Posts: 91
    edited 2010-03-01 01:31
    This should send a byte with the value of $FB over the serial connection at a specified baudrate (assuming you assigned the full duplex serial object of your choice the name "serial"):

    serial.tx($FB)
    



    Dr. Acula, the hex function sends two bytes because it is printing the hex value over the serial connection, not sending the value. It is sending an F and then a B using the ascii values of those characters. That is what the lookupz function is for.

    The Propeller Tool will recognize $FB as its hex value, no conversion to decimal is necessary.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Not the fish.
    sites.google.com/site/bitwinproject/
  • w8anw8an Posts: 176
    edited 2010-03-01 01:37
    There's quite a bit to learn to communicate with your radio, Bill

    Without going into analysis of the protocol, their definition of "packet" is a short burst of bytes being sent to or from the radio.
    The contents of this packet of bytes determines what you want to tell the radio or what the radio is telling you. You will need to
    understand the series of bytes necessary to send your desired command, then also listen to the serial port for information
    being sent back from the radio.

    Your packet or command string needs to be in a proper format for the radio to understand what you're sending. That's why the
    first byte is always a $FB byte indicating the start-of-packet. The second byte tells it how many bytes in your packet so it
    knows what to expect. This is used to determine if the whole packet made it -- a sort of error detection.

    Here's a very basic program that will do nothing but send "Hello World" to your radio as described in the example in section 4.1.5
    of your manual.

    Your radio should ACK (acknowledge) this packet with the following string of bytes if received correctly
    $FB $06 $15 $00 $02 $01 $00 $C4
    but this program isn't listening for any response.

    Notice the SOP character at the beginning, then the number 6 showing there will be six characters in the rest of the message.

    
    CON
    
      _clkmode = xtal1 + pll16x
      _xinfreq = 5_000_000
    
      ' Set pins and baud rate for DNT communication
      DNT_Rx         = 10        
      DNT_Tx         = 11       
      DNT_Baud       = 9600    
    
    OBJ
    
      DNT  : "FullDuplexSerial"            'Load the serial object
      
    PUB Start  
    
      DNT.start(DNT_Rx, DNT_Tx, 0, DNT_Baud)   'Configure DNT serial communication
    
      DNT.tx($FB)        'Send start-of-packet [noparse][[/noparse]SOP] -always $FB
      DNT.tx($0F)        'Length of this packet - $0F = 15
      DNT.tx(%0000_0101) 'Type of packet (Shown in binary format since different bits specify different mode settings)
      DNT.tx($02)        'Address byte 3 of the radio you are sending to (Raio address is $00 $01 $02) 
      DNT.tx($01)        'Address byte 2 
      DNT.tx($00)        'Address byte 1
      DNT.tx($48)        'H
      DNT.tx($65)        'e
      DNT.tx($6C)        'l
      DNT.tx($6C)        'l
      DNT.tx($6F)        'o
      DNT.tx($20)        '<space char>
      DNT.tx($57)        'W
      DNT.tx($6F)        'o
      DNT.tx($72)        'r
      DNT.tx($6C)        'l
      DNT.tx($64)        'd
      
      repeat                'Done. Loop forever
    
    
    
  • billiam2536billiam2536 Posts: 28
    edited 2010-03-01 01:44
    Thank you SO much. This is the sort of thing I've been trying to come up with. Can't say how happy I am.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Working on getting money for autopilot project. Come check out my site if your interested in the project. Maybe go to the bottom of the page wink.gif
  • billiam2536billiam2536 Posts: 28
    edited 2010-03-01 01:56
    But why did you change the $05 to binary with 6 and 8 high?

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Working on getting money for autopilot project. Come check out my site if your interested in the project. Maybe go to the bottom of the page wink.gif
  • w8anw8an Posts: 176
    edited 2010-03-01 02:24
    Bits 0 and 2 are high. %101 = 5. The most significant digits are on the left, same as the other number systems.

    HEX    Binary   Decimal  Exponential
    $00  %00000000  0
    $01  %00000001  1        2^0
    $02  %00000010  2        2^1
    :
    $0F  %00001111  15
    $10  %00010000  16       2^4
    $11  %00010001  17
    :
    $80  %10000000  128      2^7
    :
    $FF    %11111111  255
    $100  %100000000  256    2^8
    ..and so on..
    
    

    Post Edited (w8an) : 3/1/2010 2:39:47 AM GMT
  • w8anw8an Posts: 176
    edited 2010-03-01 02:49
    I added the exponential column so you can see how the binary bit position relates to the value. Notice how you can multiply by two by shifting a binary pattern to the left one place. Multiply by 4 by shifting left 2 places. Divide by shifting right. Of course you must be careful with this technique as shifting bits out of the register will result in overflows and underflows.
  • billiam2536billiam2536 Posts: 28
    edited 2010-03-01 02:59
    w8an, that code was just the thing I needed, I replaced some of the fields to suit my purposes and figured out the code to receive and display the replies, and it all works flawlessly. Many thanks to you and everybody.

    Bill

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Working on getting money for autopilot project. Come check out my site if your interested in the project. Maybe go to the bottom of the page wink.gif
Sign In or Register to comment.