Announcement

Collapse
No announcement yet.

Printer commands and control codes

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Printer commands and control codes

    I am trying to send commands and control codes to a printer.

    Below is an example of some PJL and PCL commands used to print a sample check.

    Does anyone have any suggestions on how to accomplish this in PowerBasic?

    I am trying to avoid placing these commands in a text file and copying the file to the printer.



    <ESC>%[email protected] LDECLARE LRESOURCE:"flash:" LRWLOCK="PASSWORD"

    @PJL MICRJOB

    @PJL ENTER LANGUAGE = PCL

    <ESC>&l2a1h6d1e64F



    Thanks for any assistance.
    Mark

  • #2
    Mark
    Can you write All these statements into a string PrinStr and then send this string to the printer?

    Code:
    LOCAL  PrinStr  AS WSTRING
    PrinStr =  "<ESC>%[email protected] LDECLARE LRESOURCE:"+CHR$(34) + flash:+CHR$(34)  + _
                     "LRWLOCK=" + CHR$(34) +PASSWORD+CHR$(34) +  CRLF$ + _
                     "@PJL MICRJOB" + CRLF$ + _
                     "@PJL ENTER LANGUAGE = PCL" + CRLF$ + _
                     "<ESC>&l2a1h6d1e64F"​

    Comment


    • #3
      Originally posted by Mark Gregory View Post
      I am trying to send commands and control codes to a printer.

      Below is an example of some PJL and PCL commands used to print a sample check.

      Does anyone have any suggestions on how to accomplish this in PowerBasic?

      Anne's example is a good one on how to format the data.

      Part of the challenge will be: how is the printer connected, and how are you sending the data? If you are attaching to a COM port, an LPT port, or a \\network\share, you should be able to just use LPRINT ATTACH and send the string. If you are printing through windows (LPRINT ATTACH <printer name>), you may need to put the windows driver into "passthrough" mode so that Windoze and the associated driver does not mung up the data stream. I have to do that with the Zebra barcode label printers we use at work in order to use the native language (ZPL).

      Real programmers use a magnetized needle and a steady hand

      Comment


      • #4
        Originally posted by Bud Durland View Post
        LPRINT ATTACH and send the string. If you are printing through windows (LPRINT ATTACH <printer name>), you may need to put the windows driver into "passthrough" mode so that Windoze and the associated driver does not mung up the data stream.).
        Is that a typo? Did you mean "printing through windows (XPRINT ATTACH <printer name>)"

        Comment


        • #5
          Originally posted by Stuart McLachlan View Post

          Is that a typo? Did you mean "printing through windows (XPRINT ATTACH <printer name>)"
          XPRINT, yes, thanks for the correction. When using a Windows/GDI printer, windows will process the data through the printer driver. If the objective is to send Epson ESC codes or HP PJL codes to the printer, they will get garbled on their way. We use Zebra barcode printers at work, and when they are connected via USB, I have to use XPRINT, and have the driver on the windows machine set to enable "bypass mode" which in this case means wrapping special characters around my text:

          Code:
          ' snippet
          XPrint Attach LabelPrinter
          tStr1 = XPrint$
          If Len(tStr1) = 0 Then
            MsgBox "failed to connect to printer",%MB_OKMODAL,"Printer Error"
          else
            XPrint "${" & LabelData & "}$"
            XPrint Close​​​
          End If
          Some of the Zebras are connected via serial (COM1), and I don't need bypass mode; jsut a couple lines to open the COM port and send the data. Easier yet with LPT1, but we haven't been able to buy a PC with a parallel port in years.
          Real programmers use a magnetized needle and a steady hand

          Comment


          • #6
            I use Windows API OpenPrinter, WritePrinter etc. to send raw data to printers, such as Zebra label printers or Esc/POS printers etc.
            Here a working example (constants in Italian)..
            By the way, some of my customers are using it on old PC, so I just discovered that to work on Windows 10 it's necessary to change a parameter (datatype from RAW to XPS_PASS).
            So I added here a test on Windows version using the first one I found; maybe there are better ways to do this job..
            In the example I use xprint attach choose to select the printer; the printer must be installed in Windows.
            The file to print can contain any kind of control codes.

            Code:
                  #include "win32api.inc"
                  #include "winspool.inc"
                  declare function RtlGetVersion lib "NtDll.dll" alias "RtlGetVersion" (OsVerInfoEx as OSVERSIONINFOEXW) as long
            
            Function PBMain() as long
                  file$=trim$(command$)
                  if file$="" then
                     display openfile 0,,,"File da stampare","","","","",%ofn_filemustexist to file$
                  end if
                  if file$="" then
                     exit function
                  end if
                  xprint attach choose, "Stampa "+file$
                  if xprint$<>"" then
                     lpt$=xprint$
                     xprint cancel
                     call StampaDiretta(file$,lpt$,er$)
                     if er$<>"" then
                        msgbox er$,%mb_iconwarning or %mb_taskmodal,"Stampa "+file$
                     else
                        msgbox "Stampa completata",%mb_iconinformation or %mb_taskmodal,"Stampa "+file$
                     end if
                  end if
                  exit function
            End Function
            
            sub StampaDiretta(fileprn$,printername$,er$)
                  Dim lStatus As Long
                  Dim hPrinter As Dword
                  Dim dmDevmod As DEVMODE
                  Dim lLevel As Dword
                  Dim diDocInfo As DOC_INFO_1
                  Dim lWritten As Long
                  Dim iFile As Integer
                  Dim i As Long
                  Dim Spazi as asciiz * 128
                  Dim sPrinter as asciiz * 128
                  Dim sFileprn as asciiz * 128
                  Dim sFileout as asciiz * 128
                  Dim sDatatype as asciiz * 128
            
                  er$=""
                  if dir$(fileprn$)="" then
                     er$="Archivio "+fileprn$+" non trovato"
                     exit sub
                  end if
            
                  Spazi=space$(127)
                  sPrinter=printername$
                  lStatus=OpenPrinter(sPrinter,hPrinter,ByVal 0&)
                  if lStatus=0 then
                     er$="Errore durante l'apertura stampante "+printername$
                     exit sub
                  end if
            
                  lLevel=1
                  sFileprn=fileprn$
                  sFileout=""
                  if Winver()>=6.2! then ' Windows 8 o maggiore
                     sDatatype="XPS_PASS"
                  else
                     sDatatype="RAW"
                  end if
                  diDocInfo.pDocName=varptr(sFileprn)
                  diDocInfo.pOutputFile=varptr(sFileout)
                  diDocInfo.pDatatype=varptr(sDatatype)
                  lStatus=StartDocPrinter(hPrinter,lLevel,varptr(diDocInfo))
                  if lStatus=0 then
                     AbortPrinter hPrinter
                     er$="Errore durante l'inizio della stampa di "+fileprn$
                     exit sub
                  end if
            
                  lStatus=StartPagePrinter(hPrinter)
                  if lStatus = 0 then
                     AbortPrinter hPrinter
                     er$="Errore durante l'inizio della stampa di "+fileprn$
                     exit sub
                  end if
            
                  iFile=freeFile
                  open fileprn$ for binary as #ifile base=1
                  redim Buffer(1 to &h4000) as Byte
                  for i=1 to lof(iFile)\&h4000
                     get #iFile,,Buffer()
                     lStatus=WritePrinter(hPrinter,Buffer(1),&h4000,lWritten)
                     if lStatus=0 then
                        AbortPrinter hPrinter
                        er$="Errore durante la stampa di "+fileprn$
                        exit sub
                     end if
                  next i
                  if (lof(iFile) mod &h4000)>0 then
                     redim Buffer(1 to (lof(iFile) mod &h4000)) as Byte
                     get #iFile,,Buffer()
                     lStatus=WritePrinter(hPrinter,Buffer(1),lof(iFile) mod &h4000,lWritten)
                     if lStatus=0 then
                        AbortPrinter hPrinter
                        er$="Errore durante la stampa di "+fileprn$
                        exit sub
                     end if
                  end if
                  close #iFile
            
                  lStatus=EndPagePrinter(hPrinter)
                  if lStatus = 0 then
                     AbortPrinter hPrinter
                     er$="Errore durante la chiusura della stampa di "+fileprn$
                     exit sub
                  end if
                  lStatus=EndDocPrinter(hPrinter)
                  if lStatus = 0 then
                     AbortPrinter hPrinter
                     er$="Errore durante la chiusura della stampa di "+fileprn$
                     exit sub
                  end if
                  lStatus=ClosePrinter(hPrinter)
                  if lStatus = 0 then
                     AbortPrinter hPrinter
                     er$="Errore durante la chiusura stampante "+printername$
                     exit sub
                  end if
                  exit sub
            end sub
            
            function Winver() as single
                  local OsInfoEx as osversioninfoexw
                  RtlGetVersion OsInfoEx
                  function=osinfoex.dwmajorversion+osinfoex.dwminorversion / iif(osinfoex.dwminorversion>9,100,10)
            end function
            
            ​

            Comment


            • #7
              Originally posted by Stuart McLachlan View Post

              Is that a typo? Did you mean "printing through windows (XPRINT ATTACH <printer name>)"
              Is LPRINT ATTACH not correct?

              XPRINT uses the windows GDI printer drivers to create an internal image and then sends that bitmap image to the printer.
              LPRINT just sends the codes direct to the printer and lets the printer handle them. That's how PCL printers work.​

              Comment


              • #8
                Originally posted by Paul Dixon View Post
                Is LPRINT ATTACH not correct?

                XPRINT uses the windows GDI printer drivers to create an internal image and then sends that bitmap image to the printer.
                LPRINT just sends the codes direct to the printer and lets the printer handle them. That's how PCL printers work.​
                Yes, it's not correct
                see Post #5, You can't use LPRINT easily with a USB attached device, which is why you have to avoid the GDI page creation when using XPRINT for ZPL etc.

                Comment


                • #9
                  Originally posted by Stuart McLachlan View Post

                  Yes, it's not correct
                  see Post #5, You can't use LPRINT easily with a USB attached device, which is why you have to avoid the GDI page creation when using XPRINT for ZPL etc.
                  It works ok here.
                  I disconnected my network PCL printer, connected it with USB (although the OP doesn't say it needs to be USB connected) and I can send PCL codes to it using LPRINT in PBCC6 and it prints as I expect.​

                  Comment


                  • #10
                    Thanks to everyone for your assistance. I will see what I can do to make this work. Thanks again.

                    Comment

                    Working...
                    X