Murali425891

I have used Following Code to print to the LPT1.

It is printing but only when i close my handle.

Here is a code I have written.

This is a impact printer (24 columns)

UINT PrintThread(LPVOID lp)
{

while(stop)
{
if(Prn)
{
Prn = 0;
HANDLE hLPT;
unsigned char ch;
unsigned long lLen;

hLPT = CreateFile("LPT1",
GENERIC_WRITE,
0,
0,
CREATE_ALWAYS,
0,
0);

if(hLPT == INVALID_HANDLE_VALUE)
{
AfxMessageBox("Cannot open port");
return 0 ;
}


WriteFile(hLPT,str,size,&lLen,NULL); // Data to be printed

ch = 0x0F;
WriteFile(hLPT,&ch,1,&lLen,NULL); //Reset command for Printer

if(Color)
{
ch = 0x13; //This is to change Color
WriteFile(hLPT,&ch,1,&lLen,NULL);

ch = 0x0D; //This command is to print
WriteFile(hLPT,&ch,1,&lLen,NULL);

}
else
{
ch = 0x0D; // This command is to print
WriteFile(hLPT,&ch,1,&lLen,NULL);
}
CloseHandle(hLPT);
}
}
return 1;
}





Re: Visual C++ Express Edition Problem in Printing using API CreateFile();

JamesSexton

Murali,

I'm having a hard time understanding all your PrintThread()'s globals, but it might be a good idea to try to open "LPT1" with :

hLPT = CreateFile("LPT1",(GENERIC_READ | GENERIC_WRITE),
0,
0,
CREATE_ALWAYS,
0,
0);

Afterall, most printers do talk back to you unless you are really dumping stuff in a hurry.

I am not sure about your CREATE_ALWAYS. OPEN_EXISTING might be safer, but that doesn't explain why it prints only after you close the handle... My guess is your're going to fast and not listening to your printer.

JamesSexton






Re: Visual C++ Express Edition Problem in Printing using API CreateFile();

Murali

Dear James,

I have impact printer which prints the data as soon as it receives 0x0D from the parallel port so i am not able to send data untill I close the handle created.

Murali.





Re: Visual C++ Express Edition Problem in Printing using API CreateFile();

JamesSexton

Dear Murali,

Ok, let me think on it, I've written C printer routines in the past.

If you can clarify your PrinterThread()'s code for me a little bit,
then please post it or visit microcube.com to contact me directly.

JamesSexton






Re: Visual C++ Express Edition Problem in Printing using API CreateFile();

nobugz

Your output is no doubt buffered before being sent to the printer. Try calling FlushFileBuffers(hLPT);





Re: Visual C++ Express Edition Problem in Printing using API CreateFile();

Murali

JamesSexton wrote:

Dear Murali,

Ok, let me think on it, I've written C printer routines in the past.

If you can clarify your PrinterThread()'s code for me a little bit,
then please post it or visit microcube.com to contact me directly.

JamesSexton



Dear James

I have nothing done in the thread. as soon as data comes i will make "prn" (global) variable high. As soon as thread senses "prn" as high it sends data to the printer. As soon as printer senses 0x0D it starts printing what are all the data present in the buffer. if I put "Closehandle()" and "CreateFile()" inside the "if" condition it will print but I will loose lot of time which in turn it is affecting my original program.

Murali.





Re: Visual C++ Express Edition Problem in Printing using API CreateFile();

Murali

Dear,

I have used this but it didn't work.





Re: Visual C++ Express Edition Problem in Printing using API CreateFile();

JamesSexton

Dear Murali,

After thinking a little bit, maybe I have a simple solution.

You say the routine prints something when you CloseHandle(hLPT), which is the same as nobugz's idea about FlushFileBuffers(hLPT). Only you close the handle and then have to reopen it the next time around, which seems to be a unnessary waste of time.

I don't know what you are printing, but it seems like a lot of stuff. Whatever, if you have a printer connected and online there is really no sense in Opening and Closing it everytime you have something to print.

So, if everything is working, but you are losing time Opening and Closing handles then why not make hLPT into a global handle. Open it somewhere before or when you CreateThread() and close it when you TerminateThread()

Then you could use nobugz's FlushFileBuffers(hLPT) instead of Closing handles everytime you have something to print.

If this doesn't work, then I am going to have to think of something else and rewrite your Thread.

JamesSexton

NB. How do you get out of while(stop) without a break;
NB. Have you looked into:

// LPCOMMTIMEOUTS LptTimeOuts={0};
- GetCommTimeouts(hLPT,LptTimeOuts);
- SetCommTimeouts(hLPT,LptTimeOuts);







Re: Visual C++ Express Edition Problem in Printing using API CreateFile();

Murali

Dear james,

While(stop) stop is made zero as soon as i close the application. My printer is always connected to the computer.

I have tried ur suggestions but it didn't work and i have also tried SetCommTimeouts by making both the write members in COMMTIMEOUTS structure but it also failed to work so plz give me any suggestions.





Re: Visual C++ Express Edition Problem in Printing using API CreateFile();

JamesSexton

Dear Murali,

First, happy new year. Second, take a day off. Third, I'll have to think so more and I will have to see your tread's code. So, visit microcube.com and send me a FormMail contact.

JamesSexton






Re: Visual C++ Express Edition Problem in Printing using API CreateFile();

JamesSexton

Dear Murali

Just one last thought. If your printer has 3 codes (commands):

1) 0x0F //Reset
2) 0x13 //Color
3) 0x0D //Print

and it prints only when you open and close its handle in PrintThread(), which is the same as forcing it to print, then maybe it is simply not acknowledging your commands, but requires an escape sequence before each command. After all most printers do unless they're somekind of obsolete TTY (TeleTypeWriters).

Othewise, this simply doesn't make much sense to me and I am at loss to help you without seeing what's going on in your CreateThread() and TerminateThread().

JamesSexton