Help needed: Output to Parallel port.

This is a discussion on Help needed: Output to Parallel port. within the C Programming forums, part of the General Programming Boards category; I need to output an incrementing number to a parallel port. I'm currently using this code: #include <stdio.h> #include <io.h> ...

  1. #1
    Registered User
    Join Date
    Oct 2001
    Posts
    2

    Help needed: Output to Parallel port.

    I need to output an incrementing number to a parallel port. I'm currently using this code:

    #include <stdio.h>
    #include <io.h>

    int main (int argc, char **argv)
    {
    unsigned char num;

    FILE *par;

    par = fopen("0x378", "w");



    for (num=0; num!=255; num++)

    {

    fflush(par);

    fwrite(&num, sizeof(num), 1, par);

    printf("Outputting: %d\n", num);



    }



    fclose(par);

    return 0;

    }

    Where the parallel port has the adress 378h.

    But all that does is to create a file called 0x378. How do I get the program to open the parallel port of that address? I'm using MS Visual C++ compiler, but I need a C program.

    Cheers for any help.

  2. #2
    Registered User
    Join Date
    Sep 2001
    Posts
    412
    You don't open it like a file, you treat it like an address in memory -- assign a pointer to it, and use it like that.

    Here's a file I did, based on some code fragments I found. This enumerates the DOS LPT ports, (somewhat) detects the mode the port is in, and then gives you addresses of the data, status, and control ports. You can use outportb to write, and inportb to read. This is C++, as it uses streams, but it could very easily be converted to C just by changing the I/O. I hope this sample program helps.

    Code:
    unsigned long DATA = 0;
    unsigned long CONTROL = 0;
    unsigned long STATUS = 0;
    
    
    void initPort(void);
    
    
    void main(int, char**)
    {
    	clrscr();
    	unsigned long addrLPT[3];
    	addrLPT[0] = *(unsigned long far *)0x00000408L; //get LPT1 address
    	addrLPT[1]= *(unsigned long far *)0x0000040AL;  //get LPT2 address
    	addrLPT[2] = *(unsigned long far *)0x0000040CL; //get LPT3 address
    	unsigned int index, isPort = 0;
    	unsigned char usePort = 255;
    	cout << "Ports available for use:" << endl;
    	for (index = 0; index < 3; index++)
    	{
    		if (addrLPT[index] != 0){
    			cout << (index + 1) << ": LPT" << (index + 1) << endl;
    			isPort = 1;
    		}
    	}
    	if (isPort == 0)
    	{
    		cout << "No parallel ports found!  Program terminating" << endl;
    		exit(1);
    	}
    	isPort = 0;
    
    	while(isPort == 0){
    		cout << "Please type the number (1-3) of the valid port you wish to test and press Enter: ";
    		cin >> usePort;
    		cout << endl;
    		if (usePort <= '3' && usePort >= '1')
    		{
    			if (addrLPT[(unsigned int)(usePort - '1')] != 0){
    				cout << "Port LPT" << usePort << " has been selected" << endl;
    				isPort = 1;
    			}
    			else cout << "That port is not available, please try a valid port." << endl;
    		}
    		else cout << "Bad input '" << usePort <<"', enter an integer between 1 and 3" << endl;
    	}
    	DATA = addrLPT[(unsigned int)(usePort - '1')];
    	cout << "Data port is at: " << hex << DATA << "h" << endl;
    	STATUS = DATA + 1;
    	CONTROL = DATA + 2;
    	cout << "Status port is at: " << hex << STATUS << "h" << endl;
    	cout << "Control port is at: " << hex << CONTROL << "h" << endl;
    	initPort();
    
    }
    
    
    
    void initPort(void){
    	//try to detect ECP port
    	unsigned char ECR = inportb(DATA + 0x402);
    	if ((ECR & 0x03) == 1){
    		outportb(DATA + 0x402,0x34);
    		if(inportb(DATA + 0x402) == 0x35){
    			cout << "ECP port detected" << endl;
    			cout << "ECP register at: " << hex << (DATA + 0x402) << "h" << endl;
    		}
    		else cout << "SPP/EPP port detected" <<endl;
    		outportb(DATA + 0x402, ECR); // restore original value
    	}
    	else cout << "SPP/EPP port detected" << endl;
    }
    Last edited by The V.; 10-09-2001 at 12:46 PM.

  3. #3
    Registered User
    Join Date
    Sep 2001
    Posts
    412
    The basic idea of this program (which doesn't do anything beyond detection of PP settings, but I've used modifications of it for other purposes) is to do the following:

    * First, it reads the addresses of the LPT1 through LPT3 parallel ports, as assigned in DOS. You cannot assume that LPT1 is necessarily 0x378 -- the address of LPT1, though, will *always* be stored in hex location 0x408. Similarly, the addresses of the LPT2 and LPT3 data ports are as used int he program. If the value at this address is zero, then there is no port assigned to that designation.

    * It prompts the user to select a parallel port to use. This step could be bypassed if you want to only use LPT1, but my program was designed to work on either LPT1 or LPT2 (and I included LPT3 support as it took practically zero extra coding).

    * It gets the addresses of the data, status, and control ports.

    * It calls initPort, which tries to determine if the port is in ECP or SPP/EPP mode. The goal of this was in the real program, I used this info to put the ECP port in an SPP emulation mode, so that I could treat the port like an SPP port always, and have it work.

    Once everything is initialized, you can use outportb and inportb like this:
    unsigned char dataValue;

    outportb(DATA,0x05); // sends the value 0x05 to the data port
    datavalue = inportb(DATA); // read the data port's value
    Last edited by The V.; 10-09-2001 at 02:20 PM.

  4. #4
    Registered User
    Join Date
    Oct 2001
    Posts
    2
    Thanks very much for you help.
    Much appriciated!

  5. #5
    Registered User
    Join Date
    Sep 2001
    Posts
    412
    Also note, this only works in true DOS, not in a windows console window. It takes some more doing to get this code to be acceptable to windows (which doesn't like you reading specific memory addresses in this fashion)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Parallel Port IO ops not working properly
    By microtechno in forum Linux Programming
    Replies: 16
    Last Post: 06-08-2009, 01:33 PM
  2. Parallel Port to USB controller and outb()
    By coletek in forum Linux Programming
    Replies: 1
    Last Post: 06-05-2009, 07:57 AM
  3. Question on Accessing parallel port
    By Luciferek in forum C++ Programming
    Replies: 6
    Last Post: 06-16-2008, 06:36 PM
  4. Parallel port programming
    By h3ro in forum Windows Programming
    Replies: 6
    Last Post: 08-08-2007, 12:14 PM
  5. Segmentation Fault - Trying to access parallel port
    By tvsinesperanto in forum C Programming
    Replies: 3
    Last Post: 05-24-2006, 04:28 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21