通过 C# 以(并行端口)点阵打印

发布于 2024-08-19 04:12:07 字数 151 浏览 5 评论 0原文

出于项目的需要,我想在特定位置打印 LPT1,这将在点阵打印机中打印文档,我应该在它们应该去的地方打印值。我真的很讨厌回去,我不知道从哪里开始。互联网上没有关于使用 C# 在 LPT 端口进行打印的具体信息,特别是如何在打印时发送特定位置的值。有什么好的例子吗?这个的教程?将是一个救星。

For the needs of a project, i want to print over the LPT1 in specific locations, this will print a document in a dot matrix printer where i should print values in the places they should go. I really hate going back, and i don't have any idea where to start. Internet has no specific information about printing in LPT port with C# and especially how to send the values in specific locations while printing. Is there any good example? tutorial for this? would be a life savior.

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

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

发布评论

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

评论(4

┾廆蒐ゝ 2024-08-26 04:12:07

我可以建议一件事让您的生活更轻松,安装通用文本打印机驱动程序(这是标准配置)并将其设置为 LPT1 端口。然后,您只需打开“LPT1”并发送转义代码序列来指定字体类型(粗体/斜体)、强调、字体间距等。我不知道是否需要资源。但我想它会是这样的:

using (System.IO.StreamWriter sr = new System.IO.StreamWriter(@"\\.\LPT1"))
{
    sr.Write(0x1b);
    sr.Write('k');
    sr.Write('1');
    sr.Write("Hello"); // print in Sans Serif
    sr.WriteLine();
    sr.Flush();
}

资源:

  • 打印到 使用 VB.NET 的斑马打印机(这可以轻松转换为 C# 或将其编译为 DLL 并在 C# 项目中引用它)
  • 关于 < a href="http://support.microsoft.com/kb/823179" rel="nofollow noreferrer">MSDN 了解如何连接 LPT1
  • 这里是有关 并行端口。 (进一步查看标题为“端口 I/O 和中断的编程工具”的部分),讨论这个名为 inpout32 的 DLL 的用法。
  • 这是 MSDN 上的另一篇文章,展示了如何进行原始打印。

于 2017-07-12 编辑:更新了并行端口链接以使用 Wayback Archive Machine。

I could suggest one thing to make your life easier, install a generic text printer driver (this comes as standard) and set that to the LPT1 port. Then you can simply open 'LPT1' and send escape code sequences to specify font type (bold/italic), emphasized, font pitch etc. I don't know if the resources would be required. But I would imagine it would be something like this:

using (System.IO.StreamWriter sr = new System.IO.StreamWriter(@"\\.\LPT1"))
{
    sr.Write(0x1b);
    sr.Write('k');
    sr.Write('1');
    sr.Write("Hello"); // print in Sans Serif
    sr.WriteLine();
    sr.Flush();
}

Resources:

  • Printing to a zebra printer using VB.NET (This can be easily translated to C# or compile it to a DLL and reference it in your C# project)
  • An article on MSDN on how to interface to LPT1
  • Here is an extensive list of info pertaining to Parallel Port. (look further down near the section titled 'Programming Tools for Port I/O and Interrupts'), discussing the usage of this DLL called inpout32.
  • Here is another article on MSDN that shows how to do raw printing.

Edited @ 2017-07-12: Updated the Parallel Port link to use the Wayback Archive Machine.

无悔心 2024-08-26 04:12:07

有问题的打印机没有 Windows 打印驱动程序吗?如果是这样,是否通过 LPT1 打印并不重要,它只是使用标准的打印内容。

类似的问题: C# 中的点阵打印?

Doesn't the printer in question have a windows print driver? If so, it doesn't matter that it is printing over LPT1 or not, it is just using the standard Print stuff.

Similar question: Dot Matrix printing in C#?

满天都是小星星 2024-08-26 04:12:07

如果您的打印机具有适用于 Windows 的驱动程序,则您可以使用标准打印技术。请参阅 Petzold 的书用 C# 编程 Microsoft Windows 以获得很好的介绍。

If your printer has drivers for Windows, then you can use standard printing techniques. See Petzold's book Programming Microsoft Windows with C# for a good intro.

毅然前行 2024-08-26 04:12:07

嘿,我在 2019 年刚买了一台点阵打印机,你仍然可以花 5 英镑购买色带。

using System.IO;
using System.Runtime.InteropServices;
using Microsoft.Win32.SafeHandles;

public class ParallelPrinter
{
    [DllImport("kernel32.dll", SetLastError = true)]
    static extern SafeFileHandle CreateFile(string lpFileName, FileAccess dwDesiredAccess, uint dwShareMode, IntPtr lpSecurityAttributes, FileMode dwCreationDisposition, uint dwFlagsAndAttributes, IntPtr hTemplateFile);

    public static void Print(string text)
    {
        using (SafeFileHandle fileHandle = CreateFile("LPT1:", FileAccess.Write, 0, IntPtr.Zero, FileMode.OpenOrCreate, 0, IntPtr.Zero))
        {
            if (fileHandle.IsInvalid == true)
                throw new ApplicationException("Printer is Invalid");

            using (FileStream stream = new FileStream(fileHandle, FileAccess.Write))
            {
                using (StreamWriter writer = new StreamWriter(stream, Encoding.ASCII))
                {
                    writer.Write(text);
                }
            }
        }
    }
}

无需驱动程序,您只需要一个并行端口,如果没有,您可以购买一张 PCI-e 卡。

Hey I just got a dot matrix printer in 2019 and you can still buy the ribbons for £5.

using System.IO;
using System.Runtime.InteropServices;
using Microsoft.Win32.SafeHandles;

public class ParallelPrinter
{
    [DllImport("kernel32.dll", SetLastError = true)]
    static extern SafeFileHandle CreateFile(string lpFileName, FileAccess dwDesiredAccess, uint dwShareMode, IntPtr lpSecurityAttributes, FileMode dwCreationDisposition, uint dwFlagsAndAttributes, IntPtr hTemplateFile);

    public static void Print(string text)
    {
        using (SafeFileHandle fileHandle = CreateFile("LPT1:", FileAccess.Write, 0, IntPtr.Zero, FileMode.OpenOrCreate, 0, IntPtr.Zero))
        {
            if (fileHandle.IsInvalid == true)
                throw new ApplicationException("Printer is Invalid");

            using (FileStream stream = new FileStream(fileHandle, FileAccess.Write))
            {
                using (StreamWriter writer = new StreamWriter(stream, Encoding.ASCII))
                {
                    writer.Write(text);
                }
            }
        }
    }
}

No drivers you just need a parallel port, you can get a PCI-e card if you don't have one.

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