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


talk with a I/O port?

Posted on 2003-03-19
Medium Priority
Last Modified: 2013-11-15
I'm trying to send&recieve infomation from a port of my pc. I know that I have to use in C the command _inp and _outp, but I can't make it work... I'd like to see a simple working example of a C program using those two functions, for example with the port number (300H)... Thank you for your colaboration, Rui_Cadete from Portugal.
Question by:rui_cadete

Expert Comment

ID: 8170509
What are you trying to read from the port?  Are you sure that data is there?

int buf[100];
int x;

/* read 100 integers from port 300  - they better be there, or the program will just wait for them and appear hung...*/
  buf[x] = _inp(0x300);


Expert Comment

ID: 8170561
The _inp and _outp work only on DOS, Windows 16 bits and Windows 95/98. It won't work on Win NT/2000/XP.
Are you on the right platform ?
LVL 30

Accepted Solution

Mayank S earned 225 total points
ID: 8172167
Assuming that you are on the right platform:

inp () reads an value from the port number passed to it as an argument, and returns that value (integer), like:

int n ;
n = inp ( 0x300 ) ; // read from port no. 300H
printf ( "\n %d", n ) ;

Similarly, to send a value to a port, you have to use outp () which takes 2 arguments, the port number and the value to be sent:

int n = 45 ;
outp ( 0x300, n ) ; // send the value to port no. 300H

Generally, while sending/ reading values to/ from a port, we prefer to interpret in hexadecimal format, and I would do the above as:

int n = 0x2D ;
outp ( 0x300, n ) ;

Hope that helps! If you want to know something more, then please post a comment.



Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.


Expert Comment

ID: 8177282
what hardware do you want to program? do u have to send bytes with a defined baudrate, parities and so on, or do you have to make the outputs high or low?

e.g. i've made a project to program a relaisboard at com-port. this needed a baudrate of 19200, no parity, one stopbit, and a wordlength of 8bit. u then have to put the bits of the registers of the com-port (there are 8 of them and each has it's own address) and then, when you write to the base-address of the com-port, data will be sent. if you want to write to the com1 u have the addresses 03F8 to 03FF. writing to 03F8 would look like this:

outportb(0x03F8, 65);

sends 1 byte to 3F8. this byte would mean an 'A' in ascii-code and a 01000001 in dual-code. so 8 bits are sent to this address.

perhaps u could give us further information about the port you want to write to and the hardware you connect. it could be helpful.

Author Comment

ID: 8177401
The answer may work under win98 but it seem not to be working under winXP
LVL 30

Expert Comment

by:Mayank S
ID: 8178904
Try outport () or outportb ().


Expert Comment

ID: 8179287
"outportb(0x03F8, 65);" (mirko_germany = me)
"Try outport () or outportb ()." (mayankeagle)

very senceful to write the same again, what other already wrote. however: "The answer may work under win98 but it seem not to be working under winXP" did you refer to my answer by that? it'll work with all windows-systems. at borland c++ 5.01 for win (that's what i use) u have to set the target-expert: target-model->dos(standard). i used the outportb-command at win98, win2k and winxp. it worked on every system.
LVL 30

Expert Comment

by:Mayank S
ID: 8179334
>> very senceful to write the same again, what other already wrote.

You did not mention about outport (), Mirko. Anyways, I did not go through your comment entirely before mentioning that. Please take it from here if you wish to.


Expert Comment

ID: 8179422
but i wouldn't recommend outport, but outportb. the difference is, that with outport you can only send 2bytes in one command. with outportb you can also send 1byte in a command, what i still think is to much, because it would make more sense to send each bit alone to the port, but i'm afraid, i think it's impossible.

@rui_cadete: if u want to program the serial port, i could even send u a .dll-file which makes programming easier, because u don't have to program each register, but u get commands like sendbyte, readbyte etc., so that it's easier to programm the hardware and more compatibility to other port-chips. i had the problem, when i didn't use this file, that the program ran on my computer perfectly, but on my teacher's computer it didn't run, because i had programmed it (by outportb) for the ns16450 (which i obviously have, but he not). but then he gave me this dll and i used it and now it works at both pcs. so if you are interested in this file, just leave your e-mail-address and i'll send it to u. but it only includes commands for com-port, time-routines and sound-commands.


Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

Tech giants such as Amazon and Google have sold Alexa and Echo to such an extent that they have become household names. And soon they are expected to be used by commoners in their homes, ordering takeout, picking out a song, answering trivia questio…
In today’s time where quality is an essential factor all over the world, software testing and effective QA (Quality Assurance) is an all-important element for any business to ensure less risk for an end product. A good software testing company deliv…
In a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…
Is your OST file inaccessible, Need to transfer OST file from one computer to another? Want to convert OST file to PST? If the answer to any of the above question is yes, then look no further. With the help of Stellar OST to PST Converter, you can e…

564 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