如何格式化和发送命令以在热敏打印机 Datecs LP-50 H 上打印账单项目?

How to format and send command for printing bill items on termal printer Datecs LP-50 H?

我的打印机是通过串行端口(COM3、9600)连接的,现在我想在上面打印一些文本。我已经尝试了我在网上找到的所有代码,主要以 epson 打印机命名,但其中 none 对我不起作用。

只是我想在 Button_click 事件上向打印机发送消息。

 private void Button_Click(object sender, RoutedEventArgs e)
    {
        if (!serialPort.IsOpen)
            serialPort.Open();
        serialPort.Write(Message());
    }

简单的测试消息是(这就是我需要帮助的地方):

private String Message() {
    char[] init = new char[] { (char)0x1b, '@' };
    String msg = "";
    foreach (char c in init)
        msg += c;

       msg+="Hello World";
    return msg;
}

我也有打印机的用户手册,伪命令:

LPRINT “0123456789012345678901”; 
LPRINT CHR$ (&HA); 
LPRINT CHR$ (&H9) + “AAA”; 
LPRINT CHR$ (&H9) + “BBB”; 
LPRINT CHR$ (&HA); 
LPRINT CHR$ (&H1B) + “D”; 
LPRINT CHR$ (3) + CHR$ (7) + CHR$ (14) + CHR$ (0); 
LPRINT CHR$ (&H9) + “AAA”; 
LPRINT CHR$ (&H9) + “BBB”; 
LPRINT CHR$ (&H9) + “CCC” + CHR$ (&HA);

如何将其转换为 C# 并在此伪命令中缺少某些内容?我在这里需要帮助来正确定义我的 Message() 方法并打印文本。

我找到了解决问题的方法。 Datecs 有 windows 此类打印机的驱动程序,因此该打印机可用作常规打印机。不要弄乱 com 端口和它的参数,只需发送常规格式的字符串进行打印。这是打印 String

的方法
public void PrintText(StringBuilder s, String PrinterName)
    {
        PrintDocument p = new PrintDocument();
        p.PrintPage += delegate(object sender1, PrintPageEventArgs e1)
        {
            e1.Graphics.DrawString(s.ToString(), new Font("Times New Roman", 11), new SolidBrush(System.Drawing.Color.Black), new RectangleF(0, 0, p.DefaultPageSettings.PrintableArea.Width, p.DefaultPageSettings.PrintableArea.Height));

        };
        try
        {
            p.PrinterSettings.PrinterName = PrinterName;
            p.Print();
        }
        catch (Exception ex)
        {
            throw new Exception("Exception Occured While Printing", ex);
        }
    }

此方法需要添加引用:

System.Drawing

并在顶部添加 using 语句

using System.Drawing;