Thread: Parallel Port IO ops not working properly

  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
    Registered User
    Join Date
    Mar 2009
    Posts
    8
    its been baffling me as well. Because i want to be able to connect this board that i have written the main code for. For home automation and random projects.
    But it seems otherwise to not work. I have the bios on normal from memory, one of the first things that i checked. I am using a Via Epia Mini-Itx but that shouldnt make any diffrence as it should all be standard. Unless there is som driver for the board that i dont have. I shall have a look around again.
    unless someone else has an idea.

    Edit: I just tried the same program on my laptop (old one with parallel) and i got the same thing. first 5 high and last 3 low. I then tried the next few pins (10-16), and they where changing. I noticed this before with the mini-itx, but forgot about it. So am I writting to the wrong base address then? I am using 0x378 but that appears to be writting (activating) base+1.
    Last edited by microtechno; 03-16-2009 at 05:07 AM.

  8. #8
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Ok, looking at this: http://www.piclist.com/techref/io/parallel/signals.htm and this: http://www.doc.ic.ac.uk/~ih/doc/par/
    it seems like pin1 is not ground - it is the strobe output (which may well be LOW if you haven't done anything to it, but still not technically ground). So you may want to use pins 18-25 instead - I'm not actually expecting this to CHANGE anything - but to be perfectly correct, we should do that.

    Obviously, some driver may CHANGE the way the parallel port setup is, but you shouldn't need a driver to output things on the pins itself.

    I would verify the parallel port settings too: The fact that some pins are permanently low and others are permanently high makes it sound like it's in IEEE 1284 mode.

    Back to stupid questions: I take it that the parallel port is built into the motherboard (or connected directly with a flat-cable) and not a USB-connector or some such?

    --
    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.

  9. #9
    Registered User
    Join Date
    Mar 2009
    Posts
    8
    yes it is connected to the motherboard.
    With the Led's I am using the propper grounds, 18-25 (one of them). I checked in the bios and its on normal mode, so I have no idea at this stage.

  10. #10
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    I have no idea either.

    Would you be able to boot DOS on the machine, and use debug to get data out on port 378 - just to check things?

    --
    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
    Mar 2009
    Posts
    8
    i could try booting dos, would take a while finding a floppy and all that.. but i can give it a go.

  12. #12
    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);
    //}

  13. #13
    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

  14. #14
    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

  15. #15
    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.

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