通过串口打印后如何发送裁纸命令? [英] How do i send cut paper command after printing through serial port?

查看:84
本文介绍了通过串口打印后如何发送裁纸命令?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用SerialPort将字符串直接发送到通过com端口连接的打印机.现在,我需要发送一个命令来最后裁切纸张.我该怎么办?
到目前为止,这是我的代码...

I''m using SerialPort to send strings directly to a printer connected through a com port. Now i need to send a command to cut the paper in the end. How can i do that?
Here is my code so far...

SerialPort Porta = new SerialPort();
Porta.PortName = "com2";
Porta.BaudRate = 9600;
Porta.Parity = Parity.None;
Porta.DataBits = 8;
Porta.StopBits = StopBits.One;
Porta.Handshake = Handshake.None;

Porta.Open();
Porta.WriteLine("Line1");
Porta.WriteLine("Line2");
Porta.WriteLine("Line3");
Porta.Close();

推荐答案

marco.constantino - 2 hrs ago
This worked: Porta.WriteLine("\x1b" + "\x69"). thanks...



我很高兴您找到一个解决方案,但这是一种令人讨厌的方法-根据打印机可以处理的其他命令,由于Windows(Unicode,8到32位)和Windows之间的字符集差异,您可能会遇到问题.您的打印机(可能是ASCII,7或8位).
但更糟糕的是,它很难维护.

取而代之的是,将命令定义为字节流:



I''m glad you found a solution, but that is a nasty way to do it - depending on the other commands your printer can process, you may get problems due to character set differences between Windows (Unicode, 8 to 32 bits) and your printer (likely to be ASCII, 7 or 8 bits).
But worse, it is very difficult to maintain.

Instead of this, define your command as a stream of bytes:

private const byte ESC = 0x1B;
private const byte[] CutPaper = { ESC, 0x69 };


然后将其写入打印机:


And then write that to your printer:

Porta.Write(CutPaper, 0, CutPaper.Length);

它变得更容易阅读和维护-特别是如果您出于任何原因不得不更换打印机.

It becomes a lot easier to read, and to maintain - particularly if you have to change printers for any reason.


这篇关于通过串口打印后如何发送裁纸命令?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆