题
我正在尝试将原始 ascii 数据打印到热敏打印机上。我通过使用以下代码示例来做到这一点: http://support.microsoft.com/kb/322091 但我的打印机总是只打印一个字符,直到我按下换页按钮。如果我用记事本打印某些内容,打印机将自动换页,但不打印任何文本。
打印机通过 lpt2usb 适配器通过 USB 连接,Windows 7 使用“通用 -> 通用/仅文本”驱动程序。
任何人都知道出了什么问题吗?怎么可能打印一些文字并进行一些换页呢?我必须发送一些控制字符吗?如果是的话:我如何发送它们?
编辑 2010年4月14日 21:51
我的代码(C#)如下所示:
PrinterSettings s = new PrinterSettings();
s.PrinterName = "Generic / Text Only";
RawPrinterHelper.SendStringToPrinter(s.PrinterName, "Test");
在我按下换页按钮后,此代码将返回一个“T”(这里的小黑色按钮:swissmania.ch/images/935-151.jpg - 抱歉,两个超链接的声誉不够)
编辑 2010年4月15日 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
好了,所有的东西的原因仅仅是我使用的适配器,因为我的电脑不有老LPT端口的事实。我复制我的应用程序,以一台旧电脑运行Windows XP和一切工作正常。
现在我不得不希望其他一些lpt2usb adaters我买了正确的做好自己的工作。
修改20.04.2010 强>
通过另一lpt2usb适配器现在一切工作正常。如果有人在所有我现在使用的代码位数的,请与我联系或留言在这里。
其他提示
快速一步步解决方案
由于未提供代码,我在提供的链接的帮助下使其工作,代码如下:
代码
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(打印机地址, 文档文本, 文档名称);