Shop OBEX P1 Docs P2 Docs Learn Events
Storing text sentences in variables. — Parallax Forums

Storing text sentences in variables.

Clock LoopClock Loop Posts: 2,069
edited 2005-07-06 16:03 in BASIC Stamp
I feel stoopid asking this, but I have searched the forum, and the nuts and volts, and the stampworks, and the help file for anything related to working with text sentences. I can only find DEBUG use of sentences, and can only find EEPROM using sentences. I dont see any examples of storing sentences in a variable.

Why doesn't this work?


' {$STAMP BS2}
' {$PBASIC 2.5}

test    VAR   Word           

Main:

test = "hello how are you"

DEBUG test




I am guessing it has to do with the length of the variable, and the length of the sentence. So how DO I store sentences in variables?
[noparse]:)[/noparse]

Comments

  • OrionOrion Posts: 236
    edited 2005-07-04 17:37
    Word vars hold 16bits, each ascii charcter = 8bits. So "hello how are you" = 17chars*8bits=136bits, not going to fit in a word. You will need to use an array of bytes, or eeprom space to store strings.
  • allanlane5allanlane5 Posts: 3,815
    edited 2005-07-04 17:45
    Because you only have 26 bytes of RAM in the BS2. Thus it is very difficult to try to 'store' a string temporarily in RAM so you can send it.

    Now, in a line like:

    SEROUT 16, I9600, [noparse][[/noparse]"Hello, how are you?", 13]

    the bytes "Hello..." are actually stored in the EEPROM. You have 2 KBytes of eeprom, so this works well.

    I suppose you could do:

    String1 CON "Hello there"

    SEROUT 16, I9600, [noparse][[/noparse]String1,13] ' This sends the string "Hello there" out the serial port.

    Except CON statements don't support strings. Oh well.
  • Clock LoopClock Loop Posts: 2,069
    edited 2005-07-04 17:58
    Darn, I knew it, So thats why all string examples are in eeprom. Because theres just not enough room to store it in ram... [noparse]:)[/noparse]
    Or, I COULD store it one letter at a time.. LOL

    Thanks guys.
  • Clock LoopClock Loop Posts: 2,069
    edited 2005-07-04 17:59
    Wait does this mean that this code

    
    debug "hello how are you today" 
    
    
    



    Then this code MUST use eeprom to store its text! right?
  • Jon WilliamsJon Williams Posts: 6,491
    edited 2005-07-04 18:16
    Yes, it gets put into the program space by the compiler.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Jon Williams
    Applications Engineer, Parallax
  • allanlane5allanlane5 Posts: 3,815
    edited 2005-07-04 18:32
    OK, Jon, so is there a way to get something like this to work, where the string is stored in EEPROM by a DATA statement, and then gets output by some reference to the string?

    '---- Code
    ' {$STAMP BS2}
    ' {$PBASIC 2.5}

    String1 DATA "Hello There",0
    I9600·· CON 16468· ' 84 + 16384, or 9600 baud, Inverted

    MAIN:
    · SEROUT 16, I9600, [noparse][[/noparse]String1,13]
    · PAUSE 200
    · GOTO MAIN
    The question is, how do you put in the SEROUT statement a 'reference' to String1 that will result in the string being output.· One workaround is a subroutine that will read the data a byte at a time and send it with a SEROUT for each byte.· Is there an easier way?
    ·
  • allanlane5allanlane5 Posts: 3,815
    edited 2005-07-04 18:40
    Try number 2:

    ' {$STAMP BS2}
    ' {$PBASIC 2.5}

    String1 DATA "Hello There",0
    StringLoc VAR Word· ' 1..2000
    MyByte··· VAR Byte
    I9600·· CON 16468 '16384 + 84

    MAIN:
    · ' SEROUT 16, I9600, [noparse][[/noparse]String1,13]· ' This doesn't work...

    · ' Hopefully, this does...
    · StringLoc = String1
    · GOSUB SendString
    · PAUSE 200
    · GOTO MAIN

    SendString:
    · READ StringLoc, MyByte
    · IF MyByte <> 0 THEN
    ··· SEROUT 16, I9600, [noparse][[/noparse]MyByte]
    ··· StringLoc = StringLoc + 1
    ··· GOTO SendString
    · ENDIF
    · RETURN
    ·
  • Jon WilliamsJon Williams Posts: 6,491
    edited 2005-07-04 19:43
    No, you cannot put a string pointer in a SEROUT command as you've shown ... though I asked for that exact feature a few years ago. While not too tough to implement, it takes a lot of space because that feature would have to work in all serial routines (SEROUT, I2COUT, OWOUT, LCDOUT, etc.) -- and there's just not enough space to do it.

    You can, of course, implement it yourself -- here's my version of your subroutine:

    Print_Str:
    · DO
    ··· READ eePntr, char
    ··· IF (char = 0) THEN EXIT
    ··· SEROUT SPin, Baud, [noparse][[/noparse]char]
    ··· eePntr = eePntr + 1
    · LOOP
    · RETURN

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Jon Williams
    Applications Engineer, Parallax

    Post Edited (Jon Williams (Parallax)) : 7/4/2005 7:49:27 PM GMT
  • allanlane5allanlane5 Posts: 3,815
    edited 2005-07-05 00:47
    Aha, the "Exit" which breaks out of a 'Do Loop'. Very nice. Thanks for the simplification.

    Though "eePntr" would actually be my "StringLoc" WORD variable.
    And "char" would be MyByte.
  • Jon WilliamsJon Williams Posts: 6,491
    edited 2005-07-05 02:24
    Yes, sorry for any confusion -- 'eePntr' and 'char' are names I use for those kinds of routines; I actually typed that without thinking because I've used it so many times.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Jon Williams
    Applications Engineer, Parallax
  • nick bernardnick bernard Posts: 329
    edited 2005-07-06 16:03
    READ offset + eePntr + idx ,value
    i use simular code with this line to read tabled text.
    offset points to one data set
    eePntr points to the line you wish to print
    idx points to the chacter of the line of the dataset

    it just makes organizing large tables of data easier; for me anyway.
    ROX ON

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Byte walks into a bar and orders a pint. Bartender asks him "What's wrong?"
    Byte says "Parity error." Bartender nods and says "Yeah, I thought you looked a bit off."
Sign In or Register to comment.