LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 01-12-2005, 10:32 AM   #1
bobbens
Member
 
Registered: Sep 2004
Location: Barcelona
Distribution: Debian, FreeBSD, Gentoo
Posts: 586

Rep: Reputation: 30
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.
 
Old 01-14-2005, 11:45 AM   #2
Nerox
Member
 
Registered: Jul 2004
Location: Spain
Posts: 111

Rep: Reputation: 20
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
 
Old 01-15-2005, 04:33 AM   #3
bobbens
Member
 
Registered: Sep 2004
Location: Barcelona
Distribution: Debian, FreeBSD, Gentoo
Posts: 586

Original Poster
Rep: Reputation: 30
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.
 
Old 01-15-2005, 05:50 AM   #4
acid_kewpie
Moderator
 
Registered: Jun 2001
Location: UK
Distribution: Gentoo, RHEL, Fedora, Centos
Posts: 43,417

Rep: Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985
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..
 
Old 01-15-2005, 06:49 AM   #5
bobbens
Member
 
Registered: Sep 2004
Location: Barcelona
Distribution: Debian, FreeBSD, Gentoo
Posts: 586

Original Poster
Rep: Reputation: 30
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.
 
Old 01-15-2005, 08:04 AM   #6
Nerox
Member
 
Registered: Jul 2004
Location: Spain
Posts: 111

Rep: Reputation: 20
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;
}

Last edited by Nerox; 01-15-2005 at 08:10 AM.
 
Old 01-15-2005, 08:13 AM   #7
Nerox
Member
 
Registered: Jul 2004
Location: Spain
Posts: 111

Rep: Reputation: 20
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().

Last edited by Nerox; 01-15-2005 at 08:15 AM.
 
Old 01-15-2005, 08:35 AM   #8
bobbens
Member
 
Registered: Sep 2004
Location: Barcelona
Distribution: Debian, FreeBSD, Gentoo
Posts: 586

Original Poster
Rep: Reputation: 30
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.

Last edited by bobbens; 01-15-2005 at 11:47 AM.
 
Old 01-15-2005, 01:09 PM   #9
Nerox
Member
 
Registered: Jul 2004
Location: Spain
Posts: 111

Rep: Reputation: 20
What do you get exactly while running my code? Do you get an error or so?
 
Old 01-15-2005, 01:40 PM   #10
bobbens
Member
 
Registered: Sep 2004
Location: Barcelona
Distribution: Debian, FreeBSD, Gentoo
Posts: 586

Original Poster
Rep: Reputation: 30
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.
 
Old 01-15-2005, 01:59 PM   #11
Nerox
Member
 
Registered: Jul 2004
Location: Spain
Posts: 111

Rep: Reputation: 20
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.
 
Old 01-15-2005, 03:55 PM   #12
bobbens
Member
 
Registered: Sep 2004
Location: Barcelona
Distribution: Debian, FreeBSD, Gentoo
Posts: 586

Original Poster
Rep: Reputation: 30
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...
 
Old 01-16-2005, 02:42 PM   #13
sasho
Member
 
Registered: Jan 2005
Distribution: Arch
Posts: 120

Rep: Reputation: 17
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.
 
Old 02-08-2005, 07:40 PM   #14
ThePhantom1492
LQ Newbie
 
Registered: Feb 2005
Posts: 1

Rep: Reputation: 0
Hi
I noticed a small error here.
My guess is that your program is segfaulting

outb(value, address);

so outb(0xFF, 0x378);
 
Old 02-09-2005, 08:10 AM   #15
Nerox
Member
 
Registered: Jul 2004
Location: Spain
Posts: 111

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


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Where is the parallel port? kwyjibo99 Linux - Hardware 5 07-20-2005 05:47 PM
What's using my parallel port?! DiBosco Linux - Hardware 10 06-20-2004 10:18 AM
Cannot use parallel port under 2.6.4 jimbob1234 Slackware 2 03-14-2004 08:34 PM
Parallel Port CD-RW dbrook42 Linux - Hardware 3 09-30-2003 07:54 PM
Parallel Port Crashed_Again Linux - Hardware 1 01-20-2003 08:52 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 10:49 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration