PDA

View Full Version : Using "EXPECT" directly with serial port


Sonny
04-14-04, 11:27 AM
I have written several routines in Forth using the word "EXPECT" to take in data from the keyboard. Now I need to download data via another computer through the serial port. Using "EXPECT" does not seem to work directly. I know I can go directly to the serial send/xmit register but is there not a more elegant way? with Std Forth words?

RMDumse
04-14-04, 04:41 PM
EXPECT is the next highest word above KEY in gathering serial strings. EXPECT is basically a loop that takes a given number of characters, but it also processes the "back up" character (back space).

In what sense doesn't EXPECT work for you?

Processing the number once you get it is a function of using NUMBER or FNUMBER, FNUMBER being the most general case.

Can you tell me what format of numbers you expect from the other computer?

RMDumse
04-14-04, 04:46 PM
I went searching on the forum here for FNUMBER and found I'd worked through this before. Here is the result of that effort:

CONVERT and FNUMBER are both primitives of NUMBER. FNUMBER is the most general input. FNUMBER in V3.5 will convert all numbers, floats or otherwise.

NUMBER calls FNUMBER, then does a check for an error. Usually in run time situations you want to avoid taking this error routing (which leads back to the outer interpreter and not back to your program) and do your error checking yourself.

NUMBER is defined in V3.5 as:

: NUMBER
FNUMB C@ ( look at next char
BL - (see if it is the same as a blank
0 ?ERROR ( call to see if it is an error, return if not
;

CONVERT is a deeper primative that will convert a string up to the first non-numeric character, and return. The non-numeric character could be a space, or a decimal point, or a floating point character (e) or something unexpected which would be considered a true error (by
something else beyond CONVERT).

So you use FNUMBER the same way you do NUMBER without the worry of loosing control of the program on error. Feed it a double word zero, and the address of the counted string (-1 as I vaguely recall) to convert. Check DPL to determine what kind of result has been returned. If DPL is FFFF, the result is a single precision number left on the stack (having no decimal point embedded). If DPL is FFFE, the result is a float, and it is already on the float stack. Any other value in DPL says a decimal point was encountered in the string, and the result is a double precision number left on the stack.

Sonny
04-19-04, 08:08 PM
Thanks for the info. I must have had a bug in the routines for number handling with expect. I now have a working routine that can be acessed either thru the keyboard or by remote computer download.

Thanks again