Numeric Values


Closed Thread
Results 1 to 9 of 9

Thread: Numeric Values

  1. #1

    Question Numeric Values

    Hello again, hope everybody´s fine!

    I have some little problem here. I am receiving serial data (numbers) and I need to compare them (is it "compare them" well written?), anyways, I need to do so, but it seems that when I try to compare this data, it is taken as ascii code, so I always get the same result no matter what the data is. Is there a way to make a kind of cast to convert this ascii data into numeric data so I can do my comparison?
    Thank all of you and special thank to Adam who´s I´ve been receiving too much help from.

    Armando.

  2. #2
    Join Date
    Sep 2004
    Location
    montreal, canada
    Posts
    6,898


    Did you find this post helpful? Yes | No

    Default

    look for DEC modifier or #

    Post your code here, it will be easier to answer
    Steve

    It's not a bug, it's a random feature.
    There's no problem, only learning opportunities.

  3. #3
    Join Date
    Feb 2003
    Location
    Salt Lake City, Utah USA
    Posts
    517


    Did you find this post helpful? Yes | No

    Smile

    As Steve said, post your code so we do not have to guess. If your data is in ASCII, then

    Decimal 0 = 48 in ASCII
    Decimal 1 = 49 in ASCII
    Decimal 2 = 40 in ASCII
    etc.

    You can convert your received ASCII numbers to decimal by

    Code:
    X VAR Byte ' Received value
    X = X – 48 ' convert from ASCII to decimal (or X = X - “0” does the same thing)
    If X < 7 THEN Something ' your compare
    or you could do your compare like this
    Code:
    X VAR Byte ' Received value
    If X < “7” THEN Something ' “7” = decimal 55 but is 7 in ASCII
    Again, post your code if you cannot make yours work.

    Paul Borgmeier
    Salt Lake City, Utah
    USA

  4. #4


    Did you find this post helpful? Yes | No

    Talking Here´s my code

    Well, thank you for your answers. The code I am using is something like this:

    hserin [skip 2, str var\4 \32]

    I know that I´m gonna recieve numbers, so I need to do something like this:

    if var == 100 then
    code here...
    else
    if var < 100 then
    code her...
    else
    code here...
    endif
    endif

    just that var contains ascii data. I had already though about some code to convert that ascii data to numbers but I´d like to know if there is something quicker because I receive data from 1 to 3 digits and I´m trying to write the less lines possible.

    Thanks to all of you again.

    Armando Hdez.

  5. #5
    Join Date
    Feb 2003
    Location
    Salt Lake City, Utah USA
    Posts
    517


    Did you find this post helpful? Yes | No

    Smile

    Armando,

    I believe “str” it is referring to an array string not a string string. You should have something like this as the variable declare. (Let's change your variable name from VAR to RX_Var since VAR is reserved)

    RX_Var var byte[4]

    Then with your call, if your incoming data looks like this

    49 ' ascii 1
    50 ' ascii 2
    51 ' etc.
    52
    53
    54


    hserin [skip 2, str RX_Var\4 \32 ]

    Your data captured data will look like

    RX_Var[0]=51
    RX_Var[1]=52
    RX_Var[2]=53
    RX_Var[3]=54

    If you need more help or suggestions, let us know ...

    Paul Borgmeier
    Salt Lake City, Utah
    USA
    Last edited by paul borgmeier; - 25th June 2006 at 17:44.

  6. #6


    Did you find this post helpful? Yes | No

    Smile Ohhhh

    Ok thank you! You´re right I have an array but then you are telling me there´s another way to get a string of characters serialy?? What do you mean with a "string string"?? Thank you very much I really appreciate it!

    Armando.

  7. #7
    Join Date
    Feb 2003
    Location
    Salt Lake City, Utah USA
    Posts
    517


    Did you find this post helpful? Yes | No

    Smile

    Quote Originally Posted by Armando Herjim
    ... What do you mean with a "string string"?? ...
    Poor choice of words on my part – there is no string variable in PBP. i.e., you cannot have RX_Var = “Hi There” in PBP even though it is allowed in most traditional BASIC programs if RX_Var is declared as a string variable (See Visual Basic for example). You can have

    X[0]=”H”
    X[1]=”i”
    X[2]=” “
    etc.

    You have a few choices from what I can see. You can make sure you always send and receive 4 digits of a decimal number and use DEC as Steve suggested. Or you could “roll your own” with your array values: (highly unoptimized but should get the point across)

    NewValue VAR WORD

    RX_Var[0]=51
    RX_Var[1]=52
    RX_Var[2]=53
    RX_Var[3]=54

    IF RX_Var[0] = “ “ THEN GoSomeWhere
    NewValue = RX_Var[0] - “0”

    FOR X = 1 to 3
    IF RX_Var[X] = “ “ THEN GoSomeWhere
    NewValue = (NewValue * 10) + RX_Var[X]-”0”
    NEXT X

    You also can probably do it with all the options of SERIN2 but I have not used them enough to be able to suggest the format. Someone else?

    Good Luck,

    Paul Borgmeier
    Salt Lake City, Utah
    USA

  8. #8
    Join Date
    Feb 2003
    Location
    Salt Lake City, Utah USA
    Posts
    517


    Did you find this post helpful? Yes | No

    Smile

    A quick after thought - why not just send and receive your data a low byte and a high byte of a word sized variable and be done.

    For example

    X var word

    X = 456 ' your "desired" value

    send
    X.lowbyte = 200
    X.highbyte = 1

    then X will be 456 - done.

    Paul

  9. #9


    Did you find this post helpful? Yes | No

    Smile Good Idea!

    Hey that seems to be an excellent idea. Thank you very much!!!

Similar Threads

  1. Formating numeric values to text
    By Andre_Pretorius in forum General
    Replies: 2
    Last Post: - 1st August 2009, 08:39
  2. WRITECODE stores wrong 14-bit word values in FlashMEM
    By BobPigford in forum mel PIC BASIC Pro
    Replies: 18
    Last Post: - 26th June 2009, 04:35
  3. Looking at pic basic pro symbol values in mplab 8.15a simulator
    By ukemigrant in forum mel PIC BASIC Pro
    Replies: 10
    Last Post: - 16th May 2009, 13:18
  4. Funny PULSIN values: what is going on???
    By xnihilo in forum mel PIC BASIC Pro
    Replies: 5
    Last Post: - 30th April 2008, 08:02
  5. 16F877A problem: "coupled" digital values
    By Lupo83 in forum General
    Replies: 16
    Last Post: - 4th December 2007, 12:46

Members who have read this thread : 2

You do not have permission to view the list of names.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts