Shop OBEX P1 Docs P2 Docs Learn Events
Missing first byte stored in a buffer. Need some help with this. — Parallax Forums

Missing first byte stored in a buffer. Need some help with this.

Don MDon M Posts: 1,652
edited 2013-05-01 19:13 in Propeller 1
I am receiving in some data into a buffer. For some reason I am missing the first byte. It must be the way I have this loop structured but can't figure why it won't work.

I can see the data on the analyzer so I know it's there but somehow my indexing is off by one. The buffer is 32 bytes. The final check byte has 9th bit set so that is the reason for the $1_00 check.
  inx := 0 
  s := serial.rx_time(20)
  if s > -1
    'inx := 0
    chksum := s 
    buffer[inx] := s 
    repeat
      s := serial.rx_time(5)
      if (s & $1_00)
        chksum &=  $0_FF
        if (chksum == (s & $0_FF))
          pollack
          seq++ 
          term.str(string("ID: ",13,10,03))
          quit
      chksum += s
      buffer[inx] := s
      inx ++
  else
    term.str(string("No Response to ID",13,10,03)) 
    seq := 4        

  repeat inx from 0 to 28
    term.tx(byte[@buffer][inx])
    term.tx(32)
  term.tx(13)

Comments

  • Don MDon M Posts: 1,652
    edited 2013-05-01 18:23
    If I add a "term.tx(s)" in a couple places I see on PST what I need but for some reason the buffer misses the first byte.
      if s > -1
        chksum := s 
        buffer[inx] := s
        term.tx(s)     '<<< This shows the first byte. Why isn't it stored in buffer?
        repeat
          s := serial.rx_time(5)
          buffer[inx] := s
          if (s & $1_00)
            chksum &=  $0_FF
            if (chksum == (s & $0_FF))
              pollack
              seq++ 
              term.str(string("ID: ",13,10,03))
              quit
          chksum += s
          buffer[inx] := s
          term.tx(s)      
          inx ++
    
  • kwinnkwinn Posts: 8,697
    edited 2013-05-01 18:31
    It looks like it should work. Try changing inx := 0 to inx := 1 and increasing the buffer size by one to see if the first byte is lost in the input loop or output loop.
  • Don MDon M Posts: 1,652
    edited 2013-05-01 18:38
    No that didn't help.

    What I don't understand is if I can see the first byte from the first term.tx(s) then why isn't this same byte stored to buffer[idx] when idx starts at 0?

    I can do term.tx(byte[@buffer][0]) and I see the second byte that is received not the first. Weird....
  • juropjurop Posts: 43
    edited 2013-05-01 18:42
    Try changing
    ...
          chksum += s
          buffer[inx] := s
          inx ++
      else
    ...
    
    with
    ... 
           chksum += s
           buffer[++inx] := s
      else
    
  • Don MDon M Posts: 1,652
    edited 2013-05-01 18:46
    jurop wrote: »
    Try changing
    ...
          chksum += s
          buffer[inx] := s
          inx ++
      else
    ...
    
    with
    ... 
           chksum += s
           buffer[++inx] := s
      else
    

    Nope. Still doesn't show correct first byte from buffer.
  • Don MDon M Posts: 1,652
    edited 2013-05-01 19:09
    Ok I got it to work.

    Seems as though inx was not being advanced after the first byte stored. See below.
      inx := 0 
      s := serial.rx_time(20)
      
      if s > -1
        chksum := s 
        buffer[inx] := s
        inx++               '<<<< Here is where my problem was....
        repeat
          s := serial.rx_time(5)
          if (s & $1_00)
            chksum &=  $0_FF
            if (chksum == (s & $0_FF))
              pollack
              seq++ 
              term.str(string("ID: ",13,10,03))
              quit
          chksum += s
          buffer[inx] := s
          inx++
      else
        term.str(string("No Response to ID",13,10,03)) 
        seq := 4        
    
  • Don MDon M Posts: 1,652
    edited 2013-05-01 19:13
    This works too...
    inx := 0 
      s := serial.rx_time(20)
      
      if s > -1
        chksum := s 
        buffer[inx++] := s
        repeat
          s := serial.rx_time(5)
          if (s & $1_00)
            chksum &=  $0_FF
            if (chksum == (s & $0_FF))
              pollack
              seq++ 
              term.str(string("ID: ",13,10,03))
              quit
          chksum += s
          buffer[inx++] := s
      else
        term.str(string("No Response to ID",13,10,03)) 
        seq := 4
    
Sign In or Register to comment.