Shop OBEX P1 Docs P2 Docs Learn Events
Question about code using Full Duplex Serial — Parallax Forums

Question about code using Full Duplex Serial

JomsJoms Posts: 279
edited 2010-10-11 17:26 in Propeller 1
I am having some problems using code with the Full Duplex Serial object, and wondering if someone can explain why this doesn't work.

Using the following code works...
checksum := 655      ' hex is 28F

Code that works is:
debug.hex(checksum,2)

I copied this from the FDS object. Why wouldn't this work just in a differen't spin file?
    checksum <<= (8 - 2) << 2

    debug.tx(lookupz((checksum <-= 4) & $F : "0".."9", "A".."F"))
    debug.tx(lookupz((checksum <-= 4) & $F : "0".."9", "A".."F"))

When I use the 1 line version, I get 8F, which is what I need. My end goal is to get "8?", which is the same as 8 + 30 = 38 or "8" and F + 30 = 3F or "?".

But right now I am just trying to take small steps and learn as I go. Eventually I will to those calculations for each line, basically removing the lookup command and do the math.

Thanks in advance for help understanding this...

Comments

  • Mike GreenMike Green Posts: 23,101
    edited 2010-10-11 15:46
    FullDuplexSerial already has the .hex routine. Are you just trying to understand how it works or do you want to duplicate its functionality?

    Your 3-line version is
    checksum <<= (8 - 2) << 2
        debug.tx(lookupz((checksum <-= 4) & $F : "0".."9", "A".."F"))
        debug.tx(lookupz((checksum <-= 4) & $F : "0".."9", "A".."F"))
    

    It's much easier to understand if you separate things out:
    checksum := checksum << 24  ' position byte of interest in upper 8 bits of long
    repeat 2   ' for each of the two hex digits, repeat the following
       checksum := checksum <- 4   ' rotate long to bring most significant 4 bits around
       temp := lookupz(checksum & $F : "0".."9", "A".."F")   ' get equivalent character
       debug.tx(temp)   ' output from left to right
    

    Does this help? (Note that this "destroys" the value in checksum)
  • John AbshierJohn Abshier Posts: 1,116
    edited 2010-10-11 15:47
    Here is a code fragment that prints 8 and ?
    [code]
    Sio.tx(((checksum <-= 4) & $F) + $30)
    Sio.tx(13)
    Sio.hex(checksum,8)
    Sio.tx(13)
    Sio.tx(((checksum <-= 4) & $F) + $30)
    Sio.tx(13)
    Sio.hex(checksum,8)
    Sio.tx(13)
    [\code]

    Your code with the lookupz returns the ASCII value of "0".."9", "A".."F" $30 for "0", $39 for "9", $41 for "A" and $46 for "F"

    John Abshier
  • JomsJoms Posts: 279
    edited 2010-10-11 16:15
    OK...

    Mike, I think I understand what is going on. I sat and spent some time, even before I posted, trying to figure out what was going on behind the scenes... The main question is, if it works in the FDS object, why wouldn't just copy and pasting it over make it work? To trouble shoot this further, I am going to try and recode it step by step like you showed in your explination. My end goal is to do the conversion per byte like I explained in the end of my first post, but I am also trying to learn how it works while I am putting it together this time. I try not to just make it work once and forget it, but learn from it so I can use bits and peices of what I learn in my next code...

    John, I seem to be kind of confused about your response. Why should I send 8 charactors? I think I understand what you are trying to get at, but all I get is '0' in any debug that I do.

    Thanks both of you for the help. If you have any ideas why I just keep getting a '0', please let me know...
  • Mike GreenMike Green Posts: 23,101
    edited 2010-10-11 16:21
    When you take a code fragment out of context, you may miss important initial conditions or final conditions. That's why it's very important to include a large enough piece of code along with any related declarations. In your case, you can take a small fragment out of a hexadecimal output routine and expect it will work in some other context. It may or it may work in some situations, but not in others and you have to look at the larger program to figure out why.
  • JomsJoms Posts: 279
    edited 2010-10-11 16:35
    I think I got it figured out guys. I didn't more reading on both your responses, and they helped me a lot. Mike hit it on the head when taking code out, the problem I found, I needed to reserve a LONG instead of a BYTE.

    But your help was not wasted. I am still learning, so thank you for your help!
  • John AbshierJohn Abshier Posts: 1,116
    edited 2010-10-11 17:26
    I printed out checksum as an 8 hex character value to see what its value was at each step.

    Sio.tx(((checksum <-= 4) & $F) + $30) only prints out 1 character.

    John Abshier
Sign In or Register to comment.