博尔杰PTS - 工业标签标识打印整体解决方案
首页 > 技术支持 > 斑马条码打印机通过TCP/IP发送ZPL指令的方法(C#与VB.NET示例)

斑马条码打印机通过TCP/IP发送ZPL指令的方法(C#与VB.NET示例)

本文介绍如何通过以太网接口使用TCP/IP协议向斑马(Zebra)条码打印机发送ZPL指令,并提供了C#与VB.NET的代码示例。适用于支持ZPL语言的斑马打印机型号,如110Xi4、140Xi4、170Xi4、220Xi4、R110Xi、R110Xi HF、R170Xi、110XiIIIPlus、140XiIIIPlus、170XiIIIPlus、220XiIIIPlus、105SL、S4M、ZM400、ZM600、RZ400、RZ600、GX420t、GX420d、GX430t、GK420t、GK420d、RW 220、RW 420、RW 420 Print Station、110PAX4、170PAX4、R110PAX4、QLn 220、QLn 320等。

一、C#示例

以下C#代码演示了如何通过TCP/IP连接到打印机并发送ZPL指令:

// Printer IP Address and communication port
string ipAddress = "10.3.14.42";
int port = 9100;

// ZPL Command(s)
string ZPLString =
   "^XA" +
   "^FO50,50" +
   "^A0N50,50" +
   "^FDHello, World!^FS" +
   "^XZ";

try
{
   // Open connection
   System.Net.Sockets.TcpClient client = new System.Net.Sockets.TcpClient();
   client.Connect(ipAddress, port);

   // Write ZPL String to connection
   System.IO.StreamWriter writer = new System.IO.StreamWriter(client.GetStream());
   writer.Write(ZPLString);
   writer.Flush();

   // Close Connection
   writer.Close();
   client.Close();
}
catch (Exception ex)
{
   // Catch Exception
}

二、VB.NET示例

以下VB.NET代码实现了相同的功能:

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

三、小结

通过上述示例,您可以轻松地将ZPL指令发送到支持TCP/IP的斑马条码打印机。请确保打印机已正确配置网络参数,并允许9100端口通信。在实际应用中,可根据需要修改IP地址、端口和ZPL指令内容。

« 返回列表
博尔杰PTS|博尔杰标识商城|博盾安全标识|博锐迪生物标签