C#-斑马打印机Helper(二)_使用Zebra.Printer.SDK类库

一、安装打印机驱动

 

二、C#示例

  目前Zebra.Printer.SDK最新版本为3.0;.NetFrameWork可使用2.16版本。

1、使用Tcp网口

 string zplStr = "";

 string tcpIp = "192.168.1.101";
 int tcpPort = 6101;  // 9100
 int maxTimeout = 10000;

 Connection connection = new TcpConnection(tcpIp, tcpPort);  // new TcpConnection("192.168.1.22",9100)
 connection.MaxTimeoutForRead = maxTimeout;

 connection.Open();
 if (connection.Connected)
 {
     ZebraPrinter printer = ZebraPrinterFactory.GetInstance(PrinterLanguage.ZPL, connection);
     printer.SendCommand(zplStr);
 }
 else
 {
     //MessageBox.Show("未连接到任何TCP打印设备!");
     //return;
 }
 connection.Close();

2、使用Usb串口

string zplStr = "";

Connection connection;
if (cmbchkMAC.SelectedText == "扫描USB串口打印机")  // 扫描方式
{
    List<DiscoveredPrinterDriver> drivers = UsbDiscoverer.GetZebraDriverPrinters();  //UsbDiscoverer.GetZebraUsbPrinters();
    if (drivers == null || drivers.Count < 1)
    {
        //MessageBox.Show("未找到任何的USB斑马打印设备");
        //return;
    }
    connection = drivers[0].GetConnection();
}
else  // MAC方式
{
    string usbMac = "";
    connection = new UsbConnection(usbMac);
}

connection.Open();
if (connection.Connected)
{
    ZebraPrinter printer = ZebraPrinterFactory.GetInstance(PrinterLanguage.ZPL, connection);
    printer.SendCommand(zplStr);
}
else
{
    //MessageBox.Show("未连接到任何USB斑马打印设备");
    //return;
}
connection.Close();

3、使用蓝牙

 string zplStr = "";

 #region 扫描方式
 //List<DiscoveredPrinterDriver> drivers = BluetoothDiscoverer.();
 //if (drivers == null || drivers.Count < 1)
 //{
 //    MessageBox.Show("未找到任何的蓝牙斑马打印设备");
 //}
 //Connection connection = drivers[0].GetConnection();
 #endregion 扫描方式
 
 #region MAC方式
 string bluetoothMac = "";
 Connection connection = new BluetoothConnection(bluetoothMac);
 #endregion MAC方式

 connection.Open();
 if (connection.Connected)
 {
     ZebraPrinter printer = ZebraPrinterFactory.GetInstance(PrinterLanguage.ZPL, connection);
     printer.SendCommand(zplStr);
 }
 else
 {
    // 
 }
 connection.Close();
posted @ 2024-10-14 15:44  ꧁执笔小白꧂  阅读(142)  评论(0编辑  收藏  举报