【问题标题】:Sending raw data/printer commands to Printer将原始数据/打印机命令发送到打印机
【发布时间】:2014-11-28 11:12:18
【问题描述】:

我知道这可能/稍微在这里偏离主题,但它与对点阵打印机编程有关。

我正在尝试在 OKI Microline 5520 上生成一个新字符,并尝试为此使用命令行。

我要发送到打印机的命令是:

CHR$(27);%a;@;CHR$(28);CHR$(34);CHR$(65);CHR$(0);CHR$(65);CHR$(0);CHR$(28);
CHR$(34);CHR$(73);CHR$(0);CHR$(73);

哪个应该创建CE 符号而不是@ 字符。

+-+-+-+-+-+-+-+-+-+-+-+
     X   X       X   X
+-+-+-+-+-+-+-+-+-+-+-+
   X           X
+-+-+-+-+-+-+-+-+-+-+-+
 X           X       
+-+-+-+-+-+-+-+-+-+-+-+
 X           X   X   X
+-+-+-+-+-+-+-+-+-+-+-+
 X           X
+-+-+-+-+-+-+-+-+-+-+-+
   X          X
+-+-+-+-+-+-+-+-+-+-+-+
     X   X       X   X
+-+-+-+-+-+-+-+-+-+-+-+
28| 65| 65| 28| 73| 73|
  |34 |0  |0  |34 |0

但是,我似乎无法以打印机可以理解的方式将此命令添加/发送到打印机。

我在命令提示符下尝试该命令:

net use Lpt1 \\ComputerName\\datFileName

但这似乎不起作用。

有人对我如何将这个命令发送到这台点阵打印机有任何建议吗?

【问题讨论】:

    标签: printing command-prompt dot-matrix


    【解决方案1】:

    事实证明,这些打印机有多种模式,因此,它需要传递一个特定十六进制字符串。

    根据是 9 针还是 24 针也有不同的命令,因此,需要与documentation 进行大量斗争才能找出要发送的命令。

    我最终使用类似于these 的命令将命令写入内存。

    程序看起来类似于this,十六进制数据被发送到打印机

     [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 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 = "My C#.NET RAW Document";
            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;
    

    【讨论】:

      【解决方案2】:

      此命令可能会有所帮助:copy /B afile.bin LPT1:,其中afile.bin 应包含您的打印机命令字符串。在十六进制编辑器中查看(×ascii 显示中显示任何不可打印的字符):

      hexa  1B2561401C22410041001C22490049
      ascii  × % a @ × " A × A × × " I × I
      

      这个copy /B afile.bin LPT1:,从命令行执行,没有带来任何麻烦。

      但在将文件发送到打印机之前,许多文字处理器发送 向打印机发送“初始化字符串”或I-Prime 信号。您的打印机用户指南应提供忽略重置代码并消除可能由您的文字处理器引起的I-Prime 信号问题的程序。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2020-01-03
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-01-27
        • 1970-01-01
        • 1970-01-01
        • 2011-02-08
        相关资源
        最近更新 更多