Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1367
  • Last Modified:

Parallel port programming in Win32

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
cunningham
Asked:
cunningham
1 Solution
 
maries032698Commented:
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
 
cunninghamAuthor Commented:
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
 
RONSLOWCommented:
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
[Webinar On Demand] Database Backup and Recovery

Does your company store data on premises, off site, in the cloud, or a combination of these? If you answered “yes”, you need a data backup recovery plan that fits each and every platform. Watch now as as Percona teaches us how to build agile data backup recovery plan.

 
cunninghamAuthor Commented:
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
 
iron_fistCommented:
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
 
RONSLOWCommented:
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

Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now