2021年9月28日 星期二

EscPos class

 參考來源

--

Public Class EscPos


'if you want to use decimal values in your methods.

Public Function intTobyte(data As Integer()) As Byte()


Dim byteData As Byte() = data.[Select](Function(x) CType(x, Byte)).ToArray()

' coonvert int array to byte

Return byteData

End Function


'initialize printer

'ESC @

Public Sub initializePrinter(szPrinterName As String)


Dim command As Integer() = {27, 64}

RawPrinterHelper.SendBytesToPrinter(szPrinterName, intTobyte(command))

End Sub



'print text


Public Function printText(szPrinterName As String, data As String) As Boolean

'for more character sets: http://www.ascii-codes.com/


'for another charsets: 

'Encoding ascii = Encoding.GetEncoding("ascii");

'Encoding windows = Encoding.GetEncoding("Windows-1252");


'you must use this encoding  for  brazilian portuguese special characters: ^,~,ç,á...

Dim brazilian As Encoding = Encoding.GetEncoding("IBM860")

Dim byteData As Byte() = brazilian.GetBytes(data)


RawPrinterHelper.SendBytesToPrinter(szPrinterName, byteData)





Return True

End Function


' Print Position Commands

'ESC a n

Public Function SelectJustification(szPrinterName As String, justification_code As Integer) As Boolean


'0= default 

'48 left

'1,49 centering

'2,50 right


Dim align As Integer() = {27, 97, justification_code}

RawPrinterHelper.SendBytesToPrinter(szPrinterName, intTobyte(align))


Return True

End Function






'Character Control Commands


'use this mode to cancel another mode.

Public Function normalModeText(szPrinterName As String) As Boolean

Dim normal As Integer() = {27, 33, 0}

RawPrinterHelper.SendBytesToPrinter(szPrinterName, intTobyte(normal))


Return True

End Function


'Character font A (12 × 24) selected.

Public Function charFontAText(szPrinterName As String) As Boolean


Dim fontA As Integer() = {27, 33, 0}

RawPrinterHelper.SendBytesToPrinter(szPrinterName, intTobyte(fontA))



Return True

End Function


'Character font B (9 × 17) selected.

Public Function charFontBText(szPrinterName As String) As Boolean

Dim fontB As Integer() = {27, 33, 1}

RawPrinterHelper.SendBytesToPrinter(szPrinterName, intTobyte(fontB))



Return True


End Function


'Emphasized mode is turned on

Public Function emphasizedModeText(szPrinterName As String) As Boolean

Dim mode As Integer() = {27, 33, 8}

RawPrinterHelper.SendBytesToPrinter(szPrinterName, intTobyte(mode))

Return True

End Function


'Double-height selected.

Public Function doubleHeightText(szPrinterName As String) As Boolean

Dim height As Integer() = {27, 33, 16}

RawPrinterHelper.SendBytesToPrinter(szPrinterName, intTobyte(height))


Return True

End Function

'Double-width selected.

Public Function DoubleWidthText(szPrinterName As String) As Boolean

Dim width As Integer() = {27, 33, 32}

RawPrinterHelper.SendBytesToPrinter(szPrinterName, intTobyte(width))

Return True

End Function



'Underline mode is turned on

Public Function UnderlineModeText(szPrinterName As String) As Boolean

Dim underline As Integer() = {27, 33, 128}

RawPrinterHelper.SendBytesToPrinter(szPrinterName, intTobyte(underline))

Return True

End Function



'print and Line feed

Public Function lineFeed(szPrinterName As String, numLines As Integer) As Boolean

' fucntion LF 

Dim lf As Integer() = {10}

Dim i As Integer = 1

While i <= numLines

RawPrinterHelper.SendBytesToPrinter(szPrinterName, intTobyte(lf))

i += 1

End While

Return True

End Function


'Generate pulse in Real Time

Public Function drawerKick(szPrinterName As String) As Boolean

' function DLE DC4 fn m t (fn=1)



Dim pulse As Integer() = {27, 112, 0, 100, 200}

RawPrinterHelper.SendBytesToPrinter(szPrinterName, intTobyte(pulse))



Return True

End Function


'execute test print

Public Function testPrint(szPrinterName As String) As Boolean

'function GS ( A pL pH n m


Dim test As Integer() = {29, 40, 65, 2, 0, 0, _

2}

RawPrinterHelper.SendBytesToPrinter(szPrinterName, intTobyte(test))


Return True

End Function



'Select an international character set

Public Function charSet(szPrinterName As String, language As Integer) As Boolean

'function ESC R n

'0-USA

'12-Latin America

'

Dim char_set As Integer() = {27, 82, language}


RawPrinterHelper.SendBytesToPrinter(szPrinterName, intTobyte(char_set))


Return True



End Function



'select character code table

Public Function codeTable(szPrinterName As String, language As Integer) As Boolean

'function Esc t n

' 0 - PC437 (USA: Standard Europe)]

' 40 [ISO8859-15 (Latin9)]

' 3 [PC860 (Portuguese)]


Dim code As Integer() = {27, 116, language}

RawPrinterHelper.SendBytesToPrinter(szPrinterName, intTobyte(code))



Return True

End Function


'Select cut mode and cut paper

Public Function CutPaper(szPrinterName As String) As Boolean

'hex 1D 56 m, m =0,1,48,49


Dim cut As Integer() = {29, 86, 0}

RawPrinterHelper.SendBytesToPrinter(szPrinterName, intTobyte(cut))


Return True

End Function



'activate printer buzzer

Public Function buzzer(szPrinterName As String) As Boolean




'hex data = "1b 28 41 05 00 61 64 03 0a 0a";

Dim buzzer As Integer() = {27, 40, 65, 5, 0, 97, _

100, 3, 10, 10}


RawPrinterHelper.SendBytesToPrinter(szPrinterName, intTobyte(buzzer))

' RawPrinterHelper.SendASCiiToPrinter(szPrinterName, data);

Return True


End Function


'*************************** barcode  commands **********************************


'GS h n - sets bar the height of bar code to n dots.

Public Function barcode_height(szPrinterName As String, Optional range As Integer = 162) As Boolean

' default = 162


'range 1 ≤ n ≤ 255

Dim height As Integer() = {29, 104, range}

RawPrinterHelper.SendBytesToPrinter(szPrinterName, intTobyte(height))

Return True

End Function


' GS w n Set bar code width

Public Function barcode_width(szPrinterName As String, Optional range As Integer = 3) As Boolean

'range = 2 ≤ n ≤ 6

Dim width As Integer() = {29, 119, range}

RawPrinterHelper.SendBytesToPrinter(szPrinterName, intTobyte(width))

Return True

End Function


'GS f n Select a font for the HRI characters when printing a bar code.

Public Function barcodeHRI_chars(szPrinterName As String, Optional font_code As Integer = 0) As Boolean

'default 0 

'[Range] n = 0, 1, 48, 49

Dim hri As Integer() = {29, 102, font_code}



RawPrinterHelper.SendBytesToPrinter(szPrinterName, intTobyte(hri))

Return True

End Function



'GS H n Select print position of HRI characters

Public Function barcodeHRIPostion(szPrinterName As String, Optional position_code As Integer = 1) As Boolean

'default = 0

'[Range] 0 ≤ n ≤ 3, 48 ≤ n ≤ 51 


Dim print_position As Integer() = {29, 72, position_code}


RawPrinterHelper.SendBytesToPrinter(szPrinterName, intTobyte(print_position))

Return True



End Function


'GS k Print barcode

'<Function A>

Public Function printBarcode(szPrinterName As String, data As String, Optional type As Integer = 2) As Boolean

'for this example 2 = JAN/EAN13

Dim barcode As Integer() = {29, 107, type}

RawPrinterHelper.SendBytesToPrinter(szPrinterName, intTobyte(barcode))


RawPrinterHelper.SendStringToPrinter(szPrinterName, data)

Dim nul As Integer() = {0}

' null char at the end.

RawPrinterHelper.SendBytesToPrinter(szPrinterName, intTobyte(nul))

Return True

End Function


'GS k Print Barcode

' <Function B>

Public Function printBarcodeB(szPrinterName As String, data As String, Optional type As Integer = 73) As Boolean

'for this example 73 = CODE128

Dim size As Integer = CType(data.Length, Integer)

'  the number of bytes of bar code data

Dim barcode As Integer() = {29, 107, type, size}

RawPrinterHelper.SendBytesToPrinter(szPrinterName, intTobyte(barcode))

RawPrinterHelper.SendStringToPrinter(szPrinterName, data)



Return True

End Function


'*************************** barcode  commands **********************************




'function to print Qrcode

Public Function printQrcode(Strdata As String, szPrinterName As String) As Boolean






Dim length As Integer = Strdata.Length + 3

'  string size  + 3

'int length = Strdata.Length; 

Dim length_low_byte As Byte = 0, length_high_byte As Byte = 0

length_low_byte = CType((length And &Hff), Byte)

'low byte used in function 180 

length_high_byte = CType(((length >> 8) And &Hff), Byte)

'high byte in function 180 


'if you don't want to use shift operator:

'int length_low_byte = length % 256;

'int length_high_byte = length / 256;



initializePrinter(szPrinterName)


'<Function ESC a n> Select justification  

Dim escAn As Integer() = {27, 97, 0}

RawPrinterHelper.SendBytesToPrinter(szPrinterName, intTobyte(escAn))




'<Function GS L> Set left margin

Dim fGsl As Integer() = {29, 76, 0, 0}

RawPrinterHelper.SendBytesToPrinter(szPrinterName, intTobyte(fGsl))


'<Function 165> GS ( k p L p H cn fn n (cn = 49,fn = 65)  QR Code: Select the model

Dim f165 As Integer() = {29, 40, 107, 4, 0, 49, _

65, 50, 0}

RawPrinterHelper.SendBytesToPrinter(szPrinterName, intTobyte(f165))



'<Function 167> GS ( k pL pH cn fn n (cn = 49, fn = 67) QR Code: Set the size of module

Dim f167 As Integer() = {29, 40, 107, 3, 0, 49, _

67, 4}

'  size of qrcode:  1-16

RawPrinterHelper.SendBytesToPrinter(szPrinterName, intTobyte(f167))




'<Function 169> GS ( k pL pH cn fn n (cn = 49, fn = 69) QR Code: Select the error correction level

Dim f169 As Integer() = {29, 40, 107, 3, 0, 49, _

69, 48}

RawPrinterHelper.SendBytesToPrinter(szPrinterName, intTobyte(f169))



'<Function 180> GS ( k pL pH cn fn m d1…dk (cn = 49, fn = 80) QR Code: Store the data in the symbol storage area

'pL and pH are the low- and high-order bytes of a 16-bit integer value that specifies the length in bytes of the following data  


Dim f180 As Integer() = {29, 40, 107, length_low_byte, length_high_byte, 49, _

80, 48}

RawPrinterHelper.SendBytesToPrinter(szPrinterName, intTobyte(f180))




'send string/url to printer

'RawPrinterHelper.SendASCiiToPrinter(szPrinterName, Strdata);

RawPrinterHelper.SendStringToPrinter(szPrinterName, Strdata)


'<Function 181> GS ( k pL pH cn fn m (cn = 49, fn = 81) QR Code: Print the symbol data in the symbol storage area

Dim f181 As Integer() = {29, 40, 107, 3, 0, 49, _

81, 48}

RawPrinterHelper.SendBytesToPrinter(szPrinterName, intTobyte(f181))


'



Return True

End Function


End Class

沒有留言:

張貼留言