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
}
}