Shop OBEX P1 Docs P2 Docs Learn Events
Passing byte strings to a routine - clarity of thinking help please — Parallax Forums

Passing byte strings to a routine - clarity of thinking help please

pacmanpacman Posts: 327
edited 2013-03-25 20:13 in Propeller 1
I know this has been asked before, but I'm unable to find the thread.

I have a string (of hex bytes) that is of the format "09 01 04 00 00 00 04 F1 C9" {The spaces are my addition to make reading easier}

The first byte contains the number of bytes of information (the sting often contains '00' thus null terminated methods don't work (like strlength - I've discovered)

Now this string lives in a VAR declaration
byte InBuffer[25]

So when I rattle through the array from the MAIN routine I get the responses I expect -
j := 0
  repeat while j < Inbuffer[0]
    PST.newline  
    PST.dec(j)
    PST.str(string(" : "))
    PST.hex(inbuffer[j++],2)


It's when I pass this string to a function that it gets all wierd on me
eg: CheckFrame[@inbuffer]

So in CheckFrame(mesg) I do the same repeat loop
j := 0
  repeat while j < mesg[0]
    PST.newline  
    PST.dec(j)
    PST.str(string(" : "))
    PST.hex(mesg[j++],2)

The second set of debug info has vaules like D9 as the 'first result'


I'm currently thinking that is _might_ be that as 'mesg' is not defined as a byte array I'm getting some overlap from the higher order bits. Is my thinking correct? - My theory _seems_ (by me) to be backed up as if I run the first snippet in CheckFrame {without passing the string pointer, and thus using global varaibles) I get the same result correct result.

If that is correct - then now can I specificy that mesg is a btye array in CheckFrame? should I just be using something like PST.hex(mesg.byte[j++],2)?

Or am I just overly confused and the solution is painfully ovbious to all (except me) - so can you explain it in _simple_ terms.



Isn't it amazing how you can come up with 'other' ideas after you have pressed submit? or even at 11pm...

Other potential solutions I can think of - may or may not be good
Copy InBuffer to a long array like InBufferLong then I could just pass @inBufferLong to CheckFrame
Define Inbuffer as Long to start with - even though no single 'value' will be more than FF, memory {at this stage} is not an issue.

Merits/problems with alternate solutions?

Comments

  • kuronekokuroneko Posts: 3,623
    edited 2013-03-25 16:51
    In Checkframe all you have is a byte address so you should use byte[mesg][idx].
  • JonnyMacJonnyMac Posts: 9,108
    edited 2013-03-25 16:57
    I'm really not very good at fixing code written by others, but if I understand an requirement I can usually come up with a solution. Last year I was doing a lot of parsing experiments and wrote a string library that could extract various numeric formats from a string. I've used it to tackle your problem. It may not be what you want, but will give you something that works to think about.
  • pacmanpacman Posts: 327
    edited 2013-03-25 20:01
    kuroneko wrote: »
    In Checkframe all you have is a byte address so you should use byte[mesg][idx].

    Thanks but are the indexes the same?

    would Inbuffer[2] == byte(mesg[2]) for example

    or do I have to work with some offsets on the mesg array?
  • kuronekokuroneko Posts: 3,623
    edited 2013-03-25 20:13
    Indices are the same, e.g. Inbuffer[2] == byte[mesg][2] (note the use of brackets).
    CON
      _clkmode = XTAL1|PLL16X
      _xinfreq = 5_000_000
      
    OBJ
      serial: "FullDuplexSerial"
    
    VAR
      byte  Inbuffer[16]
      
    PUB null | j
    
      serial.start(31, 30, %0000, 115200)
      waitcnt(clkfreq*3 + cnt)
      serial.tx(0)
      
      bytemove(@Inbuffer{0}, @value, value{0})
      
      j := 0
      repeat while j < Inbuffer{0}
        serial.tx(13)
        serial.dec(j)
        serial.str(string(" : "))
        serial.hex(inbuffer[j++],2)
    
      serial.tx(13)
      Checkframe(@Inbuffer{0})
      
    PRI Checkframe(mesg) | j
    
      j := 0
      repeat while j < byte[mesg]{0}
        serial.tx(13)
        serial.dec(j)
        serial.str(string(" : "))
        serial.hex(byte[mesg][j++],2)
        
    DAT
    
    value   byte    $09, $01, $04, $00, $00, $00, $04, $F1, $C9
                            
    DAT
    
Sign In or Register to comment.