简体   繁体   English

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

[英]Send raw commands to Zebra printer in Windows 10 UWP application

I'm developing an application for Windows 10 to send ZPL commands to a Zebra printer. 我正在为Windows 10开发一个应用程序,以将ZPL命令发送到Zebra打印机。 For the life of me I cannot figure out how to send the raw commands. 为了我的一生,我无法弄清楚如何发送原始命令。 I can't use the printer dialog box before each print because the app needs to print documents as they come in from an external service. 我无法在每次打印之前使用打印机对话框,因为该应用程序需要打印来自外部服务的文档。 In short: no user intervention. 简而言之:无需用户干预。

So far I'm successfully getting the DeviceInformation instance for the printer connected via USB. 到目前为止,我已经成功获取了通过USB连接的打印机的DeviceInformation实例。 This is where I'm stuck. 这就是我卡住的地方。 Here are the callbacks bound to XAML components for the device selection. 这是绑定到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)");
}

I've tried using UsbDevice to send raw commands, but the app errors out with System.IO.FileNotFoundException: 'The system cannot find the file specified. (Exception from HRESULT: 0x80070002)' 我尝试使用UsbDevice发送原始命令,但是应用程序出现System.IO.FileNotFoundException: 'The system cannot find the file specified. (Exception from HRESULT: 0x80070002)'错误System.IO.FileNotFoundException: 'The system cannot find the file specified. (Exception from HRESULT: 0x80070002)' System.IO.FileNotFoundException: 'The system cannot find the file specified. (Exception from HRESULT: 0x80070002)' on var usbDevice = await UsbDevice.FromIdAsync(device.Id); System.IO.FileNotFoundException: 'The system cannot find the file specified. (Exception from HRESULT: 0x80070002)' on var usbDevice = await UsbDevice.FromIdAsync(device.Id); Full code: 完整代码:

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();
}

Update: the USB cord had come loose. 更新:USB电缆松动。 So I plugged it in, and now UsbDevice.FromAsync is returning null. 因此我将其插入,现在UsbDevice.FromAsync返回null。 I guess MS REALLY doesn't want you mucking around with printers... 我想MS真的不希望您与打印机混为一谈...

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM