Board index » Visual Studio » Readfile hangs while trying to read the response from printer

Readfile hangs while trying to read the response from printer

Visual Studio83
Hi all,

Im trying to send a comamnd to the printer and read the

response for it thru parallel port(LPT1:). The write file always

succeeds, but in readfile it goes to hanging state. I have given my

code below.





int g;

HANDLE h;

DWORD NumberOfBytesWritten,NumberOfBytesRead;

unsigned char buff1[12];

unsigned char buff_read[30];



h = CreateFile("LPT1:",

GENERIC_READ | GENERIC_WRITE,

FILE_SHARE_READ,

NULL,

OPEN_ALWAYS,

FILE_ATTRIBUTE_NORMAL |

FILE_FLAG_SEQUENTIAL_SCAN,

NULL);

if (h == INVALID_HANDLE_VALUE)

{

g= 0;

return 0;

}

else

{



g=1;

buff1[0] = '{';

buff1[1] = 'W';

buff1[2] = 'B';

buff1[3] = '|';

buff1[4] = '}';





i = WriteFile(h,buff1,5,&NumberOfBytesWritten ,NULL);

i =ReadFile(h,buff_read,2,&NumberOfBytesRead,NULL);

CloseHandle(h);

return 400;

}


-
 

Re:Readfile hangs while trying to read the response from printer

What do you mean by hangs? Are you sure there is any data coming in from

the port? If there is no data in the receive buffer then ReadFile will just

sit there. until there is data to be read.



Also you might want to poll the port for data, in a worker thread before

reading from it.



AliR.



"hari" <haricibi83@gmail.com>wrote in message

Quote
Hi all,

Im trying to send a comamnd to the printer and read the

response for it thru parallel port(LPT1:). The write file always

succeeds, but in readfile it goes to hanging state. I have given my

code below.





int g;

HANDLE h;

DWORD NumberOfBytesWritten,NumberOfBytesRead;

unsigned char buff1[12];

unsigned char buff_read[30];



h = CreateFile("LPT1:",

GENERIC_READ | GENERIC_WRITE,

FILE_SHARE_READ,

NULL,

OPEN_ALWAYS,

FILE_ATTRIBUTE_NORMAL |

FILE_FLAG_SEQUENTIAL_SCAN,

NULL);

if (h == INVALID_HANDLE_VALUE)

{

g= 0;

return 0;

}

else

{



g=1;

buff1[0] = '{';

buff1[1] = 'W';

buff1[2] = 'B';

buff1[3] = '|';

buff1[4] = '}';





i = WriteFile(h,buff1,5,&NumberOfBytesWritten ,NULL);

i =ReadFile(h,buff_read,2,&NumberOfBytesRead,NULL);

CloseHandle(h);

return 400;

}







-

Re:Readfile hangs while trying to read the response from printer

Which version of Windows? Which printer driver?

On Thu, 10 Jan 2008 07:17:40 -0800 (PST), hari <haricibi83@gmail.com>wrote:



Quote
Hi all,

Im trying to send a comamnd to the printer and read the

response for it thru parallel port(LPT1:). The write file always

succeeds, but in readfile it goes to hanging state. I have given my

code below.





int g;

HANDLE h;

DWORD NumberOfBytesWritten,NumberOfBytesRead;

unsigned char buff1[12];

unsigned char buff_read[30];



h = CreateFile("LPT1:",

GENERIC_READ | GENERIC_WRITE,

FILE_SHARE_READ,

NULL,

OPEN_ALWAYS,

FILE_ATTRIBUTE_NORMAL |

FILE_FLAG_SEQUENTIAL_SCAN,

NULL);

if (h == INVALID_HANDLE_VALUE)

{

g= 0;

****

Since g is not used, why is it assigned? What does the return type indicate?

****

Quote
return 0;

}

else

{



g=1;

****

Since g is not used, why is it assigned? Given that g seems to have only two values, 0

and 1, why is it an int? It should be a BOOL and get TRUE or FALSE, but it still isn't

used and doesn't seem to have any meaning.

****

Quote
buff1[0] = '{';

buff1[1] = 'W';

buff1[2] = 'B';

buff1[3] = '|';

buff1[4] = '}';





i = WriteFile(h,buff1,5,&NumberOfBytesWritten ,NULL);

****

Seems unnecessarily complicated way to send a constant string; for example

i = WriteFile("{WB|}", 5, &NumberOfBytesWritten, NULL);

does it without requiring a byte-by-byte assignment of characters to an array



Note that you have not declared variable i, so we don't know its type. You should declare

it of type BOOL. Since you do not test it after the WriteFile, you have no idea if the

WriteFile actually worked, so perhaps the ReadFile hanging makes perfect sense, because

nothing was written.

****

Quote
i =ReadFile(h,buff_read,2,&NumberOfBytesRead,NULL);

****

If the driver is not a bidirectional driver; if the printer is not sending data; if the

driver is not prepared to handle data coming back from the printer, and so on, the

ReadFile will hang. You should probably use asynchronous I/O for this, but you have

omitted massive amounts of critical data here. Note that not all printer drivers are

prepared to deal with bidirectional printer ports, and you may need to replace your

printer driver.

joe

****

Quote
CloseHandle(h);

return 400;

}

Joseph M. Newcomer [MVP]

email: newcomer@flounder.com

Web: www.flounder.com">www.flounder.com

MVP Tips: www.flounder.com/mvp_tips.htm">www.flounder.com/mvp_tips.htm

-

Re:Readfile hangs while trying to read the response from printer

You can also use asynchronous/overlapped IO and then wait for input, but you

can do other things in addition to wait.



The printer driver probably has a way to determine if input is available,

but you must refer to the documentation; we don't know what the

documentation for your printer driver says.





"hari" <haricibi83@gmail.com>wrote in message

Quote
Hi all,

Im trying to send a comamnd to the printer and read the

response for it thru parallel port(LPT1:). The write file always

succeeds, but in readfile it goes to hanging state. I have given my

code below.





int g;

HANDLE h;

DWORD NumberOfBytesWritten,NumberOfBytesRead;

unsigned char buff1[12];

unsigned char buff_read[30];



h = CreateFile("LPT1:",

GENERIC_READ | GENERIC_WRITE,

FILE_SHARE_READ,

NULL,

OPEN_ALWAYS,

FILE_ATTRIBUTE_NORMAL |

FILE_FLAG_SEQUENTIAL_SCAN,

NULL);

if (h == INVALID_HANDLE_VALUE)

{

g= 0;

return 0;

}

else

{



g=1;

buff1[0] = '{';

buff1[1] = 'W';

buff1[2] = 'B';

buff1[3] = '|';

buff1[4] = '}';





i = WriteFile(h,buff1,5,&NumberOfBytesWritten ,NULL);

i =ReadFile(h,buff_read,2,&NumberOfBytesRead,NULL);

CloseHandle(h);

return 400;

}











-

Re:Readfile hangs while trying to read the response from printer

On Jan 11, 10:32=A0pm, "Sam Hobbs"

<sam...@social.rr.com_change_social_to_socal>wrote:

Quote
You can also use asynchronous/overlapped IO and then wait for input, but y=

ou

can do other things in addition to wait.



The printer driver probably has a way to determine if input is available,

but you must refer to the documentation; we don't know what the

documentation for your printer driver says.



"hari" <haricib...@gmail.com>wrote in message



news:722ce690-2dde-4acd-ad74-93b772f9cec7@v46g2000hsv.googlegroups.com...







>Hi all,

>=A0 =A0 =A0 Im trying to send a comamnd to the printer and read the

>response for it thru parallel port(LPT1:). The write file always

>succeeds, but in readfile it goes to hanging state. I have given my

>code below.



>int g;

>=A0HANDLE h;

>=A0DWORD NumberOfBytesWritten,NumberOfBytesRead;

>=A0unsigned char buff1[12];

>=A0unsigned char buff_read[30];



>=A0h =3D CreateFile("LPT1:",

>=A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 GENERIC_READ | GENERIC_WRITE=

,

>=A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 FILE_SHARE_READ,

>=A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 NULL,

>=A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 OPEN_ALWAYS,

>=A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 FILE_ATTRIBUTE_NORMAL |

>=A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 FILE_FLAG_SEQUENTIAL_SCAN,

>=A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 NULL);

>=A0if (h =3D=3D INVALID_HANDLE_VALUE)

>{

>=A0 g=3D 0;

>=A0 return 0;

>}

>=A0else

>=A0{



>=A0g=3D1;

>=A0 buff1[0] =3D '{';

>=A0 buff1[1] =3D 'W';

>=A0 buff1[2] =3D 'B';

>=A0 buff1[3] =3D '|';

>=A0 buff1[4] =3D '}';



>=A0i =3D WriteFile(h,buff1,5,&NumberOfBytesWritten ,NULL);

>=A0i =3DReadFile(h,buff_read,2,&NumberOfBytesRead,NULL);

>=A0CloseHandle(h);

>=A0return 400;

>}- Hide quoted text -



- Show quoted text -



Th problem is,if the printer driver is installed in LPT1: and if we

are trying to do readfile from other application in LPT1.It goes to

hanging state

-

Re:Readfile hangs while trying to read the response from printer

See far below...

On Thu, 24 Jan 2008 04:28:51 -0800 (PST), hari <haricibi83@gmail.com>wrote:



Quote
On Jan 11, 10:32 pm, "Sam Hobbs"

<sam...@social.rr.com_change_social_to_socal>wrote:

>You can also use asynchronous/overlapped IO and then wait for input, but you

>can do other things in addition to wait.

>

>The printer driver probably has a way to determine if input is available,

>but you must refer to the documentation; we don't know what the

>documentation for your printer driver says.

>

>"hari" <haricib...@gmail.com>wrote in message

>

>news:722ce690-2dde-4acd-ad74-93b772f9cec7@v46g2000hsv.googlegroups.com...

>

>

>

>>Hi all,

>>      Im trying to send a comamnd to the printer and read the

>>response for it thru parallel port(LPT1:). The write file always

>>succeeds, but in readfile it goes to hanging state. I have given my

>>code below.

>

>>int g;

>> HANDLE h;

>> DWORD NumberOfBytesWritten,NumberOfBytesRead;

>> unsigned char buff1[12];

>> unsigned char buff_read[30];

>

>> h = CreateFile("LPT1:",

>>                      GENERIC_READ | GENERIC_WRITE,

>>                      FILE_SHARE_READ,

>>                      NULL,

>>                      OPEN_ALWAYS,

>>                      FILE_ATTRIBUTE_NORMAL |

>>                      FILE_FLAG_SEQUENTIAL_SCAN,

>>                      NULL);

>> if (h == INVALID_HANDLE_VALUE)

>>{

>>  g= 0;

>>  return 0;

>>}

>> else

>> {

>

>> g=1;

>>  buff1[0] = '{';

>>  buff1[1] = 'W';

>>  buff1[2] = 'B';

>>  buff1[3] = '|';

>>  buff1[4] = '}';

>

>> i = WriteFile(h,buff1,5,&NumberOfBytesWritten ,NULL);

>> i =ReadFile(h,buff_read,2,&NumberOfBytesRead,NULL);

>> CloseHandle(h);

>> return 400;

>>}- Hide quoted text -

>

>- Show quoted text -



Th problem is,if the printer driver is installed in LPT1: and if we

are trying to do readfile from other application in LPT1.It goes to

hanging state

****

I have no idea what this means. A printer driver is installed FOR (not in) a printer, and

it is installed when the plug-and-play system detects the printer (for built-in legacy

parallel ports, there is a default mechanism for loading the driver). I have no idea what

you mean by "other application" because only one application at a time can open the

parallel port. And you did not say there is any other application involved in the

original post. Printer drivers are not applications, they are drivers.



If you open a printer device (e.g., _T("LPT1") as the filename parameter to CreateFile)

then it uses the printer driver associated with LPT1. If that driver cannot accept data

back from the device, it won't do any good to do a ReadFile; the ReadFile will hang. So

you need to know WHICH driver is loaded for LPT1, which you can find out my going to "My

Computer", selecting "Manage", selecting "Device Manager", find the Ports (COM & LPT)

entry, pick LPT1, right-click on it and ask for Properties. Then select the Driver tab

and see what driver is associated with it. As far as I know, the standard Microsoft

drivers do NOT support bidirectional printer ports, so if you have a standard Microsoft

driver, you will need to get a new driver from one of the many third-party driver

companies. Pointers have been posted in this newsgroup, you've have to search the

archives.

joe

****

Joseph M. Newcomer [MVP]

email: newcomer@flounder.com

Web: www.flounder.com">www.flounder.com

MVP Tips: www.flounder.com/mvp_tips.htm">www.flounder.com/mvp_tips.htm

-