PDA

View Full Version : Serout command



CraigR
12-05-2009, 05:11 AM
I am trying to use the serout command to accept a user input from the keyboard. The three options for the user are 1)pressing a pushbutton, 2)pressing 0, or 3)pressing any other key on the keyboard. If the user presses the pushbutton I want the program to go·to the Start subroutine. If the user presses 0 I want the program to go to the Quit·Subroutine. If the user·presses any other key on the keyboard I want the program to display "Incorrect Entry". My problem is that when I run the program·the keyboard entrys work, but the program does not·do anything when the pushbutton is pressed.·Can the Serout command work with the pushbutton?
----------------------------------------------
BAUD CON 16468
PORT CON 16

DO
· SEROUT PORT, BAUD, ["To Play Press Blue Button or·0 to Quit", CR]
· SERIN PORT, BAUD, [keypressed]
·
· IF (IN0 = 1) THEN
········GOTO Start
· ELSEIF keypressed = "0" THEN
······· GOTO Quit
··ELSE
······· SEROUT PORT, BAUD, [CR, "Incorrect Entry", CR]
······· GOTO Start
· ENDIF
LOOP

----------------------------------------------
Start:
----------------------------------------------
Quit:

Mike Green
12-05-2009, 06:21 AM
SEROUT can work with pushbuttons. Your problem is that the SERIN will wait indefinitely for a key to be typed. Read the chapter in the BASIC Stamp Syntax and Reference Manual on the SEROUT statement. There's a description of a timeout parameter. This specifies a label to get control when the specified timeout period is exceeded. You could set up a timeout for maybe 1/4 or 1/2 a second, check whether the button is pressed, and go back to the SERIN if the button isn't pressed. The only problem you'll have is that, when the SERIN is interrupted to check the pushbutton, the Stamp may miss the keypress sent from the PC since a Stamp can't do two things at once and the SERIN isn't buffered.

Julie in Texas
12-08-2009, 01:03 AM
I'd suggest a shorter timeout period than 250ms. I'd also suggest using hardware handshaking -- see the "FPIN" parameter to SERIN.

The trick with RS-232 communication is that there is no point in waiting for more than one bit time because there is either a bit, or there is no bit. And if you run off to do something else (like, poll IN0) and can't get back in a bit time, you lose the framing bit anyway. So, shorten your polling routines (and testing IN0 is already pretty short) to avoid dropped bits, then run the polling routines more often.

It's a computer. It doesn't get tired. Might as well make it earn its keep.

Try this (I didn't -- I reserve the right to make syntax and other errors) --




BAUD CON 16468
PORT CON 16

Top:
'
' Only output this once, will be polling to serial I/O and button press
' starting at PollLoop.
'
SEROUT PORT, BAUD, ["To Play Press Blue Button or 0 to Quit", CR]

'
' Top of polling loop -- sleep 10ms for serial I/O, then check button,
' then back to the top to try again.
'
PollLoop:
'
' Wait 10ms for a character, jumping to "NoData" if none.
'
SERIN PORT, BAUD, 10, NoData, [keypressed]
'
' See if we got a useful character.
'
IF (keypressed = "0") THEN
GOTO Quit
ELSE
SEROUT PORT, BAUD, [CR, "Incorrect Entry", CR]
' Was "GOTO Start" in original, but is that correct?
ENDIF

NoData:
'
' Didn't receive any serial data, check the push button.
'
IF (IN0 = 1) THEN
GOTO Start
ENDIF
'
' Redo loop from the top.
'
GOTO PollLoop

----------------------------------------------
Start:
----------------------------------------------
Quit: