Click here to Skip to main content
15,867,453 members
Articles / Programming Languages / C++
Article

Port Access

Rate me:
Please Sign up or sign in to vote.
3.78/5 (34 votes)
17 Aug 20041 min read 175.2K   4.1K   40   40
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.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Web Developer
Iran (Islamic Republic of) Iran (Islamic Republic of)
I live in Iran . I started hardware programming when I was young. I designed and built some ISA cards for attaching to PC.like ocsiloscope and signal generator. Now I am working for a engineering company and Manager of some project.

Comments and Discussions

 
QuestionUserPort under WINDOWS 10 Pin
LERAYORGUE26-Dec-18 5:17
LERAYORGUE26-Dec-18 5:17 
GeneralMy vote of 4 Pin
Member 870111428-Apr-12 22:19
Member 870111428-Apr-12 22:19 
GeneralRun file Pin
ROVIO5-Oct-09 3:47
ROVIO5-Oct-09 3:47 
Questionemergency help Pin
Ahmad KASSEM19-Apr-08 0:39
Ahmad KASSEM19-Apr-08 0:39 
QuestionI have a problem with a parallel port, could you please help me? Pin
luixilva9-Oct-07 11:53
luixilva9-Oct-07 11:53 
QuestionInput Problem Pin
mirispearl5-Sep-07 19:37
mirispearl5-Sep-07 19:37 
Questiona question from lovers_ali Pin
Sean Ewington6-Jun-07 8:41
staffSean Ewington6-Jun-07 8:41 
AnswerRe: a question from lovers_ali Pin
asef9-Jun-07 19:26
asef9-Jun-07 19:26 
Hi Go to this link. It works well:
http://www.logix4u.net/inpout32.htm

Have fun
Smile | :)

MOSTAFA

GeneralPort access project Pin
Rupanagudi4-May-07 15:25
Rupanagudi4-May-07 15:25 
GeneralRe: Port access project Pin
f227-May-07 16:49
f227-May-07 16:49 
GeneralRe: Port access project Pin
asef9-Jun-07 19:42
asef9-Jun-07 19:42 
GeneralRe: Port access project Pin
Wamuti16-Nov-07 6:07
Wamuti16-Nov-07 6:07 
Generalplotting a graph in VC++ Pin
Member 343294419-Mar-07 22:07
Member 343294419-Mar-07 22:07 
GeneralVisual C++ 6 Or Visual C++.Net 2005 Pin
saman_programing29-May-06 1:13
saman_programing29-May-06 1:13 
GeneralRe: Visual C++ 6 Or Visual C++.Net 2005 Pin
asef29-May-06 2:40
asef29-May-06 2:40 
Questionejraye barname dar Turbo C++ 3.0 DOS Pin
saman_programing24-May-06 18:55
saman_programing24-May-06 18:55 
AnswerRe: ejraye barname dar Turbo C++ 3.0 DOS Pin
asef26-May-06 18:49
asef26-May-06 18:49 
Generalserial ( COM) port accessing Pin
bala nagesh15-May-06 18:29
bala nagesh15-May-06 18:29 
GeneralRe: serial ( COM) port accessing Pin
asef15-May-06 20:14
asef15-May-06 20:14 
QuestionInput Probs Pin
bhaskar_gupta8-Feb-06 8:31
bhaskar_gupta8-Feb-06 8:31 
AnswerRe: Input Probs Pin
asef11-Feb-06 17:45
asef11-Feb-06 17:45 
GeneralHow to control parallel port by programming Visual C++ Pin
BK200725-Dec-06 4:07
BK200725-Dec-06 4:07 
GeneralParallel port Pin
dzldan2-Nov-05 10:46
dzldan2-Nov-05 10:46 
GeneralRe: Parallel port Pin
asef4-Nov-05 17:36
asef4-Nov-05 17:36 
GeneralRe: Parallel port Pin
BK200725-Dec-06 4:05
BK200725-Dec-06 4:05 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.