Using Serin on a basic stamp 2
romaycomputer
Posts: 2
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
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
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,
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.
Now if the <CR> may not come in, you should add a timeout to SERIN so that it doesn't wait forever.
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.
Give that a try and see if it does what you want.
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