I use Rpi Zero 2 and Adafruit Nano Printer (link) for Bitmap printing. I ported the C++ Adafruit Thermal Printer library (link) for my Rpi Cpp program, which was pretty straightforward, but I am having difficulty doing DTR flow control.
Currently, I can print images, but I have to generously set a waiting time not to overflow the printer's buffer, which slows down the printing speed too much.
From googling, I realized DTR isn't supported with Rpi (Did I get this right?). So instead, I should use an XON/XOFF control. RX(GPIO 14) and TX(GPIO15) are already taken by the printer. Then, for the XON/XOFF serial interface, which pin should I use, and how should I enable and use it?
- I am using gpiod.h library, and below is the code to read GPIO25 I tried to get a DTR communication with my printer, which failed.
//-----------------------------------------------------------------------------
// gpio
// Open GPIO chip
//const char *chipname = "gpiochip0";
//struct gpiod_chip *chip;
//chip = gpiod_chip_open_by_name(chipname);
chip = gpiod_chip_open("/dev/gpiochip0");
// Open GPIO lines
//struct gpiod_line *line;
line = gpiod_chip_get_line(chip, 25);
if (!line) {
std::cout << "failed to get line 25" << std::endl;
gpiod_chip_close(chip);
//return -1;
}
gpiod_line_request_input(line, "test1");
// Getting an input from gpio pin 25
int input_val = gpiod_line_get_value(line);
std::cout << "input_val: " << input_val << std::endl;
- Here, I used serial_write just like it's written in the Adafruit library to enable DTR. Unsure what (1 << 5) means.
if (dtr != 255)
{
write_bytes(ASCII_GS, 'a', (1 << 5));
dtr_enabled = true;
std::cout << "dtr_enabled" << std::endl;
}
else
{
dtr_enabled = false;
}
- Now... in the code below, you can see how I tried to enable XON/XOFF.
int Thermal::open_serial()
{
std::cout << "open_serial called" << std::endl;
termios* tty = new termios;
file_descriptor = open(serial_path, O_RDWR | O_NOCTTY | O_NONBLOCK);
if (file_descriptor < 0) {
delete tty;
return -1;
}
// Take over existing settings from open serial port
if (tcgetattr(file_descriptor, tty) != 0) {
delete tty;
return -1;
}
// Clear parity bit, disabling parity (most common)
//tty->c_cflag &= ~PARENB;
// Clear stop field, only one stop bit used in communication (most common)
//tty->c_cflag &= ~CSTOPB;
// Use 8 bits per byte
//tty->c_cflag |= CS8;
// Enable RTS/CTS hardware flow control
tty->c_cflag &= CRTSCTS;
// Enables software flow control
tty->c_iflag &= (IXON | IXOFF | IXANY);
tty->c_lflag &= ~ICANON;
cfsetispeed(tty, baud_to_const(baud_rate));
if (tcsetattr(file_descriptor, TCSANOW, tty) != 0) {
delete tty;
return -1;
}
delete tty;
wake();
setPrintDensity(print_density, print_break_time); //-> default 10, 2
//setPrintDensity(1, 2); //-> for URL-A3 V3.0 -> 1, 4
return file_descriptor;
}
- Finally, the printer's manual talks about flow control.
Needing help urgently to complete my prototype. Any guidance would be greatly appreciated!
RX(GPIO 14) and TX(GPIO15)
– jsotola Oct 26 '22 at 02:44