c# - 无法打印到包含 ZPL (Zebra) 的打印机
问题描述
我想在包含 ZPL 的打印机上打印标签。
打印机已连接到 USB。我使用函数作为入口点
尝试了这段代码。SendStringToUsbPrinter
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;
using System.Runtime.InteropServices;
namespace Core.Helpers
{
public class clsUsb
{
[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);
public void SendBytesToUsbPrinter(string pPrinterName, IntPtr pBytes, Int32 pCount)
{
DOCINFOA doc = new DOCINFOA();
IntPtr hPrinter = new IntPtr(0);
Int32 iWritten = 0;
doc.pDocName = "Document";
doc.pDataType = "RAW";
try
{
//Ouvre l'acces à l'imprimante
if (OpenPrinter(pPrinterName.Normalize(), out hPrinter, IntPtr.Zero))
{
//Débute l'impression de document
StartDocPrinter(hPrinter, 1, doc);
if (StartPagePrinter(hPrinter))
{
//Ecrit les données
WritePrinter(hPrinter, pBytes, pCount, out iWritten);
EndPagePrinter(hPrinter);
}
EndDocPrinter(hPrinter);
ClosePrinter(hPrinter);
}
}
catch (Exception e)
{
throw new Exception(e.Message);
}
}
public void SendStringToUsbPrinter(string pPrinterName, string pData)
{
IntPtr pBytes;
Int32 iCount;
iCount = pData.Length;
pBytes = Marshal.StringToCoTaskMemAnsi(pData);
SendBytesToUsbPrinter(pPrinterName, pBytes, iCount);
Marshal.FreeCoTaskMem(pBytes);
}
public void SendFileToUsbPrinter(string pPrinterName, string pFile)
{
//Ouverture du fichier
FileStream fs = new FileStream(pFile, FileMode.Open);
//Création du binaire
BinaryReader br = new BinaryReader(fs);
//Création d'un tableau d'octets da la longueur du fichier
Byte[] tab = new byte[fs.Length];
IntPtr pBytes;
int iLength;
iLength = Convert.ToInt32(fs.Length);
//met le contenu du fichier dans le fichier
tab = br.ReadBytes(iLength);
pBytes = Marshal.AllocCoTaskMem(iLength);
//Copie le tableau
Marshal.Copy(tab, 0, pBytes, iLength);
//envoie le tableau à l'impresssion
SendBytesToUsbPrinter(pPrinterName, pBytes, (uint) iLength);
//Libère la mémoire
Marshal.FreeCoTaskMem(pBytes);
}
}
}
它工作了一段时间,然后在不更改代码的情况下,至少在我看来,它不再打印了。
如果在打印机的属性中我激活了假脱机程序,.SHD 和 .SPL 文件都存在,除了 .SPL 文件保持在 0k。
当我在代码中用 TEXT 替换 RAW 参数时,我可以看到这个文件的大小在 ZPL 代码打印之前发生了变化。
当我禁用打印机后台处理程序时,我得到相同的结果。
输入字符串中发送的字符串
^XA\r^PR2\r^A0N,58,58^FO36,40^CI0^FD\r批次:\r^FS\r^A0N,58,58^FO144,40^CI0^FD\r210618-0\ n^FS\r^BY2^FO160,104^BCN,64,N,N,N^FD>:\r210618-0\n@\r1\n^FS\r^A0N,58,58^FO180,185 ^CI0^FD\rSPX4.PACK.BB.NN000\n^FS\r^BY2^FO207,256^BCN,64,N,N,N^FD>:\rSPX4.PACK.BB.NN000\n^FS \r^A0N,58,58^FO35,414^CI28^FH^FD\r纸箱 n_C3_B8:\r^FS\r^A0N,58,58^FO35,485^CI28^FH^FD\rQuantit_C3_A9:\r^ FS\r^A0N,58,58^FO35,345^CI0^FD1er IMEI: \r2\n^FS\r^A0N,58,58^FO308,485^CI0^FD\r5\n^FS\r^ A0N,58,58^FO308,414^CI0^FD\r1\n^FS\r^BY3^FO540,380^BEN,102,Y^FD\r3700764701584\n^FS\r^PQ1,0,1, Y\r^XZ\r"
我还尝试使用 Vanara 库,它的代码完全相同,但它是相同的。
解决方案
所以,我解决了我的问题。我浏览了 Vanara Nugget 并通过只执行一个函数并发送一个字节数组来简化我的代码。
同时,我重新安装了打印机驱动程序。
到目前为止,一切正常。
我只是做了几个测试并重组了我的代码以使其更具可读性。
谢谢你的帮助
推荐阅读
- r - 使用 dplyr 查找具有最短日期的列的名称
- gmail-api - service.users().messages().get() 错误:java.io.EOFException:SSL 对等体错误关闭
- database - 如何解析 Redis AOF 文件?
- mysql - 将几个替代表表示为一个表?
- python - 如何在具有多行字符串的文件中搜索多个字符串
- html - Timevis - 使用自定义 css 更改字体大小
- javascript - 如何在canvas html内的多个图像上调用函数
- matlab - 使用 Matlab perfcurve 在交叉验证中绘制 ROC 曲线
- java - Java - 如果我知道域,如何将相对 URL 字符串更改为绝对 URL?
- ruby-on-rails - CSV.foreach 内存使用量不断增加