Shop OBEX P1 Docs P2 Docs Learn Events
Combining Strings... — Parallax Forums

Combining Strings...

starionstarion Posts: 27
edited 2005-11-29 20:06 in BASIC Stamp
I racked my brain, tried 30 different ways of doing it and still couldn't get it to work. Here's what I'm trying to do in a nutshell:

Here is the data I am working with:

counter VAR WORD ' A number from 0000 to 9999.

fixedword DATA "theword" ' Just a word.

I am trying to created a concatenated string with the end result being:

"0000theword" and each increment of the counter would be "0001theword", "0002theword" and so on.

Ultimately I will be sending this string out a serial port, but for now, I would be happy just to get them combined and output them as a DEBUG command. I'm sure this is an easy thing, but I'm fairly new to this and haven't quite got my syntax down.

Quick word of advice anyone? This is a BS2.

Post Edited (Starion) : 11/27/2005 4:04:20 PM GMT

Comments

  • TiboTibo Posts: 81
    edited 2005-11-27 16:15
    I'd say use a word array like this, hope it helps (here i used word to respect your precise needs but no need, use bytes instead).
    ' {$STAMP BS2e}
    ' {$PBASIC 2.5}
    '
    ' Concatenation Example
    '
    
    str1    VAR Word 'Stores the first string to concatenate
    str2    VAR Word 'Stores the second string to concatenate
    concat  VAR Word(2) 'Stores the resulting string of the concatenation process
    
    '---------------------------------------------
    
    main:
      str1=65'"a"
      str2=66'"b"
      GOSUB concatenate
      GOSUB debug_output
      END
    
    concatenate:
      concat(0)=str1
      concat(1)=str2
      RETURN
    
    debug_output:
      DEBUG HOME, "str1:", str1," str2:", str2," concat:",concat(0),concat(1), CR
      RETURN
    
    

    Post Edited (Tibot) : 11/27/2005 4:28:13 PM GMT
  • Jon WilliamsJon Williams Posts: 6,491
    edited 2005-11-27 16:56
    Embedded programming with languages like PBASIC occassionally require a different approach -- here's how I'd approach this particular situation.

    ' {$STAMP BS2}
    ' {$PBASIC 2.5}
    
     
    counter         VAR     Word
    eePntr          VAR     Byte
    char            VAR     Byte
    idx             VAR     Nib
     
    TheWord         DATA    "0000theword", CR, 0
    
    Reset:
      counter = 0
    
    Main:
      DO 
        ' update string
        '
        FOR idx = 0 TO 3
          WRITE TheWord + idx, (counter DIG (3-idx)) + "0"
        NEXT
    
     
        ' print the string
        '
        eePntr = TheWord
        GOSUB Print_Str
     
        ' update counter
        '
        counter = counter + 1 
        PAUSE 500
      LOOP
      END
    
    Print_Str:
      DO
        READ eePntr, char
        eePntr = eePntr + 1
        IF (char = 0) THEN EXIT
        DEBUG char
      LOOP
      RETURN
    

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

    Post Edited (Jon Williams (Parallax)) : 11/27/2005 5:01:42 PM GMT
  • metron9metron9 Posts: 1,100
    edited 2005-11-27 17:23
    Well, I guess there are many ways to skin a cat here is mine.


    {$STAMP BS2}
    ' {$PBASIC 2.5}
    
    
    CX VAR Byte 'For next loop counter
    
    p VAR Byte      'Bytes for each place, 1,10,100,1000
    p1 VAR Byte
    p2 VAR Byte
    p3 VAR Byte
    offset VAR Byte 'The ascii code for "0"
    N VAR Word      ' Your number
    W VAR Byte(10)  ' Your String
    
    
    offset="0"
    
    FOR cx=0TO 10   'Read in string to memory variable
     READ cx,W(cx)   'Locations 0 through 3 placeholder for number default "0000"
    NEXT
    
    
    n=0  ' Number from 1 to 9999
    
    main:
     GOSUB stringcon
     GOSUB display
     PAUSE 1000
     n=n+1 'Your number, for testing I am incrementing
     IF n>9999 THEN n=0
    GOTO main
    END
    
    
    stringcon:
    'extract each number
    p=n/1000
    p1=n -(P*1000)
    p2=p1
    p1=p1/100
    p2=p2-(p1*100)
    p3=p2
    p2=p2/10
    p3=p3-(p2*10)
    
    'Modify string
    w(0)=p+offset
    w(1)=p1+offset
    w(2)=P2+offset
    W(3)=P3+offset
    RETURN
    
    display:
    'display results
    DEBUG STR w,CR
    RETURN
    
    DATA "0000theword"
    
    

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Think outside the BOX!
  • metron9metron9 Posts: 1,100
    edited 2005-11-27 17:49
    I suppose a note of caution should be placed here about the WRITE command a maximum of 100,000 writes on stamps above the Bs1 and Bs2 (1 million on Bs1 and Bs2)
    So if you are going to be doing alot of it you may want to convert Jon's program to use RAM memory instead of EEPROM memory. If you leave out the display and pause commands in that one you can burn up your eeprom memory locations 0 through 3 in about an hour. On higher stamps running faster and only 100,000 writes could bee poof before you blink.


    smhair.gif

    I could be wrong about this , if so please correct me....

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Think outside the BOX!
  • Jon WilliamsJon Williams Posts: 6,491
    edited 2005-11-27 18:06
    The program I provided was an example of concatenating data, and I guess it should have come with a disclaimer that writing to the same data space *could* ultimately cause an EEPROM failure (though with hundreds of programs and 13 years of BASIC Stamp experience I have yet to see that actually happen...).

    Here's a "safe" version that is RAM and program space efficient (which is critical in small controllers like BASIC Stamp -- in my opinion using RAM for strings is a huge waste of resources); pass the value you want to print in "temp" and the string you want to append in "eePntr."

    ' {$STAMP BS2}
    ' {$PBASIC 2.5}
    
     
    counter         VAR     Word
    temp            VAR     Word
    eePntr          VAR     Byte
    char            VAR     Byte
    idx             VAR     Nib
    
    TheWord         DATA    "theword", CR, 0
    
    Reset:
      counter = 0
    
    Main:
      DO
        temp = counter
        eePntr = TheWord
        GOSUB Print_Str4
        counter = counter + 1
        PAUSE 500
      LOOP
      END
    
    Print_Str4:
      DEBUG DEC4 temp
      DO
        READ eePntr, char
        eePntr = eePntr + 1
        IF (char = 0) THEN EXIT
        DEBUG char
      LOOP
      RETURN
    



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

    Post Edited (Jon Williams (Parallax)) : 11/27/2005 6:09:26 PM GMT
  • starionstarion Posts: 27
    edited 2005-11-28 18:44
    Turns out, I think the solution I came up with is even simpler yet. I had it in my mind that a string had to be "built" first before I could send it out a serial port, under the assumption that a serial port is "opened", data sent, then "closed". Once I got past that notion, and looked at the examples you all so generously provided, I came up with something like this:

    DO WHILE COUNTER<10000
    SEROUT [noparse][[/noparse]DEC4 counter]
    SEROUT [noparse][[/noparse]"theword",CR]
    counter=counter+1
    LOOP

    Once I realized that the serial device on the other end doesn't know whether I used one or six SEROUT commands, it all appears continuous until a CR is sent, my thinking cleared up a bit. If you are at all interested in this project, read on, details below:

    BTW, this is my FIRST project with the BS2. I can't believe I actually got it working about 1:30AM. Here's the scenario:

    My company sells, services, and installs a brand of business telephone system (among a zillion other things we do related to technology). When we take over a new customer's system, we have no idea what the 4-digit code is to get into the programming on the phone switch.

    The phone system has a DB9 serial port for PC hookup and programming. When you hook it up, press return in a terminal, you are presented with a password prompt. If you enter a wrong password, it simply does nothing. However, if you enter the correct code, you get a specific prompt that I wait for with SERIN.

    So, I built a very simple BS2 circuit using the professional development board that simply tries every number from 0000 to 9999 with "theword" appended to the code. I have discovered that you can crack any code inside of 20 minutes, and usually shorter depending on how close the actual code is to 0000.

    The code I wrote is considerably more complex than my example above, accounting for things like no response from the phone switch, using a 7-segment display to indicate working status, and percentage complete, as well as a way to display the cracked code, all on one 7-segment display.

    It works like a champ, has correctly cracked every code I tried so far. Future additions might be auto-baud detect AND...(speeding up the search)

    leading to my next related question which I will start in a topic of its own entitled "Guessing a number".

    Post Edited (starion) : 3/4/2007 3:33:05 PM GMT
  • TiboTibo Posts: 81
    edited 2005-11-28 18:48
    smart solution for nice problem, way to go !
  • allanlane5allanlane5 Posts: 3,815
    edited 2005-11-28 21:43
    Fascinating. This might have hollywood possibilities.

    This points out something else. Almost any security system can be hacked. What the security system does is slow down the hacker (or raise flags "Hey! Somebody just tried 100 different PIN numbers!") so it takes long enough the hacker can be apprehended.
  • Chris SavageChris Savage Parallax Engineering Posts: 14,406
    edited 2005-11-28 22:30
    Many systems prevent this kind of brute-force attack by limiting the number of consecutive wrong attempts and taking some action.· For example, many of the alarm systems I used to design and install would allow only 3 wrong attempts at a password before setting off the alarm, in many cases calling the Sheriff's Dept.· Some computer systems go into lockout mode after 3 wrong attempts.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Chris Savage
    Parallax Tech Support
    csavage@parallax.com
  • starionstarion Posts: 27
    edited 2005-11-28 22:42
    I chose this project because I was already familiar with the security (or lack of) in this particular phone system. That is not to say they won't change it with a future firmware release. But the system in question has been around for about 6 years now with the same procedure.

    The issue here is whether anybody (besides myself) would find any value to hacking the installer code. Only myself and possibly other dealers who sell and maintain this type of system. I have no malicious intent here, simply something that makes my job easier. I suppose someone could wreak havoc with this kind of tool.

    The other consideration is that this method requires physical access to the switch. Although I am working on a software cracker to perform the same function remotely.
  • Chris SavageChris Savage Parallax Engineering Posts: 14,406
    edited 2005-11-29 15:07
    Jeff,

    ·· My point was directed more at Allan's comment about security.· I guess my point was, even though it is quite lax in this device, you can't expect that everywhere.· For example, in the movie The Terminator (I think 3) they show the kid hacking the ATM machine using the same exact method you're using.· That wouldn't work on any ATM I have ever seen.· =)

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Chris Savage
    Parallax Tech Support
    csavage@parallax.com
  • Tom WalkerTom Walker Posts: 509
    edited 2005-11-29 20:06
    Actually, that was Terminator 2...and wasn't it nice that the ATM actually provided feedback on the order of "you got the first 2 digits right"...ATM by Hasbro (makers of Mastermind)...

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Truly Understand the Fundamentals and the Path will be so much easier...
Sign In or Register to comment.