通常,当我插在我的斑马LP2844-Z的USB接口、电脑认为这是一个打印机,我可以打印到它从记事本像其他任何通用的打印机。然而,我应用程序有一些条码的功能。我应用程序分析的一些输入和生存中的串ZPL.我怎么会把这个送ZPL数据来我USB装置?

有帮助吗?

解决方案 2

我还发现了一种更简单的方法,可以通过COM端口写入斑马打印机。我去了Windows控制面板,并添加了一台新打印机。对于端口,我选择了COM1(打印机已插入到端口)。我使用了“仅通用 /文本”打印机驱动程序。我禁用了打印式剥离器(打印机首选项中的标准选项)以及所有高级打印选项。现在,我可以将任何字符串打印到该打印机,如果字符串包含ZPL,则打印机将ZPL呈现好!不需要特殊的“开始序列”或类似的时髦事物。是的,为简单起见!

其他提示

我找到了答案...或者至少是最简单的答案(如果有多个)。当我安装打印机时,我将其重命名为“ ICS标签打印机”。这是更改选项以允许传递ZPL命令的方法:

  1. 右键单击“ ICS标签打印机”,然后选择“属性”。
  2. 在“常规”选项卡上,单击“打印首选项...”按钮。
  3. 在“高级设置”选项卡上,单击“其他”按钮。
  4. 确保在标有“启用通行模式”的框中有一张检查。
  5. 确保“开始序列:”是“ $ {”。
  6. 确保“端序列:”是“} $”。
  7. 单击“关闭”按钮。
  8. 单击“确定”按钮。
  9. 单击“确定”按钮。

在我的代码中,我只需要将“ $ {”添加到我的zpl的开头和“} $”的开始,然后将其打印为纯文本。这与“ Zdesigner LP 2844-Z打印机版本2.6.42(构建2382”)的“ Windows驱动程序”。奇迹般有效!

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。您的使用取决于您的需求。

您尚未提及一种语言,所以我将为您提供一些有关C的直接Windows API的提示。

首先,与打印机打开连接 OpenPrinter. 。接下来,用 StartDocPrinterpDatatype 领域 DOC_INFO_1 结构设置为 "RAW" - 这告诉打印机驱动程序不要编码任何进入打印机的任何内容,而是将其传递到没有变化的情况下。利用 StartPagePrinter 要指示第一页 WritePrinter 将数据发送到打印机,然后将其关闭 EndPagePrinter, EndDocPrinterClosePrinter 完成后。

ZPL是正确的方法。在大多数情况下,使用将GDI命令抽象的驱动程序使用是正确的。但是,斑马标签打印机是一种特殊情况。打印到斑马打印机的最佳方法是直接生成ZPL。请注意,Zebra打印机的实际打印机驱动程序是“纯文本”打印机 - 从我们想到大多数打印机都有驱动程序的意义上讲,没有一个可以更新或更改的“驱动程序”。从绝对极简主义的意义上讲,这只是一个驱动程序。

我花了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(创建一个新的文件)。它将在7和XP。

安装共享打印机: localhost Zebra将ZPL作为文本发送,请先尝试使用:

复制file.zpl localhost Zebra

非常简单,几乎没有编码。

您可以使用com,也可以从.NET中使用P/Indoke,打开Winspool.drv驱动程序并将字节直接发送到设备。但是你不想这样做;这通常仅适用于您测试的一个驱动程序的一个版本上的一个设备,并在其他所有设备上损坏。从漫长,痛苦,个人经历中汲取这一点。

您要做的是获取使用普通的旧GDI或GDI+命令绘制条形码的条形码字体或库;有一个.net 这里. 。即使在Zebra更改驱动程序之后,这也可以在所有设备上工作。

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top