Results 1 to 6 of 6
Hi.
I've found this code on the web, compiled it and hooked it up with some parallel cable and a LED. Can someone please explain to me what it means?
...
- 08-16-2009 #1
Need help understanding this code
Hi.
I've found this code on the web, compiled it and hooked it up with some parallel cable and a LED. Can someone please explain to me what it means?
Code:#include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <sys/io.h> #include <sys/types.h> #include <fcntl.h> #define BASEPORT 0x3bc /* lp0 */ int main() { char c; int n, tem; printf("Hit enter to stop\n"); if (ioperm(BASEPORT, 3, 1)) {perror("ioperm"); exit(1);} tem = fcntl(0, F_GETFL, 0); fcntl (0, F_SETFL, (tem | O_NDELAY)); while (1) { n = read(0, &c, 1); if (n > 0) break; outb(16, BASEPORT); usleep(250000); outb(0, BASEPORT); usleep(250000); } fcntl(0, F_SETFL, tem); outb(0, BASEPORT); if (ioperm(BASEPORT, 3, 0)) {perror("ioperm"); exit(1);} exit(0); }
I fasten the negative leg on the led to pin 20 (=ground) and the positive leg to parallel pin no 6. when I run the program as root, the LED starts to flash. (Cool...)
I'm currently using Ubuntu 9.04, and the latest build-essential. If someone took the time to comment nearly each line, I'd be very happy!
J1s
- 08-16-2009 #2
Hi!
I have no idea what that code does because I suck at programming. I just thought it might be a good idea to point out something here....I don't think it's a very good idea to run random code on your machine, especially if you don't know what the code is for or capable of doing. That code looks pretty benign to me but then again, what do I know? It could be bad next time, you might run some code that really messes things up without you even knowing. Be careful out there.I do not respond to private messages asking for Linux help, Please keep it on the forums only.
All new users please read this.** Forum FAQS. ** Adopt an unanswered post.
- 08-16-2009 #3
Hi.
I totally agree with you, but this time, I'm having some sort of clue. I found this code, while searching the net for parallel port programming. This program sends a signal to parallel port 2-9. It sends a signal, which means about +3V, then it turns off and send +0V. It does this in a loop, until the user, as in me, press enter.
I test the code on a different machine, from what I'm programming on. You know, when you're having too much hardware floating around. I made an usb stick bootable and used the included tool to install Ubuntu 9.04. So if anything goes wrong, I can just pull the plug. When I reinsert the powercable, it boots up and is just like a fresh install.
J1s
- 08-16-2009 #4Just Joined!
- Join Date
- Jul 2009
- Posts
- 58
From the look of it, it just either sends the same two bits over and over over the some port (comment says lp0, but I have no idea -- will have to look it up). So 16,0,16,0,16,0 with a delay in between forever. Now what that does depends on what is reading the bits.
The details are:
ioperm is needed for outb to give the kernel notice that something below is going to talk out of a port that is restricted.
fcntl is the controller, and F_GETFL/F_SETFL are I believe synchronous communication.
read() is checking to see if there is a sync on the port (something is attached).
Sends out 16, sleeps, sends out 0, sleeps, and repeats.
the rest is cleanup.
- 08-17-2009 #5Linux Guru
- Join Date
- Apr 2009
- Location
- I can be found either 40 miles west of Chicago, or in a galaxy far, far away.
- Posts
- 8,974
What aram535 is pretty much it. The O_NDELAY option set with fcntl() told the OS to use non-blocking reads. I'm not sure what an out byte of 16 and subsequently 0 to /dev/lp0 do, but his surmise that it is polling the port to see if anything was attached is a good "guess". I haven't done raw parallel port programming like this in many years, so I would have to blow the dust off a lot of old programming manuals to find out.
Sometimes, real fast is almost as good as real time.
Just remember, Semper Gumbi - always be flexible!
- 08-17-2009 #6
Thank you all for your time and effort.
It's clearer to me now.
j1s


Reply With Quote