Author Topic: How to send ESC sequences and text to my dot matrix printer?  (Read 3866 times)

0 Members and 1 Guest are viewing this topic.

Offline radio-hobby

  • Newbie
  • Posts: 4
    • View Profile
How to send ESC sequences and text to my dot matrix printer?
« on: January 16, 2019, 01:37:35 pm »
Hello specialists,
I'm just a beginner since 40 years...

With Windows XP (32 bit) using QBASIC and my Epson LQ300 dot matrix printer connected to LPT1: I wrote the printer routine the following way


OPEN "LPT1:" FOR OUTPUT AS #1: WIDTH #1, 150
GOSUB ELITE    ' this subroutine will send some ESC sequences to the EPSON LQ300 in order to switch it to the ELITE character set

' now put some text to the text variable P$
P$ = "This is just some text that you want the EPSON LQ300 to print out." : GOSUB PRINTER
P$ = "And this is a scond line you want to be printed." : GOSUB PRINTER
GOSUB FORMFEED
CLOSE
END



PRINTER: P$ = "      " + P$: PRINT #1, P$: RETURN        ' the printed text is moved to the right a bit, then printed in one line

ELITE:
P$ = CHR$(&H1B) + CHR$(&H40) + CHR$(&H1B) + CHR$(&H78) + CHR$(&H1) + CHR$(&H1B) + CHR$(&H6B) + CHR$(&H1) + CHR$(&H1B) +  CHR$(&H4D)
GOSUB PRINTER: P$ = ""
RETURN

FORMFEED:                            ' the paper comes out of the printer
P$ = CHR$(&HC) : GOSUB PRINTER: P$ = "" : RETURN



Now, with QB64 and today's computers without centronics serial interface LPT1, but the EPSON LQ300 connected via USB001, I was able to get something printed by use of the command LPRINT as follows:


LPRINT "The quick brown fox jumped over the lazy dog"

(Thank you for making this work, indeed!)

But... up to now I did not find out how to send ESC sequences to the printer in order to change the font.

OPEN "USB001" FOR OUTPUT AS #1          did not work, sorry. Instead it produced an error "illegal function call"
P$ = CHR$(&H1B)   .........    LPRINT P$     did not work, either, sorry. Instead it printed some arrows and playing card symbols.

Please indulge me in being without any deeper knowledge,
Georg

Marked as best answer by radio-hobby on January 17, 2019, 02:28:37 pm

Offline Pete

  • Forum Resident
  • Posts: 2361
  • Cuz I sez so, varmint!
    • View Profile
Re: How to send ESC sequences and text to my dot matrix printer?
« Reply #1 on: January 16, 2019, 02:10:08 pm »
I used to do a lot with printer esc codes 20+ years ago. I have not used them at all with QB64, so I'm not sure if they chan be used anymore. If LPRINT CHR$(12) still ejects the paper, then maybe so.

If you can't use the, you could do something like slave printing...

1) Open Wordpad and make a blank document with the fonts selected that you want. Save it, and open it in notepad. The .rtf format will appear. Now you just open this file as a template, in other words read the lines that set the formatting into your own file as the header, followed by your text you want printed. Now you have your own .rtf file, which you can SHELL to wordpad and have it pront the document for you.

Here's some sample code I wrote to help you get what I'm trying to convey about slave printing...

Code: QB64: [Select]
  1. LINE INPUT "Type some stuff here to be printed in Comic Sans: "; mystuff$
  2.  
  3. myfile$ = "mycomicsans.rtf"
  4.  
  5. OPEN myfile$ FOR OUTPUT AS #1
  6. PRINT #1, "{\rtf1\ansi\ansicpg1252\deff0\nouicompat\deflang1033{\fonttbl{\f0\fnil\fcharset0 Comic Sans MS;}{\f1\fnil\fcharset0 Calibri;}}"
  7. PRINT #1, "{\*\generator Riched20 10.0.17134}\viewkind4\uc1"
  8. PRINT #1, "\pard\sa200\sl276\slmult1\i\f0\fs28\lang9"
  9. PRINT #1, mystuff$ + "\f1\fs22\par"
  10. PRINT #1, "}"
  11.  
  12. SHELL _HIDE _DONTWAIT "wordpad /min /p " + myfile$ ' Opens Wordpad minimzed and prints the document.
  13. SHELL _HIDE "TASKKILL /F /IM wordpad.exe" ' Terminate (Close) Wordpad.
  14.  
Want to learn how to write code on cave walls? https://www.tapatalk.com/groups/qbasic/qbasic-f1/

Offline Petr

  • Forum Resident
  • Posts: 1720
  • The best code is the DNA of the hops.
    • View Profile
Re: How to send ESC sequences and text to my dot matrix printer?
« Reply #2 on: January 16, 2019, 02:23:30 pm »
Hi. WIDTH with LPRINT is not supported. Not all CHR codes also are not supported. Create image with text using _NEWIMAGE and then print this image using _PRINTIMAGE. (set white background on this image to save your cartdridge).

Offline radio-hobby

  • Newbie
  • Posts: 4
    • View Profile
Re: How to send ESC sequences and text to my dot matrix printer?
« Reply #3 on: January 16, 2019, 03:41:20 pm »
Thank you Pete and Petr,
I will try to write a .rtf file from qb64. Thank you for providing those SHELL commands I did not know before to start the printout of the .rtf file by wordpad.
Hope I understand what to write into the header - I'll try to copy like "black box". Thanks again.
Georg

Offline radio-hobby

  • Newbie
  • Posts: 4
    • View Profile
Re: How to send ESC sequences and text to my dot matrix printer?
« Reply #4 on: January 17, 2019, 03:38:04 pm »
Hope it's o.k. to you if I post my proceedings here.

1st step was to look whether the SHELL command lines would really work with my computer.
I wrote a .rtf file on an old Win XP computer, transfered it to the Win10 computer and tried to print it using your SHELL command lines.
Only in the beginning they didn't work.

Thus I searched for wordpad on Win10 computers. You'll find it under C:\program files (x86)\windows nt\accessories
Then I told win10 to always open .rtf files with this wordpad.
I think this was neccessary, otherwise your SHELL command lines would not work.

After that, thank you, Pete, your SHELL command lines work well now.

Next I'll try to write some .rtf text file by QB64 the way you showed to me.
Please provide me if possible with some information how to change fonts within the text, and what the content of the header is.
Even after some contemplation over your commands following PRINT #1, I did not understand much.
What I understood is that your knowledge about .rtf files is very detailed, my respect.
My goal is to print a DIN A4 letter with the name of the sender in bigger letters on top right, then very tiny small lines (planned to fit in the upper part of the window of the envelope) with name and adress of the sender, then normal size for the adress, the text and so on. I guess I can do it if I know how to change font within the text.

Thank you very much in advance,
Georg

Offline SMcNeill

  • QB64 Developer
  • Forum Resident
  • Posts: 3972
    • View Profile
    • Steve’s QB64 Archive Forum
Re: How to send ESC sequences and text to my dot matrix printer?
« Reply #5 on: January 17, 2019, 03:42:49 pm »
Specifications for rtf formatting:  http://www.biblioscape.com/rtf15_spec.htm

Explains all you’d ever need to know about how it all works.  ;)
https://github.com/SteveMcNeill/Steve64 — A github collection of all things Steve!

Offline radio-hobby

  • Newbie
  • Posts: 4
    • View Profile
Re: How to send ESC sequences and text to my dot matrix printer?
« Reply #6 on: January 17, 2019, 04:16:51 pm »
Thank you, I think I'm on the way...  I'm going to study it the way you showed to me by reading the headers of example .rtf files...
Thank you for the link to knowledge of .rtf formatting.
Thank you all for your work on QB64 and for your help!!!
Georg
« Last Edit: January 17, 2019, 04:19:02 pm by radio-hobby »

Offline Petr

  • Forum Resident
  • Posts: 1720
  • The best code is the DNA of the hops.
    • View Profile
Re: How to send ESC sequences and text to my dot matrix printer?
« Reply #7 on: January 18, 2019, 12:11:07 pm »
A small demonstration of what PRINTIMAGE can do. Not only can images be inserted into print, but you can directly use fonts and everything else that QB64 allows. It then depends on fantasy.

Code: QB64: [Select]
  1. path$ = ENVIRON$("windir") + "\fonts\" 'path for loading fonts in windows
  2. FontA& = _LOADFONT(path$ + "arial.ttf", 5, "MONOSPACE")
  3. FontB& = _LOADFONT(path$ + "Bkant.ttf", 15, "monospace")
  4. FontC& = _LOADFONT(path$ + "arial.ttf", 50, "monospace")
  5.  
  6.  
  7. IF FontA& AND FontB& AND FontC& THEN
  8.     image& = _SCREENIMAGE 'set current screen (windows only) image as background
  9.     _SETALPHA 40, , image& 'set all colors in image as transparent (0 = full transparent, not visible, 255 = not transparent, full visible)
  10.  
  11.  
  12.     paper& = _NEWIMAGE(297 * 3, 210 * 3, 32) ' A4 size
  13.     SCREEN paper& '                    for view it, or just use _DEST paper&
  14.     CLS , _RGB32(255, 255, 255) '      set white background
  15.     _PUTIMAGE , image&, paper&
  16.     COLOR _RGB32(0, 0, 0), _RGB32(255, 255, 255) 'set black characters on white background
  17.     _FONT FontA&, paper&
  18.     LOCATE 1, 1: PRINT "This is first line, printed with ARIAL.TTF font, size 5"
  19.     LOCATE 2, 1: PRINT "This is next line printed with same font"
  20.     _FONT FontB&, paper&
  21.     LOCATE 4, 1: PRINT "BKANT.TTF font, size 15"
  22.     _FONT FontC&, paper&
  23.     _PRINTSTRING (1, _HEIGHT - 2 * _FONTHEIGHT(FontC&)), "Arial, size 50!"
  24.  
  25.     _PRINTIMAGE paper& 'print this screen as image
  26.     PRINT "Some from font is not loaded correctly. Please use valid font (TTF) file name.": END
  27.