Futurebasic/Language/Reference/open

Revised
March 2, 2001 (Release 5)

Description
This statement opens a serial communications port (the modem port or the printer port) so that your program can write to or read from a serial device. The optimal values for the various parameters depend on the device and the desired communications protocol; see the device's manual for more information. The parameters are interpreted as follows:

¥ Set this either to   or to  or to any port specified to a maximum -8. (Ports are numbered from -1 for the printer port to -8.) The value also usually works to communicate with a built-in modem. Some Macintosh computers provide different values. A Powerbook generally uses  as the infra red port. and  as the internal modem. USB adapters such as the Keyspan adapter will provide different values if the device is connected before booting as opposed to plugging it in after the computer is running.

¥ Set this to one of the following values: 110; 300; 1200; 1800; 2400; 3600; 4800; 7200; 9600; 19200; 38400; 57600, 115200, 230400.

¥ Set this to one of the following values:  ;  ;. The default value is.

¥ Set this to one of the following values:  ;  ;   (1.5 stop bits). The default value is.

¥ Set this to one of the following values:  ;  ;  ;. The default value is. Note: do not set this parameter to the values 5, 6, 7 or 8: these are different from the values of the symbolic constants.

¥ Set this to a number in the range 1 through 32,768. This parameter indicates how many bytes to allocate for an input buffer. The input buffer stores data that is being received, even when the program is not reading it, allowing the program to process data while data is being received in the background. The default value for  is 4096 bytes. To determine the number of unread characters currently in the buffer, use.

Reading Data To read incoming data from an open serial port, use the same commands that you would use to read data from a file; e.g.,,  , etc. Since it's difficult to predict when (if ever) the data will come in, it's best to design your program so that it won't get "stuck" on a single statement waiting for incoming data. Instead, you should execute a loop that periodically checks whether there is any data to read. This will allow your program to proceed with other activities while it's waiting; or to quit waiting if too much time has elapsed.

There are basically three ways to check whether there is any data available in the buffer:

 You can check the value of. This will return zero if no data is available to read; otherwise, it returns the number of bytes waiting to be read.

You can use the  statement. By specifying " ", you instruct the  statement to return immediately if there is no data available; if there is data, the statement reads all the characters currently in the input buffer (up to the maximum allowable length of  ), and puts them into. You can use  after the   statement to determine how many (if any) characters were read.

You can use the  function. It will either return one character from the buffer, or a null string if the buffer was empty.



Writing Data To write data out to an open serial port, use the same commands that you would use to write data to a file; e.g.,,  , etc.

FB Runtime Globals FB has several reserved global variables. (See Subs Files.Incl in Header folder)

After any communications port has been opened or after you make your own call to the runtime, you may refer to   for the total number of devices (maximum 8). contains the name of the device. With this in mind, the serial ports can best be referred to by name rather than number when multiple ports are present or when USB devices are in use for the purpose of emulating serial ports.

To search all available communication ports use the folling lines. This is especially important if a USB/serial port adapter is inserted after the program has started.

Example: 