I have written this code quite a long time ago for a Point of Sale system. I was working fine with both Epson POS TM-U220 and Samsung BIXOLON SRP270A Receipt Printer. However this code must work with any ESC/POS standard unit.
Since the commands are sent directly to either parallel or serial port, there is no requirement to install the driver. However as a practice I used to install it to ease the testing of the ports with the printer.
Also note that the code to operate the Cash Drawer is also included.
Code: Select all
'/******************************************************************************************************
'*
'* Project : POS
'* File : ESC_P.c
'* Description : ESC POS commands for ESC/POS standard receipt printers
'* Author : Neo
'* Copyright : ROBOT.LK Labs (https://robot.lk)
'*
'*
'* Version Date Author Change/Reason
'* ------- ---------- -------------------- -------------------------------------------------------
'* 1.0 10/02/2005 Neo Created
'*******************************************************************************************************/
Option Explicit
'ESC/POS constants standard commanding to receipt printer
Private Const LF As Byte = &HA
Private Const ESC As Byte = &H1B
Private Const GS As Byte = &H1D
'ESC/POS constants standard commanding to customer display
Private Const NLL As Byte = &H0
Private Const MD1 As Byte = &H1
Private Const MD2 As Byte = &H2
Private Const MD3 As Byte = &H3
Private Const MD4 As Byte = &H4
Private Const MD5 As Byte = &H5
Private Const MD6 As Byte = &H6
Private Const MD7 As Byte = &H7
Private Const MD8 As Byte = &H8
Private Const BS As Byte = &H8
Private Const HT As Byte = &H9
'Private Const LF As Byte = &HA
Private Const HOM As Byte = &HB
Private Const CLR As Byte = &HC
Private Const CR As Byte = &HD
Private Const SLE1 As Byte = &HE
Private Const rs As Byte = &HF
Private Const SLE2 As Byte = &HF
Private Const DLE As Byte = &H10
Private Const DC1 As Byte = &H11
Private Const DC2 As Byte = &H12
Private Const DC3 As Byte = &H13
Private Const DC4 As Byte = &H14
Private Const CAN As Byte = &H18
'Private Const ESC As Byte = &H1B
Private Const SF1 As Byte = &H1E
Private Const US As Byte = &H1F, SF2 As Byte = &H1F
'Customer display scrolling control variables
Private loc1 As Byte
Private loc2 As Byte
Private wMsg As String
Private nMsg As String
'Class variables
Private IORP As IO
Private IODR As IO
Private IOCD As IO
'common use for printing funtions
Private tmp As String, ret As Integer
Private font As Byte, Height As Byte, Width As Byte, Emp As Byte, Colour As Byte
Private ADOCon As Connection
Private rec_no As String
Private DRStatus As Byte '0-no drawer 1 - stand alone 2-with printer
Private CDStatus As Byte '0-no display 1 - stand alone 2-with drawer 3-with printer
Public Sub initVar()
DRStatus = 0
CDStatus = 0
wMsg = "Welcome to ROBOT.LK.org, Sharing is a passion * "
End Sub
Public Sub initRP(rp As IO, newCon As DBCon)
Set IORP = rp
IORP.WriteString (Chr(ESC) & "@") 'ESC @ - initialise code
lineFeed
Set ADOCon = newCon.con
End Sub
Public Sub initDR(dr As IO)
Set IODR = dr
End Sub
Public Sub initCD(cd As IO)
Set IOCD = cd
End Sub
Public Property Let gdrStatus(cdrStatus As Byte)
DRStatus = cdrStatus
End Property
Public Property Let gcdStatus(ccdStatus As Byte)
CDStatus = ccdStatus
'0-no display 1 - stand alone 2-with drawer 3-with printer
If CDStatus = 2 Then
Set IOCD = IODR
ElseIf CDStatus = 3 Then
Set IOCD = IORP
End If
End Property
'*********** ESC/POS standard commanding to receipt printer *************
Private Function selectDevice(dv As Byte) As String '1 - printer 2 - display
selectDevice = Chr(ESC) & "=" & Chr(dv)
End Function
Private Function lineFeed() As String 'print and feed line
lineFeed = Chr(LF)
End Function
Private Function lineFeedN(n As Byte) As String 'print and feed n lines
lineFeedN = Chr(ESC) & "d" & Chr(n)
End Function
Private Function selectMode(cFont As Byte, cEmp As Byte, cDheight As Byte, cDwidth As Byte, cUnderline As Byte, cColour As Byte) As String
Dim cBit As Byte
Static preColour As Byte
'Bit positions
'7-underline 6-reserved 5-double width 4-double height 3-emphazise 2&1-reserved 0-font A or B
If font = 2 Then
'select fontA
cBit = cUnderline * (2 ^ 7) + 0 + cDwidth * (2 ^ 5) + cDheight * (2 ^ 4) + cEmp * (2 ^ 3) + 0 + 0 + 0 * (2 ^ 0)
selectMode = Chr(ESC) & "!" & Chr(cBit)
selectMode = selectMode & Chr(ESC) & "M" & Chr(2) 'font c selected
Else
cBit = cUnderline * (2 ^ 7) + 0 + cDwidth * (2 ^ 5) + cDheight * (2 ^ 4) + cEmp * (2 ^ 3) + 0 + 0 + cFont * (2 ^ 0)
selectMode = Chr(ESC) & "!" & Chr(cBit)
End If
'0,48-colour 1 1,49-colour 2
If preColour <> cColour Then
selectMode = selectMode & Chr(ESC) & "r" & Chr(cColour) 'setting colour
preColour = cColour
End If
End Function
'Private Function setULine(n As Byte) As String
''0,48-turn off 1,49-turn on set at 1-dot width 2,50-turn on set at 2-dot width
'setULine = Chr(ESC) & "-" & Chr(n)
'End Function
'Private Function setEmp(n As Byte) As String
''LSB of n is 0, emphasized mode is turned off.
''LSB of n is 1, emphasized mode is turned on.
'setEmp = Chr(ESC) & "-" & Chr(n)
'End Function
'Private Function setDstrike(n As Byte) As String
''LSB of n is 0, double-strike mode is turned off.
''LSB of n is 1, double-strike mode is turned on.
'setDstrike = Chr(ESC) & "G" & Chr(n)
'End Function
'Private Function setFont(n As Byte) As String
''0,48-font A 1,49-font B 2,50-font C
'setFont = Chr(ESC) & "M" & Chr(n)
'End Function
Private Function setJust(n As Byte) As String
'0,48-left A 1,49-centre B 2,50-right
setJust = Chr(ESC) & "a" & Chr(n)
End Function
Private Function genPulse(pin As Byte) As String
'0,48-pin 2 1,49-pin 5
genPulse = Chr(ESC) & "p" & Chr(pin) & Chr(50) & Chr(50)
End Function
'Private Function setColour(n As Byte) As String
''0,48-colour 1 1,49-colour 2
'setColour = Chr(ESC) & "r" & Chr(n)
'End Function
'Private Function setCut() As String 'modified according to Samsung Manual
'setCut = Chr(ESC) & "m"
'End Function
Private Function setCut(m As Byte) As String
'0,48-full cut 1,49-half cut
setCut = Chr(GS) & "V" & Chr(m)
End Function
'Private Function setCutN(m As Byte, n As Byte) As String
''execute paper (cutting position + N x verticle motion unit)
''65-full cut 66-half cut
'setCut = Chr(GS) & "V" & Chr(m) & Chr(n)
'End Function
Please make sure that you post a reply to say thanks if it works okay for you.
See also:
ESC/POS Customer Display source code at https://robot.lk/viewtopic.php?f=39&t=875
Point of Sale printer/drwer and Customer Display source code at https://robot.lk/viewtopic.php?f=39&t=876
Barcode printer source code at https://robot.lk/viewtopic.php?f=39&t=877