Shop OBEX P1 Docs P2 Docs Learn Events
variable casting/conversion in pBasic — Parallax Forums

variable casting/conversion in pBasic

bryan costanichbryan costanich Posts: 29
edited 2007-01-21 08:38 in BASIC Stamp
i have a variable "200" and i need to parse that into an integer 100. how do i cast, convert, or parse that in pBasic? do they offer any commands?

Context:

i have a c# windows application that opens up a serial port connection, writes a number say "30" to the port and then closes the port.

my bs2 listens on that port and sticks that value into a variable, but that variable is text, not an integer. right now i have this temporary/hacky stuff:
SERIN 16, Baud9600, [noparse][[/noparse]numberOfBlinks] ' receive one byte

'DEBUG ? numberOfBlinks

SELECT numberOfBlinks
CASE "10"
Temp = 10
CASE "20"
Temp = 20
CASE "30"
Temp = 30
CASE "100"
Temp = 100
ENDSELECT

there's got to be a better way.

Comments

  • bryan costanichbryan costanich Posts: 29
    edited 2007-01-20 09:53
    ok, i found the formatters or whatever, but i can't seem to get the syntax right. i've tried:

    temp = DEC(numberOfBlinks)
    temp = DEC numberOfBlinks
    DEC temp = numberOfBlinks
    DEC(temp) = numberOfBlinks

    i've also tried

    SERIN 16, BAUD_9600, DEC([noparse][[/noparse]numberOfBlinks])
    SERIN 16, BAUD_9600, DEC [noparse][[/noparse]numberOfBlinks]

    etc.

    all of the syntax examples in the book are using

    DEBUG DEC 7

    or whatever. how do i do it on a variable?
  • bryan costanichbryan costanich Posts: 29
    edited 2007-01-20 10:08
    ok, i figured it out, but now i have a follow-up question. the syntax is as follows:
    SERIN 16, BAUD_9600, [noparse][[/noparse]DECx numberOfBlinks]

    where x is the number of digits. this is fine if i know the number of digits before hand, but what if i don't? for example, if i send in 20, then i would use [noparse][[/noparse]DEC20 numberOfBlinks], but what if it's 100? then i would have to use [noparse][[/noparse]DEC3 numberOfBlinks[noparse][[/noparse]

    so isn't there some smarter formatting available? any suggestions?
  • Bruce BatesBruce Bates Posts: 3,045
    edited 2007-01-20 11:56
    Bryan -

    With output formatter DEC x the x represents the MAXIMUM number of digits to be displayed. So, for 20 it would show "020" and for 100, it would show "100". Variables have no internal format other than as a binary number.

    If you have the ASCII equivalent of a decimal number and you want to convert the ASCII "number" to a decimal number, here is a technique suggested by "Bullwinkle" in a prior reply:

    quote

    Just subtract "0" (letter zero or $30 (hex)) from each byte, turning them into their "real" decimal equivalent. Moving from left to right, multiply result by 10 and add the next digit subtracting "0" each time.

    Like this:

    A VAR Byte
    B VAR Byte
    C VAR Byte
    D VAR Byte

    R VAR Word

    A = "1"
    B = "2"
    C = "3"
    D = "4" ' for example

    R = A - $30
    R = R*10 + B - $30
    R = R*10 + C - $30
    R = R*10 + D - $30

    DEBUG "Result is ",DEC R

    R will be stored internally and displayed as 1234 decimal.

    If the bytes your want converted to decimal are in contigous memory locations you use indexed addressing, like this:

    idx VAR Nib
    R = 0
    FOR idx = 0 TO 3 ' read bytes
    R = R*10 + A(idx) - $30
    NEXT

    end quote

    Regards,

    Bruce Bates

    Post Edited (Bruce Bates) : 1/20/2007 1:13:19 PM GMT
  • UnsoundcodeUnsoundcode Posts: 1,532
    edited 2007-01-20 17:44
    Hi bryan, Serin will accept your integer without naming·a character length if you send an end of string or new line character from your C# app. I dont know the syntax in C# but in VB it would be like this

    Stamp :- SERIN 16,BAUD_9600,[noparse][[/noparse]DEC numberOfBlinks]

    VB :- Serialport1.Write(my_number & Chr(10))· or· Serialport1.Writeline(my_number)

    Also be aware that the default port 16 will echo back so any subsequent transmisions will need to have the echo removed before sending the next integer.

    Jeff T.
  • bryan costanichbryan costanich Posts: 29
    edited 2007-01-20 20:59
    ah, i gotcha, newline. cool.

    i'm not sure i follow about the echo though. i understand that the chip echos back all commands, but i'm not doing anything with the echo, so why do i have to remove the echo?
  • Kevin WoodKevin Wood Posts: 1,266
    edited 2007-01-20 21:24
    If you were awaiting a response from the BS2 immediately following your C# write, you would need to account for the echo to avoid reading it as the response.
  • bryan costanichbryan costanich Posts: 29
    edited 2007-01-20 21:52
    ah, right. but that's the same for any thing you send down the pipe, no? be it a new line character or other
  • UnsoundcodeUnsoundcode Posts: 1,532
    edited 2007-01-20 22:46
    Hi bryan, if your only transmitting from the PC it isn't an issue but if you have two way communication the echo can get mixed with the Stamp to PC data unless you remove it, pins 0 to 15 don't give you that problem. I made the assumption you were transmitting and recieving and that is why I mentioned it, but it looks like you have all that under control.

    Jeff T.
  • bryan costanichbryan costanich Posts: 29
    edited 2007-01-21 08:38
    Gotcha. thanks for the headsup.

    by the way, the newline works like a charm.
Sign In or Register to comment.