Reading Data from the Communications Port

Overview of 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. The communications parameters that you specify are specific to each data collection device.
After you invoke SAS, 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 SAS encounters an end-of-file (the hexadecimal 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 Function: Windows and WAKEUP Function: Windows.

Communications Port Timeouts

By default, if you are reading from a communications port and a time-out 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 time-out occurs. This behavior is the default. This behavior causes the current DATA step to terminate.
WAIT
instructs the communications port to wait forever for data. This value overrides the time-out. In this case, no record is returned to the DATA step until data are available. This action can cause your program to go into a 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 time-out occurs:
filename test commport "com1" comtimeout=eof;
data test;
   infile test length=linelen recfm=F eof=eof;
   input @;
eof:  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 time-out 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 time-out 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 time-out 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 time-out 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.