Send raw commands to Zebra printer in Windows 10 UWP application

0

I'm developing an application for Windows 10 to send ZPL commands to a Zebra printer. 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. This is where I'm stuck. Here are the callbacks bound to XAML components for the device selection.

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)' 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. So I plugged it in, and now UsbDevice.FromAsync is returning null. I guess MS REALLY doesn't want you mucking around with printers...

c#
uwp
.net-core
zebra-printers
asked on Stack Overflow Apr 6, 2018 by eltiare • edited Apr 7, 2018 by eltiare

1 Answer

0

Sending raw ZPL to printers can be done with the Generic Text based driver.

answered on Stack Overflow Apr 24, 2018 by EdHayes3

User contributions licensed under CC BY-SA 3.0