Click here to Skip to main content
Click here to Skip to main content

Port Access

By , 17 Aug 2004
 

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

About the Author

asef
Web Developer
Iran (Islamic Republic Of) Iran (Islamic Republic Of)
Member
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.

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
GeneralMy vote of 4memberMember 870111428 Apr '12 - 22:19 
its helpful!
GeneralRun filememberROVIO5 Oct '09 - 3:47 
Good day
Can you please upload the .exe file for me, and send me the link.
I do not have a clue when it comes to things like source code.
 
Thank you very much.
Regards Riaan Deyzel Laugh | :laugh:
Questionemergency helpmembermechatron19 Apr '08 - 0:39 
Roll eyes | :rolleyes: Roll eyes | :rolleyes: Roll eyes | :rolleyes:
hello asef
i am ahmad kassem a student from Lebanon, and I want to use the parallel port to control a motor in robotic arm.
So I want information about accessing this port in c++ because I don’t know any other language, and I also need to know how to use the inpout32.dll file in c++.
So please help me
Thank you very much
QuestionI have a problem with a parallel port, could you please help me?memberluixilva9 Oct '07 - 11:53 
I have a problem: a program X send an output to the printer, but.... i need this output in a file in another computer. Then I need to "catch" this output trough the parallel port in another computer. I am using a cable DB25M - DB25M connected to the parallel port of both computers (this cable was used with a switch between two printers and worked properly), but.... i just can't do it, i don't know what happen, because i can read properly the ports: data, status and control.
 
I am using a visual C++ program to catch the output from the other computer trough the parallel port. The code i am using is something like this...
 
fin = 0;
count = 0;
data = _inp( dataport );
stat = _inp( statport );
ctrl = _inp( ctrlport );
do
{
ctrl = ctrl & 0x000000ff;
if( ctrl & 0x01 )
{
stat = stat | 0x80; // set the busy port on
retu = _outp( stat );
wait( (clock_t) 5 ); // wait for 5 micro-seconds
data = _inp( dataport );
printf( "Data read: 0x%2x\n", data );
stat = stat | 0x40; // set acknowledge on
retu = _outp( stat );
wait( (clock_t) 5 ); // wait for 5 micro-seconds
stat = stat & 0x7f; // set busy off
retu = _outp( stat );
stat = stat & 0xbf; // set ackn off
retu = _outp( stat );
++count;
}
if( _kbhit() != 0 )
{
c = getch();
if( c == 'c' || c == 'C' )
{
fin = 1;
printf("End of process\n");
}
}
} while( fin != 0 );
 
Could you please help me?
 
Thank you in advance for your attentions.
 
Regards.
QuestionInput Problemmembermirispearl5 Sep '07 - 19:37 
Hi
I'm having the same problem that bhaskar_gupta explained in the thread "Input Probs" on 8 Feb '06. any solution? Smile | :)
 
|r|spearl
Questiona question from lovers_aliadminSean Ewington6 Jun '07 - 8:41 
"hi when i ues your source code thrw this error : Unhandled exception at 0x00435cdb in UserPort.exe: 0xC0000096: Privileged instruction. what i do? plz help me"
- lovers_ali
AnswerRe: a question from lovers_alimemberasef9 Jun '07 - 19:26 
Hi
Go to this link. It works well:
http://www.logix4u.net/inpout32.htm
 
Have fun
Smile | :)
 
MOSTAFA

GeneralPort access projectmemberRupanagudi4 May '07 - 15:25 
Asef
 
Have you tested for yourself the project that you have posted in the site. I am not sure whether _inp and _outp will work properly in Windows XP. I just wanted to confirm whether you have tested it out and whether it works perfectly.
 
I heard that no programming language will be able to directly write and read data from ports in Win XP and that you need low level assembly code embedded in your C code.
 
Thanks
Phaneendra
GeneralRe: Port access projectmemberf227 May '07 - 16:49 
that's why the he said u must copy this file "PortAccess.sys" from the zip in order to run the program. the sys file provide easy calling function like old time when we did in dos platform.
 
from,
-= aLbert =-

GeneralRe: Port access projectmemberasef9 Jun '07 - 19:42 
Hi Sir or Madam
When I posted an article in the site, it means at least I tested it several times. So...
Go to this link:
http://www.logix4u.net/inpout32.htm
 
Becareful
 
Have Fun
 
MOSTAFA

GeneralRe: Port access projectmemberWamuti16 Nov '07 - 6:07 
asef wrote:
Becareful

 
Why do you say "Be careful"?
 

asef wrote:
...So...
Go to this link:...

I have downloaded the dll. What do i do with it. I am using visual C++ 6.0.
Please help because i am having the same problem but i know your code is correct.
 
Wamuti: Any man can be an island, but islands to need water around them!
Edmund Burke: No one could make a greater mistake than he who did nothing because he could do only a little.

Generalplotting a graph in VC++memberMember #343294419 Mar '07 - 22:07 
hi.my name is Smart.in my project, how do i plot graphs using VC++?
pliz help.
GeneralVisual C++ 6 Or Visual C++.Net 2005membersaman_programing29 May '06 - 1:13 
ba arze pozesh bekhatere inke man engilisi nemitonam khob sohbat konam
 
ye soal dashtam man barnameye shoma ra chejori bayad ejra konam
dar c++ 6 be man 2 ta error mide va dar .Net 2005 ke aslan mige filha ghadimi hast
 
moshkele man chegonegiye ejraye in barname hast
 
ba tashakor az shoma
GeneralRe: Visual C++ 6 Or Visual C++.Net 2005memberasef29 May '06 - 2:40 
Salam
bayad barname ra dar VC++ 6 ejra konid.
Barnameh error khassi nadarad.agar shoma ba moshkeli barkhord kardid matne daghighe error ra baraye man befrestid.
ba emaile man tamas begirid:
mostafa.asef@gmail.com
Have fun
 
MOSTAFA
Questionejraye barname dar Turbo C++ 3.0 DOSmembersaman_programing24 May '06 - 18:55 
ba salam
 
chejori man mitonam in barname ra dar Turbo C++ 3.0 DOS ejra konam
chon onja tavabeye be name _outp() nadarim va bejaye on outportb() darim
 

ba tashakor az shoma:
AnswerRe: ejraye barname dar Turbo C++ 3.0 DOSmemberasef26 May '06 - 18:49 
Hi
If your OS is not Windows XP you don't need this program to access the parallel port.

 
MOSTAFA
Generalserial ( COM) port accessingmemberbala nagesh15 May '06 - 18:29 
I have to read an 18bit data (9+9 one after other)from serial port.Is it possible,if not how can i do that.
 
I am a keen programmer.
GeneralRe: serial ( COM) port accessingmemberasef15 May '06 - 20:14 
Hi
My article is about Parallel Port not serial.
You can search serial port in code project. there are a lot of article in this feild.
Smile | :)
 
MOSTAFA
QuestionInput Probsmemberbhaskar_gupta8 Feb '06 - 8:31 
I have set parallel port as EPP+ECP mode to ensure bidirectional data port. Yet when i am reading the port, it aways shows 255 as input no matter if its your above program or any other input program (as in i set t control bit CS 5 (37A) to 1 (value 32) to disable output).
Why if there is no external voltage lines, its not able to read it? i mean y its always reading 255 only?
 

 
Bhaskar Gupta

AnswerRe: Input Probsmemberasef11 Feb '06 - 17:45 
Hi
I also have this problem.
I will update the article as soon as possible.
Have Fun.
Smile | :)
 
MOSTAFA
GeneralHow to control parallel port by programming Visual C++memberBK200725 Dec '06 - 4:07 
Dear Mr. asef,
 
My nick name is BK2007, and I know you via the code project website.
I have a small assigment to control a peripheral device via parallel port by programming Visual C++.
I'm have down loaded your program (source code)but it doens't work properly, so would you be so kind to help me to clarify it and show me the way to get the solution!
My e mail: bangkokhanoi2007@yahoo.com
Thank you in advance!
BK2007

GeneralParallel portmemberdzldan2 Nov '05 - 10:46 
I downloaded the code and was playing with it, I do not see any data on parallel port pins. I believe this is talking to the virtual printer port and not the real thing. Any clues on how to get it for real. When I say output data then inout, the data comes back on the inout pins but nothign is looped back, which led to me to say we are not talking to the real thing.
GeneralRe: Parallel portmemberasef4 Nov '05 - 17:36 
Hi dzldan;
You can send data to parallel port in real way. but you can not get data from it. input command is for verifying access to the parallel port.
 
Have FunSmile | :)
 
MOSTAFA
GeneralRe: Parallel portmemberBK200725 Dec '06 - 4:05 
Dear Mr. dzldan,
 
My nick name is BK2007, and I know you via the code project website.
I have a small assigment to control a peripheral device via parallel port by programming Visual C++.
I'm still confused about how to do it, so would you be so kind to help me to get the solution!
My e mail: bangkokhanoi2007@yahoo.com
Thank you in advance!
BK2007

Generalport Programming Problem In XPmemberMasoud Aghapour7 Sep '05 - 17:49 
I run such a program in windows XP but it crashes, Is there a way to solve this problem?

 
regards,
Aghapour-Masoud.

GeneralRe: port Programming Problem In XPmemberasef21 Oct '05 - 18:49 
Hi,
Yes,Sometimes it does.
But if you must change LPT setting you will be able to do it.
Have fun.

 
MOSTAFA
Generalport interface for XPmembersarkiszakaryan23 Aug '05 - 11:26 
I can get this code to work on WINDOWS XP.
 
Does anybody know how to make this work on XP?
 
Thanks,
Sarkis.
 


GeneralI/OmemberGeorgi Petrov19 Jul '05 - 10:37 
Hi,
I have a lot of fun before 3-5 months with this driver code.
How you exactly start the sys driver without "regedit" manipulations?
best
GeneralPrivileged Instruction problemmemberjrocnuck1 Dec '04 - 8:45 
I can get this code to work on some XP boxes, but others give me privileged instruction exceptions when I do the _inp and _outp calls. The code says that the driver loads ok, but I still get these errors..
 
Does anybody know how to make this work 100% of the time on any NT/2000/XP setup?
 
thanks,
Eric
 

by the way.. I have Service Pack 1 on some of these and have the problems... so far I haven't tested with Service Pack 2
 
Thanks.
 
Eric Nuckols
jrocnuck@hotmail.com
GeneralRe: Privileged Instruction problemmemberrichy_b20 Dec '04 - 4:15 
I have the same porblem with winXP/SP2 VC6 on a dell d800 notebook.
 
anybody?
GeneralRe: Privileged Instruction problemmemberjrocnuck20 Dec '04 - 10:41 
There is another alternative to this one that works without having privileged instruction problems.. here it is: http://www.beyondlogic.org/porttalk/porttalk.htm although as it is programmed, it does not support win95 and 98. If you wish to do _inp() and _outp() calls with this code on win95 or 98, you need to rename the #defines for inp and out in this code to something like portalk_inp() and portalk_outp() so that the regular native inp() and outp() functions are called for win95/98
 
I exchanged emails with Thomas Franzon and he informed me that he is working on a new version of his parallel port access program because he has found some problems with various other drivers such as Nvidia graphics adapter drivers.
 

 

 
Eric Nuckols
jrocnuck@hotmail.com
GeneralRe: Privileged Instruction problemmemberasef28 Aug '05 - 21:38 
Hi
You can use _inpw and _outpw to solve the problem.
Have fun
Smile | :)
 
MOSTAFA
GeneralRe: Privileged Instruction problemmemberjrocnuck29 Aug '05 - 2:42 
I don't know.. I have no need to try to figure out this problem any longer because I just use PortTalk for NT/2k/XP..etc.. and on 95/98 the regular calls to _inp and _outp work..
 

 
Eric Nuckols
jrocnuck@hotmail.com
GeneralWhere the source codesussM.Atia20 Nov '04 - 7:14 
Dea, Where is the source code? Nothing is downloaded.D'Oh! | :doh:
GeneralRe: Where the source codememberMAHDI.M24 Nov '04 - 2:58 
Hi,
U can download the source code From the address at the top of article. Then U can use it in VC++.Roll eyes | :rolleyes:
 
Fozeil
GeneralSource codememberasif ahsan18 Aug '04 - 8:04 
Where is the source code for the portaccess.sys?
GeneralRe: Source codememberjrocnuck1 Dec '04 - 9:10 
I found the source here:
 
http://www.mattjustice.com/parport/userport.zip
 
Eric Nuckols
jrocnuck@hotmail.com
GeneralAnother hardware related article!memberA. Riazi18 Aug '04 - 2:48 
Thanks for sharing your knowledge.
 
A. Riazi
GeneralRe: Another hardware related article!sussAnonymous21 Aug '04 - 19:41 
Thanks for paying attention to me.

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

Permalink | Advertise | Privacy | Mobile
Web02 | 2.6.130516.1 | Last Updated 18 Aug 2004
Article Copyright 2004 by asef
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid