斑马条码打印机(CPCL打印机:P4T , RP4T , QL 220 Plus , QL 320 Plus , QL 420 Plus , RW 220 , RW 420 , RW 420 Print Station , MZ 320 , MZ 220 , QLn 220 , QLn 320)如何通过以太网接口TCP/IP发送CPCL指令到条码打印机呢?
下面通过C#与VB.net中的例子来进行说明:
1、C#中的例子:
// Printer IP Address and communication port
string ipAddress = "10.3.14.42";
int port = 6101;
// CPCL Command(s)
string CPCLString =
"! 0 200 200 210 1" + Environment.NewLine +
"TEXT 4 0 30 40 Hello World" + Environment.NewLine +
"FORM" + Environment.NewLine +
"PRINT" + Environment.NewLine;
try
{
// Open connection
System.Net.Sockets.TcpClient client = new System.Net.Sockets.TcpClient();
client.Connect(ipAddress, port);
// Write CPCL String to connection
System.IO.StreamWriter writer =
new System.IO.StreamWriter(client.GetStream());
writer.Write(CPCLString);
writer.Flush();
// Close Connection
writer.Close();
client.Close();
}
catch (Exception ex)
{
// Catch Exception
}
2、VB.net中的例子:
Dim ipAddress As String = "10.3.14.59"
Dim port As Integer = 6101
Dim CPCLString As String = _
"! 0 200 200 210 1" & Environment.NewLine & _
"TEXT 4 0 30 40 Hello World" & Environment.NewLine & _
"FORM" & Environment.NewLine & _
"PRINT" & Environment.NewLine
Try
'Open Connection
Dim client As New System.Net.Sockets.TcpClient
client.Connect(ipAddress, port)
'Write CPCL String to Connection
Dim writer As New System.IO.StreamWriter(client.GetStream())
writer.Write(CPCLString)
writer.Flush()
'Close Connection
writer.Close()
client.Close()
Catch ex As Exception
'Catch Exception Here
End Try