14

Zebra TTP8200サーマル プリンターを試しています。私のアプリケーションでは、ユーザーが停止ボタンを押すまで、プロッター タイプのトレースを継続的に出力する必要があります。私は ZPL 言語を試してみましたが、ZPL を生データとして出力することで、ビットマップ データを正常に生成し、一度に 1 行 (または数行) ずつビットマップをダンプすることができます。

私はいくつかのMicrosoft デモ コードを使用して生データをプリンターに出力していますが、これはうまく機能します。MS rawprn.exe コードを使用してデータを出力するたびに、実際には印刷ジョブとしてスプールされてからプリンターに送信されることがわかりました。これは、スプーラーを通過するのに最大 10 秒かかり、明らかに遅すぎます。ドライバーでスプーリングを無効にしても効果はありません。ジョブがスプーラーを通過して印刷が完了するまでの間、プログラムがハングアップするだけです。

スプーラーをバイパスして、この USB プリンターに直接データを出力する方法はありますか? これまでの私の調査では、Windows API を調べている可能性のあるものは見つかりませんでした。理想的には、プリンターをシリアルプリンターのように使用できるようにしたいと思います-ポートを開いてデータを押し込みます.

ヒントをお寄せいただきありがとうございます。

4

4 に答える 4

2

USBプリンタがCOMポートとして使用できる場合は、COMポートに書き込むだけです。このように、DOSプロンプトから:

dir > com1

前者の例では、dirコマンドの結果をプリンターに出力します。

または、別の例を次に示します。

copy file.txt com1

前者の例では、の内容をfile.txtプリンターに出力します。

適切にフォーマットされたZPLデータを出力することは、プレーンテキストよりも困難になります。ただし、Ruby(およびEpson / ESCコマンド)を使用してLinuxからこれを機能させることができました。

于 2011-06-09T16:31:18.270 に答える
2

スプーラーをバイパスして、この USB プリンターに直接データを出力する方法はありますか?

そのとおり。これはほとんどの OS に組み込まれており、USB 経由で raw を印刷することは、イーサネットや COM/LPT ほど明白ではありません。メモ帳などの多くのアプリケーションはそのまま印刷できないため、アプリケーションもこれをサポートする必要があることに注意してください。

  1. USB プリンターに適切なドライバーをインストールします。プリンターのプロパティをチェックして、使用している USB ポートを確認します。USB001などの可能性があります。
  2. [デバイスとプリンター] を使用して、2 つ目のプリンターを追加します。ローカル ポート、作成したばかりのポート (つまり、USB001) を選択します。注: Windows の一部のバージョンには、自動検出するチェックボックスがあります。ある場合は、これをオフにします。
  3. メーカー: 汎用、プリンター: 汎用 / テキストのみ
  4. 現在インストールされているドライバーを使用する
  5. プリンタに、すでに作成されているものと区別できる名前を付けます (例: Zebra TTP8200 Raw)。
  6. 共有しない
  7. テストページを印刷しない、終了

raw 印刷アプリケーションで、新しく作成したプリンターを使用します。

PS これらの手順は、Java オープン ソースの raw 印刷チュートリアルの一部として、スクリーンショットと共にこちらからも入手できます。このプロジェクトは、他のプラットフォーム (Ubuntu、OS X) 用のチュートリアルも提供します。

http://qzindustries.com/TutorialRawWin

-トレス

于 2014-01-14T19:44:05.687 に答える
0

以下の C# のクラスは、Microsoft ナレッジベースの記事から引用したものです。このクラスには、印刷ジョブをstringおよびとして送信するメソッドがありbyte[]ます。そこには、選択したロギング フレームワークで削除/置換できる log4net への参照がいくつかあることに注意してください。:

/// <summary>
/// Class used to aid in sending raw printer data (PS, PRN, etc) directly to the printer.
/// This class was taken from http://support.microsoft.com/kb/322091
/// </summary>
public class PrintQueueUtility
{
    private static ILog log = LogManager.GetLogger(typeof(PrintQueueUtility));

    [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);

    /// <summary>Method which sends a <see langword="byte"/> array to a printer queue with a specific document name.</summary>
    /// <param name="bytes">Byte array to send to the printer.</param>
    /// <param name="printerName">Name of the printer to send the <paramref name="bytes"/> to.</param>
    /// <param name="documentName">The document Name.</param>
    /// <returns><see cref="bool"/> indicating whether or not the method succeeded at adding something to the print queue.</returns>
    public static bool SendBytesToPrinter(byte[] bytes, string printerName, string documentName)
    {
        bool success;

        // Allocate some unmanaged memory for those bytes into an unmanaged pointer.
        IntPtr unmanagedBytes = Marshal.AllocCoTaskMem(bytes.Length);

        // Copy the managed byte array into the unmanaged array.
        Marshal.Copy(bytes, 0, unmanagedBytes, bytes.Length);

        // Send the unmanaged bytes to the printer.
        success = SendUnmanagedBytesToPrinter(unmanagedBytes, printerName, documentName, bytes.Length);

        // Free the unmanaged memory that you allocated earlier.
        Marshal.FreeCoTaskMem(unmanagedBytes);

        return success;
    }

    /// <summary>Method which sends a string to the printer queue with a specific document name.</summary>
    /// <param name="data"><see cref="String"/> data to send to the printer.</param>
    /// <param name="printerName">Name of the printer to send the data to.</param>
    /// <param name="documentName">Name of the document in the printer queue.</param>
    /// <returns><see cref="bool"/> indicating whether or not the method succeeded at adding something to the print queue.</returns>
    public static bool SendStringToPrinter(string data, string printerName, string documentName)
    {
        bool success;
        IntPtr unmanagedBytes;

        // How many characters are in the string?
        var characterCount = data.Length;

        // Assume that the printer is expecting ANSI text, and then convert
        // the string to ANSI text.
        unmanagedBytes = Marshal.StringToCoTaskMemAnsi(data);

        // Send the converted ANSI string to the printer.
        success = SendUnmanagedBytesToPrinter(unmanagedBytes, printerName, documentName, characterCount);
        Marshal.FreeCoTaskMem(unmanagedBytes);

        return success;
    }

    private static bool SendUnmanagedBytesToPrinter(IntPtr unmanagedBytes, string printerName, string documentName, int count)
    {
        int error; 
        int written;
        IntPtr printer;
        var di = new DOCINFOA();
        var success = false;

        di.pDocName = documentName;
        di.pDataType = "RAW";

        // Open the printer.
        if (OpenPrinter(printerName.Normalize(), out printer, IntPtr.Zero))
        {
            // Start a document.
            if (StartDocPrinter(printer, 1, di))
            {
                // Start a page.
                if (StartPagePrinter(printer))
                {
                    // Write the bytes.
                    success = WritePrinter(printer, unmanagedBytes, count, out written);
                    EndPagePrinter(printer);
                }

                EndDocPrinter(printer);
            }

            ClosePrinter(printer);
        }

        // If you did not succeed, GetLastError may give more information
        // about why not.
        if (!success)
        {
            error = Marshal.GetLastWin32Error();

            log.ErrorFormat("Sending bytes to printer {0} failed. Last Win32 error = {1}", printerName, error);
        }

        return success;
    }

    // Structure and API declarations:
    [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;
    }
于 2011-06-15T18:26:17.223 に答える
0

コメントありがとうございます。

さらに調べてみると、usbprint.sys が提供する Windows プリンター機能の使用に関する興味深い記事を見つけました。サンプルコードを少しハッキングすると、動作するように見えました。この道を行くと思います。

記事に記載されている最終的なコードがあります。

/* Code to find the device path for a usbprint.sys controlled
* usb printer and print to it
*/

#include <usb.h>
#include <usbiodef.h>
#include <usbioctl.h>
#include <usbprint.h>
#include <setupapi.h>
#include <devguid.h>
#include <wdmguid.h>

/* This define is required so that the GUID_DEVINTERFACE_USBPRINT variable is
 * declared an initialised as a static locally, since windows does not include it
 * in any of its libraries
 */

#define SS_DEFINE_GUID(name, l, w1, w2, b1, b2, b3, b4, b5, b6, b7, b8) \
static const GUID name \
= { l, w1, w2, { b1, b2,  b3,  b4,  b5,  b6,  b7,  b8 } }

SS_DEFINE_GUID(GUID_DEVINTERFACE_USBPRINT, 0x28d78fad, 0x5a12, 0x11D1, 0xae,
               0x5b, 0x00, 0x00, 0xf8, 0x03, 0xa8, 0xc2);

void SomeFunctionToWriteToUSB()
{
  HDEVINFO devs;
  DWORD devcount;
  SP_DEVINFO_DATA devinfo;
  SP_DEVICE_INTERFACE_DATA devinterface;
  DWORD size;
  GUID intfce;
  PSP_DEVICE_INTERFACE_DETAIL_DATA interface_detail;

  intfce = GUID_DEVINTERFACE_USBPRINT;
  devs = SetupDiGetClassDevs(&intfce, 0, 0, DIGCF_PRESENT |
                             DIGCF_DEVICEINTERFACE);
  if (devs == INVALID_HANDLE_VALUE) {
    return;
  }
  devcount = 0;
  devinterface.cbSize = sizeof(SP_DEVICE_INTERFACE_DATA);
  while (SetupDiEnumDeviceInterfaces(devs, 0, &intfce, devcount, &devinterface)) {
    /* The following buffers would normally be malloced to he correct size
     * but here we just declare them as large stack variables
     * to make the code more readable
     */
    char driverkey[2048];
    char interfacename[2048];
    char location[2048];
    char description[2048];

    /* If this is not the device we want, we would normally continue onto the
     * next one or so something like
     * if (!required_device) continue; would be added here
     */
    devcount++;
    size = 0;
    /* See how large a buffer we require for the device interface details */
    SetupDiGetDeviceInterfaceDetail(devs, &devinterface, 0, 0, &size, 0);
    devinfo.cbSize = sizeof(SP_DEVINFO_DATA);
    interface_detail = calloc(1, size);
    if (interface_detail) {
      interface_detail->cbSize = sizeof (SP_DEVICE_INTERFACE_DETAIL_DATA);
      devinfo.cbSize = sizeof(SP_DEVINFO_DATA);
      if (!SetupDiGetDeviceInterfaceDetail(devs, &devinterface, interface_detail,
                                           size, 0, &devinfo)) {
    free(interface_detail);
    SetupDiDestroyDeviceInfoList(devs);
    return;
      }
      /* Make a copy of the device path for later use */
      strcpy(interfacename, interface_detail->DevicePath);
      free(interface_detail);
      /* And now fetch some useful registry entries */
      size = sizeof(driverkey);
      driverkey[0] = 0;
      if (!SetupDiGetDeviceRegistryProperty(devs, &devinfo, SPDRP_DRIVER, &dataType,
                                            (LPBYTE)driverkey, size, 0)) {
    SetupDiDestroyDeviceInfoList(devs);
    return;
      }
      size = sizeof(location);
      location[0] = 0;
      if (!SetupDiGetDeviceRegistryProperty(devs, &devinfo,
                                            SPDRP_LOCATION_INFORMATION, &dataType,
                                            (LPBYTE)location, size, 0)) {
    SetupDiDestroyDeviceInfoList(devs);
    return;
      }
      usbHandle = CreateFile(interfacename, GENERIC_WRITE, FILE_SHARE_READ,
                 NULL, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL |
                 FILE_FLAG_SEQUENTIAL_SCAN, NULL);
      if (usbHandle != INVALID_HANDLE_VALUE) {
    /* Now perform all the writing to the device ie.
     * while (some condition) WriteFile(usbHandle, buf, size, &bytes_written);
     */
    CloseHandle(usbHandle);
      }
    }
  }
  SetupDiDestroyDeviceInfoList(devs);
}

提案をありがとう。

于 2011-06-20T09:03:46.740 に答える