Thread: Simple Parallel Port Control

  1. #1
    Registered User BigSter's Avatar
    Join Date
    Nov 2001
    Posts
    47

    Cool Simple Parallel Port Control

    Hi, I am trying to use C++ and my Parallel port to control outside objects. I was wondering how to make a simple program that sets the certain data pins at either 0 or 1. I am using Windows 95, and trying to make a DOS program with DEV-C++.
    Thanks

  2. #2
    Registered User
    Join Date
    Sep 2001
    Posts
    412
    As long as you're compiling 16-bit DOS, this should work:

    Code:
    #include "iostream.h"
    #include "dos.h"
    #include "conio.h"
    #include "stdlib.h"
    
    
    /*
    
      Information on the standard parallel port can be found at:
      http://www.doc.ic.ac.uk/~ih/doc/par/
    
    */
    
    
    unsigned int DATA = 0;
    unsigned int CONTROL = 0;
    unsigned int STATUS = 0;
    
    
    void initPort(void);
    
    
    int main(int, char**)
    {
    	clrscr();
    	unsigned int addrLPT[3];
    	addrLPT[0] = *(unsigned int far *)0x00000408; //get LPT1 address
    	addrLPT[1]= *(unsigned int far *)0x0000040A;  //get LPT2 address
    	addrLPT[2] = *(unsigned int far *)0x0000040C; //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();
    	return 0;
    }
    
    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;
    			outportb(DATA + 0x402, ECR & 0x1F); // set mode to SPP
    			if ((inportb(DATA + 0x402) & 0xE0) == 0)
    				cout << "ECP mode set succeeded" << endl;
    			else
    				cout << "Can't set ECP mode, initialize failed" << endl;
    		}
    		else outportb(DATA + 0x402, ECR); // restore original value otherwise
    	}
    	//for EPP/ bidirectional SPP we need to make sure control bit 5 is 0 for forward direction
    	outportb(CONTROL, inportb(CONTROL) & 0xDF);
    	//now regardless of port type we can use SPP mode as planned
    }
    You need to look at the parallel port pinout to know how to set each line, but you can write to the data or control bytes, and their values will adjust the values of the pins. For example, to switch pin #4 on (which is data bit 2), you would do:

    outportb(DATA, inportb(DATA) | 0x04)

    The basic flow of the code:

    1) Determines how many parallel ports (can use LPT1-LPT3) are installed, prompts for a number -- you could easily modify this to save settings to a file.

    2) Calls InitPort(), which will detect the port type. An ECP port will be set to SPP emulation mode, and a bidirectional SPP or EPP port will be set to data output mode. After this InitPort(), assuming it succeeds (you can add error checking) your port can be used in SPP mode. This is the best way to do things if you want to adjust pin values.
    Last edited by The V.; 11-15-2001 at 11:00 PM.

  3. #3
    Registered User BigSter's Avatar
    Join Date
    Nov 2001
    Posts
    47

    Re: Simple Parallel Port Control

    Originally posted by BigSter
    Hi, I am trying to use C++ and my Parallel port to control outside objects. I was wondering how to make a simple program that sets the certain data pins at either 0 or 1. I am using Windows 95, and trying to make a DOS program with DEV-C++.
    Thanks
    Hey Thanks

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. FTP program
    By jakemott in forum Linux Programming
    Replies: 14
    Last Post: 10-06-2008, 01:58 PM
  2. programming the parallel port to control device
    By griffmaster2005 in forum C Programming
    Replies: 3
    Last Post: 02-14-2005, 07:50 AM
  3. Parallel Port Programming [led light]
    By Kristian_ in forum Networking/Device Communication
    Replies: 3
    Last Post: 07-06-2004, 06:40 AM
  4. receive signal from parallel port
    By happyspells in forum C Programming
    Replies: 1
    Last Post: 02-24-2004, 09:34 AM
  5. Help needed: Output to Parallel port.
    By Ingsy in forum C Programming
    Replies: 4
    Last Post: 10-10-2001, 12:06 PM