zebra network printer reprinted after a while - vb.net

Zebra printer printed when send data for ip address.
And after a while, it reprinted.
I just sent data once, but printed more than once.
[vb.net code]
Dim Client As New TcpClient(IpAddress, Port)
Dim writer As New Binary(Client.GetStream)
writer.Write(Data)
writer.Flush()
writer.Close()
How to make it normally?

Related

How to receive UDP datagrams with VB.NET UDPClient

I am building a simple test program for some embedded devices. They are controlled through Wi-Fi using UDP multicast messages. The test application, written using VB.NET is able to send data to the devices but I am not able to receive data back from them.
The data from the embedded devices is captured by WireShark correctly, but nothing is received by the test program.
Windows Firewall is open for the program, any port, any IP for UDP.
This is the thread code I have as a skeleton to receive the data:
Private Sub ListenForInput()
Try
Dim udpClient As New UdpClient(port)
Dim grpEndpoint As New IPEndPoint(ipAddress, port)
While keepListening
Debug.Print("Listening on IP:" + ipAddress.ToString + " port:" + port.ToString)
Dim input As Byte() = udpClient.Receive(grpEndpoint)
Debug.Print("Got data")
End While
Catch ex As SocketException
'
' What do do?
'
Debug.Print("Oops!")
End Try
End Sub
The Debug output displays the "listening" message, but the call to Receive never returns.

Print a mail from EWS in vb.net

In my vb .net program I select an email from my Exchange server as a message in Exchange Web Services.
I am looking for a solution to print that message to a network printer. Can anyone help?
Either print directly as an email from EWS or as a file.
For Each i As Item In emailitems
' search and find an email
i.Load(New PropertySet(ItemSchema.MimeContent))
Dim mc As New MimeContent
Dim fs As New FileStream("c:\temp\mail.eml", FileMode.Create)
mc = i.MimeContent
fs.Write(mc.Content, 0, mc.Content.Length)
fs.Close()
fs = Nothing
' how do I print message i or mail.eml to a network printer?
Next

Changing driver settings for printing a PDF

How can I change settings in my printer (driver), before printing out a PDF?
To be more specific - I want to force my printer driver to use a printer settings instead of driver defaults - basically an equivalent of clicking Properties in a Print window (which opens printer-specific settings), then Advanced Setup and ticking "Use printer settings" checkbox which is by default unticked.
But it could be anything, for example changing dithering mode in a printer.
Here is the functioning code I'm using right now for printing a PDF using my network printer:
Dim PrinterName As String = "\\MyNetwork\ZDesigner ZM400 200 dpi (ZPL)"
Dim WshNetwork = CreateObject("WScript.Network")
WshNetwork.SetDefaultPrinter(PrinterName)
Dim PrintingPageSettings As New Printing.PageSettings()
Me.Text = PrintingPageSettings.PrinterSettings.PrinterName()
Dim isInstalled As Boolean = False
For Each InstalledPrinter As String In Printing.PrinterSettings.InstalledPrinters()
If (PrintingPageSettings.PrinterSettings.PrinterName() = InstalledPrinter.ToString) Then
isInstalled = True
End If
Next
If (isInstalled) Then
AdobeAcrobatCOM.src = Path
AdobeAcrobatCOM.printAll()
Else
Me.Text = PrinterName & " not found"
End If
AdobeAcrobatCOM is AxAcroPDFLib.AxAcroPDF (Adobe PDF Reader from Toolbox, COM components)
Eventually I used TCP connection to the printer and printed it out this way. Here is a code sample:
Dim PrintString As String
Dim ipAddress As String
Dim port As Integer
'123123 is sample integer, "TESTstring" is sample string, Space(2) is sample of adding (two) spaces
PrintString = String.Concat("^XA", "^FO060,080", "^BXN,5,200", "^FD", "TESTstring", 123123, "%^FS", "^FO160,100", "^ACourier,14,14", "^FD", Space(2), "^FS", "^XZ")
ipAddress = "ZDesigner ZM400 200 dpi (ZPL)" 'yes, this works too
port = 9100
'Open Connection
Dim client As New System.Net.Sockets.TcpClient
client.Connect(ipAddress, port)
'Write ZPL String to Connection
Dim writer As New System.IO.StreamWriter(client.GetStream())
writer.Write(PrintString)
writer.Flush()
'Close Connection
writer.Close()
client.Close()
You might want to look for your printer documentation. Here is a C# example for Zebra.

Using images stored in Zebra printer to create a label in vb.net

Is there any way to use the images stored in a Zebra printer in the following code:
Dim g As Graphics = e.Graphics
Dim bc As New BarcodeProfessional
Dim br As Brush = New SolidBrush(Drawing.Color.Black)
Dim blackPen As New Pen(Color.Black, 5)
e.Graphics.DrawArc(blackPen, 10, 10, 70, 50, 130, 100)
g.DrawString("UPC", Arial_6_bold, br, 210, 15)
the above prints an arc, and a "UPC" text, now can I print here also an image stored in the Zebra printer?
I have found that I can send ZPL code to the printer in this way:
Dim ipAddress As String = "10.3.14.59"
Dim port As Integer = 9100
Dim ZPLString As String = _
"^XA" & _
"^FO50,50" & _
"^A0N,50,50" & _
"^FDHello, World!^FS" & _
"^XZ"
Try
'Open Connection
Dim client As New System.Net.Sockets.TcpClient
client.Connect(ipAddress, port)
'Write ZPL String to Connection
Dim writer As New System.IO.StreamWriter(client.GetStream())
writer.Write(ZPLString)
writer.Flush()
'Close Connection
writer.Close()
client.Close()
Catch ex As Exception
'Catch Exception Here
End Try
But I don't know how to put together both codes, any idea?
If the graphic will be static and doesn't change based on user input or etc., you could just create a .grf out of it. I guess the easiest way to do that would be to export the image as a bitmap, open in paint, save as a .pcx file, and then open it in ZTools to convert to a hex .grf file. Then you can send the graphic to the printer along with your other ZPL code.
For examples on how to do this, I'll refer you to the ZPL programming guide, available at http://www.servopack.de/support/zebra/ZPLbasics.pdf
You'll find an example that explains creating and printing .grf files on page 36, and an example of sending both .grf and text to a label on page 63.
It sounds like your image is already stored to the printer. If so, its file name (as stored on the printer) should be something like 'E:MYFILE.GRF'. You can use the ZPL command ^XG to recall stored graphics. So, by sending the following ZPL, the graphic should print after the text 'Hello, World!":
"^XA" & _
"^FO50,50" & _
"^A0N,50,50" & _
"^FDHello, World!^FS" & _
"^XGE:MYFILE.GRF,1,1^FS" & _
"^XZ"
In case your image isn't already stored, you can store a GRF file through the ~DY command, but it is far easier to download objects through Zebra Setup Utilities: http://www.zebra.com/us/en/products-services/software/manage-software/zebra-setup-utility.html.
Source: https://support.zebra.com/cpws/docs/zpl/zpl_manual.pdf.

TCP Server in VB.NET

I am not a software programmer but I have a task to create a TCP Server (a program that is listening on its network card interfaces for incoming data streams).
I have searched on the internet and I found that I can use two methods: Socket or TCPListener class.
I have created an example for the Socket class, but I was wondering how I can test it?
If another computer in the network sends some string data to the listener computer, then the message should be displayed.
Here is the example from Microsoft that I am using for the TCP server using a Socket:
Public Shared Sub Main()
' Data buffer for incoming data.
Dim data = nothing
Dim bytes() As Byte = New [Byte](1024) {}
Dim ipAddress As IPAddress = ipAddress.Any
Dim localEndPoint As New IPEndPoint(ipAddress, 0)
Dim intI As Integer = 0
'Display the NIC interfaces from the listener
For Each ipAddress In ipHostInfo.AddressList
Console.WriteLine("The NIC are {0}", ipHostInfo.AddressList(intI))
intI += 1
Next
Console.WriteLine("You are listening on {0}",localEndPoint)
' Create a TCP/IP socket.
Dim listener As New Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)
' Bind the socket to the local endpoint and
' listen for incoming connections.
Try
listener.Bind(localEndPoint)
listener.Listen(200)
Catch e As SocketException
Console.WriteLine("An application is alreading using that combination of ip adress/port", e.ErrorCode.ToString)
End Try
' Start listening for connections.
While True
Console.WriteLine("Waiting for a connection...")
' Program is suspended while waiting for an incoming connection.
Dim handler As Socket = listener.Accept()
data = Nothing
' An incoming connection needs to be processed.
While True
bytes = New Byte(1024) {}
Dim bytesRec As Integer = handler.Receive(bytes)
data += Encoding.ASCII.GetString(bytes, 0, bytesRec)
Console.WriteLine("The string captured is {0}", data)
If data.IndexOf("something") > -1 Then
Exit While
End If
End While
' Show the data on the console.
Console.WriteLine("Text received : {0}", data)
' Echo the data back to the client.
Dim msg As Byte() = Encoding.ASCII.GetBytes(data)
handler.Shutdown(SocketShutdown.Both)
handler.Close()
End While
End Sub
End Class
Am I on the right lead?
Thanks
Later Edit:
I have used that code in a Console Application created with Visual Studio and I want to check the scenario when a device is sending some string message through the network.
E.g:
I have two devices :Computer A, computer B connected through LAN
I have tried this command : telnet computerA port ( from computer B) but nothing is displayed in the TCP server running from computer A.
telnet 192.168.0.150 3232
I also made a TCP client for testing (derived from the Microsoft example):
Public Class SynchronousSocketClient
Public Shared Sub Main()
' Data buffer for incoming data.
Dim bytes(1024) As Byte
Dim ipHostInfo As IPHostEntry = Dns.GetHostEntry(Dns.GetHostName())
Dim ipAddress As IPAddress = ipHostInfo.AddressList(0)
Dim remoteEP As New IPEndPoint(ipAddress, 11000)
' Create a TCP/IP socket.
Dim sender As New Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)
' Connect the socket to the remote endpoint.
sender.Connect(remoteEP)
Console.WriteLine("Socket connected to {0}", _
sender.RemoteEndPoint.ToString())
' Encode the data string into a byte array.
Dim msg As Byte() = _
Encoding.ASCII.GetBytes("This is a test<EOF>")
' Send the data through the socket.
Dim bytesSent As Integer = sender.Send(msg)
' Receive the response from the remote device.
Dim bytesRec As Integer = sender.Receive(bytes)
Console.WriteLine("Echoed test = {0}", _
Encoding.ASCII.GetString(bytes, 0, bytesRec))
' Release the socket.
sender.Shutdown(SocketShutdown.Both)
sender.Close()
Console.ReadLine()
End Sub
End Class 'SynchronousSocketClient
But it does not work because of the PORT setting.
If in the TCP Server I have "Dim localEndPoint As New IPEndPoint(ipAddress, 0)" then the client crashes, but if I change the port from any (0) to 11000 for example, the client works fine.
Do you know why?
Later edit 2:
Maybe I should have started with this question: Which method is recommended for my scope - asynchronous or synchronous method?
Yes, you are on the right path.
The next thing to do is to introduce message detection since TCP is stream based and not message based like UDP. This means that TCP might decide to send two of your messages in the same packet (so that one socket.Recieve will get two messages) or that it will split up your message into two packets (thus requiring you to use two socket.Recieve to get it).
The two most common ways to create message detection is:
Create a fixed size header which includes message size
Create a delimiter which is appended to all messages.
Your "server" isn't listening on a set port, so you'll need to pay attention to the "You are listening on" message that appears. Then, from another machine on the network, telnet the.ip.add.ress port. (This may require installing "telnet client", or enabling it in the Programs and Features stuff, or whatever.)
Side note...if you actually intend for this to be a server of some sort, you'll want to decide what port you want to use, so that other computers can find your service. Most people won't be able to read your screen to figure out where to connect. :)
As for your "client"...when you connect to another computer, you don't just "pick a port" (which is what a port number of 0 means in an endpoint). You need to know what port the server uses. (Reread what i said in the previous paragraph. A program running on another computer has no idea what port to use to connect to the server -- any server could be running on any port.) You need to pick a port number for the server (say, 11000...good as any, really) rather than letting it use port 0.