LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   parallel port output (https://www.linuxquestions.org/questions/programming-9/parallel-port-output-276915/)

bobbens 01-12-2005 10:32 AM

parallel port output
 
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.

Nerox 01-14-2005 11:45 AM

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.

I hope it helps

bobbens 01-15-2005 04:33 AM

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.

acid_kewpie 01-15-2005 05:50 AM

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..

bobbens 01-15-2005 06:49 AM

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.

Nerox 01-15-2005 08:04 AM

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;
}


Nerox 01-15-2005 08:13 AM

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().

bobbens 01-15-2005 08:35 AM

Thanks alot, ill get to work right away :P.

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.

Nerox 01-15-2005 01:09 PM

What do you get exactly while running my code? Do you get an error or so?

bobbens 01-15-2005 01:40 PM

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.

Nerox 01-15-2005 01:59 PM

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.

bobbens 01-15-2005 03:55 PM

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...

sasho 01-16-2005 02:42 PM

keep on trying, I am interested in seeing a working solution since I have (not immediate ) plans to port a similar app to Linux myself.

ThePhantom1492 02-08-2005 07:40 PM

Hi
I noticed a small error here.
My guess is that your program is segfaulting

outb(value, address);

so outb(0xFF, 0x378);

Nerox 02-09-2005 08:10 AM

Yeah, the call is defined as outb(value, port), that's why it didn't work!!


All times are GMT -5. The time now is 06:41 AM.