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
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions

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.


Does Your Cloud Backup Use Blockchain Technology?

Blockchain technology has already revolutionized finance thanks to Bitcoin. Now it's disrupting other areas, including the realm of data protection. Learn how blockchain is now being used to authenticate backup files and keep them safe from hackers.


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

Moving data to the cloud? Find out if you’re ready

Before moving to the cloud, it is important to carefully define your db needs, plan for the migration & understand prod. environment. This wp explains how to define what you need from a cloud provider, plan for the migration & what putting a cloud solution into practice entails.

Question has a verified solution.

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

Ever wonder what it's like to get hit by ransomware? "Tom" gives you all the dirty details first-hand – and conveys the hard lessons his company learned in the aftermath.
If you're a modern-day technology professional, you may be wondering if certifications are really necessary. They are. Here's why.
Using Adobe Premiere Pro, the viewer will learn how to set up a sequence with proper settings, importing pictures, rendering, and exporting the finished product.
XMind Plus helps organize all details/aspects of any project from large to small in an orderly and concise manner. If you are working on a complex project, use this micro tutorial to show you how to make a basic flow chart. The software is free when…

800 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