Shop OBEX P1 Docs P2 Docs Learn Events
Comunications help / using a repeat loop and I am screaming mad!!!! — Parallax Forums

Comunications help / using a repeat loop and I am screaming mad!!!!

Zap-oZap-o Posts: 452
edited 2009-08-09 03:55 in Propeller 1
Below I am trying to run a loop that can eventiually do other task while waiting to fill a byte array. For some reason I am way out on this one.

Code
CON                                                  
  _clkmode = xtal1 + pll16x                             
  _xinfreq = 5_000_000                          
  USBRx             = 31                            
  USBTx             = 30                                                          
OBJ
  Coms         : "FullDuplex4"    
VAR                                                
        
  Byte RxData[noparse][[/noparse] 20 ] ,Delimiter
  
  
Pub Initialize
{{Starts Cogs and sets some data structure }}

  Coms.AddPort(0,USBRX,USBTX,-1,-1,0, %000000,19200)
  Coms.start
  MainLoop

Pub MainLoop  | idx

  bytefill(@rxData,0,20)
 
  idx :=0 
  Delimiter := ","
  
  Repeat                                     
      IF (Coms.rxCheck(0) > -1)
               RxData[noparse][[/noparse] idx ] := Coms.Rx(0)
               idx++
              ' Coms.str(0,string("debug")) this works so far                   
               IF (RxData[noparse][[/noparse] idx ] == Delimiter)
                                idx := 0                                                                                              
                                Coms.hex(0,rxData[noparse][[/noparse] 0 ],2)                                                          
                                Coms.hex(0,rxData[noparse][[/noparse] 1 ],2)
                                Coms.hex(0,rxData[noparse][[/noparse] 2 ],2)
                                Coms.hex(0,rxData[noparse][[/noparse] 3 ],2)
                                Coms.hex(0,rxData[noparse][[/noparse] 4 ],2)
                                Coms.hex(0,rxData[noparse][[/noparse] 5 ],2)
                                Coms.hex(0,rxData[noparse][[/noparse] 6 ],2)
                                Coms.hex(0,rxData[noparse][[/noparse] 7 ],2)
                                Coms.hex(0,rxData[noparse][[/noparse] 8 ],2)
                                Coms.hex(0,rxData[noparse][[/noparse] 9 ],2)
                                Coms.hex(0,rxData[noparse][[/noparse] 10 ],2)




I am doing something horribly wrong. I know I can use the strings object but I want to learn on my own.

Comments

  • John AbshierJohn Abshier Posts: 1,116
    edited 2009-08-09 01:14
    You read a char into RxData[noparse][[/noparse] idx ], then you increment idx. You then check if RxData[noparse][[/noparse] idx ] == Delimiter. But idx now points to the char in the array just past the one you read. Use IF (RXData[noparse][[/noparse] idx-1 ] == Delimiter

    John Abshier
  • Peter JakackiPeter Jakacki Posts: 10,193
    edited 2009-08-09 01:29
    Also rxcheck returns with the character if it is available OR a -1. You are assuming that rxcheck always returns with the flag and not the character so therefore you are waiting for the next character by using com.rx(0).

    PUB rxcheck(port) : rxbyte
    '' Check if byte received (never waits)
    '' returns -1 if no byte received, $00..$FF if byte

    So I would use:
    ch := com.rxcheck(0)
    if ch+1
    RxData[noparse][[/noparse] idx ] := ch

    An easier way would be to modify you serial object so that you specify the buffer to use which would then be your array.

    *Peter*
  • Zap-oZap-o Posts: 452
    edited 2009-08-09 01:36
    What you both mentioned makes since or works so far. Thanks John for clearing up that little flaw.

    Peter what is this doing? Forgive me if it seems stupid.

    if ch+1
    
    



    I have never seen this before? I understand the rest of the code you posted but this line has me confused.
  • Peter JakackiPeter Jakacki Posts: 10,193
    edited 2009-08-09 01:59
    Assuming you have defined a local variable such as ch then that's just a shortcut method for testing a -1 as -1+1 = 0 or false. Any non-zero is accepted as a boolean TRUE. The normal method of comparison is always less efficient as it compiles a constant to compare against and then the comparison operation.

    You could also use "if ++ch" I guess as this should preincrement then test.

    *Peter*
  • Peter JakackiPeter Jakacki Posts: 10,193
    edited 2009-08-09 02:24
    Just as a follow-up on what I said previously about booleans and conditional operators. If you understand that the Spin interpreter (vs compiler) is a stack based virtual machine and not altogether different from the Forth virtual machine then you will know that conditional operators accept booleans but those booleans are represented by a number on the stack. By convention a 0 represents a "false" while any other number is "true" although it can be promoted to a proper true which is a -1. This may be necessary if you are performing operations on booleans as in line 17 of the example code listing (BST).

    *Peter*

    10                        if ch > -1
    Addr : 0018:             64  : Variable Operation Local Offset - 1 Read
    Addr : 0019:             34  : Constant 0 $FFFFFFFF
    Addr : 001A:             FA  : Math Op >     
    Addr : 001B: JZ Label0002
    Addr : 001B:          0A 00  : jz Address = 001D 0
    Addr : 001D: Label0002
    Addr : 001D: Label0003
    11                        if ch+1
    Addr : 001D:             64  : Variable Operation Local Offset - 1 Read
    Addr : 001E:             36  : Constant 2 $00000001
    Addr : 001F:             EC  : Math Op +     
    Addr : 0020: JZ Label0004
    Addr : 0020:          0A 00  : jz Address = 0022 0
    Addr : 0022: Label0004
    Addr : 0022: Label0005
    13                        if ++ch
    Addr : 0022:          66 A6  : Variable Operation Local Offset - 1 Assign ++VAR pre-inc Long Push
    Addr : 0024: JZ Label0006
    Addr : 0024:          0A 00  : jz Address = 0026 0
    Addr : 0026: Label0006
    Addr : 0026: Label0007
    15                        if ch++
    Addr : 0026:          66 AE  : Variable Operation Local Offset - 1 Assign VAR++ post inc Long Push
    Addr : 0028: JZ Label0008
    Addr : 0028:          0A 00  : jz Address = 002A 0
    Addr : 002A: Label0008
    Addr : 002A: Label0009
    17                        if ch AND true
    Addr : 002A:             64  : Variable Operation Local Offset - 1 Read
    Addr : 002B:             34  : Constant 0 $FFFFFFFF
    Addr : 002C:             F0  : Math Op AND   
    Addr : 002D: JZ Label000A
    Addr : 002D:          0A 00  : jz Address = 002F 0
    Addr : 002F: Label000A
    Addr : 002F: Label000B
    
    
    
  • Zap-oZap-o Posts: 452
    edited 2009-08-09 02:31
    So peter what you are saying in that line of code is

    IF (CH is a positive 1)

    I was thinking it meant that CH was to be added to 1

    I think I understand it now.

    *Edited

    Could I also sate this:
    IF (CH == true)
  • Peter JakackiPeter Jakacki Posts: 10,193
    edited 2009-08-09 03:03
    Yes, but notice that IF always compiles to a postfix "jump-on-zero" so whether you perform an operation or not it will only execute if the result/variable is true. So your redundant operation is no different to comparing it with a -1. The pre or post increment is the most efficient way to check for a -1.

    *Peter*


    10                        if ch > -1
    Addr : 0018:             64  : Variable Operation Local Offset - 1 Read
    Addr : 0019:             34  : Constant 0 $FFFFFFFF
    Addr : 001A:             FA  : Math Op >     
    Addr : 001B: JZ Label0002
    Addr : 001B:          0A 00  : jz Address = 001D 0
    Addr : 001D: Label0002
    Addr : 001D: Label0003
    11                        IF (CH == true)
    Addr : 001D:             64  : Variable Operation Local Offset - 1 Read
    Addr : 001E:             34  : Constant 0 $FFFFFFFF
    Addr : 001F:             FC  : Math Op ==    
    Addr : 0020: JZ Label0004
    Addr : 0020:          0A 00  : jz Address = 0022 0
    Addr : 0022: Label0004
    Addr : 0022: Label0005
    
    
  • Zap-oZap-o Posts: 452
    edited 2009-08-09 03:55
    I went back to the old prop book and with the input you have provided in conjunction with the text, its sinking in deeply.

    Thank you kindly.
Sign In or Register to comment.