Solved

Parallel port programming in Win32

Posted on 1998-04-07
6
1,200 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 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
Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

 

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

Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

Question has a verified solution.

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

What my article will show is if you ever had to do processing to a listbox without being able to just select all the items in it. My software Visual Studio 2008 crystal report v11 My issue was I wanted to add crystal report to a form and show…
Entering time in Microsoft Access can be difficult. An input mask often bothers users more than helping them and won't catch all typing errors. This article shows how to create a textbox for 24-hour time input with full validation politely catching …
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…
There are cases when e.g. an IT administrator wants to have full access and view into selected mailboxes on Exchange server, directly from his own email account in Outlook or Outlook Web Access. This proves useful when for example administrator want…

724 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