Click or drag to resize

Understanding Buffered I/O

Most of the I/O functions exposed via the I/O object send data to or read data from an in-memory buffer managed by Nimbus. Rather than sending data directly to the device, write operations format the data and send it to an internal buffer where data accumulates until it is ready to be sent. A similar operation takes place with read functions -- data is read from the device in "chunks" and stored in a read buffer from which the various Nimbus Template Library read functions obtain their data. Buffering data in this way can improve overall performance by minimizing the total number of I/O roundtrips between the control computer and the instrument.

Buffered I/O Write Operations

With the exception of the four low-level write operations, data sent to the device using any of the I/O object functions first passes into the I/O write buffer. The four low-level write operations that send data directly to the device without passing through the write buffer are:

Functions such as Printf format the data and then copy it into the write buffer. Data accumulates in the write buffer for all successive write operations. The data in the write buffer is sent to the device when the write buffer is flushed. The write buffer will be flushed in the following circumstances:

  • When an explicit call to FlushWrite is made.

  • When the bFlush parameter of a write function is set to true.

  • When the write buffer is full.

The size of the write buffer can be controlled via the SetBufferSize function. The default size of the write buffer is 4096 bytes.

Buffered I/O Read Operations

With the exception of the four low-level read operations, data read from the device using any of the I/O object functions first passes into the I/O read buffer. The four low-level read operations that read data directly from the device without passing through the read buffer are:

Functions such as Scanf read data from the device in "chunks" of data (4096 bytes, by default) and store the data in the read buffer. Data is only read from the device when the read buffer is empty and a call to a read function requires more data.

Often, a device will send more data in response to a query than is needed. For example, only the first 100 bytes of a lengthy device response block might be needed for processing in the driver. In such cases, the read buffer can be flushed -- either by an explicit call to the FlushRead function or by setting the bFlush parameter of a read function to true. It is important to note that flushing the read buffer also involves reading any pending data from the device itself. This ensures that both the read buffer and the instrument's internal buffer are empty and free of spurious data for the next read operation.

The size of the read buffer can be controlled via the SetBufferSize function. The default size of the read buffer is 4096 bytes.

See Also

Download a complete CHM version of this documentation here.