Tech Note                                        January 16, 1998

36. The Basics of Communications
-----------------------------------------------------------------

A. Serial Communcations

You can send and receive data via the serial port of your Newton. NS
BASIC
can PRINT to the serial port and INPUT from it. There are four steps to
using the serial port:

1 Initialize the port to the desired settings.
You control the settings for the serial port using the environment 
variable s0.

2 Tell NS BASIC to use the serial port.
You control the input and output device using the environment variable
IO.

3 Exchange data or output information as desired using the serial port.
You control the prompt displayed by NS BASIC using the environment 
variable
inputPrompt. You send data using PRINT and receive data using INPUT.

4 Tell NS BASIC to stop using the serial port.

You control the input and output device using the environment variable
IO.
It is very important that you perform these steps in this order. If your
program quits unexpectedly and leaves the serial port open, other Newton
applications may not be able to use the port. If this happens, reset
your
Newton.
The first example program accepts input via the serial port. This
program
leaves the serial port in its default configuration.
You will need to connect your Newton to a desktop computer to run this
program. See the Using NS BASIC With a Computer or Terminal section of
the
Handbook for more information.

10 REM Serial Port Example
20 f={viewBounds:SETBOUNDS(100,100,130,110)}
30 tr = 0
40 translist = ""
50 CLS
60 WINDOW w1,f
70 SHOW w1
80 ENVIRON io="extr"
90 REM get a transaction
100 INPUT trans
110 tr = tr+1
120 translist = translist & trans & CHR(13)
130 WPRINT w1, tr
140 IF trans <> "BYE" THEN GOTO 90
150 ENVIRON io="scre"
160 PRINT translist
170 HIDE w1

The next example program dumps all of your appointments from the
Calendar
file to the serial port. The port is set to 4800 bps first. See the
Handbook for more information on accessing Calendar and other built-in
applications data.

10 REM Serial Dump Calendar Example
20 REM set serial params
30 currPort := env("extr")
40 currPort.data = [4800,8,1,"no"]
50 ENVIRON extr=currPort
60 ON ERROR GOTO 120
70 ENVIRON IO="extr" // switch to serial
80 OPEN ch,"calendar",mtgstartdate
90 GET ch, n
100 PRINT n // dump record
110 GOTO 90
120 REM EOF or other problem
130 ENVIRON IO="scre"
140 END

B. Infrared Communications

You can send and receive data via the infrared port of your Newton. NS
BASIC can PRINT to the infrared port and INPUT from it. There are four
steps to using the infrared port:

1 Initialize the port to the desired settings.
You control the settings for the infrared port using the environment
variable infr.

2 Tell NS BASIC to use the infrared port.
You control the input and output device using the environment variable
IO.

3 Exchange data or output information as desired using the infrared
port.
You control the prompt displayed by NS BASIC using the environment 
variable
inputPrompt. You send data using PRINT and receive data using INPUT.

4 Tell NS BASIC to stop using the infrared port.
You control the input and output device using the environment variable
IO.
It is very important that you perform these steps in this order. If your
program quits unexpectedly and leaves the infrared port open, other
Newton
applications may not be able to use the port. If this happens, reset
your
Newton.


C. Hardware specs for the Serial Port

All inputs are:
                Ri 12K ohms
                minimum Vih 0.2v, Vil -0.2V
                maximum tolerance Vih 15V, Vil -15V
        All outputs are:
                Rl 450 ohms
                minimum Voh 3.6V, Vol -3.6V
                maximum Voh 5.5V, Vol -5.5V
        No more than 40mA total can be drawn from all pins on the
serial 
port.
Pins 3 & 6 tri-state when SCC's /RTS is not asserted." 
(Serial Port Hardware Specs 5/23/96)


D. Some Additional Notes

1. To set a timeout, use ENVIRON EXTR.INPUT.REQTIMEOUT=1000 for a one 
second timeout. If input is not received within the timeout period, the 
string "TIMEOUT: -16005" will be returned to the variable in your INPUT 
statement. Timeouts only work when ENVIRON IO is not "SCRE".

2. Communications parameters are set when you do the ENVIRON IO 
statement, and can't be changed on the fly. You can reset the
parameters 
by switching back to "SCRE", then returning to your comms session with 
new parameters set.

3. When doing an ENVIRON IO statement, use a WAIT statement to give the 
system a chance to complete the operation - otherwise you may get
hangs. 
How long the WAIT needs to be depends on how fast your Newton is.