Skip to content

Character Code Table

Marco Antonio edited this page Dec 4, 2021 · 5 revisions

You can write text from other languages by configuring the character code table.

The first option is to call escpos.setCharacterCodeTable

  escpos.setCharacterCodeTable(CharacterCodeTable.CP863_Canadian_French);
  escpos.writeLF("Liberté et Fraternité.");

The function escpos.setCharacterCodeTable have the most common codes used to configure the printer and it is based on epson TM-T20.

Unfortunately other models can have slight different codes, and the printer output can be unreadable. In this case we need to manually configure the character code table.

Manually configure the character code table

The manual configuration needs two steps:

  1. Configure the printer character table
  2. Configure the java charset

Requirements:

  • Find on your printer's programming manual the session Esc t n with title "Character code table" or similar description with one table like this:

Character code table

Code Description
0 CP437 (U.S.A., Standard Europe)
1 Katakana
2 CP850 (Multilingual)
3 CP860 (Portuguese)
4 CP863 (Canadian-French)
n n-description

Suppose that you need to configure your printer to print Portuguese characters and accents.

As you can see, on the table above, the code for Portuguese (on this printer) is 3. You can configure the character table for Portuguese with command below:

escpos.setPrinterCharacterTable(3);

Now, you need to find the portuguese code at Java 8 Supported Encoding and get one alias In this case, the java appoint two lines.

Canonical Name for java.nio API Canonical Name for java.io API and java.lang API Alias or Aliases Description
IBM037 Cp037 cp037 ibm037 ibm-037 csIBM037 ebcdic-cp-us ebcdic-cp-ca ebcdic-cp-nl ebcdic-cp-wt 037 cpibm37 cs-ebcdic-cp-wt ibm-37 cs-ebcdic-cp-us cs-ebcdic-cp-ca cs-ebcdic-cp-nl USA, Canada (Bilingual, French), Netherlands, Portugal, Brazil, Australia
IBM860 Cp860 ibm860 860 cp860 csIBM860 ibm-860 MS-DOS Portuguese

Trick: as indicated on printer's programming manual Character code table, the alias for code 3 should be cp860, in other words, the second line of following table. (3 | CP860 (Portuguese))

To configure the Java Encoding (for cp860), you need to choose one alias, and not more than one from the alias list:

escpos.setCharsetName("cp860"); // right
escpos.setCharsetName("860"); // right

escpos.setCharsetName("ibm860 860 cp860 csIBM860 ibm-860"); // wrong

Complete code:

        escpos.setPrinterCharacterTable(3);
        escpos.setCharsetName("cp860");
        escpos.writeLF("Some Portuguese special characters: çÇÃãõà");

In summary, the manual configuration needs two commands:

  1. Configure the printer character table - escpos.setPrinterCharacterTable(3);
  2. Configure the java charset - escpos.setCharsetName("cp860");

Trobleshooting

Generally, on the printer's programming manual, it shows the name of the code of java encoding or canonical name. as in the example table below, the (supposed) printer have the name of encoding for Portuguese: (3 - "CP860") and (64 - "CP037")

Code Description
0 CP437 (U.S.A., Standard Europe)
2 CP850 (Multilingual)
3 CP860 (Portuguese)
13 CP863 (Canadian-French)
64 CP037 (Portuguese)
n n-description

Knowing the encoding name, you can easily configure the printer, in this case, for Portuguese, with two options:

        escpos.setPrinterCharacterTable(3);
        escpos.setCharsetName("cp860");
        escpos.writeLF("Some Portuguese special characters: çÇÃãõà");

OR

        escpos.setPrinterCharacterTable(64);
        escpos.setCharsetName("cp037");
        escpos.writeLF("Some Portuguese special characters: çÇÃãõà");

But, in some cases, we need to test all possibilities combined, analise differences of printing results and choose the best option.

escpos.setPrinterCharacterTable escpos.setCharsetName
3 "cp860"
3 "cp037"
64 "cp860"
64 "cp037"

Hacks

Using the tools below, you can check specifically if the printer is printing correctly as expected by printer's documentation. And you can test if one determined Java charset table is working as expected too.

Then, with the diagnoses, you can take more assertive decisions, such as configure the printer's hardware or apply some firmware or review the java environment settings.

Character code table hack

Aditional and comparative reference from Epson site

Here, you can print a range of characters (0..255) for one code table. With this tool, you can test the "expected characters" versus "printed characters". For example, searching on the google, you can get the cp860 code page and discover that for code 128, the expected character is 'Ç', and test if your printer is really printing as expected by the this command: printCodeTable(escpos,3, 128,128); and the output printing should be like this:

character code table for code [3], 
between 128 and 128
Ç  

Use the function bellow

    void printCodeTable(EscPos escpos, int codeTable, int codMin, int codMax) throws IOException {
        escpos.setCharacterCodeTable(EscPos.CharacterCodeTable.CP437_USA_Standard_Europe);
        escpos.writeLF(String.format("character code table for code [%d], " +
                "\nbetween %d and %d", codeTable,codMin, codMax));
        escpos.setPrinterCharacterTable(codeTable);
        for(int code = codMin; code <= codMax; code++){
            escpos.write(code);
            escpos.write("  ");

        }
        escpos.writeLF("");
        escpos.feed(5).cut(EscPos.CutMode.FULL);
        
    }

Usages

Imagine that you want to know all characters of code page number (3 - Portuguese), then you can make like this:

printCodeTable(escpos,3, 1,255);

Or you can print only extended character set (128..255)

printCodeTable(escpos,3, 128,255);

Java Encoding Hack

Ok, on Character code table hack session we saw how to check expected versus printed character on the escpos printer. Now you can make in the same way, check expected versus printed character not on the printer, but on terminal.

Giving the same case as above, searching on the google, you can get the cp860 code page and discover that for code 128, the expected character is 'Ç', and test if your terminal is really printing as expected by the this command: terminalPrintJavaCharSet("860",128,128); and the output printing should be like this:

List of Java CharSet for charsetName = "860", between 128 and 128
code = [128] = char [Ç]

Use the function bellow

    void terminalPrintJavaCharSet(String charSetName, int codMin, int codMax) throws UnsupportedEncodingException {
        System.out.println(String.format("List of Java CharSet for charsetName = \"%s\", " +
                "between %d and %d", charSetName, codMin, codMax));
        byte[] bytes = new byte[1];
        for (int code = codMin; code <= codMax; code++) {
            bytes[0] = (byte) code;
            System.out.println(String.format("code = [%d] = char [%s]", code, new String(bytes, charSetName)));

        }
    }

Usages

Imagine that you want to know all characters of charset (cp860), then you can make like this:

terminalPrintJavaCharSet("cp860",1,255);

Or you can print only extended character set (128..255)

terminalPrintJavaCharSet("cp860",128,255);