ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Just a quick question, how would i get around to outputting data through the parallel port? This is for porting an app i have made in windows. I just need to know the base function, ex: outportb(addr as short, datum as short) equivalent. Thanks.
At first you must get parallel port access permissions, so you must use ioperm(), take a look at man 2 ioperm for further information. After that, use outb() and inb() in order to write/read from/to the parallel port.
Usage of outb:
outb(port, value);
/* for parallel port */
out(0x378, value);
Value (1 byte) is written to the data port (pins between 2 and 9).
You may also want to use sleep() and usleep() to delay.
Thanks man, just need to know one more thing, what headers have those functions?
You've helped alot, thanks alot man.
BTW, do you know how to read the lpt addresses from the bios? I remember i got that working once but it was a long time ago, sorta better to have them like that then hardcoded. Thanks alot.
you'll want to use "#include <sys/io.h>" as you'll also probably need access to ioperm() to gain access to the hardware. no idea if it is of interest to you, but certain forms of lpt action might benefit from an external library i used to use called parapin. auomates a lot of stuff for pin switching etc..
Nah, i've done all the coding myself, all the functions and shit, all i need is to be able to output to an address a certain data, and be able to port the gui to X11, but i'll figure that out on my own, it's probably just getting used to the equivalent of W32 API. Gonna be my first time coding in linux :P.
What you need is to get parallel port access permissions. Look at the following code:
Code:
#include <asm/io.h>
#include <stdio.h>
#include <unistd.h>
int main(void)
{
int value, err;
/* Get parallel port access permissions */
err = ioperm(0x378,1,1);
if(err){
perror("ioperm");
exit(-1);
}
/* Enter the value to be sent by means of stdin */
printf("Type a value and hit Enter: ");
value = getchar();
outb(0x378,value);
return 0;
}
You must run the code above as root in order to get parallel port access permissions, otherwise ioperm() will fail. You can do away with root permissions once you've called ioperm() by calling seteuid().
EDIT: I'm having problems with your code, it compiles perfectly, but nothing happens, i have a little circuit i made myself where it uses LED's to show the status of the parallel port, but they don't change when i run it. When i tried to change the W32 system to Linux in my code, it gets an error along the way, i'll have to look more into that. Do you have any idea of what could be wrong with your code? Or maybe it's my machine... ><. Thanks.
I get no error, no visible error, it runs smoothly, but i get no parallel port output, i have 8 LED's connected to the data lines and GND of my LPT1 (0x378), but when i run your code and type in any number, it doesn't change, the same 3 are lit, and the rest are dark. I don't know what could be the problem. I think it has something to do with permissions and shit, but i'm not sure.
I think it is due to the behaviour of getchar(), since it's providing an ASCII value.
Try this code instead:
Code:
#include <asm/io.h>
#include <stdio.h>
#include <unistd.h>
int main(void)
{
unsigned char value;
int err;
/* Get parallel port access permissions */
err = ioperm(0x378,1,1);
if(err){
perror("ioperm");
exit(-1);
}
/* Enter the value to be sent by means of stdin */
printf("Type a value and hit Enter: ");
scanf("%u",&value);
outb(0x378,value);
return 0;
}
All you have to do is run it as root in order to get permissions. Enter 255 so as to light all LED's.
Still not working, i think it's a permission problem or something like that. To get this working in windows i had to use a 3rd party driver, must be some way to get around system security or something like that...
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.