通常情况下,当我将Zebra LP 2844-Z插入USB端口时,计算机将其视为打印机,我可以像任何其他通用打印机一样从记事本打印到它。但是,我的应用程序有一些条形码功能。我的应用程序解析一些输入并生成内存中的ZPL字符串。如何将此ZPL数据发送到USB设备?
答案 0 :(得分:17)
我找到答案......或者至少是最简单的答案(如果有多个答案)。当我安装打印机时,我将其重命名为“ICS Label Printer”。以下是如何更改选项以允许传递ZPL命令:
在我的代码中,我只需要将“$ {”添加到我的ZPL开头,将“} $”添加到结尾,然后将其打印为纯文本。这是“用于ZDesigner LP 2844-Z打印机版本2.6.42(Build 2382)的Windows驱动程序”。像魅力一样工作!
答案 1 :(得分:15)
我发现通过COM端口写入Zebra打印机的方法更简单。我去了Windows控制面板并添加了一台新打印机。对于端口,我选择了COM1(打印机插入的端口)。我使用了“Generic / Text Only”打印机驱动程序。我禁用了打印假脱机程序(打印机首选项中的标准选项)以及所有高级打印选项。现在,我可以只打印任何字符串到该打印机,如果字符串包含ZPL,打印机就会使ZPL正常!不需要特殊的“开始序列”或像这样的时髦的东西。为简单起见!
答案 2 :(得分: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;
}
}
第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.");
}
}
使用此解决方案,您可以调整以满足特定要求。也许硬编码特定的打印机。也许是动态地从文本框中导出ZPL文本。随你。也许您不需要图形界面,但这显示了如何发送ZPL。您的使用取决于您的需求。
答案 3 :(得分:8)
你还没有提到一种语言,所以我将给你一些提示,告诉你如何使用C语言中的直接Windows API。
首先,使用OpenPrinter
打开与打印机的连接。接下来,启动一个文档StartDocPrinter
,DOC_INFO_1
结构的pDatatype
字段设置为"RAW"
- 这告诉打印机驱动程序不要对打印机进行任何编码,但是传递它不变。使用StartPagePrinter
表示第一页,WritePrinter
将数据发送到打印机,完成后用EndPagePrinter
,EndDocPrinter
和ClosePrinter
关闭。< / p>
答案 4 :(得分:3)
ZPL是正确的方法。在大多数情况下,使用抽象到GDI命令的驱动程序是正确的;但Zebra标签打印机是一种特殊情况。打印到Zebra打印机的最佳方法是直接生成ZPL。请注意,Zebra打印机的实际打印机驱动程序是“纯文本”打印机 - 在我们认为大多数打印机具有驱动程序的意义上,没有可以更新或更改的“驱动程序”。它只是绝对极简主义意义上的推动者。
答案 5 :(得分:0)
我花了8个小时才做到这一点。 这很简单......
你应该有这样的代码:
private const int GENERIC_WRITE = 0x40000000;
//private const int OPEN_EXISTING = 3;
private const int OPEN_EXISTING = 1;
private const int FILE_SHARE_WRITE = 0x2;
private StreamWriter _fileWriter;
private FileStream _outFile;
private int _hPort;
将变量内容从3(已存在的文件)更改为1(创建新文件)。 它适用于Windows 7和XP。
答案 6 :(得分:0)
安装共享您的打印机:\ localhost \ zebra 发送ZPL作为文本,首先尝试复制:
复制file.zpl \ localhost \ zebra
非常简单,几乎没有编码。
答案 7 :(得分:0)
找到了惊人的简单解决方案-适用于Chrome浏览器(Windows,未在Mac上进行测试)
Zebra ZP 450
答案 8 :(得分:0)
对于任何在 2021 年因上述问题而折磨自己的人,如果您在 .NET 世界中,那么请获取 RawPrint NuGet 包。
从那里将您的 ZPL 字符串直接打印到打印机,绕过驱动程序,它是:
IPrinter printer = new Printer();
printer.PrintRawStream(@"Your Printer Name", GenerateStreamFromString(yourZplString), @"docname");
答案 9 :(得分:-1)
您可以使用COM或.Net的P / Invoke打开Winspool.drv驱动程序并将字节直接发送到设备。但你不想这样做;这通常仅适用于您测试的一个驱动程序的一个版本上的一个设备,并且其他所有内容都会中断。从长期,痛苦的个人经历中获取这一点。
您要做的是获取使用普通旧GDI或GDI +命令绘制条形码的条形码字体或库;有一个.Net here。这适用于所有设备,即使在Zebra更改驱动程序之后也是如此。