Shop OBEX P1 Docs P2 Docs Learn Events
Bluesmirf reading hex value with propeller - Page 2 — Parallax Forums

Bluesmirf reading hex value with propeller

2»

Comments

  • RyoshimaRyoshima Posts: 34
    edited 2011-11-30 21:07
    ok, I apologize I'm running on little sleep and I'm on the verge of getting my senior project working and this one thing is what is standing in my way.
  • kuronekokuroneko Posts: 3,623
    edited 2011-11-30 21:11
    No need to apologize. The incoming data will most likely be always lower case anyway so the conversion is there just in case. Effectively "a".."f" is treated the same as "A".."F". You mentioned that you had ratronic's program working (reading $66). Meaning you already read the first byte of the number. Now simply read another byte (io.rx) and put both values through the conversion which gives you the value you want.
    repeat                                          'repeat continued
        repeat until io.rx == "R"                     'wait here until "R" received
        repeat i from 0 to 2                          'receive 3 more characters "SSI"
          io.rx                                       '
        if io.rx == "="                               'if the fifth character is "="
          A := io.rx                                  ' then assign 6th character to A
          B := io.rx                                  ' then assign 7th character to B
          C := convert(A)*16 + convert(B)
    
  • RyoshimaRyoshima Posts: 34
    edited 2011-11-30 22:00
    Ok, trying to put the result of C on the serial terminal, but nothing is displaying
    EDIT: Once this part gets fixed I think that should do it, just need to return the value of C.
    CON
            _CLKMODE = XTAL1 + PLL16X
            _XINFREQ = 5_000_000
        
    OBJ
    io      :       "FullDuplexSerial.spin"
    pst     :       "Parallax Serial terminal" 
    Pub getRSSI | i, A, B, C
      pst.StartRxTx(31,30,0,9600)  'this starts the serial terminal for debug display
      waitcnt(clkfreq*3 + cnt)     'delay 3 seconds so you can open the parallax serial terminal
      io.start(0, 1, 0, 9600)      'this starts the serial port for your bluesmirf
      io.str(string("$$$"))        'this sends string
      
      repeat 1000000               'use waitcnt for delay
      io.tx(13)                    'transmit CHR/LF
      io.tx("C")                   'transmit "C"
      io.tx(13)                    'transmit CHR/LF
      repeat 1000000               'use waitcnt for delay
      io.str(string("$$$"))        'send string
      repeat 1000000               'use waitcnt for delay
      io.tx(13)                    'transmit CHR/LF 
      io.tx("L")                   'transmit "L"   
      io.tx(13)                    'transmit CHR/LF  
      repeat                                          'repeat continued
        repeat until io.rx == "R"                     'wait here until "R" received
        repeat i from 0 to 2                          'receive 3 more characters "SSI"
          io.rx                                       '
        if io.rx == "="                               'if the fifth character is "="
          A := io.rx
          B := io.rx                                  ' then assign A to sixth character
        waitcnt(clkfreq/10 + cnt)                     'delay 1/10 of a second
        C := convert(A)*16 + convert(B)
        waitcnt(clkfreq/10 + cnt)
        pst.Str(C)
        waitcnt(clkfreq/10 + cnt)
        
    PRI convert(char)
    
    
      case char
        "0".."9":           return char - "0"
        "a".."f", "A".."F": return (char | $20) - "a" + 10
        other:              abort
    
  • kuronekokuroneko Posts: 3,623
    edited 2011-11-30 23:28
    C is not a string, just a byte value. So try pst.hex(C, 2) or pst.dec(C) instead. Also, while still in the experimental state you might want to consider replacing abort with return.
  • RyoshimaRyoshima Posts: 34
    edited 2011-11-30 23:45
    Ok I will try that, I have to go to bed, need to get up to a controls systems class in a few hours. When I try this I will write back with results. Also again, thank you so much for the help. This is the most helpful website I've ever used.
  • kuronekokuroneko Posts: 3,623
    edited 2011-11-30 23:47
    The code as such works. I used this slightly modified test case (fake bluesmirf):
    CON
      _CLKMODE = XTAL1 + PLL16X
      _XINFREQ = 5_000_000
        
    OBJ
      io[3]: "FullDuplexSerial"
    
    VAR
      long  stack[32]
      
    PUB getRSSI | i, A, B, C
    
      cognew(blue, @stack{0})
      
      io{0}.start(31, 30, 0, 115200)                        'this starts the serial terminal for debug display
      waitcnt(clkfreq*3 + cnt)     'delay 3 seconds so you can open the parallax serial terminal
      io[1].start(16, 17, 0, 9600) 'this starts the serial port for your bluesmirf
      io[1].str(string("$$$"))     'this sends string
      
      repeat 1000000               'use waitcnt for delay
      io[1].tx(13)                 'transmit CHR/LF
      io[1].tx("C")                'transmit "C"
      io[1].tx(13)                 'transmit CHR/LF
      repeat 1000000               'use waitcnt for delay
      io[1].str(string("$$$"))     'send string
      repeat 1000000               'use waitcnt for delay
      io[1].tx(13)                 'transmit CHR/LF 
      io[1].tx("L")                'transmit "L"   
      io[1].tx(13)                 'transmit CHR/LF  
      repeat                                          'repeat continued
        repeat until io[1].rx == "R"                  'wait here until "R" received
        repeat i from 0 to 2                          'receive 3 more characters "SSI"
          io[1].rx                                    '
        if io[1].rx == "="                            'if the fifth character is "="
          A := io[1].rx
          B := io[1].rx                               ' then assign A to sixth character
          C := convert(A)*16 + convert(B)
          waitcnt(clkfreq/10 + cnt)
          io{0}.hex(C, 2)
          waitcnt(clkfreq/10 + cnt)
        
    PRI convert(char)
    
      case char
        "0".."9":           return char - "0"
        "a".."f", "A".."F": return (char | $20) - "a" + 10
        other:              return
    
    PRI blue
    
      io[2].start(17, 16, %0000, 9600)
    
      repeat while io[2].rx <> "L"
      repeat
        io[2].str(string("RSSI="))
        io[2].hex(frqa++, 2)
        io[2].tx(",")
        io[2].hex(0, 2)
        io[2].str(string(13, 10))
        waitcnt(clkfreq/2 + cnt)
        
    DAT
    
  • RyoshimaRyoshima Posts: 34
    edited 2011-12-01 09:35
    Great! instead of io{0}.hex(C,2), I put pst.hex(C,2) and got the two digit hex readings that I wanted on the serial terminal, now I would just like to have that result returned to the method so I can call RSSI in another program. To do that should I just say
    result io{0}.hex(C,2)
    

    ?

    Also how do I do a hex greater than or less than comparison?

    EDIT:
    I just put this as my code:
    CON
            _CLKMODE = XTAL1 + PLL16X
            _XINFREQ = 5_000_000
        
    OBJ
    io      :       "FullDuplexSerial.spin"
    pst     :       "Parallax Serial terminal" 
    Pub getRSSI | i, A, B, C
      pst.StartRxTx(31,30,0,9600)  'this starts the serial terminal for debug display
      waitcnt(clkfreq*3 + cnt)     'delay 3 seconds so you can open the parallax serial terminal
      io.start(0, 1, 0, 9600)      'this starts the serial port for your bluesmirf
      io.str(string("$$$"))        'this sends string
      
      repeat 1000000               'use waitcnt for delay
      io.tx(13)                    'transmit CHR/LF
      io.tx("C")                   'transmit "C"
      io.tx(13)                    'transmit CHR/LF
      repeat 1000000               'use waitcnt for delay
      io.str(string("$$$"))        'send string
      repeat 1000000               'use waitcnt for delay
      io.tx(13)                    'transmit CHR/LF 
      io.tx("L")                   'transmit "L"   
      io.tx(13)                    'transmit CHR/LF  
      repeat                                          'repeat continued
        repeat until io.rx == "R"                     'wait here until "R" received
        repeat i from 0 to 2                          'receive 3 more characters "SSI"
          io.rx                                       '
        if io.rx == "="                               'if the fifth character is "="
          A := io.rx
          B := io.rx                                  ' then assign A to sixth character
        return C := convert(A)*16 + convert(B)
        
    PRI convert(char) 
    
    
      case char
        "0".."9":           return char - "0"
        "a".."f", "A".."F": return (char | $20) - "a" + 10
        other:              return
    

    I have it set to return C. If I do this, I should be able to call the getRSSI method in another program and set a variable equal to getRSSI right?
    x := hex(getRSSI,2) ?
    or
    VAR
            BYTE X
    OBJ
    RSSI:      "getRSSI"
    
    PUB main | X
    
    X := RSSI
    
  • ratronicratronic Posts: 1,451
    edited 2011-12-01 14:52
    Ryoshima instead of
    X := RSSI
    
    you need to specify the method in RSSI your calling to get your value from
    X := RSSI.getRSSI
    
    Also you would usually set your clock and set up pst in your top object.

    Edit: I am assuming that you saved the top program in your post above as getRSSI.spin

    Edit2: Sometime's that can be a little confusing having an object named after one of it's methods.
  • RyoshimaRyoshima Posts: 34
    edited 2011-12-01 16:05
    Ok, I see, that's good to know.

    The code I currently have is not displaying my result.
    I just currently had pst.Str(X) setup, Im not sure what data type X is, even though it is displaying hex values.
    CON
            _CLKMODE = XTAL1 + PLL16X
            _XINFREQ = 5_000_000
        
    OBJ
    io      :       "FullDuplexSerial.spin"
    pst     :       "Parallax Serial terminal"
    VAR
    BYTE X
    PUB main
    X := getRSSI
    pst.str(X) 
    Pub getRSSI | i, A, B, C
      pst.StartRxTx(31,30,0,9600)  'this starts the serial terminal for debug display
      waitcnt(clkfreq*3 + cnt)     'delay 3 seconds so you can open the parallax serial terminal
      io.start(0, 1, 0, 9600)      'this starts the serial port for your bluesmirf
      io.str(string("$$$"))        'this sends string
      
      repeat 1000000               'use waitcnt for delay
      io.tx(13)                    'transmit CHR/LF
      io.tx("C")                   'transmit "C"
      io.tx(13)                    'transmit CHR/LF
      repeat 1000000               'use waitcnt for delay
      io.str(string("$$$"))        'send string
      repeat 1000000               'use waitcnt for delay
      io.tx(13)                    'transmit CHR/LF 
      io.tx("L")                   'transmit "L"   
      io.tx(13)                    'transmit CHR/LF  
      repeat                                          'repeat continued
        repeat until io.rx == "R"                     'wait here until "R" received
        repeat i from 0 to 2                          'receive 3 more characters "SSI"
          io.rx                                       '
        if io.rx == "="                               'if the fifth character is "="
          A := io.rx
          B := io.rx                                  ' then assign A to sixth character
        return C := convert(A)*16 + convert(B)
        
    PRI convert(char) 
    
    
      case char
        "0".."9":           return char - "0"
        "a".."f", "A".."F": return (char | $20) - "a" + 10
        other:              return
    
  • kuronekokuroneko Posts: 3,623
    edited 2011-12-01 16:07
    Ryoshima wrote: »
    return C := convert(A)*16 + convert(B)
    
    That looks OK (for returning the value). The issue I see here is that from the hex dump you sent in post #23 it looks like the bluesmirf keeps repeating the RSSI sequence on its own once it got the L command. If you keep calling the getRSSI method it may get confused because you restart the serial drivers etc. In that case it would be better to either stop said sequence (not sure if there is a command) before returning the result or let the bluesmirf loop run in its own cog and simply report the result to a global variable.
  • RyoshimaRyoshima Posts: 34
    edited 2011-12-01 16:50
    ok, how would I run it in its own cog? I've seen a way to do it, something like
    COGNEW(getRSSI, 'not sure what goes here')
    
  • kuronekokuroneko Posts: 3,623
    edited 2011-12-01 16:56
    The second parameter is a pointer to the private SPIN stack for this method, e.g.
    VAR
      long  stack[32]
    
    PUB start
    
      cognew(getRSSI, @stack{0})
    
    Inside that method you simply assign convert(A)*16 + convert(B) to a global variable instead of C. And you obviously don't return from it.
  • ratronicratronic Posts: 1,451
    edited 2011-12-01 17:36
    Ryoshima you don't have to start a new cog you could try something like this
    CON                                                                                           
            _CLKMODE = XTAL1 + PLL16X                                                             
            _XINFREQ = 5_000_000                                                                  
                                                                                                  
    OBJ                                                                                           
    io      :       "FullDuplexSerial.spin"                                                       
    pst     :       "Parallax Serial terminal"                                                    
    VAR                                                                                           
                                                                                                  
      long x                                                                                      
    PUB main    
                                                                                      
      io.start(0, 1, 0, 9600)      'this starts the serial port for your bluesmirf                                                                                                            
      pst.StartRxTx(31,30,0,9600)  'this starts the serial terminal for debug display             
      waitcnt(clkfreq*3 + cnt)     'delay 3 seconds so you can open the parallax serial terminal  
      repeat                                                                                      
        x := getRSSI               'assign x with value returned calling getRSSI                  
        pst.char(1)                'home debug cursor                                             
        pst.hex(x, 2)              'print 2 digit hex value of x                                  
        waitcnt(clkfreq+cnt)       'wait one second                                               
        io.rxflush                                                                                          
    Pub getRSSI | i, A, B, C                                                                      
                                                                      
                                                                                                  
     
      io.str(string("$$$"))        'this sends string                                             
                                                                                                  
      repeat 1000000               'use waitcnt for delay                                         
      io.tx(13)                    'transmit CHR/LF                                               
      io.tx("C")                   'transmit "C"                                                  
      io.tx(13)                    'transmit CHR/LF                                               
      repeat 1000000               'use waitcnt for delay                                         
      io.str(string("$$$"))        'send string                                                   
      repeat 1000000               'use waitcnt for delay                                         
      io.tx(13)                    'transmit CHR/LF                                               
      io.tx("L")                   'transmit "L"                                                  
      io.tx(13)                    'transmit CHR/LF                                               
      repeat until io.rx == "R"                     'wait here until "R" received                 
      repeat i from 0 to 2                          'receive 3 more characters "SSI"              
        io.rx                                       '                                             
      if io.rx == "="                               'if the fifth character is "="                
        A := io.rx                                                                                
        B := io.rx                                  ' then assign A to sixth character            
      return C := convert(A)*16 + convert(B)                                                      
                                                                                                  
                                                                                                  
    PRI convert(char)                                                                             
                                                                                                  
                                                                                                  
      case char                                                                                   
        "0".."9":           return char - "0"                                                     
        "a".."f", "A".."F": return (char | $20) - "a" + 10                                        
        other:              return
    

    Edit: you should replace the repeat 1000000 with a waitcnt and I added a rxflush for the io
  • RyoshimaRyoshima Posts: 34
    edited 2011-12-01 18:15
    Ok, once we get the returned value out of getRSSI, I would like to get 3 different readings with variables x,y,z. I would then like to compare x,y,z and find which variable has the greatest value. How would those comparisons go? Since these are hex values would I have to do something special?
  • ratronicratronic Posts: 1,451
    edited 2011-12-01 18:30
    I was also going to say that if the device spits out a comma between the two hex digits then you should add an extra io.rx between A and B. As far as comparing hex, decimal or anything else you would do something like this
    if x > y
      'do something
    

    Edit: nevermind about the comma I forgot you were wanting only one digit
Sign In or Register to comment.