Author Topic: ESC/POS 列印指令的使用方法(程式碼取自 CashierXtra)  (Read 13972 times)

admin

  • Administrator
  • *****
  • Posts: 0
    • View Profile
在REALBasic 環境下使用ESC/POS Command 直接輸出至POS 印表機, 最簡單的方法是購買 MonkeyBread MBS Real Studio Win Plugin, 下面是筆者的使用範例 :

http://www.monkeybreadsoftware.net/plugins-mbsrealstudiowinplugin.shtml

Code: [Select]
Private Sub PrintReceipt(optional tmpOpenDrawer as boolean=false)
  dim w as new WindowsAddPrintJobMBS
  dim tmpMaxColumnWidth as integer
  dim intY as integer
 
  if gintOperateMode=1 then
    gintCurrReceiptType=0
  else
    gintCurrReceiptType=1
  end if
 
  if pintFormMode=3 then
    gstrposdocname=lblQryMain(2).Caption
  else
    gstrposdocname=lblMain(2).Caption
  end if
 
  if not w.OpenPrinter(gstrPOSPrinter) then
    'MsgBox "OpenPrinter failed. Is the printer name correct in the source code?"
    call winMessage.ShowMsg(1,31)
    Return
  end if
 
  if not w.StartDocPrinter(gstrPOSDocName, w.kDataFormatRAW) then
    call winMessage.ShowMsg(1,32)
    'MsgBox "StartDocPrinter failed."
    Return
  end if
 
 
  if pintFormMode=3 then
    garyReceiptHeader(1,0)=ConvertEncoding(lblQryMain(0).Caption ,gprinterEncoding)
    garyReceiptHeader(1,1)=ConvertEncoding(lblQryMain(1).Caption,gprinterEncoding)
    garyReceiptHeader(1,2)=ConvertEncoding(lblQryMain(2).Caption,gprinterEncoding)
   
  else
    garyReceiptHeader(1,0)=ConvertEncoding(lblMain(0).Caption,gprinterEncoding)
    garyReceiptHeader(1,1)=ConvertEncoding(lblMain(1).Caption,gprinterEncoding)
    garyReceiptHeader(1,2)=ConvertEncoding(lblMain(2).Caption,gprinterEncoding)
   
  end if
 
  select case gintReceiptWidth
  case 58
    tmpMaxColumnWidth=22
   
  case else
    tmpMaxColumnWidth=40
   
  end select
 
  garyReceiptFooter(1,0)=ConvertEncoding(lblMain(3).Caption,gprinterEncoding)
  garyReceiptFooter(1,1)=ConvertEncoding(lblMain(4).Caption,gprinterEncoding)
  garyReceiptFooter(1,2)=ConvertEncoding(lblMain(5).Caption,gprinterEncoding)
 
  call w.StartPagePrinter
  call w.writeprinter chr(27)+"@"
  call w.writeprinter chr(27)+"!"+ chr(1)
  call w.writeprinter chr(&H1c)+ chr(&h26)
 
  if gblnReceiptLogo=true then
    call w.writeprinter chr(27) +"a" + chr(1)
    call w.WritePrinter chr(&h1b)+"u"+chr(1)
    call w.WritePrinter chr(&H1c) +"p" +chr(1) +chr(0)
  end if
 
  PrintReceiptHeader w
 
  call w.writeprinter chr(27) +"a" + chr(0)
 
  if lstDetails.ListCount >0 then
    for inty=0 to lstDetails.ListCount-1
      call w.WritePrinter chr(27)+"$" +chr(1)+chr(0)
      call w.WritePrinter AdjustTextWidth(lstdetails.cell(inty,cntSDShowDescription),tmpMaxColumnWidth).ConvertEncoding(gPrinterEncoding)
     
      select case gintReceiptWidth
      case 58
        call w.WritePrinter chr(27)+"$"+chr(220)+chr(0)
        call w.WritePrinter AlignRightLineText(lstdetails.cell(inty,cntSDQty),3)
        call w.WritePrinter chr(27)+"$"+chr(40)+chr(256)
        call w.WritePrinter AlignRightLineText(lstdetails.cell(inty,cntSDNetamount),6) + chr(&ha)
       
      case else
        call w.WritePrinter chr(27)+"$"+chr(80)+chr(256)
        call w.WritePrinter AlignRightLineText(lstdetails.cell(inty,cntSDQty),3)       
        call w.WritePrinter chr(27)+"$"+chr(150)+chr(256)
        call w.WritePrinter AlignRightLineText(lstdetails.cell(inty,cntSDNetamount),7) + chr(&ha)
       
      end select
     
    next
   
  end if
 
  PrintReceiptFooter w
   
  call w.EndPagePrinter
 
  '必須放在開Drawer之前, 否則不能切紙
  if gblnReceiptAutoCut=true then
    call w.WritePrinter chr(&h1d)+"V"+ chr(66)+chr(0)
  else
    call w.writeprinter chr(27)+"d"+chr(5)
  end if
 
 
  if gblnEnableDrawer=true then
    if tmpOpenDrawer=true then
      call w.WritePrinter chr(&h1b)+chr(&h70)+chr(&h0) +chr(&H60)+chr(&h120)
    end if
  end if
   
  call w.EndDocPrinter
  w = nil // close printer
 


Code: [Select]
Sub PrintReceiptHeader(tmpPrintJob as WindowsAddPrintJobMBS)
  dim intY as integer
 
  call tmpPrintJob.writeprinter chr(27)+"@"
  call tmpPrintJob.writeprinter chr(27)+"!"+chr(0)
  call tmpPrintJob.writeprinter chr(27)+"a"+chr(1)
 
  call tmpPrintJob.writeprinter chr(&h1b)+"!"+chr(9)
  call tmpPrintJob.WritePrinter ConvertEncoding(gstrCompanyConfig(0),gPrinterEncoding)+ chr(&ha)
  call tmpPrintJob.writeprinter chr(&h1b)+"!"+chr(0)
  call tmpprintjob.WritePrinter ConvertEncoding(garyReceiptstring(0) + gstrCompanyConfig(3),gPrinterEncoding)+ chr(&ha)
  call tmpprintjob.WritePrinter ConvertEncoding(garyReceiptstring(1) + gstrCompanyConfig(4),gPrinterEncoding)+ chr(&ha)
 
  for inty=0 to 2
    if len( garyReceiptHeading(inty)) >0 then
      call tmpPrintJob.writeprinter ConvertEncoding(garyReceiptHeading(inty),gPrinterEncoding)+ chr(&ha)
    end if
  next
 
  call tmpPrintJob.writeprinter chr(27)+"d"+chr(1)
 
  call tmpPrintJob.writeprinter chr(&h1b)+"!"+chr(17)
  call tmpPrintJob.writeprinter chr(&h1c)+"!"+chr(8)
  call tmpPrintJob.WritePrinter garyReceiptTitle(gintCurrReceiptType) + chr(&ha)
  call tmpPrintJob.writeprinter chr(&h1b)+"!"+chr(0)
  call tmpPrintJob.writeprinter chr(&h1c)+"!"+chr(0)
 
  call tmpPrintJob.writeprinter chr(27)+"d"+chr(1)
 
  call tmpPrintJob.writeprinter chr(27)+"a"+chr(0)
  call tmpPrintJob.WritePrinter garyReceiptHeader(0,0) + garyReceiptHeader(1,0) +chr(&ha)
  call tmpPrintJob.WritePrinter garyReceiptHeader(0,1) + garyReceiptHeader(1,1) +chr(&ha)
  call tmpPrintJob.WritePrinter garyReceiptHeader(0,2) + garyReceiptHeader(1,2) +chr(&ha)
 
  call tmpPrintJob.writeprinter chr(27)+"d"+chr(1)
 
  call tmpPrintJob.writeprinter chr(27)+"a"+chr(0)
 
  call tmpPrintJob.WritePrinter chr(27)+"$" +chr(20)+chr(0)
  call tmpPrintJob.WritePrinter garyReceiptDetailsHeader(0)
 
  select case gintReceiptWidth
  case 58
    call tmpPrintJob.WritePrinter chr(27)+"$" +chr(220)+chr(0)
    call tmpPrintJob.WritePrinter garyReceiptDetailsHeader(1)   
    call tmpPrintJob.WritePrinter chr(27)+"$" +chr(50)+chr(256)
    call tmpPrintJob.WritePrinter garyReceiptDetailsHeader(2) + chr(&ha)   
    call tmpPrintJob.WritePrinter gstrSeparateLine + chr(&ha)
   
  case else
    call tmpPrintJob.WritePrinter chr(27)+"$" +chr(80)+chr(256)
    call tmpPrintJob.WritePrinter garyReceiptDetailsHeader(1)   
    call tmpPrintJob.WritePrinter chr(27)+"$" +chr(160)+chr(256)
    call tmpPrintJob.WritePrinter garyReceiptDetailsHeader(2) + chr(&ha)   
    call tmpPrintJob.WritePrinter gstrSeparateLine + chr(&ha)
   
  end select
 
End Sub


Code: [Select]
Sub PrintReceiptFooter(tmpPrintJob as WindowsAddPrintJobMBS)
  dim intY as integer
 
  call tmpPrintJob.writeprinter gstrSeparateLine+ chr(&ha)
  call tmpPrintJob.writeprinter chr(&h1b)+"!"+chr(17)
  call tmpPrintJob.writeprinter chr(&h1c)+"!"+chr(8)
 
  call tmpPrintJob.WritePrinter chr(27)+"$" +chr(1)+chr(0)
  call tmpPrintJob.WritePrinter garyReceiptFooter(0,0)
 
  select case gintReceiptWidth
  case 58
    call tmpPrintJob.WritePrinter chr(27)+"$" +chr(20)+chr(256)
  case else
    call tmpPrintJob.WritePrinter chr(27)+"$" +chr(150)+chr(256)
  end select
  call tmpPrintJob.WritePrinter AlignRightLineText(garyReceiptFooter(1,0),9) +chr(&ha)
 
  call tmpPrintJob.writeprinter chr(&h1b)+"!"+chr(0)
  call tmpPrintJob.writeprinter chr(&h1c)+"!"+chr(0)
  call tmpPrintJob.writeprinter gstrSeparateLine+ chr(&ha)
 
  if gintCurrReceiptType=0 then
    call tmpPrintJob.WritePrinter chr(27)+"$" +chr(1)+chr(0)
    call tmpPrintJob.WritePrinter garyReceiptFooter(0,1)
   
    select case gintReceiptWidth
    case 58
      call tmpPrintJob.WritePrinter chr(27)+"$" +chr(20)+chr(256)
      call tmpPrintJob.WritePrinter AlignRightLineText(garyReceiptFooter(1,1),7) +chr(&ha)
     
    case else
      call tmpPrintJob.WritePrinter chr(27)+"$" +chr(150)+chr(256)
      call tmpPrintJob.WritePrinter AlignRightLineText(garyReceiptFooter(1,1),7) +chr(&ha)
     
    end select
   
    call tmpPrintJob.WritePrinter chr(27)+"$" +chr(1)+chr(0)
    call tmpPrintJob.WritePrinter garyReceiptFooter(0,2)
   
    select case gintReceiptWidth
    case 58
      call tmpPrintJob.WritePrinter chr(27)+"$" +chr(20)+chr(256)
      call tmpPrintJob.WritePrinter AlignRightLineText(garyReceiptFooter(1,2),7) +chr(&ha)+chr(&ha)
     
    case else
      call tmpPrintJob.WritePrinter chr(27)+"$" +chr(150)+chr(256)
      call tmpPrintJob.WritePrinter AlignRightLineText(garyReceiptFooter(1,2),7) +chr(&ha)+chr(&ha)
     
    end select
   
  else
    call tmpPrintJob.writeprinter chr(27)+"d"+chr(4)
    call tmpPrintJob.writeprinter chr(&h1b)+"!"+chr(17)
    call tmpPrintJob.writeprinter chr(&h1c)+"!"+chr(8)
    call tmpprintjob.WritePrinter ConvertEncoding(garyReceiptstring(2) + gstrSignatureLine,gPrinterEncoding)+ chr(&ha)+chr(&ha)
  end if
 
 
  call tmpPrintJob.writeprinter chr(27)+"a"+chr(1)
  call tmpPrintJob.writeprinter chr(&h1b)+"!"+chr(0)
  call tmpPrintJob.writeprinter chr(&h1c)+"!"+chr(0)
 
  for inty=0 to 2
    if len( garyReceiptEnding(inty)) >0 then
      call tmpPrintJob.writeprinter ConvertEncoding(garyReceiptEnding(inty),gPrinterEncoding)+ chr(&ha)
    end if
  next
 
 
End Sub


Code: [Select]
Function AlignRightLineText(tmpString as string,TextWidth as integer) As string
  dim tmpResult as string
  tmpResult=tmpString
  if (lenb(tmpstring) >0) and (lenb(tmpstring) <textwidth) then
    while lenb(tmpResult) < textwidth
      tmpResult=" "+tmpResult
    wend
   
  end if
 
  return tmpResult
End Function

« Last Edit: August 17, 2012, 05:36:34 PM by Roy Chan »