Shop OBEX P1 Docs P2 Docs Learn Events
Serial Syntax Question: What is this doing? — Parallax Forums

Serial Syntax Question: What is this doing?

fmaurerfmaurer Posts: 4
edited 2011-06-14 15:40 in Propeller 1
I don't understand what my code is doing; the result is unexpected in my eyes.

I'm reading serial bytes on my propeller using JDCogSerial (http://obex.parallax.com/objects/398/) and this command:
recByte = Comm.rxCheck
if (recByte <> -1)
      ... do something

If I send $FF, the firmware will run the code inside the "if" (but I don't want it to). I need it to only perform the code inside when a byte is received. I'm assuming that $FF can also be -1 and this is causing the problem?

Here's the rxCheck function definition:
PUB rxcheck : rxbyte
{ Check if byte received (never waits)
  returns < 0 if no byte received, $00..$FF if byte }
  if (rxbyte := rx1_buf) => 0
    rx1_buf := -1

Is there a way to check if a byte is received, and if so do something with it (even if it is 255)?

My code runs fine if I make sure I don't send a byte value of 255 (I cap everything at 254).

Comments

  • SapiehaSapieha Posts: 2,964
    edited 2011-06-14 15:03
    Hi fmauer.

    "
    if (recByte <> -1)" Is not $FF - But $FFFFFFFF.
    And $FF are still received Character = 255 Decimal.

    You need always check for -1 for no received character.


    fmaurer wrote: »
    I don't understand what my code is doing; the result is unexpected in my eyes.

    I'm reading serial bytes on my propeller using JDCogSerial (http://obex.parallax.com/objects/398/) and this command:
    recByte = Comm.rxCheck
    if (recByte <> -1)
          ... do something
    
    If I send $FF, the firmware will run the code inside the "if" (but I don't want it to). I need it to only perform the code inside when a byte is received. I'm assuming that $FF can also be -1 and this is causing the problem?

    Here's the rxCheck function definition:
    PUB rxcheck : rxbyte
    { Check if byte received (never waits)
      returns < 0 if no byte received, $00..$FF if byte }
      if (rxbyte := rx1_buf) => 0
        rx1_buf := -1
    
    Is there a way to check if a byte is received, and if so do something with it (even if it is 255)?

    My code runs fine if I make sure I don't send a byte value of 255 (I cap everything at 254).


    Ps. That give You possibility to receive all characters from 0 to 255
  • Dave HeinDave Hein Posts: 6,347
    edited 2011-06-14 15:40
    fmaurer,

    Can you post all your code? rxCheck should be doing the right thing. Maybe there is a problem in how you handle recByte. If you happened to declare recByte as a byte or word it will never contain a value of -1, but will be 255 or 65535 instead.

    A value of 255 is as valid as any other 8-bit value. It shouldn't cause your code to do anything differently unless you are testing for 255.

    Dave
Sign In or Register to comment.