在Windows 10 UWP应用程序中将原始命令发送到Zebra打印机

时间:2018-04-06 18:48:22

标签: c# uwp .net-core zebra-printers

我正在开发一个Windows 10应用程序,用于将ZPL命令发送到Zebra打印机。对于我的生活,我无法弄清楚如何发送原始命令。我无法在每次打印前使用打印机对话框,因为应用程序需要在外部服务进入时打印文档。简而言之:没有用户干预。

到目前为止,我已成功获取通过USB连接的打印机的DeviceInformation实例。这就是我被困住的地方。以下是绑定到设备选择的XAML组件的回调。

private async void selectPrinter_Click(object sender, RoutedEventArgs e)
{
  picker = new DevicePicker();
  picker.Filter.SupportedDeviceSelectors.Add("System.Devices.InterfaceClassGuid:=\"{0ecef634-6ef0-472a-8085-5ad023ecbccd}\"");
  picker.DevicePickerDismissed += DevicePickerDismissed;
  picker.DeviceSelected += DeviceSelected;
  Rect rect = new Rect(100, 100, 200, 200);
  picker.Show(rect);
}

private async void DeviceSelected(DevicePicker sender, DeviceSelectedEventArgs args)
{ 
  settings.PrinterId = args.SelectedDevice.Id;
  device = args.SelectedDevice;
  await UpdatePrinterText();
  picker.Hide();
}

private async Task UpdatePrinterText()
{ 
  if (device == null)
  {
    var printerId = settings.PrinterId;
    if (!string.IsNullOrWhiteSpace(printerId))
      device = await DeviceInformation.CreateFromIdAsync(printerId);
  }
  await Dispatcher.RunAsync(CoreDispatcherPriority.High, () => printerName.Text = device?.Name ?? "(no printer selected)");
}

我已尝试使用UsbDevice发送原始命令,但System.IO.FileNotFoundException: 'The system cannot find the file specified. (Exception from HRESULT: 0x80070002)'var usbDevice = await UsbDevice.FromIdAsync(device.Id);的应用程序错误已完整代码:

private async void printTest_Click(object sender, RoutedEventArgs e)
{
  if (device == null)
    return;
  var usbDevice = await UsbDevice.FromIdAsync(device.Id);
  var outPipe = usbDevice.DefaultInterface.BulkOutPipes[0];
  var stream = outPipe.OutputStream;
  var writer = new DataWriter(stream);
  var command = "^XA^FO10,10,^AO,30,20^FDFDTesting^FS^FO10,30^BY3^BCN,100,Y,N,N^FDTesting^FS^XZ";
  var buffer = Encoding.ASCII.GetBytes(command);
  writer.WriteBytes(buffer);
  await writer.StoreAsync();
}

更新:USB线松动了。所以我插入它,现在UsbDevice.FromAsync返回null。我想MS真的不想让你乱搞打印机......

1 个答案:

答案 0 :(得分:0)

可以使用基于通用文本的驱动程序将原始ZPL发送到打印机。