Solved

Parallel port programming in Win32

Posted on 1998-04-07
6
1,074 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
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 100 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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 

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

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Suggested Solutions

This article shows how to make a Windows 7 gadget that extends its U/I with a flyout panel -- a window that pops out next to the gadget.  The example gadget shows several additional techniques:  How to automatically resize a gadget or flyout panel t…
A theme is a collection of property settings that allow you to define the look of pages and controls, and then apply the look consistently across pages in an application. Themes can be made up of a set of elements: skins, style sheets, images, and 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…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

747 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now