The Device Kit Table of Contents | The Device Kit Index |
Derived from: none
Declared in: be/device/SerialPort.h
Library: libdevice.so
A BSerialPort object represents an RS-232 serial connection to the computer. 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. To find the names of all the serial ports on the computer, use the CountDevices() and GetDeviceName() functions:
BSerialPort serial; char devName[B_OS_NAME_LENGTH]; int32 n = 0; for (int32 n = serial.CountDevices()-1; n >= 0; n--) { serial.GetDeviceName(n, devName); if ( serial.Open(devName) > 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.
|
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()
|
Makes sure the port is closed before the object is destroyed.
|
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()
|
Returns true if the Clear to Send (CTS) pin is asserted, and false if not.
|
Returns true if the Data Carrier Detect (DCD) pin is asserted, and false if not.
|
Returns true if the Data Set Ready (DSR) pin is asserted, and false if not.
|
Returns true if the Ring Indicator (RI) pin is asserted, and false if not.
|
These functions open the name serial port and close it again. To get a serial port name, use the GetDeviceName() function; an example is shown in the introduction.
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.)
When a serial port is opened, its input and output buffers are emptied and the Data Terminal Ready (DTR) pin is asserted.
RETURN CODES
positive integers (not 0). Success.
|
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 (2048 on Mac hardware). 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.
The default shouldBlock setting is true.
There is no time limit if the timeout is set to B_INFINITE_TIMEOUT—Read() (and WaitForInput()) will block forever. Otherwise, the timeout is expressed in microseconds and can range from a minimum of 100,000 (0.1 second) through a maximum of 25,500,000 (25.5 seconds); differences less than 100,000 microseconds are not recognized; they're rounded to the nearest tenth of a second.
RETURN CODES
Read() returns...
RETURN CODES
SetTimeout() returns...
|
These functions set and return characteristics of the serial unit used to send and receive data.
|
These functions set and return the rate (in bits per second) at which data is both transmitted and received.
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.
RETURN CODES
SetDataRate() returns...
|
Asserts the Data Terminal Ready (DTR) pin if the pinAsserted flag is true, and de-asserts it if the flag is false. The function should always returns B_OK.
|
These functions set and return the type of flow control the driver should use. There are four possibilities:
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.
|
Asserts the Request to Send (RTS) pin if the pinAsserted flag is true, and de-asserts it if the flag is false. The function always returns B_OK.
|
Waits for input data to arrive at the serial port and returns the number of bytes available to be read. If data is already waiting, the function returns immediately.
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.
|
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 (1024 on Mac hardware).
RETURN CODES
non-negative integer. Success; the value is the number of bytes that were written.
The Device Kit Table of Contents | The Device Kit Index |
Copyright © 2000 Be, Inc. All rights reserved..