Dester Dezzods
Dester Dezzods

Reputation: 1545

Open cash drawer with printer connected to USB in vb.net

I was designing a simple pos application but I got stuck on a problem. I have a cash drawer connected to a thermal printer (tsp100 from star micronics) which is then connected to my computer with usb. can anyone help me how to just open the cash drawer (without printing anything) from vb.net. ?

Upvotes: 1

Views: 17006

Answers (3)

DenysT
DenysT

Reputation: 1

This worked for me on a Munbyn Printer. Note. I created a NEW module with the Imports System.Runtime.InteropServices & Module mdlprint Then put the Public Sub OpenCashdrawer() In my own Public Class Main

Upvotes: 0

James Pang
James Pang

Reputation: 71

It used to be very simple for LPT port, with less than 5 lines. For USB receipt printer, I found this VB solution elsewhere which works fine for me.

To open cash drawer, just execute OpenCashdrawer

Imports System.Runtime.InteropServices

Module mdlprint
   Public Class RawPrinter
      ' ----- Define the data type that supplies basic print job information to the spooler.
      <StructLayout(LayoutKind.Sequential, CharSet:=CharSet.Unicode)> _
      Public Structure DOCINFO
         <MarshalAs(UnmanagedType.LPWStr)> _
         Public pDocName As String
         <MarshalAs(UnmanagedType.LPWStr)> _
         Public pOutputFile As String
         <MarshalAs(UnmanagedType.LPWStr)> _
         Public pDataType As String
      End Structure

      ' ----- Define interfaces to the functions supplied in the DLL.
      <DllImport("winspool.drv", EntryPoint:="OpenPrinterW", SetLastError:=True, CharSet:=CharSet.Unicode, ExactSpelling:=True, CallingConvention:=CallingConvention.StdCall)> _
      Public Shared Function OpenPrinter(ByVal printerName As String, ByRef hPrinter As IntPtr, ByVal printerDefaults As Integer) As Boolean
      End Function

      <DllImport("winspool.drv", EntryPoint:="ClosePrinter", SetLastError:=True, CharSet:=CharSet.Unicode, ExactSpelling:=True, CallingConvention:=CallingConvention.StdCall)> _
      Public Shared Function ClosePrinter(ByVal hPrinter As IntPtr) As Boolean
      End Function

      <DllImport("winspool.drv", EntryPoint:="StartDocPrinterW", SetLastError:=True, CharSet:=CharSet.Unicode, ExactSpelling:=True, CallingConvention:=CallingConvention.StdCall)> _
      Public Shared Function StartDocPrinter(ByVal hPrinter As IntPtr, ByVal level As Integer, ByRef documentInfo As DOCINFO) As Boolean
      End Function

      <DllImport("winspool.drv", EntryPoint:="EndDocPrinter", SetLastError:=True, CharSet:=CharSet.Unicode, ExactSpelling:=True, CallingConvention:=CallingConvention.StdCall)> _
      Public Shared Function EndDocPrinter(ByVal hPrinter As IntPtr) As Boolean
      End Function

      <DllImport("winspool.drv", EntryPoint:="StartPagePrinter", SetLastError:=True, CharSet:=CharSet.Unicode, ExactSpelling:=True, CallingConvention:=CallingConvention.StdCall)> _
      Public Shared Function StartPagePrinter(ByVal hPrinter As IntPtr) As Boolean
      End Function

      <DllImport("winspool.drv", EntryPoint:="EndPagePrinter", SetLastError:=True, CharSet:=CharSet.Unicode, ExactSpelling:=True, CallingConvention:=CallingConvention.StdCall)> _
      Public Shared Function EndPagePrinter(ByVal hPrinter As IntPtr) As Boolean
      End Function

      <DllImport("winspool.drv", EntryPoint:="WritePrinter", SetLastError:=True, CharSet:=CharSet.Unicode, ExactSpelling:=True, CallingConvention:=CallingConvention.StdCall)> _
      Public Shared Function WritePrinter(ByVal hPrinter As IntPtr, ByVal buffer As IntPtr, ByVal bufferLength As Integer, ByRef bytesWritten As Integer) As Boolean
      End Function

      Public Shared Function PrintRaw(ByVal printerName As String, ByVal origString As String) As Boolean
         ' ----- Send a string of  raw data to  the printer.
         Dim hPrinter As IntPtr
         Dim spoolData As New DOCINFO
         Dim dataToSend As IntPtr
         Dim dataSize As Integer
         Dim bytesWritten As Integer

         ' ----- The internal format of a .NET String is just
         '       different enough from what the printer expects
         '       that there will be a problem if we send it
         '       directly. Convert it to ANSI format before
         '       sending.
         dataSize = origString.Length()
         dataToSend = Marshal.StringToCoTaskMemAnsi(origString)

         ' ----- Prepare information for the spooler.
         spoolData.pDocName = "OpenDrawer" ' class='highlight'
         spoolData.pDataType = "RAW"

         Try
            ' ----- Open a channel to  the printer or spooler.
            Call OpenPrinter(printerName, hPrinter, 0)

            ' ----- Start a new document and Section 1.1.
            Call StartDocPrinter(hPrinter, 1, spoolData)
            Call StartPagePrinter(hPrinter)

            ' ----- Send the data to the printer.
            Call WritePrinter(hPrinter, dataToSend, _
               dataSize, bytesWritten)

            ' ----- Close everything that we opened.
            EndPagePrinter(hPrinter)
            EndDocPrinter(hPrinter)
            ClosePrinter(hPrinter)
            PrintRaw = True
         Catch ex As Exception
            MsgBox("Error occurred: " & ex.ToString)
            PrintRaw = False
         Finally
            ' ----- Get rid of the special ANSI version.
            Marshal.FreeCoTaskMem(dataToSend)
         End Try
      End Function
   End Class
End Module

Public Class Main
   Public Sub OpenCashdrawer()
      'Modify DrawerCode to your receipt printer open drawer code
      Dim DrawerCode As String = Chr(27) & Chr(112) & Chr(48) & Chr(64) & Chr(64)
      'Modify PrinterName to your receipt printer name
      Dim PrinterName As String = "Your receipt printer name"

      RawPrinter.PrintRaw(PrinterName, DrawerCode)
   End Sub
End Class

Upvotes: 7

David Schwartz
David Schwartz

Reputation: 182827

Just follow the documentation and send the sequence of codes needed to open the drawer. You can look up sequences here if you don't have the documentation for your printer. I believe that on your printer, just sending an ASCII bell character (decimal 7) will open the drawer.

Upvotes: 2

Related Questions