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

发布于 2024-08-29 01:44:56 字数 1445 浏览 9 评论 0原文

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

打印机通过 lpt2usb 适配器通过 USB 连接,Windows 7 使用“通用 -> 通用/纯文本”驱动程序。

任何人都知道出了什么问题吗?怎么可能打印一些文字并进行一些换页呢?我必须发送一些控制字符吗?如果是的话:我如何发送它们?

编辑 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 - 抱歉,两个超链接的声誉不够)

编辑 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电缆才能使其再次工作。有什么想法吗?

I'm trying to print out raw ascii data to a thermal printer. I do this by using this code example: http://support.microsoft.com/kb/322091 but my printer prints always only one character and this not until I press the form feed button. If I print something with notepad the printer will do a form feed automatically but without printing any text.

The printer is connected via usb over a lpt2usb adapter and Windows 7 uses the "Generic -> Generic / Text Only" driver.

Anyone knows what is going wrong? How is it possible to print some words and do some form feeds? Are there some control characters I have to send? And if yes: How do I send them?

Edit 14.04.2010 21:51

My code (C#) looks like this:

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

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

This code will return a "T" after I pressed the form feed button (This litte black button here: swissmania.ch/images/935-151.jpg - sorry, not enough reputation for two hyperlinks)

Edit 15.04.2010 16:56

I'm using now the code form here: c-sharpcorner.com/UploadFile/johnodonell/PrintingDirectlytothePrinter11222005001207AM/PrintingDirectlytothePrinter.aspx

I modified it a bit that I can use the following code:

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

Running this code has the same effekt like pressing the form feed button, it works fine!

But code like this still does not work:

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

This will print out just a "B" but I expect "BC" and after running any code I have to reconnect the USB cable to make it work agian. Any ideas?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

临风闻羌笛 2024-09-05 01:44:56

快速分步解决方案

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

代码

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

来源:

Quick step by step solution

Because code was not provided, I make it work with help of provided links, and here is the code:

Code

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

Usage

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

Sources:

笑,眼淚并存 2024-09-05 01:44:56

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

现在我不得不希望我购买的其他一些 lpt2usb 适配器能够正常工作。

编辑 20.04.2010

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

Okay, the reason for all that stuff is just the fact that I use an adapter because my computer does not has an old lpt port. I copied my application to an old computer running windows xp and everything works fine.

Now I have to hope that some other lpt2usb adaters I bought do their work correctly.

Edit 20.04.2010

With another lpt2usb adapter everything works fine now. If anyone is intersted in all the code I am using now, please contact me or comment here.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文