Shop OBEX P1 Docs P2 Docs Learn Events
Using Serin on a basic stamp 2 — Parallax Forums

Using Serin on a basic stamp 2

romaycomputerromaycomputer Posts: 2
edited 2012-12-30 11:46 in BASIC Stamp
Hello All,

I am hoping someone can point me in the right direction. I have been playing with the basic stamp 2 homework board to receive serial data in using serin command and depending the the data turn on led's. I am very close but not quite there as I am having minor problems with it. I am working with the following data coming in, e.g. I100J000K100 the problem I am having is that sometimes it may only be J000K100 or just J100 and the code I have does not want to compensate for just J000K100 or just J000. Here is the code I am using:

'

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

letterI VAR Byte ' Will be letter "I"
mainIa VAR Byte ' Will be the first digit after "I" either a one or a zero
mainIb VAR Byte ' Will be the second digit after "I" and is not needed
mainIc VAR Byte ' Will be the third digit after "I" and is not needed

letterJ VAR Byte ' Will be letter "I"
mainJa VAR Byte ' Will be the first digit after "J" either a one or a zero
mainJb VAR Byte ' Will be the second digit after "J" and is not needed
mainJc VAR Byte ' Will be the third digit after "J" and is not needed

letterK VAR Byte ' Will be letter "I"
mainKa VAR Byte ' Will be the first digit after "K" either a one or a zero
mainKb VAR Byte ' Will be the second digit after "K" and is not needed
mainKc VAR Byte ' Will be the third digit after "K" and is not needed

finalCR VAR Byte ' Will be Carriage Return

Main:

SERIN 16, 84, [letterI, mainIa, mainIb, mainIc, letterJ, mainJa, mainJb, mainJc, letterK, mainKa, mainKb, mainKc, finalCR] 'Read the serial data comming in

IF mainIa = 49 THEN HIGH 10 ELSE LOW 10 'If first digit after "I" is "1" then set pin 10 HIGH otherwise set pin 10 LOW

IF mainJa = 49 THEN HIGH 12 ELSE LOW 12 'If first digit after "J" is "1" then set pin 12 HIGH otherwise set pin 12 LOW

IF mainKa = 49 THEN HIGH 8 ELSE LOW 8 'If first digit after "K" is "1" then set pin 8 HIGH otherwise set pin 8 LOW

GOTO Main ' Start all over

END

'

So just a recap the data coming in will be I100, J100, K100 (without commas or spaces) and can be any combination of those data sets.

Thank You,
Roger Mayers

Comments

  • stamptrolstamptrol Posts: 1,731
    edited 2012-12-24 08:33
    Hi and welcome to the forums.

    Probably all that's wrong is that you've specified 9600 baud for your incoming serial data. Along with the parsing into the list of variables, the Stamp is not keeping up.
    This is largely due there being no communication buffer in the Stamp. If you can adjust things down to 4800 or 2400 baud, you'll likely be OK.

    The BS2sx and BS2px series of Stamps run much faster and can cope at higher speeds.

    Cheers,
  • SapphireSapphire Posts: 496
    edited 2012-12-24 13:45
    Roger,

    If I understand you correctly, what you want to do is be able to receive some or all of the IxxxJxxxKxxx<CR> string and act on it accordingly. If so, you will need to restructure your program to do this.

    Instead of reading in specific bytes (i.e. letterJ, mainJa, mainJb) you should instead define an array of bytes large enough to hold the entire string, and then process the bytes one by one once the SERIN ends.
    comm  VAR  byte(13)   ' 13 byte array for IxxxJxxxKxxx<CR>
    x     VAR  byte       ' loop variable
    
    SERIN 16, 84, [STR comm\13\CR]   ' read in up to 13 bytes, and stop at <CR>
    

    Now if the <CR> may not come in, you should add a timeout to SERIN so that it doesn't wait forever.
    SERIN 16, 84, 20000, Timeout, [STR comm\13\CR]  ' read in up to 13 bytes, stop at <CR> or after 20 sec
    
    Timeout:          ' continue with processing here
    

    Once you have received the comm array, you will then have to go through it one byte at a time to find out what you got.
    FOR x = 0 to 12  ' look at all bytes received
      SELECT comm(x)
        CASE "I" : IF comm(x+1) = "1" THEN HIGH 10 ELSE LOW 10  ' if byte was I then set pin 10 based on next byte
        CASE "J" : IF comm(x+1) = "1" THEN HIGH 12 ELSE LOW 12  ' if byte was J then set pin 12 based on next byte
        CASE "K" : IF comm(x+1) = "1" THEN HIGH 8 ELSE LOW 8    ' if byte was K then set pin 8 based on next byte
        CASE CR,0 : EXIT  ' stop if <CR> or end of string
      ENDSELECT
    NEXT
    

    Give that a try and see if it does what you want.
  • romaycomputerromaycomputer Posts: 2
    edited 2012-12-30 11:46
    Thank You Both,

    I ran the one Sapphire gave and works great and works more quickly than my code, I only had to make a small change. The line:

    CASE CR,0 : EXIT ' stop if <CR> or end of string

    I just looped it back to the top othwise it would just end. Again thank you for all your help in this

    Roger Mayers
Sign In or Register to comment.