You are here:

C++/using c++ to use parallel or serial port


how can we use c++/c to use parallel and serial ports??
are there any special classes defined for use of them or special programming

The answer depends on what operating system (if any) you are using.

You will often find parallel and serial ports can be opened like files, using the (system specific) device name instead of a file pathname (e.g. "LPT1", "COM2" etc. on a Windows system).

This means you can open and read/write to such devices just as you would a file: Using C++ (file) IOStream classes, using C fxxx functions (e.g. fopen) or using system specific API functions such as the Win32 function CreateFile et. al.

For further control you will have to use system-specific functionality such as the Win32 Comm functions (SetCommState  etc.). or UN*X ioctl.

As to special classes - no not in the standard C++ library (C of course does not support classes).

As to special programming techniques - well there will be considerations because you are sending/receiving data through a device which - especially in the case of serial ports - can be quite slow. Hence handling data should be tackled with care - maybe having a queues of sent/received data from which bytes are sent/received in small chunks at regular intervals (if for example you are using a reactive system such as a GUI program) or on a separate thread.

There may be special steps needed to set up the device into the required mode (e.g. serial port bit rate, data bits, start and stop bits, parity etc.).

You may need to follow or define a specific protocol to initiate a connection with some other device or computer - depending on what you are trying to do. However these sorts of things are not just limited to serial / parallel port usage - you have to perform similar steps with for example setting up a network conversation using sockets.

Additional care will probably need to be taken in the face of error conditions (again, similar to network programming) as there are more ways for operations to fail - e.g. someone trips over the cable or turns off the device at the other end...

Note that in modern desktop and server operating systems you cannot access the hardware that supports the ports directly - the operating system controls such access through the use of device drives and a higher level set of application programming interfaces (usually sets of functions together with various types) like the Win32 functions mentioned above.

On the other hand if you are targeting small, embedded devices then maybe you can access the hardware directly - and how you do this depends on the hardware. Sometimes on such a system you would use C or C++ in an unhosted mode with very limited library support - so you would effectively have to supply  functionality such as file and port access yourself (or someone else on the team will). Even with some OS support you may still prefer to access the hardware on such systems directly for the added degree of control it affords. If you are interested in these types of scenarios then you might be interested in a report by the ISO C++ standard committee on C++ performance which has sections on using C++ on embedded systems and a hardware addressing interface.

Other possible useful links are:

1/ There is an old MS technical article titled "Serial Communications in Win32" by Allen Denver from December 1995 which shows the basics of using serial ports with Win32. It uses the Win32 APIs throughout - so you will see CreateFile rather than fopen, or std::fstream for example. Try to read this article.

There is of course the standard reference documentation for all Microsoft development topics on the MSDN site at, including specifically in the online copy of the MSDN library at (links seem ok at time of writing but may vary over time...).

2/ For information on hardware port interfacing - with some background to doing so under MS Windows NT/2000/XP - try and specifically This should give you more than enough information to be going on with although not specifically C/C++.

Hope this is of some help.


All Answers

Answers by Expert:

Ask Experts


Ralph McArdell


I am a software developer with more than 15 years C++ experience and over 25 years experience developing a wide variety of applications for Windows NT/2000/XP, UNIX, Linux and other platforms. I can help with basic to advanced C++, C (although I do not write just-C much if at all these days so maybe ask in the C section about purely C matters), software development and many platform specific and system development problems.


My career started in the mid 1980s working as a batch process operator for the now defunct Inner London Education Authority, working on Prime mini computers. I then moved into the role of Programmer / Analyst, also on the Primes, then into technical support and finally into the micro computing section, using a variety of 16 and 8 bit machines. Following the demise of the ILEA I worked for a small company, now gone, called Hodos. I worked on a part task train simulator using C and the Intel DVI (Digital Video Interactive) - the hardware based predecessor to Indeo. Other projects included a CGI based train simulator (different goals to the first), and various other projects in C and Visual Basic (er, version 1 that is). When Hodos went into receivership I went freelance and finally managed to start working in C++. I initially had contracts working on train simulators (surprise) and multimedia - I worked on many of the Dorling Kindersley CD-ROM titles and wrote the screensaver games for the Wallace and Gromit Cracking Animator CD. My more recent contracts have been more traditionally IT based, working predominately in C++ on MS Windows NT, 2000. XP, Linux and UN*X. These projects have had wide ranging additional skill sets including system analysis and design, databases and SQL in various guises, C#, client server and remoting, cross porting applications between platforms and various client development processes. I have an interest in the development of the C++ core language and libraries and try to keep up with at least some of the papers on the ISO C++ Standard Committee site at


©2017 All rights reserved.

[an error occurred while processing this directive]