Printing to a USB Printer

Just starting out? Need help? Post your questions and find answers here.
timwil1963
New User
New User
Posts: 7
Joined: Sat Dec 28, 2019 4:26 pm

Printing to a USB Printer

Post by timwil1963 »

I am totally new to PureBasic having not written a single program yet. I however have had many many years of Visual Basic 6. One of the things I frequently do is print to printers that print bar codes or tickets or special receipts that do not use the regular way of printing and are not based on a page model but rather a line model.

The following is code I use to send raw control codes to a Receipt or Ticket printer in VB6

Code: Select all

Public Declare Function ClosePrinter Lib "winspool.drv" (ByVal hPrinter As Long) As Long
Public Declare Function EndDocPrinter Lib "winspool.drv" (ByVal hPrinter As Long) As Long
Public Declare Function OpenPrinter Lib "winspool.drv" Alias "OpenPrinterA" (ByVal pPrinterName As String, phPrinter As Long, ByVal pDefault As Long) As Long
Public Declare Function StartDocPrinter Lib "winspool.drv" Alias "StartDocPrinterA" (ByVal hPrinter As Long, ByVal Level As Long, pDocInfo As DOCINFO) As Long
Public Declare Function WritePrinter Lib "winspool.drv" (ByVal hPrinter As Long, pBuf As Any, ByVal cdBuf As Long, pcWritten As Long) As Long

Public Type DOCINFO
   pDocName As String
   pOutputFile As String
   pDatatype As String
End Type


Public Sub usbPrint(ByVal DataToBePrinted As String)
   Dim lhPrinter As Long
   Dim lReturn As Long
   Dim lpcWritten As Long
   Dim lDoc As Long
   Dim sWrittenData As String
   Dim MyDocInfo As DOCINFO

   lReturn = OpenPrinter(Printer.DeviceName, lhPrinter, 0)

   If lReturn = 0 Then
      MsgBox "Printer Error!" & Chr(10) & "Please Check Printer!", vbExclamation
      Exit Sub
   End If

   MyDocInfo.pDocName = "Receipt"
   MyDocInfo.pOutputFile = vbNullString
   MyDocInfo.pDatatype = vbNullString
   lDoc = StartDocPrinter(lhPrinter, 1, MyDocInfo)
   sWrittenData = DataToBePrinted
   lReturn = WritePrinter(lhPrinter, ByVal sWrittenData, Len(sWrittenData), lpcWritten)
   lReturn = EndDocPrinter(lhPrinter)
   lReturn = ClosePrinter(lhPrinter)
End Sub
Anyone game on assisting in translation?

From a search I see that it is a common problem. The person with the Zebra printer issue is at the very least one.
IdeasVacuum
Always Here
Always Here
Posts: 6426
Joined: Fri Oct 23, 2009 2:33 am
Location: Wales, UK
Contact:

Re: Printing to a USB Printer

Post by IdeasVacuum »

Welcome to PB!

here: https://www.rsbasic.de/backups/

you will find some User-defined libraries for printing, including bar codes. If those libs include their source code (some do, some don't) then they could help you to define your own.
IdeasVacuum
If it sounds simple, you have not grasped the complexity.
timwil1963
New User
New User
Posts: 7
Joined: Sat Dec 28, 2019 4:26 pm

Re: Printing to a USB Printer

Post by timwil1963 »

This is how it is used

Code: Select all

Public Sub POSPrint(xData As String)
   Dim vbp As Printer

   For Each vbp In Printers
      If vbp.DeviceName = "POS" Then
         Set Printer = vbp
      End If
   Next

   usbPrint xData
End Sub

Public Sub BDPrint(xData As String)
   Dim vbp As Printer

   For Each vbp In Printers
      If vbp.DeviceName = "BD" Then
         Set Printer = vbp
      End If
   Next

   usbPrint xData
End Sub

Public Sub NYPrint(xData As String)
   Dim vbp As Printer

   For Each vbp In Printers
      If vbp.DeviceName = "NY" Then
         Set Printer = vbp
      End If
   Next

   usbPrint xData
End Sub

timwil1963
New User
New User
Posts: 7
Joined: Sat Dec 28, 2019 4:26 pm

Re: Printing to a USB Printer

Post by timwil1963 »

This post worked out to be very helpful viewtopic.php?f=13&t=59132&hilit=winspool.drv

I think I might have it ..... just have to test.....
timwil1963
New User
New User
Posts: 7
Joined: Sat Dec 28, 2019 4:26 pm

Re: Printing to a USB Printer

Post by timwil1963 »

I have been wasting time on this......I guess that is how one learns :D

I found ABBKlaus Printer_Lib and this does exactly what I need - it took a while for me to realize (I found it on purearea.net)

I am making good progress in migrating my Visual Basic Application to Pure Basic - definitely a BIG IMPROVEMENT
IdeasVacuum
Always Here
Always Here
Posts: 6426
Joined: Fri Oct 23, 2009 2:33 am
Location: Wales, UK
Contact:

Re: Printing to a USB Printer

Post by IdeasVacuum »

Hi timwil1963

You will find it suddenly gets a lot easier and you are on your way. PB is better than VB imho.

ABBKlaus also took care of an excellent PDF library, I use it all the time.
IdeasVacuum
If it sounds simple, you have not grasped the complexity.
Post Reply