LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
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 08-24-2004, 05:54 AM   #1
Nerox
Member
 
Registered: Jul 2004
Location: Spain
Posts: 111

Rep: Reputation: 20
Inputting the parallel port


Hello there, i've already known how to write the parallel port using outb function and take advantage of this with NPN transistors. Now i'm trying to know how can i input the parallel port, i know i must use inb function to read, but i do not know how to input at hardware level.
i think i must supply a voltage between 2.4 and 5v to pins i want to be 1 and connect the rest to ground to be 0. If so, Can i connect a 4.5v battery directly?

Thank you.
 
Old 08-24-2004, 06:36 AM   #2
rjlee
Senior Member
 
Registered: Jul 2004
Distribution: Ubuntu 7.04
Posts: 1,994

Rep: Reputation: 76
First of all, I wouldn't connect a 4.5V battery; I'd at least use a TTL buffer. There is a 4.7kOhm pull-up resistance, and they can sink up to 20 milliamps, so in theory you'd be okay. But that only applies to a standard Intel port, and I'm not convinced you won't get smoke anyway.

There's a fair bit of information on the parallel port at http://www.ibiblio.org/pub/Linux/doc...ing.html#ss6.1

You can inb from the port after the data port (if you have outb(x), use inb(x+1)) to get the contents of the status registers. If you set bit 3 of the next port (outb(x+2,inb(x+1)|4)), you can then read from the data lines. You probably shouldn't get your device to write to the data lines until you've set this flag.

Also, you might want to look at /dev/port; it provides a much nicer programming interface than inb and outb.

Hope that helps,

— Robert J. Lee
 
Old 08-24-2004, 08:02 AM   #3
Nerox
Member
 
Registered: Jul 2004
Location: Spain
Posts: 111

Original Poster
Rep: Reputation: 20
What are the differences between using outb/inb and /dev/port?

Thank you very much.
 
Old 08-24-2004, 12:12 PM   #4
rjlee
Senior Member
 
Registered: Jul 2004
Distribution: Ubuntu 7.04
Posts: 1,994

Rep: Reputation: 76
/dev/port can be opened like a regular file. You then seek to the I/O address you want, and then perform a read or write operation using read() to input a byte and write() to output one. You can even use fread()/fwrite() for buffered I/O if your hardware can keep up.

One major advantage is that you just set the permissions on /dev/port itself to be accessible to your user, and you don't have to have raw access to the hardware, so you don't have run the program as root (which can cause all kinds of security nightmares).

Code:
#include<stdio.h>
#include <errno.h>

int main() {
 //1-Byte buffer:
 char buffer[1];

 //Open the file and create file discriptor:
 int fd = open("/dev/port", O_NONBLOCK|O_SYNC);
 //Check for errors:
 if (fd == 0) {
  fprintf(stderr,"Error opening /dev/port: %s\n", strerror(errno));
  return(-1);
 }

 //Choose parallel port output (assuming 0x378):
 lseek(fd,0x378,SEEK_SET);

 //Write a byte to the port:
 buffer[0] = 'A';
 write(fd,buffer,1); //Returns 0 or -1 for error

 //Get a byte from the port:
 lseek(fd,0x379,SEEK_SET);
 read(fd,buffer,1); //Returns 0 or -1 for error (0==EOF, which can't happen here!)

 close(fd);
}
See the manual pages in section 2 for read/write/lseek/open/close. In particular, there are lots of options to open:
Code:
man 2 open
 
Old 08-24-2004, 03:20 PM   #5
Nerox
Member
 
Registered: Jul 2004
Location: Spain
Posts: 111

Original Poster
Rep: Reputation: 20
Can I connect two PC's with a parallel port cable directly?
Isn't there any risk?

Thank you.
 
Old 08-24-2004, 03:28 PM   #6
rjlee
Senior Member
 
Registered: Jul 2004
Distribution: Ubuntu 7.04
Posts: 1,994

Rep: Reputation: 76
You certainly can connect two computers with a parallel cable. You might also like to look into the plip module if you want to get two PCs communicating over the parallel port. (Search for “plip” on http://www.tldp.org).

Also, I should have mentioned before: not all the parallel port pins are bidirectional. It's designed so that you can connect the output pins from two ports together without causing any serious harm (you just lose the signaling capabilities) but some pins are hard-wired to inputs and some to outputs. Only the 8 data lines can be selected as I/O, and even then only all at once (all inputs, or all outputs).
 
Old 08-25-2004, 04:00 AM   #7
Nerox
Member
 
Registered: Jul 2004
Location: Spain
Posts: 111

Original Poster
Rep: Reputation: 20
but, must i make my own cable? A parallel port cable used for enlarged won't work.

Status port is read-only, control port is write-only, and data port is for I/O, but only in extended read mode, how must i enable the extended read mode to get also input from data port?

TIA
 
Old 08-25-2004, 01:04 PM   #8
rjlee
Senior Member
 
Registered: Jul 2004
Distribution: Ubuntu 7.04
Posts: 1,994

Rep: Reputation: 76
You can use any parallel cable, it's just a question of how the pins are wired together, which you can find out with a multimeter. Normally, either all pins are connected 1–1 (meaning you can only usefully communicate half-duplex with the data pins), or only the first four data pins are connected to the four status pins.

To get into extended read mode, you need to take the port out of SPP mode (usually a setting in the BIOS), and then set bit 3 of the control register to a 1 to enable inputs.
 
Old 09-03-2004, 08:01 AM   #9
urrg
LQ Newbie
 
Registered: Sep 2004
Posts: 2

Rep: Reputation: 0
Hello! I am trying to use "/dev/port" to send/receive bytes. But I failed with errno 'operation not permitted' reading from desired port. Could you tell me how to set proper permitions on "/dev/port"? I use Fedora 2 and 2.6.7 kernel.
 
Old 09-03-2004, 10:24 AM   #10
rjlee
Senior Member
 
Registered: Jul 2004
Distribution: Ubuntu 7.04
Posts: 1,994

Rep: Reputation: 76
Look at the chown and chmod commands.
The easyest way is probably to change the ownership of /dev/port to that of your user:
Code:
su -c 'chown username /dev/port'
 
Old 09-06-2004, 07:03 AM   #11
urrg
LQ Newbie
 
Registered: Sep 2004
Posts: 2

Rep: Reputation: 0
I did so. I tried different groups (mem, kmem, ...). It wouldn't work.
 
Old 09-06-2004, 07:51 AM   #12
rjlee
Senior Member
 
Registered: Jul 2004
Distribution: Ubuntu 7.04
Posts: 1,994

Rep: Reputation: 76
I don't think this thread is really the place to be discussing file access permissions as it's not a hardware issue (even though the file happens to be a device-special file).
Rather than me giving you a few unexplained one-liners to try and fix it, I suggest that you read http://www.tldp.org/HOWTO/Security-H...-security.html and http://www.tldp.org/HOWTO/User-Authe...HOWTO/x71.html to get a better feel for how Linux file security works.

Hope that helps,

—Robert J. Lee
 
  


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 02:50 AM.

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