65.9K
CodeProject is changing. Read more.
Home

Port Access

starIconstarIconstarIcon
emptyStarIcon
starIcon
emptyStarIcon

3.78/5 (34 votes)

Aug 18, 2004

1 min read

viewsIcon

180983

downloadIcon

4123

A way to access parallel port in Windows XP

Introduction

At first I want to thank Mr. Tomas Franzon as I used his help a lot in this project. This code opens and closes 0x378 port ( printer port ) manually. In XP we have no permission to access external port like Printer. Here we copy a sys file to system32 and use it in the code so we can send and receive data through this port.

Using the code

For using the code you have to copy PortAccess.sys ( exists in source ) to system32\drivers. Then you can run the program.

For sending and receiving data we do this :

#include <conio.h>
#include <math.h>
// 

         // for sending data
         _outp(Port_Address,iByte); 

//

Return Value:

This function returns data output . There is no error return.

Parameters:

Unsigned short Port Number ( defined 0x378 )

Int Output value

         // for receiving data
         iByte=_inp(Port_Address); 

Return Value:

This function returns the byte, word or int from port. There is no error report

Parameters:

Unsigned short Port Number ( defined 0x378 )

When you check a check box the iBitOut value is Set or Reset.

// to set or reset a bit
iBitOut[i]=1-iBitOut[i];
 

When you click output button the Bits are changed to int value to send to port.

 
         // to send value to port

         iByte=0;

         for (i=0;i<=7;i++)

         {

                 iTemp=pow(2,i);

                 nIndex=iTemp*iBitOut[i];

                 iByte=iByte+nIndex;

         }

         _outp(Port_Address,iByte);
 

When you click input button the input value is read and iBitIn[i] becomes set or reset.

 

         // to get data from port 

         iByte=_inp(Port_Address);

         for (i=7;i>=0;i--)

         {

                 nIndex=pow(2,i);

                 if (iByte>=nIndex)

                 {        iByte=iByte-nIndex;

                          iBitIn[i]=1;

                 }

                 else iBitIn[i]=0;

         } 

For checking a check box we use code below :

         // to check a check box

         if (iBitIn[0]==1)
                 CheckDlgButton(hDlg,IDC_CHECKINPUT1,BST_CHECKED);
 

Points of Interest

I always work with peripheral devices. In these cases, communicating in parallel mode is more flexible and easier than the other modes like serial communication. So I am forced to go to this field. You can send and receive int, long, Byte, unsigned short, unsigned char etc as easy as possible.