Click here to Skip to main content
13,354,885 members (78,153 online)
Click here to Skip to main content
Add your own
alternative version


25 bookmarked
Posted 20 Jun 2007

Generic driver interface

, 20 Jun 2007
Rate this:
Please Sign up or sign in to vote.
This article explains a generic communication interface to a peripheral device with a slower or faster data transfer rate than the data input.


This article explains a generic communication interface to a peripheral device with a slower or faster data transfer rate than the data input. A typical scenario would be transferring data from a file to a serial port or a USB device or to the Internet wherein the data transfer rate is slower.

Buffered Carrier

Buffered carrier is a multi threaded interface which can be used for bulk data transfer to a slower peripheral device or network. If you need to transfer data to an external device to which the data transfer rate is slow, then this interface could be an option. The code is programmed in such way that the users can easily connect between the data provider and peripheral driver.

Screenshot - general3.jpg

The diagram above explains the basic structure of the interface. Following are the participants:

Data Provider

Prepare data required to be passed to the terminal device.

Data Carrier

Supply the data to the terminal device or device driver.

Common buffer

This is the critical resource which acts as the channel for data transfer between the data provider and data carrier.

Here there are two threads: one for supplying data to the common buffer and one to push the data from the buffer to a peripheral device. The common buffer acts as the channel between the data provider and the data carrier. This is a critical resource and access to it is synchronized.

Check the flow chart below for more details.

Main Thread (Data provider)

Screenshot - Thread_main.jpg

Buffered carrier thread

Screenshot - Thread_carrier.jpg


To explain the usage of the buffered carrier interface, we will consider an example of copying a file. Here the data provider is the file reader and the device driver is the file writer. We introduce an additional delay of 10ms between each write operation to simulate a peripheral communication.

Dataprovider Code

Step 1

First create a file carrier with the specified buffer size and block size. The buffer size represents the block of data which will be shared between the data provider and the data supplier. The block size represents the unit data taken from the buffer by the carrier for each transfer operation.

BufferedCarrier myBufferedCarrier(BUFFER_SIZE,BLOCK_SIZE); 

Step 2

Now prepare data for transfer.

int count = fread(buffer,sizeof(char),BLOCK_SIZE,fpsrc); 

Step 3

Now check whether there is space for one more block in the buffer. If there is, push the data. Or else, wait for the buffer to get freed.

while(myBufferedCarrier.getFreeBufferSize() < BLOCK_SIZE)
    CLogger::getInstance()->log(5,"Waiting for enough buffer space. 
        Buffer size %d\n",myBufferedCarrier.getFreeBufferSize());

Step 4

Push data to the buffer once space is available in the buffer.


Repeat steps 2 to 4 and push all the data to the buffer.

Step 5

Inform the buffered carrier that there is no more data to transfer.


Step 6

Wait for the buffered carrier to transfer all the data.


Driver Connection Interface

In the buffered carrier, the user needs to add codes for peripheral communication. The following functions are provided for the same.

void BufferedCarrier::initializeDriver()

void BufferedCarrier::transferDatadriver()
    //Slow operation..may be usb / serial / TCP/IP
    FILE *fpDest = fopen("C:\\temp\\out.txt","ab+");
    if(fpDest != NULL)

void BufferedCarrier::deInitializeDriver()

To view the sequence of operations, a simple logger code has been provided along with the code. By setting appropriate values for the predefined values in logger.h, you can view the operations.

#define TRACE                      //Enable or disable logging. 
                                   // If not defined, no log at all
#define TRACE_FILE                 //Enable file logging.
#define TRACE_FILE_NAME "c:\\temp\\bpt_trace.txt"
#define TRACE_CONSOLE             // Enable console logging. 


This article is an attempt to provide a generic interface to a communication channel which is much slower or faster than the input data provider (a peripheral device or a slow communication channel). This is implemented using the Windows thread functions and synchronization between threads are achieved using critical section. Similar implementation can be done for Linux using "pthread_" functions. I can write one for you if you need but on request.

Please send your valuable suggestions and feedback to


This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


About the Author

Boby Thomas P
Software Developer (Senior) DWS
Australia Australia

You may also be interested in...

Comments and Discussions

Questionusb Pin
HOYAM14-Jul-07 7:41
memberHOYAM14-Jul-07 7:41 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.180111.1 | Last Updated 20 Jun 2007
Article Copyright 2007 by Boby Thomas P
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid