Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Reading/Writing to the Parallel port in XP

Status
Not open for further replies.

BrimStonePhoenix

Programmer
Jun 22, 2006
7
US
I've been charged with the task to interface with a device through the parallel port. After much searching, I discovered the CreateFile, ReadFile, and WriteFile functions on msdn.com. I've gottem them to work for text files and the like, but When I change my code to try to read/write to the parallel port, the program lock up when I try to write to the port.

Here is some code I have :

#include <Windows.h>
#include <stdio.h>
#define BUFFSIZE 1
#define GETDATA 0xFF

int main()
{
HANDLE test;
int success;
BYTE buffer[BUFFSIZE] = {GETDATA};
DWORD bytestowrite;
DWORD byteswritten;
DWORD bytesread;

test = CreateFile("LPT1", GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
if (test == INVALID_HANDLE_VALUE)
{
printf ("CreateFile1 failed - Invalid Handle Value\n");
return (1);
}
else
{
printf("CreateFile1 Successful\n");
}

printf("here1\n");
success = WriteFile(test, buffer, BUFFSIZE, &byteswritten, NULL);
printf("here2\n");
if (success)
{
printf("File Write Successful - %i bytes\n", byteswritten);
}
else
{
printf("File Write Failed\n");
return (1);
}

return (0);
}

It locks up during the WriteFile line. Anybody have any ideas on how to remedy this situation?

Output looks a bit like :

CreateFile1 Successful
here1

and it sits there forever, until I force quit.

Any ideas, or other ways to get data in and out of the parallel port?
 
Is there on the port some device managing all parallel port handshaking? If not, probably Windows outputs the first byte and waits for confirmation. Try to shorten BUSY pin (11) to the ground (25) for testing (I'm not shure about pin numbers).
 
Are you using NT/2K/XP or 95/98/ME? The NT family may have restrictions on the parallel port.
 
I'm using XP - which I know has some restrictions on the port access. The one thing I've found so far is that if I change "LPT1" to "\\\\.\\LPT1" it works some of the time. I suppose I'll continue to look for drivers that allow me to access the port more directly
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top