Thread: Parallel Port IO ops not working properly

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Registered User
    Join Date
    Mar 2009
    Posts
    8

    Parallel Port IO ops not working properly

    Hi
    I am trying to get a program in C to write data to the parallel port byte wise.
    I have got the program running of sorts, I can write to the port and then read the data back with the same value obtained. The problem is that it isnt writting to the port physically. I have some leds connected and they all stay the same (1111000) from mem. Doesnt matter what value I pass to the port.
    I am using port 0x378 the default value, setting it to open with ioperm(), and writting with inb and outb.
    I am not sure what I am doing wrong, if I have the wrong address. There isnt a port in /proc/ioports, there was but it was being used by parport so i removed that and the port also stoped being listed.
    This is my first program using IO ops, well my first program in Linux. So I am a newbie to how it all works.
    any help would be much appreciated. I havent attached my code because I dont think that it would help. But if it does i can attach my test program.

    microtechno

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    It would probably help a lot if you:
    1. Show us your code - maybe you are making some mistake there?
    2. Tell us how you are identifying what is on the parallel port?

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Registered User
    Join Date
    Mar 2009
    Posts
    8
    Hi,
    I have attached my code. To run the code I am compiling it, gcc parallel.c -o parallel, chown root parallel, and chmod u+s parallel which allows ioperm() to return a positive value not -1.
    To test what values I am getting on the parallel port, I have attached LED's which activate when the port goes high (5v). I also tested them with a multimeter. But the LED's are the easiest to see what value the port has. While trying to change the values with the program nothing happened to the LED's they just stayed the same, first 5 on last 3 off.
    Code:
    /* paralell port test bed */
    #include <stdio.h>
    #include <stdlib.h>
    #include <unistd.h>
    #include <sys/io.h>
    #define basePort 0x378 //port number
    
    int main()
    {
        int byte;
        printf("Parallel Port Interface\n");
        /* Allow access to the ports - Start port - Number of ports - 1=on 0=off */
        ioperm(basePort, 1, 1);
        printf("Enter value 0-255: ");
        scanf("%d" , &byte);
        while ( byte >= 0 )
        {
            outb(byte, basePort);
            printf("value: %d\n", inb(basePort));
    
            printf("Enter value 0-255:(>0 exit) ");
            scanf("%d", &byte);
        }
        return 0;
    }
    Last edited by microtechno; 03-16-2009 at 03:38 AM.

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    That does look correct.

    Stupid question: Are you sure you are measuring the right pins on the parallel port?

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  5. #5
    Registered User
    Join Date
    Mar 2009
    Posts
    8
    I am mesuring pins 1-9, although the data pins that i require are 2-9. The leds are grounded to one of the many grounds.
    Does it have anything to do with the mode that the parallel port operates in? or is there something in the kernel that is need. I am running gentoo, and i took the parallel port module out as it was parport. That said I have enabled the parallel port just it doesnt have a module.

  6. #6
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    To be honest, I'm not quite sure what could be wrong. From memory, pins 2-9 are the right ones.

    What is your BIOS settings for the parallel port - you probably need to set it to "basic", "standard" or whatever the mode may be called.

    I have done basic IO from user-mode in Linux, but the kernel I used at the time was something like 2.2 (or maybe even 1.x). Last time I used parallel port in a similar way to what you are doing was to measure something in a Windows graphics driver (set pin X on parallel port in one place, then unset it in another place, and use a timer/oscilloscope/logic analyzer to see what is going on - I used high-tech paper-clips as "pins" to get the signal out of the female connector).

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  7. #7
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Perhaps a stupid question... But is the parallel port HIGH voltage actually greater than the LED conductance cutoff? And even if it is, is the port able to source enough current to maintain the voltage at that level?

    It would not have occurred to me to connect an LED directly to a parallel port pin... I would have used the pin to gate a transistor which drives the LED...
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  8. #8
    Registered User
    Join Date
    Mar 2009
    Posts
    8
    Hi
    yes the HIGH of the parallel port should be between 4.7 and 5v, and an LED operates at around 2-3 volts depending on the type and age. So i have a resistor attached in series to decrese the voltage. This should also decrease the current draw on the parallel port.
    Parallel port output
    This is what i used as a referance.
    By the way I have managed to get some of the leds to flash, not the right ones though at the right times. Yes they are all connected to the correct pins.

    edit: just checked the voltage accross the resistor is 0.26 volts. Current draw from that is around 0.5mA well below the max of 2.5mA for the port. Also I have used a multimeter on the port when its high, around 5v without the leds. Incase the leds where causing a problem, but the same ports where still high.
    Last edited by microtechno; 03-27-2009 at 12:55 AM. Reason: voltmeter

  9. #9
    Registered User
    Join Date
    Mar 2009
    Posts
    8
    Hi
    This makes me feel really stupid. It was working all along. The cable that i was using attached to the parallel port was switching all the pins around and doing strange things. I thought of it this evening, tested the pins and almost killed my self.. Should have checked it ages ago. I had only checked one the the cables that i was using not the extender cable.
    So really annoyed at that.
    Thanks though guys for all the help. Now i have some fancy dancing leds and am happy.
    Now onto the actuall project.
    Microt

  10. #10
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Well, as Sherlock Holmes would say "When you have investigated all possible causes, you have to look at the impossible ones".

    Congratulations to getting it working.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  11. #11
    Registered User
    Join Date
    Jun 2009
    Posts
    9

    prob running the exe file

    Hello, THis is Tariq and i am new here. i m working on interfacing parallel port through ' C ' (OS = Win XP) . I have already successfully lighted Leds within TurboC v3.0 enviroment, using "User Port" for access to parallel port. But when i tried to run the exe file only messages appeared as programmed, but the leds doesn't blink.. i have tried a lot, and also have started userport before executing the exe file of the program.. what should i do? help me.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. input to parallel port prob
    By tariq7868 in forum Tech Board
    Replies: 4
    Last Post: 06-11-2009, 12:35 PM
  2. Parallel Port to USB controller and outb()
    By coletek in forum Linux Programming
    Replies: 1
    Last Post: 06-05-2009, 06:57 AM
  3. Question on Accessing parallel port
    By Luciferek in forum C++ Programming
    Replies: 6
    Last Post: 06-16-2008, 05:36 PM
  4. How to read/write to parallel port?
    By Pooyae in forum C Programming
    Replies: 9
    Last Post: 03-10-2005, 09:21 PM
  5. programming the parallel port to control device
    By griffmaster2005 in forum C Programming
    Replies: 3
    Last Post: 02-14-2005, 07:50 AM