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.
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?
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.
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.
/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:
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).
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?
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.
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.
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.