Search code examples
c#.netstringprintingthermal-printer

Send escape character to printer


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.


Solution

  • 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.