StarIO Line Mode C#SDK不会打印

时间:2016-04-08 01:24:06

标签: c# printing

我一直在使用2台StarIO打印机(TSP100 USB和LAN)。 使用StarIO线路模式C#SDK可以使用端口名称" usbprn:Cashier"来检测两者。和" tcp:192.168.1.xxx"。

但是,“打印样本收据”功能不会向打印机打印任何内容,调试根本不会显示任何问题。

同样的事情(在网上检测到,但在打印时保持沉默)也发生在其他SDK(iOS,Android)上。

因此,这是一个与打印机有关的问题(假设打印机在应用程序中打印样本并且#34;配置实用程序TSP100")。

非常感谢任何帮助!

1 个答案:

答案 0 :(得分:1)

@Sleette,对于迟到的回复感到抱歉,通知内容深入到一堆通知电子邮件中。

我确实从FAQ网站上找到了一个临时解决方案,那里有很多Q& A和示例代码,我找不到原始链接。但是代码可以“某种程度上”起作用。

它没有通过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;
    }
  }