|
-
June 23rd, 2006, 02:45 PM
#1
How to get the number of the printer port ? Solved ! :-)
My program needs to find out the resource number of lpt1: which can be different from computer to computer. It is usually something like 0x278 or 0x378...
Can someone please help me with this ?
Last edited by Zaph-0; June 25th, 2006 at 02:48 PM.
-
June 23rd, 2006, 03:19 PM
#2
Re: How to get the number of the printer port ?
Regards,
Ramkrishna Pawar
-
June 23rd, 2006, 07:19 PM
#3
Re: How to get the number of the printer port ?
Hi,
I did take a look at it, but all that I can see is that I can get the name of the printer ports like "lpt1" and I can get that already with enumports. What I need is the corresponding hardware resource port adress (0x278, 0x378, etc...).
But thanks anyway...
-
June 23rd, 2006, 09:37 PM
#4
Re: How to get the number of the printer port ?
I found a way to enumerate the number of the real lpt ports and find the matching port addresses.
If anybody wants to know the solution let me know and I will upload it...
-
June 23rd, 2006, 11:18 PM
#5
-
June 24th, 2006, 05:52 AM
#6
Re: How to get the number of the printer port ?
 Originally Posted by jayender.vs
Thats so nice u dude...  and nothing wrong in sending the code now as it might use for someone latter when they search this forum .. 
Well, I needed this function because I am controlling a robot arm with avr microprocessors connected to the parallel port. I didn't find anything on the net on how to get the port adresses automatically which would mean I would have to set the parallel port adress manually and I didn't like that. So I had to get a little "creative" with the registry...
I tried it on several computers with different settings for the parallel port (including automatic setting, boot setting and forced setting) and it always worked for me.
If someone uses it and finds a mistake please let me know...
But here you go:

Code:
int * CMicrobotView::GetPortAddresses()
{ HKEY key1;
HKEY key2;
int i,x;
char buffer[2000];
char branch1[300];
char branch2[300];
int type,size;
int * list;
//
if(RegOpenKeyEx(HKEY_LOCAL_MACHINE, "SYSTEM\\CurrentControlSet\\Enum\\ACPI", NULL, KEY_READ, &key1) != ERROR_SUCCESS)
{ return NULL;
}
//
list = new int[2*10 + 1];
x = 0;
// Enumerate HW
for(i=0;(RegEnumKey(key1, i, buffer, sizeof(buffer)) == ERROR_SUCCESS) && (x<10); i++)
{ if((strlen("SYSTEM\\CurrentControlSet\\Enum\\ACPI") + strlen(buffer)) < (sizeof(branch1) - 2))
{ sprintf(branch1, "SYSTEM\\CurrentControlSet\\Enum\\ACPI\\%s\\", buffer);
// Enter HW-Tree
if((RegOpenKeyEx(HKEY_LOCAL_MACHINE, branch1, NULL, KEY_READ, &key2) != ERROR_SUCCESS)
|| (RegEnumKey(key2, 0, buffer, sizeof(buffer)) != ERROR_SUCCESS))
{ RegCloseKey(key2);
continue;
}
strcat(branch1,buffer);
// Check Device Parameters
size = sizeof(buffer);
sprintf(branch2,"%s\\Device Parameters", branch1);
if((RegOpenKeyEx(HKEY_LOCAL_MACHINE, branch2, NULL, KEY_READ, &key2) != ERROR_SUCCESS)
|| (RegQueryValueEx(key2, "PortName", 0, (DWORD*)&type, (BYTE*)buffer, (DWORD*)&size) != ERROR_SUCCESS)
|| (strncmp(buffer,"LPT",3) != 0))
{ RegCloseKey(key2);
continue;
}
list[x+1+0] = buffer[3]-'0';
// Portname found -> Extract Data
RegCloseKey(key2);
size = sizeof(buffer);
sprintf(branch2,"%s\\LogConf", branch1);
if(RegOpenKeyEx(HKEY_LOCAL_MACHINE, branch2, NULL, KEY_READ, &key2) != ERROR_SUCCESS)
{ RegCloseKey(key2);
continue;
}
//
if((RegQueryValueEx(key2, "ForcedConfig", 0, (DWORD*)&type, (BYTE*)buffer, (DWORD*)&size) == ERROR_SUCCESS)
|| (RegQueryValueEx(key2, "BootConfig" , 0, (DWORD*)&type, (BYTE*)buffer, (DWORD*)&size) == ERROR_SUCCESS))
{ list[x+1+1] = ((BYTE)buffer[24]) + ((BYTE)buffer[25]) * 0x100;
x++;
}
else
{ if(RegQueryValueEx(key2, "BasicConfigVector", 0, (DWORD*)&type, (BYTE*)buffer, (DWORD*)&size) == ERROR_SUCCESS)
{ list[x+1+1] = buffer[57] * 0x100 + buffer[56];
x++;
}
}
//
RegCloseKey(key2);
}
}
RegCloseKey(key1);
//
if(x == 0)
{ delete [] list;
return NULL;
}
list[0] = x;
return list;
}
The return value is either NULL if it couldn't find anything or an array of ints with the first value the number of parallelports and then pairs of ints describing the lpt number and the serial adress. You have to delete the array if you don't need it anymore or it will result in a memory leak.
example:
list[0] = 2 -> number of parallel ports found (up to 10)
list[1] = 1 -> lpt1:
list[2] = 0x2f8 -> serial adress 0x278
list[3] = 2 -> lpt2:
list[4] = 0x3f8 -> serial adress 0x378
Have fun...
-
June 25th, 2006, 06:24 AM
#7
Re: How to get the number of the printer port ?
To find Printer port address
Step 1, Go to the command prompt
Type debug
ex:
c:\ debug
the prompt will change to "-"
Then type the following
- 0004:0008 l8
All four port address will be displayed( i forgot the order).
Mind you it is (L8) not (one 8), in the above command
i know, this was not the solution you were expecting, may be you may
get new ideas to solve your problem.
regards
pradish
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|