What am I doing wrong??
Navic
Posts: 38
I have a serial connection that sends a single command to the BS2, in which I'm trying to execute code depending on the value of the command sent. Here's my code:
I threw in a debug to display the ard variable after the serin to make sure I was getting the right input, which I am. The problem is that none of the 'command' debug statements ever execute. I tried to change the if statements to elseif as well as using case...select instead, still no luck. The ard variable will contain either 1,2, or 3 perfectly, but nothing will execute except the last debug "Finished". What am I doing wrong? Thanks!!
SEROUT 0, 84, [noparse][[/noparse]"1"] SERIN 1, 84, [noparse][[/noparse]WAIT("@"), ard] IF (ard = 1) THEN DEBUG "Command 1",CR ENDIF IF (ard = 2) THEN DEBUG "Command 2",CR ENDIF IF (ard = 3) THEN DEBUG "Command 3",CR ENDIF DEBUG "Finished",CR
I threw in a debug to display the ard variable after the serin to make sure I was getting the right input, which I am. The problem is that none of the 'command' debug statements ever execute. I tried to change the if statements to elseif as well as using case...select instead, still no luck. The ard variable will contain either 1,2, or 3 perfectly, but nothing will execute except the last debug "Finished". What am I doing wrong? Thanks!!
Comments
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Mike2545
This message sent to you on 100% recycled electrons.
then you have to look for "1" try
SEROUT 0, 84, [noparse][[/noparse] 1 ]
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Mike2545
This message sent to you on 100% recycled electrons.
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Don't worry. Be happy
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Visit: www.vbnoobs.co.uk
IF ard.LOWNIB = 1 THEN
etc...
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Don't worry. Be happy
Since your SERIN only has a byte, you can't receive other numbers...
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Don't worry. Be happy
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
- Stephen
When you send "1", you're not sending the decimal value 1, you're sending the ascii-encoded value for "1", which I thought was something like 51. Look up "Ascii" on google or something.
Now, the BS2 will recieve a single byte, but if the computer sending to the BS2 sends a "1", again the BS2 will recieve the ascii-value. So, that value is what you need to check for in your IF statements.
Another alternative would be to have your sending computer send you a "1\n", then in the BS2 have a "DEC" modifier in the SERIN. The DEC modifier requires a 'terminator' to the sent string, which the '\n' line-feed character will do. Then you can use your code as it is -- with the added DEC modifier of course.
Oh, and I assume, since you're using port 0 and 1, that you have a MAX232 chip on there to provide level shifting from TTL to RS-232 signalling levels.
I will add the \n terminator to the end of the serial input and see if that changes anything.
If it's a "2" character being transmitted to the Stamp, the ASCII code is·$32 (%00100000) which =·5010.· "1" = $31 (4910), "3" = $33 (5110).
If/since you're not using a level shifter then your Stamp should be set for an Inverted Baudrate (84 is 9600bps 8N1 True, 16468 is 9600bps 8N1 Inverted.)
Post Edit -- What is the source of this serial data being sent to the Stamp??
Post Edited (PJ Allen) : 8/31/2009 3:03:00 AM GMT
First of all, you did no tell· us what type of variable ard is. Than you send “1” which tells the BASIC that you are sending ASCII byte character. Then you code expects binary number 1,2, or 3. Your DEBUG lines really don’t tell you anything except that the “if “ statements did not execute and you program “falls thru “ to the last statement.
Take a look at the following snippet and you may get a better picture what I am talking about. In an essence -· it is a poor practice to use “magic numbers” in code· – assign variables and than assign values to them. Use DEBUG to your advantage – use modifiers.
I would say one positive thing about you troubleshooting skills – you did not get sidetracked by troubleshooting the serial connection.
·
Happy coding· Vaclav
·
' {$STAMP BS2e}
' {$PBASIC 2.5}
ard VAR Byte
character VAR Byte
character = "1"
DEBUG ? character
' SEROUT 1, 84, [noparse][[/noparse]character]
OUTL = character
DEBUG ? OUTL, CR
DEBUG ? OUTS, CR
DEBUG BIN OUTL, CR
DEBUG BIN16 OUTS,CR
·STOP