Send escape character to printer

橙三吉。 提交于 2019-12-10 09:54:36

问题


I am developing an C# application to print labels from a thermal transfer printer from SATO (CG408 TT)

For this I am using SBPL (Programming language for SATO). Which looks something like following:

<ESC>A
<ESC>H0050<ESC>V0100<ESC>L0303<ESC>XMSATO
<ESC>H0050<ESC>V0200<ESC>B103100*SATO*
<ESC>H0070<ESC>V0310<ESC>L0101<ESC>XUSATO
<ESC>Q1<ESC>Z

To communicate with Printer and send raw data to it I am following this technique. At first i am trying to build the escape sequences using StringBuilder Class.

StringBuilder sb = new StringBuilder();
              sb.AppendLine();
              sb.AppendLine("<ESC>A");
              sb.AppendLine("H0050<ESC>V0100<ESC>L0303<ESC>XMSATO");

and so on....

But how can I replace <ESC> part in string builder argument. I know that character 27, but then how to use it with AppendLine Command

Thanks in advance.


回答1:


const char ESC = '\x1B';

now you can use ESC like any other variable. Note that you can embed esc in a string as well: "\x1B" but I suppose that would become unwieldy (especially with adjacent numbers).

Please do not ESC + "somestring" + ESC etc. because it defeats the purpose of the StringBuilder

You could

StringBuilder sb = new StringBuilder();
          sb.AppendLine();
          sb.AppendLine("<ESC>A");
          sb.AppendLine("H0050<ESC>V0100<ESC>L0303<ESC>XMSATO");
String output = sb.ToString().Replace("<ESC>", "\x1B")

e.g.




回答2:


This should work

sb.AppendLine(((char)27).ToString());



回答3:


For OPOS Drivers, I ended up using

string ESC = System.Text.ASCIIEncoding.ASCII.GetString(new byte[] { 27 }); 


来源:https://stackoverflow.com/questions/5514815/send-escape-character-to-printer

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!