FutureBasic Logo

<<    Index    >> FutureBasic 5

open "C"   statement

open "C",portID,baud ¬

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 _modemPort or to _printerPort or to any port sepcified to a maximum -8. (Ports are numbered from -1 for the printer port to -8.) The _modemPort value also usually works to communicate with a built-in modem. Some Macintosh computers provide different values. A Powerbook generally uses _modemPort as the infra red port. and _printerPort 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: _noParity; _oddParity; _evenParity. The default value is _noParity.
Set this to one of the following values: _oneStopBit; _twoStopBits; _halfStopBit (1.5 stop bits). The default value is _oneStopBit.
Set this to one of the following values: _fiveBits; _sixBits; _sevenBits; _eightBits. The default value is _sevenBits. 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 buffer is 4096 bytes. To determine the number of unread characters currently in the buffer, use lof(portID,1).

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., input#, read#, 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:
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., print#, write, etc.

FutureBasic Runtime Globals
FutureBasic has several reserved global variables. (See Subs Files.Incl in Header folder)
gFBHasComTB% //true if comm toolbox is used...
gFBSerialPortCount% //number of com port
gFBSerialName$(n) //serial port name
gFBSerialInName$(n) //input buffer name
gFBSerialOutName$(n) //output buffe name
gOSXSerialInited //[!]0 if serial inited under MacOS X

After any communications port has been opened or after you make your own call to the runtime fn FBInitSerialPorts, you may refer to gFBSerialPortCount% for the total number of devices (maximum 8). gFBSerialOutName$(n) 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 following lines. This is especially important if a USB/serial port adapter is inserted after the program has started.
N.B. After an initial call to fn FBInitSerialPorts, subsequent calls may be needed to refresh the list after devices are removed/added. gFBSerialportCount% must be set to zero prior to any subsequent calls to fn FBInitSerialPorts.
gFBSerialportCount% = 0
// This is for MacOS X
long if system(_sysVers) => 1000
  gOSXSerialInited = _false
end if

See Also:
close; HandShake; loc; lof; input#; read#; read file; read field; inkey$; print#; write; write file; write field