Chapter Contents

Previous

Next
SAS Companion for the Microsoft Windows Environment

Reading Data from the Communications Port

You can read data directly from the communications (serial) port on your machine. To set the serial communications parameters, use the port configuration tools in the Windows Control Panel to set up the communications port. (Under Windows 95 and Windows 98, the port settings are in the System portion of the Control Panel.) The communications parameters you specify are specific to each data collection device.

After you invoke the SAS System, submit a FILENAME statement to associate a fileref with the communications port, as in the following example:

filename test commport "com1:";

This FILENAME statement defines the fileref TEST, uses the COMMPORT device-type keyword that specifies you are going to use a communications port, and specifies the COM1: reserved physical name.

Next, read the data from COM1: into a SAS data set using the TEST fileref. The following DATA step reads in the data, 1 byte at a time, until the SAS System encounters an end-of-file (the hex value of end-of-file is '1a'x):

data acquire;
   infile test lrecl=1 recfm=f unbuffered;
   input i $;
   /* Read until you find an end-of-file. */
   if i='1a'x then stop;
run;

The communications port can be accessed multiple times. However, while multiple reads are allowed, only one user at a time can write to the port.

Two useful functions in data acquisition applications are SLEEP and WAKEUP. These functions enable you to control when your program is invoked. For example, you can use the WAKEUP function to start your program at exactly 2:00 a.m. For more information about these two functions, see SLEEP and WAKEUP.


Communications Port Timeouts

By default, if you are reading from a communications port and a timeout occurs, an end-of-file (EOF) is returned to the program. You can specify how communications port timeouts are handled by using the COMTIMEOUT= option. The COMTIMEOUT= option is valid in the FILENAME statement and must be used in conjunction with the COMMPORT device-type keyword in the FILENAME statement.

The COMTIMEOUT= option accepts the following values:
EOF returns an end-of-file when a timeout occurs. This is the default behavior. This causes the current DATA step to terminate.
WAIT instructs the communications port to wait forever for data. In other words, this value overrides the timeout. In this case, no record is returned to the DATA step until data are available. This can cause your program to go into an infinite loop, so use this value with caution.
ZERO does not wait if there is no incoming data.

Here is an example of a FILENAME statement specifying that a record length of 0 bytes be returned to the program when a timeout occurs:

filename test commport "com1:"
         comtimeout=EOF;
data test;
   infile test length=linelen recfm=F;
   input @;
   if linelen ne 0 then input value;
   else put 'Timeout reading from COM1:';
run;


Options that Relate to Communications Port Timeouts

These options relate to the communications port timeouts.
RMULTI specifies the multiplier, in milliseconds, that is used to calculate the total timeout period for read operations. For each read operation, this value is multiplied by the requested number of bytes to be read.
RCONST specifies the constant, in milliseconds, that is used to calculate the total timeout period for read operations. For each read operation, this value is added to the product of RMULTI and the requested number of bytes.
WMULTI specifies the multiplier, in milliseconds, that is used to calculate the total timeout period for write operations. For each write operation, this value is multiplied by the number of bytes to be written.
WCONST specifies the constant, in milliseconds, that is used to calculate the total timeout period for write operations. For each write operation, this value is added to the product of the WMULTI member and the number of bytes to be written.
RINT specifies the maximum time, in milliseconds, that is allowed to elapse between the arrival of two characters on the communications line.


Chapter Contents

Previous

Next

Top of Page

Copyright 1999 by SAS Institute Inc., Cary, NC, USA. All rights reserved.