【问题标题】:StarIO Line Mode C# SDK won't printStarIO Line Mode C# SDK 不会打印
【发布时间】:2023-03-13 01:19:01
【问题描述】:

我一直在使用 2 台 StarIO 打印机(TSP100 USB 和 LAN)。 使用 StarIO Line Mode C# SDK 使用端口名称“usbprn:Cashier”和“tcp:192.168.1.xxx”,两者都可以检测为在线。

然而,Print Sample Receipt 功能没有向打印机打印任何内容,调试显示完全没有问题。

其他 SDK(iOS、Android)也会发生同样的事情(检测为在线,但在打印时无声)。

那么,这是与打印机有关的问题吗(假设打印机在使用“配置实用程序 TSP100”应用程序的示例打印时效果很好)。

非常感谢任何帮助!

【问题讨论】:

  • 很难说你什么时候没有提供代码,没有日志,只是一个不会打印的描述。我认为我们需要更多信息来帮助您
  • 感谢@dman2306。我已尝试使用从此链接 (starmicronics.com/support/…) 下载的 C# SDK。代码不变。运行 SDK 应用程序后,我将端口名称输入为“usbprn:Cashier”(已将打印机队列名称配置为 Cashier)。应用程序检测到打印机在线,但打印什么也不做。感谢您的快速回复。
  • Chinh,我遇到了您在此处描述的相同问题。我想知道您是否找到了解决方案?

标签: c# printing


【解决方案1】:

@Sleette,抱歉回复晚了,通知在一堆通知电子邮件中。

我确实从常见问题解答网站提出了一个临时解决方案,那里有很多问答和示例代码,我找不到原始链接。但是有些代码“以某种方式”工作。

它不通过 SDK,而是通过 Window 的打印队列,这当然是“烂”,但它现在对我有用。

限制:您必须手动将 LAN 打印机添加到打印机队列并通过名称访问它。

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 pDocName, 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 = pDocName;
      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 SendStringToPrinter(string pDocName, 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.
      var result = SendBytesToPrinter(pDocName, szPrinterName, pBytes, dwCount);
      Marshal.FreeCoTaskMem(pBytes);
      return result;
    }
  }

【讨论】:

    猜你喜欢
    • 2021-10-22
    • 2017-01-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-08-21
    • 2015-04-04
    • 1970-01-01
    • 2015-08-10
    相关资源
    最近更新 更多