This code shows the contents of a VB.Net application main file. This application prints a ticket with a 2D code, eihter through Ethernet POS named pipe, or through Ethernet POS IP address. It uses ESC/POS Epson® commands to align text, print the 2D code and perform a paper cut. If the printer doesn't support some 2D codes, Ethernet POS can emulate this feature.
To test this code, download the project ZIP archive, extract it, and open the solution file (EthPosSample.sln) with Microsoft Visual Studio.
Imports System.Text Imports System.IO.Pipes Imports System.Net.Sockets Public Class Main ' Misc escape constant definitions Const ESC = Chr(&H1B) Const FS = Chr(&H1C) Const GS = Chr(&H1D) Private Sub Main_Load(ByVal sender As Object, ByVal e As EventArgs) Handles MyBase.Load Dim codeNames() = {"PDF417 Bar", "QR Code", "Maxi Code", "Aztec Code", "DataMatrix Code"} ' Fill code types combo box, and select Qr Code by default For Each name As String In codeNames CodeTypes.Items.Add(name) Next CodeTypes.SelectedIndex = 1 ' Select named pipe output by default and call UpdateControls to update UI NamedPipe.Checked = True UpdateControls() End Sub Private Sub NamedPipe_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles NamedPipe.CheckedChanged UpdateControls() End Sub Private Sub TcpIp_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TcpIp.CheckedChanged UpdateControls() End Sub Private Sub UpdateControls() Dim isIp = TcpIp.Checked TcpIpLabel.Enabled = isIp IpAddress.Enabled = isIp End Sub Private Sub Quit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Quit.Click Close() End Sub Private Sub Print_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Print.Click Dim isIp = TcpIp.Checked Dim codeType = CodeTypes.Text Dim data = GetPrintData(isIp, codeType) Print.Enabled = False Cursor = Cursors.WaitCursor Try If isIp Then TcpIpPrint(IpAddress.Text, data) Else NamedPipePrint(data) End If Catch ex As Exception MsgBox("Error: " & ex.Message, MsgBoxStyle.OkOnly + MsgBoxStyle.Critical) End Try Print.Enabled = True Cursor = Cursors.Default End Sub Private Function GetPrintData(ByVal isIp As Boolean, ByVal codeType As String) Dim cn Dim cmd As String Dim barData As String = "https://www.activeplus.com" Dim cmdLen As Integer Dim barSize = 6 ' Get ESC/POS code number. ESC/POS commands common to all ' 2D code will be used by changing only the code number. ' More details about these commands can be found on Epson ' web site. Select Case codeType Case "PDF417 Bar" ' PDF417 needs a smaller size to avoid overflow ' with the given value to encode. cn = 48 barSize = 4 Case "Maxi Code" cn = 50 Case "Aztec Code" cn = 53 Case "DataMatrix Code" cn = 54 Case Else ' Default to QR Code cn = 49 End Select ' Initialiaze printer cmd = ESC & "@" ' Align left cmd &= ESC & "a" & Chr(1) ' Add header text cmd &= "Ethernet POS" & vbLf cmd &= "(C) Active+ Software 2016-2017" & vbLf & vbLf ' Align center cmd &= ESC & "a" & Chr(0) ' Add more text cmd &= "This is a printing sample that uses" & vbLf cmd &= "Ethernet POS " If isIp Then cmd &= "TCP/IP" Else cmd &= "named pipe" cmd &= " communication" & vbLf cmd &= "mechanism to print a " & codeType & "." & vbLf & vbLf cmd &= "The code encoded value is:" & vbLf cmd &= barData & vbLf & vbLf ' Set code data cmdLen = 3 + barData.Length cmd &= GS & "(k" & Chr(cmdLen And &HFF) & Chr((cmdLen / &H100) And &HFF) cmd &= Chr(cn) & Chr(80) & Chr(48) & barData ' Maxi code default model can't encode our value, so set it to model 5 ' (value 53). Furthermore, it can't be resized, so only other codes get ' a size command. If codeType = "Maxi Code" Then cmd &= GS & "(k" & Chr(3) & Chr(0) & Chr(cn) & Chr(65) & Chr(53) Else cmd &= GS & "(k" & Chr(3) & Chr(0) & Chr(cn) & Chr(67) & Chr(barSize) End If ' Align center cmd &= ESC & "a" & Chr(1) ' Add code print command cmd &= GS & "(k" & Chr(3) & Chr(0) & Chr(cn) & Chr(81) & Chr(48) & vbLf ' Add paper cut command cmd &= GS & "V" & Chr(65) & Chr(3) GetPrintData = Encoding.Default.GetBytes(cmd) End Function Private Sub TcpIpPrint(ByVal ip As String, ByVal data As Byte()) Dim s As Socket s = New Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp) s.Connect(ip, 9100) s.Send(data) s.Disconnect(False) End Sub Private Sub NamedPipePrint(ByVal data As Byte()) Dim p As NamedPipeClientStream p = New NamedPipeClientStream("ethpos_printer_0") p.Connect(10 * 1000) p.Write(data, 0, data.Length) p.Close() End Sub End Class
Download vb-net.zip