Can you please explain me in brief, how can I access the parallel port in XP with C++ as a programming language?

That depends on what exactly you mean by parallel port.

If you mean the (now legacy) parallel printer ports found on PCs then for Microsoft systems you can access them using file I/O (e.g. C++ IOStreams std::fstream etc.) using (device) names like "LPT1" (or possibly use "LPT1:" which would have been the MS-DOS style name). I have never done this for a parallel port but did try it once for a serial mouse connected to a serial port (COM1, COM2 etc.). For example the following small program should output  "LPT1 opened OK":" - it did on my Vista 64-bit system when built using MS Visual C++ 2008:

   #include <iostream>
   #include <fstream>

   int main()
       std::ifstream lpt1("LPT1");
       if ( lpt1.is_open() )
         std::cout << "LPT1 opened OK\n";
         std::cout << "LPT1 failed to open\n";

Note that I am using an input file stream so the device is opened for reading. If this name were interpreted as a file then that file would have to exist before hand - which is very unlikely, or the open request would fail. You might like to try replacing the name "LPT1" with "LPT1:" and then "a_file" - the former should open OK and the latter fail (unless you happen to have a file called a_file in the directory used to try to open the file in).

The Microsoft Developer Network library documentation contains an article on naming files and directories which mentions all the device names (as these need to be avoided for file names!). See http://msdn.microsoft.com/library/aa365247(VS.85).aspx (correct at time of writing).

If you need to do more with a (printer) parallel port device then you will need to step out of the support provided by the C++ library and into the world of the Windows API. I present a few points of possible interest below with, as I mentioned in my instructions to questioners, references to the MSDN library for further information. If you want more information then I suggest you continue to query the MSDN site and then the Internet at large.

Note that these pieces of information are outside of C++, and although I have some familiarity with Microsoft development topics I have not had to use these specific areas of functionality.

You may need to use device I/O control functionality provided by the parallel port driver (see for example: http://msdn.microsoft.com/library/ms798378.aspx). For more on device services see http://msdn.microsoft.com/library/aa163253.aspx and for device I/O control in particular see http://msdn.microsoft.com/library/aa363219(VS.85).aspx and the DeviceIoControl API function.

If you need to instrument parallel ports then maybe the Windows Management Instrumentation API might be of use (http://msdn.microsoft.com/library/aa394582(VS.85).aspx). I note there are some references to parallel ports: http://msdn.microsoft.com/library/aa387952(VS.85).aspx and http://msdn.microsoft.com/library/aa394247.aspx for example.

Finally, if you meant general processor parallel I/O port access as may be needed to setup and use some hardware device then you probably require a device driver as XP, like all big, grown up, operating systems (UN*X, Linux, Mac OS X, VMS etc), protects systems resources from direct access by user level code. Note that this is different to the Windows 9x,ME operating systems which were basically built on top of MS-DOS as were the 16-bit Windows versions before them (although the later versions may have been better - I did not really pay them that much attention <g>).

For information on hardware port interfacing - with some background to doing so under MS Windows NT/2000/XP - try http://www.beyondlogic.org/ and specifically http://www.beyondlogic.org/porttalk/porttalk.htm. This should give you more than enough information to be going on with although not specifically aimed at C++.

Oh, and I do not understand the link between the question subject - Physics - with the question's content.

Hope this helps - if only to point you to further reading.  


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 http://www.open-std.org/jtc1/sc22/wg21/.


©2017 About.com. All rights reserved.

[an error occurred while processing this directive]