Results 1 to 6 of 6
Hello,
I'm currently doing some parallel port programming. I would usually ask the user to enter the base address of the parallel port. Is there a way to write some ...
- 04-13-2007 #1Just Joined!
- Join Date
- Dec 2003
- Posts
- 41
Finding address of parallel port
Hello,
I'm currently doing some parallel port programming. I would usually ask the user to enter the base address of the parallel port. Is there a way to write some C code that can find this out, instead of having to ask the user?
thanks.
- 04-13-2007 #2Linux Enthusiast
- Join Date
- Aug 2006
- Posts
- 631
Read the file /proc/ioports.
Regards
- 04-13-2007 #3Just Joined!
- Join Date
- Dec 2003
- Posts
- 41
I actaully know that its in /proc/ioports. But my programming skills arent that developed that I can search within files. Mayb you can show an example?
Also, there should be another method where I can ask the kernel instead of searching through the file, but I definitely have to idea how to do this one.
- 04-13-2007 #4Linux Enthusiast
- Join Date
- Aug 2006
- Posts
- 631
Ok, here's an example to obtain the address of the parallel port (I suppose you're doing that in C):
If you're programming a device driver you can pass the grep command as a parameter to your driver.Code:#include <stdio.h> int main (int argc, char *argv[]) { FILE *pipein; char readbuf[11]; if ((pipein = popen("grep parport /proc/ioports|cut -d' ' -f1", "r")) == NULL) { perror("popen"); exit(1); } fgets(readbuf, 11, pipein); close(pipein); printf("%s", readbuf); return(0); }
Regards
- 04-14-2007 #5Just Joined!
- Join Date
- Dec 2003
- Posts
- 41
Hi Franklin52, thanks for showing how to do it. However, I want to take it a step further, by giving the user the option to enter the port address or search for it.
But I still have some questions about implementing it. I've asked the questions in the comments.
I'm by no means a good programmer. My programs usually work and get the job done. However, it may not to it in the simplest way. Its a good thing I dont design circuits the way I write programsCode://Program to send a nibble of data to the lower four bits of the data port (Base address) //Remember use the -O (the letter O) optimization flag to compile, becuase outb() and inb() //are inline marcos. #include <stdio.h> #include <sys/io.h> #define all_on 15 //I wonder why not just int main() ? Seems to compile fine with main() :) int main(int argc, char *argv[]) { FILE *pipein; //defines stream char readbuf[10]; //buffer to store string int user_num=0; int baseaddr; char user_ans; printf("Would you like me to find the address range your parallel port [y/n]?: "); printf("\n"); scanf("%c", &user_ans); if (user_ans =='y') { //Does a return of NULL mean failure? This never gets executed even when I //put a fake file name. if((pipein = popen("grep parport /proc/ioports|cut -d' ' -f1", "r")) == NULL) { close(pipein); printf("Cant find it. You need to enter it manually\n"); printf("Enter the base address of your parallel port: \n"); scanf("%x", &baseaddr); } //This always gets executed, regardless if grep finds the file or not. When it //doesnt find the file, it outputs garbage for the string else { fgets(readbuf, 10, pipein); close(pipein); //Could we also close the stream with pclose(pipein)? printf("The address range seems to be %s\n", readbuf); printf("Proceed to enter the base address\n"); scanf("%x", &baseaddr); } } //User enters 'n'. else { printf("Enter the base address of your parallel port: \n"); scanf("%x", &baseaddr); } //Permission to access the data port of the parallel port ioperm(baseaddr,1,1); //-1 is escape charater while(user_num!=-1) { printf("\nEnter a hex digit in the range of 0 to F:\n "); scanf("%x",&user_num); if((user_num >=0x0) && (user_num <=0xF)) { outb(user_num,baseaddr); } else printf("Not in range!\n"); } //Just some after effects after the loop exits.Turns all LEDs OFF, ON, then OFF outb(0,baseaddr); //OFF sleep(2); // 2 second delay outb(all_on,baseaddr); //ON sleep(1); outb(0,baseaddr); //OFF //Gives up access ioperm(baseaddr,1,0); return 0; }
- 04-14-2007 #6Linux Enthusiast
- Join Date
- Aug 2006
- Posts
- 631
It's in this case not necessary, you don't use the arguments.//I wonder why not just int main() ? Seems to compile fine with main()
This executes only when the popen function fails.//Does a return of NULL mean failure? This never gets executed even when I
//put a fake file name.
The example was just to show how you can obtain the address of the parallel port but with the right filename.//This always gets executed, regardless if grep finds the file or not. When it
//doesnt find the file, it outputs garbage for the string
There's no problem if you use the right filename in your code and it's not necessary to ask for a manually input from the user.//This always gets executed, regardless if grep finds the file or not. When it
//doesnt find the file, it outputs garbage for the string
An alternative is to pass the address to your prog:
RegardsCode:/your_dir/your_progname `grep parport /proc/ioports|cut -d' ' -f1`


Reply With Quote