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.