The Device Kit: BSerialPort

Derived from: none

Declared in: <device/SerialPort.h>


Overview

A BSerialPort object represents an RS-232 serial connection to the computer. There are four serial ports with standard DB9 connectors on the back of the BeBox; the Macintosh typically has two MiniDin8 ports, one nominally for a modem and the other for a printer.

Through BSerialPort functions, you can read data received at a serial ports and write data over the connection. You can also configure the connection--for example, set the number of data and stop bits, determine the rate at which data is sent and received, and select the type of flow control (hardware or software) that should be used.

To read and write data, a BSerialPort object must first open one of the serial ports by name. For example:

   BSerialPort *connection = new BSerialPort;
   if ( connection->Open("serial2") > 0 ) {
       . . .
   }

The BSerialPort object communicates with the driver for the port it has open. The driver maintains an input buffer to collect incoming data and a smaller output buffer to hold outgoing data. When the object reads and writes data, it reads from and writes to these buffers.

The serial port drivers, and therefore BSerialPort objects, send and receive data asynchronously only.


Constructor and Destructor


BSerialPort()


      BSerialPort(void)

Initializes the BSerialPort object to the following default values:

The new object doesn't represent any particular serial port. After construction, it's necessary to open one of the ports by name.

The type of flow control must be decided before a port is opened. But the other default settings listed above can be changed before or after opening a port.

See also: Open()


~BSerialPort()


      virtual ~BSerialPort(void)

Makes sure the port is closed before the object is destroyed.


Member Functions


ClearInput(), ClearOutput()


      void ClearInput(void)

      void ClearOutput(void)

These functions empty the serial port driver's input and output buffers, so that the contents of the input buffer won't be read (by the Read() function) and the contents of the output buffer (after having been written by Write()) won't be transmitted over the connection.

The buffers are cleared automatically when a port is opened.

See also: Read(), Write(), Open()


Close() see Open()


DataBits() see SetDataBits()


DataRate() see SetDataRate()


FlowControl() see SetFlowControl()


IsCTS()


      bool IsCTS(void)

Returns true if the Clear to Send (CTS) pin is asserted, and false if not.


IsDCD()


      bool IsDCD(void)

Returns true if the Data Carrier Detect (DCD) pin is asserted, and false if not.


IsDSR()


      bool IsDSR(void)

Returns true if the Data Set Ready (DSR) pin is asserted, and false if not.


IsRI()


      bool IsRI(void)

Returns true if the Ring Indicator (RI) pin is asserted, and false if not.


Open(), Close()


      status_t Open(const char *name)

      void Close(void)

These functions open the name serial port and close it again. Ports are identified by path names in the device file system--for example, /dev/ports/serial2. Serial ports are generally clustered in the ports subdirectory of the directory where the device file system is mounted (customarily, /dev). The string passed to Open() should be a relative path name that omits the directories and just names the port. For example, "serial2" is sufficient to name the /dev/ports/serial2 port.

This means that BeBox ports are identified by names that correspond to the labels on the back panel of the machine--or the labels in The Be User's Guide diagram of the back panel. From top to bottom, they are:

"serial1"
"serial2"
"serial3"
"serial4"

If a card with COM3 and COM4 ports has been added to the machine, a BSerialPort object will also be able to open those ports by name:

"com3"
"com4"

The ports on a Macintosh are identified by names that reflect their iconic labels:

"modem"
"printer"

To be able to read and write data, the BSerialPort object must have a port open. It can open first one port and then another, but it can have no more than one open at a time. If it already has a port open when Open() is called, that port is closed before an attempt is made to open the name port. (Thus, both Open() and Close() close the currently open port.)

Open() can't open the name port if some other entity already has it open. (If the BSerialPort itself has name open, Open() first closes it, then opens it again.)

If it's able to open the port, Open() returns a positive integer (not B_OK). If unable, it returns B_ERROR.

When a serial port is opened, its input and output buffers are emptied and the Data Terminal Ready (DTR) pin is asserted.

See also: Read()


ParityMode() see SetDataBits()


Read(), SetBlocking(), SetTimeout()


      ssize_t Read(void *buffer, size_t maxBytes)

      void SetBlocking(bool shouldBlock)

      status_t SetTimeout(bigtime_t timeout)

Read() takes incoming data from the serial port driver and places it in the data buffer provided. In no case will it read more than maxBytes--a value that should reflect the capacity of the buffer. The input buffer of the driver, from which Read() takes the data, holds a maximum of 2,024 bytes on the BeBox and 2,048 bytes on Macintosh computers. This function fails if the BSerialPort object doesn't have a port open.

The number of bytes that Read() will read before returning depends not only on maxBytes, but also on the shouldBlock flag and the timeout set by the other two functions.

Like the standard read() system function, Read() returns the actual number of bytes read (the number of bytes it succeeded in placing in the buffer), which may be 0. If something goes wrong, it returns an error code, including B_INTERRUPTED when the operation is interrupted by a signal and B_FILE_ERROR if the BSerialPort doesn't have a port open. It's not considered an error if a timeout expires.

See also: Write(), Open(), WaitForInput()


SetBlocking() see Read()



SetDataBits(), SetStopBits(), SetParityMode(), DataBits(), StopBits(), ParityMode()


      void SetDataBits(data_bits count)

      void SetStopBits(stop_bits count)

      void SetParityMode(parity_mode mode)

      data_bits DataBits(void)

      stop_bits StopBits(void)

      parity_mode ParityMode(void)

These functions set and return characteristics of the serial unit used to send and receive data. SetDataBits() sets the number of bits of data in each unit. The count can be:

B_DATA_BITS_7 or
B_DATA_BITS_8

The default is B_DATA_BITS_8.

SetStopBits() sets the number of stop bits in each unit. It can be:

B_STOP_BITS_1 or
B_STOP_BITS_2

The default is B_STOP_BITS_1.

SetParityMode() sets whether the serial unit contains a parity bit and, if so, the type of parity used. The mode can be:

B_EVEN_PARITY,
B_ODD_PARITY, or
B_NO_PARITY

The default is B_NO_PARITY.


SetDataRate(), DataRate()


      status_t SetDataRate(data_rate bitsPerSecond)

      data_rate DataRate(void)

These functions set and return the rate (in bits per second) at which data is both transmitted and received. Permitted values are:

B_0_BPS B_300_BPS B_19200_BPS
B_50_BPS B_600_BPS B_31250_BPS
B_75_BPS B_1200_BPS B_38400_BPS
B_110_BPS B_1800_BPS B_57600_BPS
B_134_BPS B_2400_BPS B_115200_BPS
B_150_BPS B_4800_BPS B_230400_BPS
B_200_BPS B_9600_BPS

The default data rate is B_19200_BPS. If the rate is set to 0 (B_0_BPS), data will be sent and received at an indeterminate number of bits per second. A rate of B_230400_BPS is valid only for Macintosh computers, not the BeBox. Rates greater than B_38400_BPS may yield undesirable results for the "serial3" and "serial4" ports on the BeBox.

SetDataRate() returns B_OK if successful, B_NO_INIT if the BSerialPort object doesn't have a port open, and B_ERROR if it fails to set the data rate for any other reason.


SetDTR()


      status_t SetDTR(bool pinAsserted)

Asserts the Data Terminal Ready (DTR) pin if the pinAsserted flag is true, and de-asserts it if the flag is false. This function should always succeed, so invariably returns B_OK.

See also: SetRTS()


SetFlowControl(), FlowControl()


      void SetFlowControl(uint32 mask)

      uint32 FlowControl(void)

These functions set and return the type of flow control the driver should use. There are two possibilities:

B_SOFTWARE_CONTROL Control is maintained through XON and XOFF characters inserted into the data stream.
B_HARDWARE_CONTROL Control is maintained through the Clear to Send (CTS) and Request to Send (RTS) pins.

The mask passed to SetFlowControl() and returned by FlowControl() can be just one of these constants--or it can be a combination of the two, in which case the driver will use both types of flow control together. It can also be 0, in which case the driver won't use any flow control. B_HARDWARE_CONTROL is the default.

SetFlowControl() should be called before a specific serial port is opened. You can't change the type of flow control the driver uses in midstream.


SetParityMode() see SetDataBits()


SetRTS()


      status_t SetRTS(bool pinAsserted)

Asserts the Request to Send (RTS) pin if the pinAsserted flag is true, and de-asserts it if the flag is false. This function should always succeed, so invariably returns B_OK.

See also: SetDTR()


SetStopBits() see SetDataBits()


SetTimeout() see Read()


StopBits() see SetDataBits()


WaitForInput()


      ssize_t WaitForInput(void)

Waits for input data to arrive at the serial port and returns the number of bytes available to be read.

If data is ready to be read when this function is called, it returns immediately without blocking and reports how many bytes there are. If data hasn't arrived, it blocks and waits for the first bytes to be transmitted. When they're detected, it immediately reports how many have arrived.

This function doesn't respect the flag set by SetBlocking(); it blocks even if blocking is turned off for the Read() function. However, it does respect the timeout set by SetTimeout(). If the timeout expires before input data arrives at the serial port, it returns 0.

See also: Read()


Write()


      ssize_t Write(const void *data, size_t numBytes)

Writes up to numBytes of data to the serial port's output buffer. This function will be successful in writing the data only if the BSerialPort object has a port open. The output buffer holds a maximum of 512 bytes on the BeBox and 1,024 bytes on Macintosh computers.

Like the write() system function, Write() returns the actual number of bytes written, which will never be more than numBytes, and may be 0. If it fails, it returns an error--for example, B_FILE_ERROR if the BSerialPort object doesn't have a serial port open and B_INTERRUPTED if a signal interrupts it before it can write anything.

See also: Read(), Open()






The Be Book, in lovely HTML, for the BeOS Preview Release.

Copyright © 1997 Be, Inc. All rights reserved.

Be is a registered trademark; BeOS, BeBox, BeWare, GeekPort, the Be logo, and the BeOS logo are trademarks of Be, Inc.

Last modified June 28, 1997.