Shop OBEX P1 Docs P2 Docs Learn Events
DAT length including HEX 00 — Parallax Forums

DAT length including HEX 00

LtechLtech Posts: 380
edited 2011-09-30 00:40 in Propeller 1
I mis something her !

->I have a DAT field : "MessageSentToServer byte "11010106002510080E0101",0 "
"StrSize(@MessageSentToServer)" Give 22 corect !

->It has to be in hexadecimal code

->I make : "MessageSentToServer byte $11, $01, $01, $06, $00, $25, $10, $08, $0E, $01, $01, 0"
Fine !
But now : StrSize(@MessageSentToServer) Give 5 !

Yes I need to send Hex00 on byte 5.
I understand the counter stop on 0 but is there a nice way to solve this?????

Thanks

Comments

  • Phil Pilgrim (PhiPi)Phil Pilgrim (PhiPi) Posts: 23,514
    edited 2011-09-29 13:07
    If you're sending ASCII hex, your first expression will work, if sent as a string. If you're sending raw binary bytes, try something like this:
    ptr := @MessageSentToServer
    repeat while ptr < @EndMessage
      send_byte(byte[ptr++])
    ...
    DAT
    
    MessageSentToServer byte $11, $01, $01, $06, $00, $25, $10, $08, $0E, $01, $01
    EndMsg
    

    -Phil
  • Mike GreenMike Green Posts: 23,101
    edited 2011-09-29 13:10
    If you have an embedded zero byte in your data, you can't use the STR functions. Typically, you'd have to write your own "transmit N characters at address X" routine that you'd use instead of the .STR routine. Something like this would do:
    PUB fixedLengthString( x, n )
       repeat n
          serial.tx(byte[x++])
    
    This assumes you're using FullDuplexSerial to send your message. You'd call fixedLengthString(@MessageSentToServer,11) since there are 11 bytes to send.
  • Duane DegnDuane Degn Posts: 10,588
    edited 2011-09-29 14:20
    LTech,

    Where does it say
    ->It has to be in hexadecimal code

    The only way I know of sending hexadecimal is to send it in ASCII characters. When I've seen this done, it's usually two characters per byte (doubling the size of the data).

    So to send the data Phil entered
    MessageSentToServer byte $11, $01, $01, $06, $00, $25, $10, $08, $0E, $01, $01
    

    You'd use
    MessageSentToServer byte "11010106002510080E0101", 0
    

    I use this technique myself since it lets me use the ASCII control characters mixed in with data.

    Here's a method I use to poke the ASCII characters into an array.
    PUB Hex(value, digits, localPtr)
    '' Print a hexadecimal number
      value <<= (8 - digits) << 2
      repeat digits
        byte[localPtr++] := lookupz((value <-= 4) & $F : "0".."9", "A".."F")
     
    

    To retrieve the values from the ASCII characters, I use.
    PUB FromAsciiHex(digits, localPtr) | localCharacter  
      repeat digits
        localCharacter := byte[localPtr++]
        if localCharacter => "0" and localCharacter =< "9"
          localCharacter -= "0"
        elseif localCharacter => "A" and localCharacter =< "F"
          localCharacter -= "A"
          localCharacter += $0A
        else
          localCharacter := 0
        result <<= 4
        result += localCharacter
     
    

    The second method isn't as efficient as the first one since I adapted the first method from someone elses code. The second method assumes capital letters are used for values $A and $F.

    Duane
  • JonnyMacJonnyMac Posts: 9,205
    edited 2011-09-29 16:51
    The last couple weeks I've been working with MODBUS and these routines are part of my experiments -- they may be useful to you.
    pub rtu2asc(val, pntr)
    
    '' Convert byte value to 2-byte MODBUS ASCII string
    '' -- output is to buffer at pntr
    
      byte[pntr++] := dec2hex(val >> 4)
      byte[pntr]   := dec2hex(val & $F)
    
    
    pub dec2hex(d)
    
    '' Converts decimal value, 0 - 15, to single ASCII character
    
      return lookupz(d : "0".."9", "A".."F")
    
    
    pub asc2rtu(pntr) 
    
    '' Converts 2-byte MODBUS ASCII string to byte
    '' -- input is from buffer at pntr
    
      return (hex2dec(byte[pntr++]) << 4) | hex2dec(byte[pntr])
    
    
    pub hex2dec(h)
    
    '' Converts hex character, "0".."F", to decimal value
    
      return lookdownz(h : "0".."9", "A".."F")
    
  • Duane DegnDuane Degn Posts: 10,588
    edited 2011-09-29 16:59
    Jon,

    That is very useful. Thank you.

    Duane
  • LtechLtech Posts: 380
    edited 2011-09-30 00:40
    Great !

    I love the way everybody is helping here !
    JonnyMac give me the best choice for my system.
    Now I count manualy the bytes of hex data

    Ps I use it on spinneret to remote control a huge live broadcast HD video switcher .
    Inpressive small spinneret !
Sign In or Register to comment.