使用.NET将原始数据打印到热敏打印机

时间:2010-04-14 16:01:08

标签: c# thermal-printer

我正在尝试将原始ascii数据打印到热敏打印机。我通过使用此代码示例执行此操作:http://support.microsoft.com/kb/322091但我的打印机始终只打印一个字符,直到我按下换页按钮。如果我用记事本打印东西,打印机会自动进行换页,但不打印任何文字。

打印机通过usb通过lpt2usb适配器连接,Windows 7使用“Generic - > Generic / Text Only”驱动程序。

任何人都知道出了什么问题?如何打印一些单词并做一些换页?我必须发送一些控制字符吗?如果是的话:我该如何发送它们?

编辑14.04.2010 21:51

我的代码(C#)如下所示:

PrinterSettings s =  new PrinterSettings();
s.PrinterName = "Generic / Text Only";

RawPrinterHelper.SendStringToPrinter(s.PrinterName, "Test");

按下换页按钮后,此代码将返回“T”(这里有一个黑色的按钮:swissmania.ch/images/935-151.jpg - 抱歉,两个超链接的声誉不够)

编辑15.04.2010 16:56

我现在在使用代码表格:c-sharpcorner.com/UploadFile/johnodonell/PrintingDirectlytothePrinter11222005001207AM/PrintingDirectlytothePrinter.aspx

我修改了一下,我可以使用以下代码:

byte[] toSend;
// 10 = line feed
// 13 carriage return/form feed
toSend = new byte[1] { 13 };
PrintDirect.WritePrinter(lhPrinter, toSend, toSend.Length, ref pcWritten);

运行此代码与按下换页按钮具有相同的效果,它工作正常!

但是这样的代码仍然不起作用:

byte[] toSend;
// 10 = line feed
// 13 carriage return/form feed
toSend = new byte[2] { 66, 67 };
PrintDirect.WritePrinter(lhPrinter, toSend, toSend.Length, ref pcWritten);

这将打印出一个“B”但我希望“BC”在运行任何代码后我必须重新连接USB电缆以使其工作。有什么想法吗?

2 个答案:

答案 0 :(得分:6)

快速一步一步的解决方案

因为没有提供代码,所以我在提供的链接帮助下工作,这里是代码:

代码

using System;
using System.Runtime.InteropServices;
using System.Windows;

[StructLayout(LayoutKind.Sequential)]
public struct DOCINFO {
    [MarshalAs(UnmanagedType.LPWStr)]
    public string pDocName;
    [MarshalAs(UnmanagedType.LPWStr)] 
    public string pOutputFile;
    [MarshalAs(UnmanagedType.LPWStr)] 
    public string pDataType;
}

public class PrintDirect {
    [DllImport("winspool.drv", CharSet = CharSet.Unicode, ExactSpelling = false, CallingConvention = CallingConvention.StdCall)]
    public static extern long OpenPrinter(string pPrinterName, ref IntPtr phPrinter, int pDefault);

    [DllImport("winspool.drv", CharSet = CharSet.Unicode, ExactSpelling = false, CallingConvention = CallingConvention.StdCall)]
    public static extern long StartDocPrinter(IntPtr hPrinter, int Level, ref DOCINFO pDocInfo);

    [DllImport("winspool.drv", CharSet = CharSet.Unicode, ExactSpelling = true, CallingConvention = CallingConvention.StdCall)]
    public static extern long StartPagePrinter(IntPtr hPrinter);

    [DllImport("winspool.drv", CharSet = CharSet.Ansi, ExactSpelling = true, CallingConvention = CallingConvention.StdCall)]
    public static extern long WritePrinter(IntPtr hPrinter, string data, int buf, ref int pcWritten);

    [DllImport("winspool.drv", CharSet = CharSet.Unicode, ExactSpelling = true, CallingConvention = CallingConvention.StdCall)]
    public static extern long EndPagePrinter(IntPtr hPrinter);

    [DllImport("winspool.drv", CharSet = CharSet.Unicode, ExactSpelling = true, CallingConvention = CallingConvention.StdCall)]
    public static extern long EndDocPrinter(IntPtr hPrinter);

    [DllImport("winspool.drv", CharSet = CharSet.Unicode, ExactSpelling = true, CallingConvention = CallingConvention.StdCall)]
    public static extern long ClosePrinter(IntPtr hPrinter);
}

private void Print(String printerAddress, String text, String documentName) {
    IntPtr printer = new IntPtr();

    // A pointer to a value that receives the number of bytes of data that were written to the printer.
    int pcWritten = 0;

    DOCINFO docInfo = new DOCINFO();
    docInfo.pDocName = documentName;
    docInfo.pDataType = "RAW";

    PrintDirect.OpenPrinter(printerAddress, ref printer, 0);
    PrintDirect.StartDocPrinter(printer, 1, ref docInfo);
    PrintDirect.StartPagePrinter(printer);

    try {
    PrintDirect.WritePrinter(printer, text, text.Length, ref pcWritten);
    } catch (Exception e) {
        Console.WriteLine(e.Message);
    }

    PrintDirect.EndPagePrinter(printer);
    PrintDirect.EndDocPrinter(printer);
    PrintDirect.ClosePrinter(printer);
}

用法

String printerAddress = "\\\\ComputerName\\PrinterName";
String documentName = "My document";
String documentText = "This is an example of printing directly to a printer.";

this.Print(printerAddress,documentText,documentName);

来源:

答案 1 :(得分:5)

好吧,所有这些东西的原因只是我使用适配器的事实,因为我的电脑没有旧的lpt端口。我将我的应用程序复制到运行Windows XP的旧计算机上,一切正常。

现在我不得不希望我买的其他一些lpt2usb adater能正常工作。

编辑20.04.2010

使用另一个lpt2usb适配器现在一切正常。如果有人对我现在使用的所有代码感兴趣,请与我联系或在此发表评论。