简体   繁体   English

我正在尝试通过 USB 将 esc/pos 数据发送到 C# 中的 VKP80II 打印机

[英]I'm trying to send esc/pos data to a VKP80II printer in C# trough usb

im trying to send a string data to my vkp80ii printer, i did this with a raw binary file and it printed but when i try to use string it doesn't od anything.我正在尝试将字符串数据发送到我的 vkp80ii 打印机,我使用原始二进制文件执行此操作并打印出来,但是当我尝试使用字符串时,它没有任何内容。 Any help?有什么帮助吗?

class VKP80II_Driver
{

    static USBH_Printer printer;
    static AutoResetEvent printerConnected = new AutoResetEvent(false);

    public static void Main()
    {
        // Subscribe to USBH event.
        USBHostController.DeviceConnectedEvent += DeviceConnectedEvent;

        // wait for printer to be connectoed.
        printerConnected.WaitOne();

        // Get file to print
        //byte[] buffer = Resources.GetBytes(Resources.BinaryResources.beep);
        byte[] buffer = StrToByteArray("$0AHello World");

        // Printing can take a long time, give it a 5 seconds timeout here
        printer.SendData(buffer, 0, buffer.Length, 5000);

        // Sleep forever
        Thread.Sleep(Timeout.Infinite);
    }

    // Is printer connected event
    static void DeviceConnectedEvent(USBH_Device device)
    {
        if (device.TYPE == USBH_DeviceType.Printer)
        {
            printer = new USBH_Printer(device);
            printerConnected.Set();
        }
    }

    // Parse string object to byte array
    static byte [] StrToByteArray(string str)
    {
        System.Text.UTF8Encoding encoding = new System.Text.UTF8Encoding();
        return encoding.GetBytes(str);
    }
}

answer to the question回答问题

is i had to send real bytes to to printer我是否必须将实际字节发送到打印机

example:例子:

byte[]  newByte = new bytes [] {0x0A};

printer.SendData(newByte, 0, buffer.Length, 5000);

When I need such a functionality I couldn't find any library related to that.当我需要这样的功能时,我找不到任何与此相关的库。 So I decided to make one.所以我决定做一个。

ESC-POS-USB-NET is a free and open source .NET (C#) Implementation of the Epson ESC/POS Printing using USB Device Driver. ESC-POS-USB-NET是使用 USB 设备驱动程序的 Epson ESC/POS 打印的免费开源 .NET (C#) 实现。

This library is available Open Source @ MIT license.这个库是可用的开源@MIT 许可证。

You can install from Nuget Packages您可以从Nuget 包安装

follow the steps below:请按照以下步骤操作:

⏳ Installation Install Strapi with this Quickstart command to create a project instantly: ⏳ 安装 使用此 Quickstart 命令安装 Strapi 以立即创建项目:

(Use nuget package manager to install (recommended)) (使用nuget包管理器安装(推荐))

Install-Package ESC-POS-USB-NET

or或者

(Use .Net Cli to install) (使用 .Net Cli 安装)

dotnet add package ESC-POS-USB-NET

This command install ESC-POS-USB-NET with your project.此命令在您的项目中安装 ESC-POS-USB-NET。

Enjoy 🎉享受🎉

❤️ Example Using C# Import ESC_POS_USB_NET Printer Class: ❤️ 使用 C# 导入 ESC_POS_USB_NET 打印机类的示例:

using ESC_POS_USB_NET.Printer;

You can find printer name from (Windows): Control Panel->Hardware and Sound->Devices and Printers-> Your Printer's Name您可以从 (Windows) 中找到打印机名称:控制面板->硬件和声音->设备和打印机->您的打印机名称

Test Print:测试打印:

Printer printer = new Printer("Printer Name");
printer.TestPrinter();
printer.FullPaperCut();
printer.PrintDocument();

Print Image:打印图像:

Printer printer = new Printer("Printer Name");
Bitmap image =new Bitmap ( Bitmap.FromFile("Icon.bmp"));
printer.Image(image);
printer.FullPaperCut();
printer.PrintDocument();

Print Barcodes:打印条码:

Printer printer = new Printer("Printer Name");
printer.Append("Code 128");
printer.Code128("123456789");
printer.Separator();
printer.Append("Code39");
printer.Code39("123456789");
printer.Separator();
printer.Append("Ean13");
printer.Ean13("1234567891231");
printer.FullPaperCut();
printer.PrintDocument();

Open Drawer:打开抽屉:

Printer printer = new Printer("Printer Name");
printer.OpenDrawer();
printer.PrintDocument();

Typography Test:排版测试:

Printer printer = new Printer("Printer Name");
printer.Append("NORMAL - 48 COLUMNS");
printer.Append("1...5...10...15...20...25...30...35...40...45.48");
printer.Separator();
printer.Append("Text Normal");
printer.BoldMode("Bold Text");
printer.UnderlineMode("Underlined text");
printer.Separator();
printer.ExpandedMode(PrinterModeState.On);
printer.Append("Expanded - 23 COLUMNS");
printer.Append("1...5...10...15...20..23");
printer.ExpandedMode(PrinterModeState.Off);
printer.Separator();
printer.CondensedMode(PrinterModeState.On);
printer.Append("Condensed - 64 COLUMNS");
printer.Append("1...5...10...15...20...25...30...35...40...45...50...55...60..64");
printer.CondensedMode(PrinterModeState.Off);
printer.Separator();
printer.DoubleWidth2();
printer.Append("Font Width 2");
printer.DoubleWidth3();
printer.Append("Font Width 3");
printer.NormalWidth();
printer.Append("Normal width");
printer.Separator();
printer.AlignRight();
printer.Append("Right aligned text");
printer.AlignCenter();
printer.Append("Center-aligned text");
printer.AlignLeft();
printer.Append("Left aligned text");
printer.Separator();
printer.Font("Font A", Fonts.FontA);
printer.Font("Font B", Fonts.FontB);
printer.Font("Font C", Fonts.FontC);
printer.Font("Font D", Fonts.FontD);
printer.Font("Font E", Fonts.FontE);
printer.Font("Font Special A", Fonts.SpecialFontA);
printer.Font("Font Special B", Fonts.SpecialFontB);
printer.Separator();
printer.InitializePrint();
printer.SetLineHeight(24);
printer.Append("This is first line with line height of 30 dots");
printer.SetLineHeight(40);
printer.Append("This is second line with line height of 24 dots");
printer.Append("This is third line with line height of 40 dots");
printer.NewLines(3);
printer.Append("End of Test :)");
printer.Separator();
printer.FullPaperCut();
printer.PrintDocument();

🎈 Features: 🎈 特点:

  • Alignment : Left, Right and Center alignment.对齐方式:左对齐、右对齐和居中对齐。
  • BarCode : Code128, Code39, Ean13 barcodes are supported.条码:支持 Code128、Code39、Ean13 条码。
  • Drawer : Drawer Open facility.抽屉:抽屉开放式设施。
  • Font Mode : Supported Bold, Underline, Expanded, Condensed modes.字体模式:支持粗体、下划线、扩展、压缩模式。
  • Change Font : Change font to device specified FontA, FontB, FontC, FontD, FontE, SpecialFontA and SpecialFontB.更改字体:将字体更改为设备指定的 FontA、FontB、FontC、FontD、FontE、SpecialFontA 和 SpecialFontB。
  • Font Width : Supported Normal, DoubleWidth2 and DoubleWidth3.字体宽度:支持 Normal、DoubleWidth2 和 DoubleWidth3。
  • Image : Print BMP Image.图像:打印 BMP 图像。
  • Paper Cut : Cut paper with Full & Partial Cut.剪纸:用完整和部分剪纸剪纸。
  • QrCode : Support 2D barcode (QrCode). QrCode : 支持二维条码(QrCode)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM