通过 C#.NET 将 ZPL 打印到 Zebra 打印机

Print ZPL to Zebra Printer via C#.NET

我正在构建一个网站,该网站将 ZPL 代码打印到 Zebra 打印机“140XiIII”。具体如下:

  1. 打印机通过并行端口连接到计算机。

  2. 打印机未连接到网络。

  3. 网站管理员希望尽可能避免Javascript使用,而是专注于 C#。

我已经尝试使用 .NET code to send ZPL to Zebra printers 中的代码,该代码声明它可以在任何端口上运行。我 运行 通过代码没有得到任何错误,但也没有任何东西被发送到打印机。

我尝试破译和使用 InpOut32/64 DLL,尽管这比我微薄的知识能够理解的要复杂得多。

其他选项要么需要打印机连接到网络,要么已经过时并且不再是选项,因为不再包含在 Windows 代码中。

谢谢。

我建议尝试以下操作:

  • 登录服务器,最好是服务设置为运行的用户。
  • 确保您的 Zebra 打印机安装为本地打印机,并且打印机名称正确。
  • 从打印机属性打印测试页。
  • 手动打印您的 ZPL:

net use lpt1 "printer shared name" print "C:\Users\serviceuser\desktop\label.txt"

如果您尝试上述方法并且有效,我会感到惊讶的是您链接的代码不起作用。

我为这种情况开发了一个 websockt ZPLwebSocket

  1. 下载文件并解压。
  2. 运行 SetUp.exe 在客户端 PC 上。
  3. 转到服务并启动 'Servicio de Impresion desde Navegador'
  4. 使用 GoogleChrome 或 mozilla
  5. 打开 trysocket.html
  6. 写入本地打印机的名称
  7. 点击按钮'enviar'
  8. 显示服务器消息

    这是页面上的 javascript 代码。

    $(document).ready(function () {  var connection = new WebSocket('ws://localhost:2645/service/');  const reader = new FileReader();
    
      $('.sendZPL').on('click', function () {
    
        var tosend = {
          TipoArchivo: 0,
          nombre: "Etiqueta de Prueba",
          code: $(".sendZPL").val(),
          Tipo: 4,
          Impresora: $(".impresora").val()
        }
        connection.send(JSON.stringify(tosend));
      });
    
      connection.addEventListener('message', function (event) {
        reader.readAsText(event.data);
      });
    
      reader.addEventListener('loadend', (e) => {
        const text = e.srcElement.result;
        console.log(text);
        $(".serverResponse").val(text);
      });
    
    });
    

这是 ConsoleApp 上 运行 的套接字代码 c#:

class Program
{

    private static Server.WebsocketServer websocketServer;
    private static System.Diagnostics.EventLog eventLog1= new EventLog();
    static void Main( string[] args )
    {
        if (!System.Diagnostics.EventLog.SourceExists( "MySource" ))
        {
            System.Diagnostics.EventLog.CreateEventSource(
                "MySource", "MyNewLog" );
        }

        eventLog1.Source = "MySource";
        eventLog1.Log = "MyNewLog";


        websocketServer = new Server.WebsocketServer();
        websocketServer.LogMessage += WebsocketServer_LogMessage; 

        websocketServer.Start( "http://localhost:2645/tryservice/" );

        Console.Read();
    }



    private static void WebsocketServer_LogMessage( object sender, Server.WebsocketServer.LogMessageEventArgs e )
    {
        // eventLog1.WriteEntry( e.Message );
        Console.ForegroundColor = ConsoleColor.Yellow;
        Console.WriteLine( e.Message );
        Console.ForegroundColor = ConsoleColor.White;
    }
public  class WebsocketServer
{
    public event OnLogMessage LogMessage;
    public delegate void OnLogMessage(Object sender, LogMessageEventArgs e);
    public class LogMessageEventArgs : EventArgs
    {
        public string Message { get; set; }
        public LogMessageEventArgs(string Message) {
            this.Message = Message;
        }
    }

    public bool started = false;
    public async void Start(string httpListenerPrefix)
    {
        HttpListener httpListener = new HttpListener();
        httpListener.Prefixes.Add(httpListenerPrefix);
        httpListener.Start();
        LogMessage(this, new LogMessageEventArgs("Listening..."));
        started = true;

        while (started)
        {
            HttpListenerContext httpListenerContext = await httpListener.GetContextAsync();
            if (httpListenerContext.Request.IsWebSocketRequest)
            {
                ProcessRequest(httpListenerContext);
            }
            else
            {
                httpListenerContext.Response.StatusCode = 400;
                httpListenerContext.Response.Close();
                LogMessage(this, new LogMessageEventArgs("Closed..."));
            }
        }
    }

    public void Stop()
    {
        started = false;
    }

    private List<string> _printers = new List<string>();
    public List<string> Printers { get
        {
            _printers.Clear();
            foreach (string imp in System.Drawing.Printing.PrinterSettings.InstalledPrinters)
            {
                _printers.Add(imp);
            }
            return _printers;
        }
    }

    private async void ProcessRequest(HttpListenerContext httpListenerContext)
    {
        WebSocketContext webSocketContext = null;

        try
        {
            webSocketContext = await httpListenerContext.AcceptWebSocketAsync(subProtocol: null);
            LogMessage(this, new LogMessageEventArgs("Connected"));
        }
        catch (Exception e)
        {
            httpListenerContext.Response.StatusCode = 500;
            httpListenerContext.Response.Close();
            LogMessage(this, new LogMessageEventArgs(String.Format("Exception: {0}", e)));
            return;
        }

        WebSocket webSocket = webSocketContext.WebSocket;
        try
        {


            while (webSocket.State == WebSocketState.Open)
            {

                ArraySegment<Byte> buffer = new ArraySegment<byte>(new Byte[8192]);

                WebSocketReceiveResult result = null;

                using (var ms = new System.IO.MemoryStream())
                {
                    do
                    {
                        result = await webSocket.ReceiveAsync(buffer, CancellationToken.None);
                        ms.Write(buffer.Array, buffer.Offset, result.Count);
                    }
                    while (!result.EndOfMessage);

                    ms.Seek(0, System.IO.SeekOrigin.Begin);

                    if (result.MessageType == WebSocketMessageType.Text)
                    {
                        using (var reader = new System.IO.StreamReader(ms, Encoding.UTF8))
                        {
                            var r = System.Text.Encoding.UTF8.GetString(ms.ToArray());
                            var t = Newtonsoft.Json.JsonConvert.DeserializeObject<Datos>(r);
                            bool valid = true;
                            byte[] toBytes = Encoding.UTF8.GetBytes("Error..."); ;

                            if (t != null)
                            {
                                if (t.Impresora.Trim() == string.Empty)
                                {
                                    var printers = "";
                                    Printers.ForEach(print => {
                                        printers += print + "\n";
                                    });

                                    toBytes = Encoding.UTF8.GetBytes("No se Indicó la Impresora\nLas Impresoras disponibles son:\n" + printers);
                                    valid = false;
                                }
                                else if(!Printers.Contains(t.Impresora))
                                {
                                    var printers = "";
                                    Printers.ForEach(print =>
                                    {
                                        printers += print + "\n";
                                    });

                                    toBytes = Encoding.UTF8.GetBytes("Impresora no valida\nLas Impresoras disponibles son:\n" + printers);
                                    valid = false;
                                }


                                if (t.nombre.Trim() == string.Empty)
                                {
                                    toBytes = Encoding.UTF8.GetBytes("No se Indicó el nombre del Documento");
                                    valid = false;
                                }
                                if (t.code== null)
                                {
                                    toBytes = Encoding.UTF8.GetBytes("No hay datos para enviar a la Impresora");
                                    valid = false;
                                }


                                if (valid && print.RawPrinter.SendStringToPrinter(t.Impresora, t.code, t.nombre))
                                {
                                    LogMessage(this, new LogMessageEventArgs(String.Format("Enviado: {0} => {1} => {2}", t.Impresora, t.nombre, t.code)));
                                    toBytes = Encoding.UTF8.GetBytes("Correcto...");
                                }

                                await webSocket.SendAsync(new ArraySegment<byte>(toBytes, 0, int.Parse(toBytes.Length.ToString())), WebSocketMessageType.Binary, result.EndOfMessage, CancellationToken.None);


                            }
                            else
                            {
                                toBytes = Encoding.UTF8.GetBytes("Error...");
                                await webSocket.SendAsync(new ArraySegment<byte>(toBytes, 0, int.Parse(toBytes.Length.ToString())), WebSocketMessageType.Binary, result.EndOfMessage, CancellationToken.None);
                            }
                        }
                    }
                }
            }
        }
        catch (Exception e)
        {
            LogMessage(this, new LogMessageEventArgs(String.Format("Exception: {0} \nLinea:{1}", e, e.StackTrace)));
        }
        finally
        {
            if (webSocket != null)
                webSocket.Dispose();
        }
    }


}

public class Datos {
    public enum TipoArchivo { zpl, pdf, doc, json, text}
    public string nombre { get; set; }
    public string code { get; set; }
    public TipoArchivo Tipo { get; set; } = TipoArchivo.text;
    public string Impresora { get; set; } = "";
}


}

RAWPrinterClass link:

 public class RawPrinter
    {
        // 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, ref IntPtr hPriknter, 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, ref 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, string DocName = "")
        {
            Int32 dwError = 0;
            Int32 dwWritten = 0;
            IntPtr hPrinter = new IntPtr(0);
            DOCINFOA di = new DOCINFOA();
            bool bSuccess = false;
            // Assume failure unless you specifically succeed.
            di.pDocName = string.IsNullOrEmpty(DocName) ? "My C#.NET RAW Document" : DocName;
            di.pDataType = "RAW";

            // Open the printer.
            if (OpenPrinter(szPrinterName.Normalize(), ref 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, ref 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 = 0;

            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, string DocName = "")
        {
            IntPtr pBytes = default(IntPtr);
            Int32 dwCount = default(Int32);
            // 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.
            var t= SendBytesToPrinter(szPrinterName, pBytes, dwCount, DocName);
            Marshal.FreeCoTaskMem(pBytes);
            return t;
        }
    }

对于我们要涵盖的特定情况,似乎正确的答案是,鉴于我们所受的限制,无法使用我们的 zebra 打印机进行打印。由于这台打印机未连接到网络,我们必须使用 stand-alone 桌面应用程序来完成我们需要的工作。

感谢所有提供帮助和想法的人。