Recent

Author Topic: [SOLVED] Printer.Write strange behavior  (Read 6634 times)

odvesims

  • Full Member
  • ***
  • Posts: 176
[SOLVED] Printer.Write strange behavior
« on: August 07, 2017, 02:48:39 pm »
Hey There!

I'm using the Printers unit to print in my app. I'm using ESC/POS commands to give format to the printer. This is working fine in most cases. I'm able to print in both Windows/Linux. However, in Linux, when I print a second document, the printer skips a few lines/characters. It doesn't print the text and it also ignores the ESC/POS commands. The problem persists until I close my app and restarted it.

Any idea what may be causing this?
« Last Edit: August 25, 2017, 03:23:09 pm by odvesims »

pixelink

  • Hero Member
  • *****
  • Posts: 1260
Re: Printer.Write strange behavior
« Reply #1 on: August 08, 2017, 11:27:00 am »
You will need to provide more details and code if you want someone to help you
Can't Type - Forgetful - Had Stroke = Forgive this old man!
LAZ 2.2.0 •  VSSTUDIO(.Net) 2022 • Win10 • 16G RAM • Nvida GForce RTX 2060

Thaddy

  • Hero Member
  • *****
  • Posts: 14213
  • Probably until I exterminate Putin.
Re: Printer.Write strange behavior
« Reply #2 on: August 08, 2017, 11:30:03 am »
Like for example:
- Is it a GUI application
- Are you emitting more LPT codes?
- Platform and OS

Preferably a small compilable example
Specialize a type, not a var.

odvesims

  • Full Member
  • ***
  • Posts: 176
Re: Printer.Write strange behavior
« Reply #3 on: August 08, 2017, 04:00:31 pm »
Apparently it is caused by me executing a command to check if the printer is connected. If I avoid this command, the printer doesn't skip any lines. Strange as I use a similar process in Windows and it doesn't cause any line skip.

odvesims

  • Full Member
  • ***
  • Posts: 176
Re: Printer.Write strange behavior
« Reply #4 on: August 21, 2017, 08:38:02 pm »
Like for example:
- Is it a GUI application
- Are you emitting more LPT codes?
- Platform and OS

Preferably a small compilable example

Sorry I'm reviving this, I just tried multiple options and nothing worked.

1- It is a GUI application.
2- I'm using the Printer unit to print, so it's basically Printer.Write('TextToWrite') that I'm sending the the printer. I'm sending commands to the set the font-type/size/etc (using char's like @, !, A).
3- I'm having this issue on Raspbian (Debian for Raspberry).

rvk

  • Hero Member
  • *****
  • Posts: 6112
Re: Printer.Write strange behavior
« Reply #5 on: August 21, 2017, 09:59:10 pm »
And....
Preferably a small compilable example

Apparently it is caused by me executing a command to check if the printer is connected. If I avoid this command, the printer doesn't skip any lines. Strange as I use a similar process in Windows and it doesn't cause any line skip.
You don't even mention what command that causes it !

odvesims

  • Full Member
  • ***
  • Posts: 176
Re: Printer.Write strange behavior
« Reply #6 on: August 21, 2017, 10:14:24 pm »
And....
Preferably a small compilable example

Apparently it is caused by me executing a command to check if the printer is connected. If I avoid this command, the printer doesn't skip any lines. Strange as I use a similar process in Windows and it doesn't cause any line skip.
You don't even mention what command that causes it !

Sorry, my bad. This is the process that causes this:

Code: Pascal  [Select][+][-]
  1. OutPutLines:= TStringList.Create;
  2. OutPutError:= TStringList.Create;
  3. hProcess:= TProcess.Create(nil);
  4. hProcess.Executable:= 'user/sbin/lpinfo';
  5. hProcess.Parameters.Add('--timeout');
  6. hProcess.Parameters.Add('1');
  7. hProcess.Parameters.Add('-v');
  8. hProcess.Options:= hProcess.Options + [poUsePipes];
  9. hProcess.Execute;
  10. OutPutLines.LoadFromStream(hProcess.Output);
  11. OutPutError.Add('Error: ');
  12. OutPutError.LoadFromStream(hProcess.Stderr);
  13. if OutPutError.Text = nil then
  14.    s:= OutPutLines.Text;
  15. hProcess.Free;
  16. OutPutLines.Free;
  17. OutPutError.Free;

I've tried using RunCommand/ExecuteProcess and all causes the same. If I omit this, then the printing goes by perfectly but I have no way of telling if the printer is/isn't plugged in prior to trying to print.

rvk

  • Hero Member
  • *****
  • Posts: 6112
Re: Printer.Write strange behavior
« Reply #7 on: August 21, 2017, 10:26:25 pm »
lpinfo shouldn't send data to the printer.

Are you executing this between the documents? If the check is ok for the first document you can expect the printer is still attach for the second document.

So maybe you can create a small example-project where you show the problem.

And on what printer is this? (so somebody can test it on the same printer).

rvk

  • Hero Member
  • *****
  • Posts: 6112
Re: Printer.Write strange behavior
« Reply #8 on: August 21, 2017, 10:38:44 pm »
Also... shouldn't that 1 be in the add-parameter with timeout??
(not sure if it makes any difference)
Code: Pascal  [Select][+][-]
  1. hProcess.Executable:= 'user/sbin/lpinfo';
  2. hProcess.Parameters.Add('--timeout 1');
  3. Process.Parameters.Add('-v');

I'm not that familiar with printing in Linux but if you are printing directly to the lpt-port... the lpinfo is part of CUPS... so maybe mixing those might not work correctly.

odvesims

  • Full Member
  • ***
  • Posts: 176
Re: Printer.Write strange behavior
« Reply #9 on: August 21, 2017, 10:45:24 pm »
lpinfo shouldn't send data to the printer.

Are you executing this between the documents? If the check is ok for the first document you can expect the printer is still attach for the second document.

So maybe you can create a small example-project where you show the problem.

And on what printer is this? (so somebody can test it on the same printer).

With lpinfo I'm simply reading if there's a device listed that matches "usb" and "printer". Those values don't show up if the printer is unplugged/turn off.

I'm sending this before I print any document. Documents are not printed successively, but it doesn't matter if I wait 1 minute, 1 hour in between each printing. If I issue this command a 2nd time, all other documents will skip some lines.

This is a mini thermal printer.

odvesims

  • Full Member
  • ***
  • Posts: 176
Re: Printer.Write strange behavior
« Reply #10 on: August 22, 2017, 02:35:30 pm »
Also... shouldn't that 1 be in the add-parameter with timeout??
(not sure if it makes any difference)
Code: Pascal  [Select][+][-]
  1. hProcess.Executable:= 'user/sbin/lpinfo';
  2. hProcess.Parameters.Add('--timeout 1');
  3. Process.Parameters.Add('-v');

I'm not that familiar with printing in Linux but if you are printing directly to the lpt-port... the lpinfo is part of CUPS... so maybe mixing those might not work correctly.

I have tried that aswell, but it throws an error as if the command was wrongly structured.

I'm using the Printers unit, like this:

Code: Pascal  [Select][+][-]
  1. Printer.BeginDock;
  2. Printer.Write(printer_text[1], Length(printer_text), w);
  3. Printer.EndDock;

I'm using it in RawMode.
« Last Edit: August 22, 2017, 04:13:09 pm by odvesims »

odvesims

  • Full Member
  • ***
  • Posts: 176
Re: Printer.Write strange behavior
« Reply #11 on: August 25, 2017, 03:22:55 pm »
Well, I figured a work-around this issue.

I'm sending this:

Code: Pascal  [Select][+][-]
  1. Printer.BeginDoc;
  2. printer_text:= #27'd'#3#27#109;
  3. Printer.Write(printer_text[1], Length(printer_text), w);
  4. Printer.EndDoc;

Right before I start my actual printing method. True this forces the printer to print 1 blank line right before my document, but it's barely noticeable (I'm using paper-roll printers). It's not the best solution  %) , but I have tried pretty much everything I'd find, to no avail. It's working, that's what matters :p

Marking it as "Solved".

J-G

  • Hero Member
  • *****
  • Posts: 953
Re: Printer.Write strange behavior
« Reply #12 on: August 25, 2017, 05:26:08 pm »
Well, I figured a work-around this issue.

I'm sending this:

Code: Pascal  [Select][+][-]
  1. Printer.BeginDoc;
  2. printer_text:= #27'd'#3#27#109;
  3. Printer.Write(printer_text[1], Length(printer_text), w);
  4. Printer.EndDoc;

Right before I start my actual printing method. True this forces the printer to print 1 blank line right before my document, but it's barely noticeable (I'm using paper-roll printers). It's not the best solution  %) , but I have tried pretty much everything I'd find, to no avail. It's working, that's what matters :p
It's a while since I really looked at ESC/P or ESC/P2 codes so had to research my manual to fathom what ESC d actually does  -  and surprize, surprize -- it doesn't exist.  ESC D sets horizontal tabs but I can't find any reference to ESC d.

If you simply need to 'Initialize' the printer then ESC @ would do the job - and shouldn't cause a line feed.

Also, I can't understand the purpose of line 3. surely you just need to 'Printer.Write(printer_text)' or even more succinctly :'Printer.Write(#27'@')'
FPC 3.0.0 - Lazarus 1.6 &
FPC 3.2.2  - Lazarus 2.2.0 
Win 7 Ult 64

rvk

  • Hero Member
  • *****
  • Posts: 6112
Re: Printer.Write strange behavior
« Reply #13 on: August 25, 2017, 06:08:11 pm »
It's a while since I really looked at ESC/P or ESC/P2 codes so had to research my manual to fathom what ESC d actually does  -  and surprize, surprize -- it doesn't exist.  ESC D sets horizontal tabs but I can't find any reference to ESC d.
Actually, ESC d 3 "Print and feed paper n lines" in ESC/POS.

It was quite easy to find in the documentation.
http://www.aures-support.fr/DATA/utility/Commande%20ESCPOS.pdf
I'm not sure why it's not in your manual.

But, odvesims, I'm not sure why the ESC m "Partial cut" was needed.
And I'm also not sure why sending more lines with ESC d 3 would help prevent lpinfo to print those lines.
Aren't you printing extra lines now (besides the ones you already got from lpinfo)?

And like J-G mentioned, the write() is really weird constructed. (what is w in your code?)
Maybe that's the reason you got those extra lines in the first place.

That's why I suggested you place a COMPLETE project with printing.
So maybe you can create a small example-project where you show the problem.
Create a small new test-project in where you print two documents with a lpinfo in the middle and let us see where it goes wrong. Maybe your problem is somewhere else.

« Last Edit: August 25, 2017, 06:13:16 pm by rvk »

odvesims

  • Full Member
  • ***
  • Posts: 176
Re: [SOLVED] Printer.Write strange behavior
« Reply #14 on: August 25, 2017, 09:55:33 pm »
Sorry guys, I was posting snippet of my codes here without giving proper context information, etc.

@J-G, sending only #27'@' did the trick. I thought it wouldn't work because that's what I basically send the the printer right before I start a new printing job, I send the @ ESC code to make sure the printer is initialized. This works fine under windows, however here in Linux it's requiring this extra initialization process.

It's true, the extra line is not necessary, I can just simply write to the printer. But I use the printer_text variable for some other purpose in my printing method, I just copy/pasted my old procedure and used it here.

@rvk, sorry for not providing a sample project. I didn't do it because I wouldn't be able to reproduce the printing outside of my environment. I actually fetch the text to print from a remote server, it comes with all the required ESC codes for formatting, so it'd be too complex to replicate it. Sorry for not stating it before.

 

TinyPortal © 2005-2018