?
Solved

Parallel port programming in Win32

Posted on 1998-04-07
6
Medium Priority
?
1,248 Views
Last Modified: 2013-12-03
I would like to use the parallel port with Win32 API routines. I know that I can open the port using 'CreateFile' and write the port using the 'WriteFile' routine. However, I assume that this lets you write 8 bits of data via the parallel port 'data' register. I need to be able to use the parallel port 'status' register and the 'control' register. Do I have to use a driver in order to do this or is there a way to access these ports within the Win32 API??

In case I am not clear on what is meant by the parallel port data, status, and control registers, I am talking about the registers that occupy I/O space. Normally, the first parallel port resides at I/O address 0x3BC. This is the 'data' register of the parallel port. The status and control registers follow at I/O address 0x3BD and 0x3BE, consecutively. These are the registers that I would like to access via the Win32 API.
0
Comment
Question by:cunningham
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
6 Comments
 

Expert Comment

by:maries032698
ID: 1412604
I was just going to post the same question to the exchange. The only difference is that on my machine, the addresses are 0x378 (data) 0x379 (status) and 0x37A and I am using the _inp() and _outp() functions. So, for example, _inp(0x378) should read the byte data register byte.
If you find out more, please let me know.


0
 

Author Comment

by:cunningham
ID: 1412605
As far as I can tell, parallel ports reside at either 0x3BC, 0x378, or 0x278. I think it is dependent on the type of motherboard you have in your system. I am not very clear on this and am not sure exactly how you find out what address is assigned to LPT1, LPT2, etc.
0
 
LVL 10

Accepted Solution

by:
RONSLOW earned 200 total points
ID: 1412606
This is code I use...

static int DataRegister = 0x278;
#define DATAREGISTER ((unsigned short) DataRegister)
#define STATUSREGISTER ((unsigned short) (DataRegister + 1))
#define CONTROLREGISTER ((unsigned short) (DataRegister + 2))

void OutData(BYTE b)      { _outp (DATAREGISTER, b); }
void OutControl(BYTE b)      { _outp (CONTROLREGISTER, b); }
void OutStatus(BYTE b)      { _outp (STATUSREGISTER, b); }

int GetStatus(void)            { return _inp(STATUSREGISTER); }
int GetControl(void)      { return _inp(CONTROLREGISTER); }

bool SetPrinterPort(int which) {
    /* Select print port at one of the allowable addresses */
    /* which = 0,1,2,... and when the selection is not ok, return false */
    switch (which) {
    case 0: DataRegister = 0x278; break;
    case 1: DataRegister = 0x3BC; break;
    case 2: DataRegister = 0x378; break;
    default: return false;
    }
    return true;
}

...

can either get port number info from user or write a loop like this...

for (int j = 0; !ok && SetPrinterPort(j); j++) {
      // try to access the port
}

Roger

0
What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

 

Author Comment

by:cunningham
ID: 1412607
RONSLOW - The answer that you gave is good. However, I am looking for a way to use the standard communication functions of the Win32 API (ie. CreateFile("LPT1"...), ReadFile, WriteFile, etc.) to be able to control all of the registers of the parallel port. I knew that I could use the _inp() and _outp() functions to directly access these registers although this was not stated in my question (sorry - my bad). Is there a way to use the Win32 API functions to control the data, status, and control registers of the parallel port or do I have to use the _inp and _outp functions??
0
 
LVL 1

Expert Comment

by:iron_fist
ID: 1412608
If your program will be run on Windows 95 only, you can use ClearCommError to read the status register. Otherwaise, I'm afraid you have to use a driver to access the parallel port registers.
0
 
LVL 10

Expert Comment

by:RONSLOW
ID: 1412609
The answer is "No" as far as Win32 API is concerned.  The best solution is to write a VxD (for win95) or NT Driver (for NT).  These would do the _inp and _outp calls on your behalf.

On Win95 this is not strictly necessary, but a VxD can make timing critical operations more reliable and, if properly written, will stop conflicts on the printer port.

On NT a driver is (almost) required, although it is possible to turn the protection off for the port access, but makes it open season on the parallel port and could cause system problems.

0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article surveys and compares options for encoding and decoding base64 data.  It includes source code in C++ as well as examples of how to use standard Windows API functions for these tasks. We'll look at the algorithms — how encoding and decodi…
For a while now I'v been searching for a circular progress control, much like the one you get when first starting your Silverlight application. I found a couple that were written in WPF and there were a few written in Silverlight, but all appeared o…
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question