Jas*_*ter 21 usb zpl zebra-printers
通常,当我将Zebra LP 2844-Z插入USB端口时,计算机将其视为打印机,我可以像其他任何通用打印机一样从记事本打印到它.但是,我的应用程序有一些条形码功能.我的应用程序解析一些输入并生成内存中的ZPL字符串.如何将此ZPL数据发送到USB设备?
Jas*_*ter 18
我找到了答案......或者至少是最简单的答案(如果有多个答案).当我安装打印机时,我将其重命名为"ICS Label Printer".以下是如何更改允许传递ZPL命令的选项:
在我的代码中,我只需要将"$ {"添加到我的ZPL的开头,将"} $"添加到结尾,然后将其打印为纯文本.这是"用于ZDesigner LP 2844-Z打印机版本2.6.42(Build 2382)的Windows驱动程序".奇迹般有效!
Jas*_*ter 15
我发现通过COM端口写入Zebra打印机的方法更简单.我去了Windows控制面板并添加了一台新打印机.对于端口,我选择了COM1(打印机插入的端口).我使用了"Generic/Text Only"打印机驱动程序.我禁用了打印假脱机程序(打印机首选项中的标准选项)以及所有高级打印选项.现在,我可以只打印任何字符串到该打印机,如果字符串包含ZPL,打印机就会使ZPL正常!不需要特殊的"开始序列"或像这样的时髦的东西.耶和简单!
bar*_*ker 11
Visual Studio C#解决方案(可在http://support.microsoft.com/kb/322091找到)
步骤1.) 创建类RawPrinterHelper ...
using System;
using System.IO;
using System.Runtime.InteropServices;
public class RawPrinterHelper
{
// Structure and API declarions:
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)]
public class DOCINFOA
{
[MarshalAs(UnmanagedType.LPStr)]
public string pDocName;
[MarshalAs(UnmanagedType.LPStr)]
public string pOutputFile;
[MarshalAs(UnmanagedType.LPStr)]
public string pDataType;
}
[DllImport("winspool.Drv", EntryPoint = "OpenPrinterA", SetLastError = true, CharSet = CharSet.Ansi, ExactSpelling = true, CallingConvention = CallingConvention.StdCall)]
public static extern bool OpenPrinter([MarshalAs(UnmanagedType.LPStr)] string szPrinter, out IntPtr hPrinter, IntPtr pd);
[DllImport("winspool.Drv", EntryPoint = "ClosePrinter", SetLastError = true, ExactSpelling = true, CallingConvention = CallingConvention.StdCall)]
public static extern bool ClosePrinter(IntPtr hPrinter);
[DllImport("winspool.Drv", EntryPoint = "StartDocPrinterA", SetLastError = true, CharSet = CharSet.Ansi, ExactSpelling = true, CallingConvention = CallingConvention.StdCall)]
public static extern bool StartDocPrinter(IntPtr hPrinter, Int32 level, [In, MarshalAs(UnmanagedType.LPStruct)] DOCINFOA di);
[DllImport("winspool.Drv", EntryPoint = "EndDocPrinter", SetLastError = true, ExactSpelling = true, CallingConvention = CallingConvention.StdCall)]
public static extern bool EndDocPrinter(IntPtr hPrinter);
[DllImport("winspool.Drv", EntryPoint = "StartPagePrinter", SetLastError = true, ExactSpelling = true, CallingConvention = CallingConvention.StdCall)]
public static extern bool StartPagePrinter(IntPtr hPrinter);
[DllImport("winspool.Drv", EntryPoint = "EndPagePrinter", SetLastError = true, ExactSpelling = true, CallingConvention = CallingConvention.StdCall)]
public static extern bool EndPagePrinter(IntPtr hPrinter);
[DllImport("winspool.Drv", EntryPoint = "WritePrinter", SetLastError = true, ExactSpelling = true, CallingConvention = CallingConvention.StdCall)]
public static extern bool WritePrinter(IntPtr hPrinter, IntPtr pBytes, Int32 dwCount, out Int32 dwWritten);
// SendBytesToPrinter()
// When the function is given a printer name and an unmanaged array
// of bytes, the function sends those bytes to the print queue.
// Returns true on success, false on failure.
public static bool SendBytesToPrinter(string szPrinterName, IntPtr pBytes, Int32 dwCount)
{
Int32 dwError = 0, dwWritten = 0;
IntPtr hPrinter = new IntPtr(0);
DOCINFOA di = new DOCINFOA();
bool bSuccess = false; // Assume failure unless you specifically succeed.
di.pDocName = "My C#.NET RAW Document";
di.pDataType = "RAW";
// Open the printer.
if (OpenPrinter(szPrinterName.Normalize(), out hPrinter, IntPtr.Zero))
{
// Start a document.
if (StartDocPrinter(hPrinter, 1, di))
{
// Start a page.
if (StartPagePrinter(hPrinter))
{
// Write your bytes.
bSuccess = WritePrinter(hPrinter, pBytes, dwCount, out dwWritten);
EndPagePrinter(hPrinter);
}
EndDocPrinter(hPrinter);
}
ClosePrinter(hPrinter);
}
// If you did not succeed, GetLastError may give more information
// about why not.
if (bSuccess == false)
{
dwError = Marshal.GetLastWin32Error();
}
return bSuccess;
}
public static bool SendFileToPrinter(string szPrinterName, string szFileName)
{
// Open the file.
FileStream fs = new FileStream(szFileName, FileMode.Open);
// Create a BinaryReader on the file.
BinaryReader br = new BinaryReader(fs);
// Dim an array of bytes big enough to hold the file's contents.
Byte[] bytes = new Byte[fs.Length];
bool bSuccess = false;
// Your unmanaged pointer.
IntPtr pUnmanagedBytes = new IntPtr(0);
int nLength;
nLength = Convert.ToInt32(fs.Length);
// Read the contents of the file into the array.
bytes = br.ReadBytes(nLength);
// Allocate some unmanaged memory for those bytes.
pUnmanagedBytes = Marshal.AllocCoTaskMem(nLength);
// Copy the managed byte array into the unmanaged array.
Marshal.Copy(bytes, 0, pUnmanagedBytes, nLength);
// Send the unmanaged bytes to the printer.
bSuccess = SendBytesToPrinter(szPrinterName, pUnmanagedBytes, nLength);
// Free the unmanaged memory that you allocated earlier.
Marshal.FreeCoTaskMem(pUnmanagedBytes);
return bSuccess;
}
public static bool SendStringToPrinter(string szPrinterName, string szString)
{
IntPtr pBytes;
Int32 dwCount;
// How many characters are in the string?
dwCount = szString.Length;
// Assume that the printer is expecting ANSI text, and then convert
// the string to ANSI text.
pBytes = Marshal.StringToCoTaskMemAnsi(szString);
// Send the converted ANSI string to the printer.
SendBytesToPrinter(szPrinterName, pBytes, dwCount);
Marshal.FreeCoTaskMem(pBytes);
return true;
}
}
Run Code Online (Sandbox Code Playgroud)
步骤2.)创建一个带有文本框和按钮的表单(文本框将保存ZPL以在此示例中发送).在按钮点击事件中添加代码...
private void button1_Click(object sender, EventArgs e)
{
// Allow the user to select a printer.
PrintDialog pd = new PrintDialog();
pd.PrinterSettings = new PrinterSettings();
if (DialogResult.OK == pd.ShowDialog(this))
{
// Send a printer-specific to the printer.
RawPrinterHelper.SendStringToPrinter(pd.PrinterSettings.PrinterName, textBox1.Text);
MessageBox.Show("Data sent to printer.");
}
else
{
MessageBox.Show("Data not sent to printer.");
}
}
Run Code Online (Sandbox Code Playgroud)
使用此解决方案,您可以调整以满足特定要求.也许硬编码特定的打印机.也许是动态地从文本框中导出ZPL文本.随你.也许您不需要图形界面,但这显示了如何发送ZPL.您的使用取决于您的需求.
你还没有提到一种语言,所以我将给你一些提示,告诉你如何使用C中的直接Windows API.
首先,打开与打印机的连接OpenPrinter.接下来,启动一个文档,StartDocPrinter其结构pDatatype字段DOC_INFO_1设置为"RAW"- 这告诉打印机驱动程序不要对打印机进行任何编码,而是将其传递给未更改的文件.使用StartPagePrinter指示的第一页,WritePrinter将数据发送到打印机,并与关闭EndPagePrinter,EndDocPrinter并ClosePrinter在完成时.
ZPL 是正确的方法。在大多数情况下,使用抽象为 GDI 命令的驱动程序是正确的;但是 Zebra 标签打印机是一个特例。打印到 Zebra 打印机的最佳方式是直接生成 ZPL。请注意,Zebra 打印机的实际打印机驱动程序是“纯文本”打印机 - 没有可以更新或更改的“驱动程序”,因为我们认为大多数打印机都有驱动程序。它只是绝对极简意义上的驱动程序。