I print Chinese by the Mini Thermal Printer finally!

I've struggled to find any successful cases throughout the process, so I want to share the code with those who might need help.

First, I'm not a programmer, and all the programming was done with the help of ChatGPT, constantly modifying and verifying the code. During this process, I went through three different printers.

I used an Arduino Uno and an Adafruit thermal printer, which supports GBK encoding (including GBK18030 for Traditional Chinese).

Initially, I followed the tutorial on Embedded Thermal Printer - DFRobot, but it printed garbled text. So, I contacted an engineer from one of the printer manufacturers, who quickly gave me some encoding formats but forgot to mention what format they were. Surprisingly, ChatGPT couldn't interpret them either. However, combining these encoding formats with the code provided by ChatGPT magically printed Traditional Chinese characters correctly.

I won't go into the details of the process but will directly share the code. Printing Simplified Chinese is straightforward; you just need to change the encoding to GBK. For Traditional Chinese, you must use GB18030 encoding.

Perhaps many people already know how to do this, given how powerful AI is, but I'm so thrilled that I want to share my success!

Here's the code:

#include "hzz.c"  // Include hz.c file
#include <SoftwareSerial.h>

// Create a new serial object using the SoftwareSerial library
SoftwareSerial mySerial(0, 1); // RX, TX

// Define Chinese strings in GB18030 encoding
const uint8_t cn_gbk[][32] = {
     {0xBD, 0xF1, 0xCC, 0xEC, 0x9A, 0xE2, 0xBA, 0xDC,0xBA, 0xC3}
};
const int cn_gbk_size = sizeof(cn_gbk) / sizeof(cn_gbk[0]);

void setup() {
  mySerial.begin(19200); // Set the baud rate for the printer
  Serial.begin(19200); // Set the baud rate for debugging

  // Initialize the printer
  initializePrinter();

  // Print Chinese characters
  printChinese();
}

void loop() {
  // Empty loop
}

void initializePrinter() {
  // Reset the printer
  mySerial.write(0x1B);
  mySerial.write(0x40);
  delay(100); // Wait for the printer to initialize

  // Select character set (assuming the printer supports GBK)
  mySerial.write(0x1B);
  mySerial.write(0x52); // Select character set command
  mySerial.write(0x0F); // Select Chinese character set
}

void printChinese() {
  // Use Chinese strings in GB18030 encoding
  for (int i = 0; i < cn_gbk_size; i++) {
    for (int j = 0; cn_gbk[i][j] != 0x00; j++) {
      mySerial.write(cn_gbk[i][j]);
    }
    mySerial.write(0x0A); // New line
  }
}
1 Like

I forgot to delete the extra references to <hzz.c>. Thank you very much for bringing this to my attention. Actually, the manual does not mention anything about how to use encoding for Traditional Chinese, nor does it mention how to reference Chinese characters. Perhaps this is something that people with a certain level of programming knowledge can quickly understand. Unfortunately, I am not one of them; I am just an artist who needs this technology. Of course, I spent a lot of time on it, as it is not my expertise. Thank you for your suggestion. I am posting this just to help others who are in the same situation as I am.

Moreover, I am indeed slowly learning programming from scratch, but this particular project is very urgent. I believe this is also one of the reasons why ChatGPT can help many people!

It's not so difficult. You have to know which charset and encoding the printer has on board, then provide your text according to that encoding. C++ and Internet sources typically use UTF-8 which may have to be converted into GBK for use with that printer.

Where is Post #2 from @zwieblum ?

Got deleted 'cause politically incorrect. Had some lines on reading documentation and questions on the sanity of ChatGPT usage in it.