PC Review


Reply
Thread Tools Rating: Thread Rating: 1 votes, 5.00 average.

WriteFile to LPT behaves strange

 
 
Sergey
Guest
Posts: n/a
 
      15th Jul 2005

Hi, everybody,

I want to send a short sequence of bytes to the printer, connected via
LPT port. Below you can find my sample code.
In this particular sample synchronous WriteFile succeeds, but the
printer does not receive data immediately. It receives data only after
CloseHandle has been called. I can see that in debugger and in the in the
dump made by the printer.
In case when instead of dwWritten WriteFile accepts some bigger value
(like 128, 512, 1024), the data will be sent to the printer immediately
after a call to WriteFile (that's what I need), but by bytes (one by one)
with huge second delays. By asynchronous WriteFile calls the same phenomenon
persists.
It seems that the issue is caused by wrong buffering mode or incorrectly
set port timeouts, but I have tried to set different buffering mode flags in
CreateFile call and even aligned the buffer. SetCommTimeouts and similar API
calls fail with "Invalid function" for LPT port.
Thus, either to call CloseHandle() in order to send bytes to the printer
or send them one by one specifying bigger buffer size. Both are unacceptable
for me.
Where is my mistake and how can I send short bytes sequence to the LPT
printer?

Thank you.


HANDLE hDirect;
DWORD dwWritten, dwRead, dwCmdLen;
char szPjlCmd[] = "\x1b%-12345X@PJL\r\n@PJL INFO ID\r\n\x1b%-12345X";
char* pszOutput;
char szInput[1024];
dwCmdLen = (_tcslen(szPjlCmd)+1)*sizeof(char);
pszOutput = (char*) VirtualAlloc(NULL,512,MEM_COMMIT,PAGE_READWRITE);
strcpy(pszOutput,szPjlCmd);
hDirect = CreateFile("LPT1",
GENERIC_READ | GENERIC_WRITE,
//FILE_SHARE_READ | FILE_SHARE_WRITE,
0,
NULL,
OPEN_EXISTING,
//FILE_FLAG_OVERLAPPED,
//FILE_FLAG_NO_BUFFERING,
//FILE_FLAG_WRITE_THROUGH | FILE_FLAG_NO_BUFFERING,
0,
NULL);
if(hDirect == INVALID_HANDLE_VALUE)
{
OUTPUT_SYSTEM_ERROR()
return -1;
}
dwWritten = (_tcslen(szPjlCmd)+1)*sizeof(TCHAR);
if(WriteFile(hDirect,
pszOutput,
dwWritten,
&dwWritten,
NULL) == 0)
{
OUTPUT_SYSTEM_ERROR()
return -2;
}

CloseHandle(hDirect);
VirtualFree(
pszOutput,
0,
MEM_RELEASE);



 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
WriteFile to LPT behaves strange Sergey Microsoft Windows 2000 Printing 0 15th Jul 2005 11:22 AM
ICS behaves strange - interrupts while 2nd DUN connection is open Thomas Osthege Windows XP Networking 4 17th Aug 2004 06:21 PM
Outlook 2003, "you have replied"-icon behaves strange Gerard Verhoef Microsoft Outlook 0 15th Apr 2004 10:13 AM
How do I remap LPT to LPT Microsoft Windows 2000 Printing 1 23rd Feb 2004 07:07 PM
Strange error with Response.WriteFile and SSL Martin Knopp Microsoft ASP .NET 6 29th Jan 2004 08:43 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 12:25 AM.