1

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?

  1. 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;

  1. 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;
    }
  1. 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 &lt; 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-&gt;c_cflag &amp;= ~PARENB;
// Clear stop field, only one stop bit used in communication (most common)
//tty-&gt;c_cflag &amp;= ~CSTOPB;
// Use 8 bits per byte
//tty-&gt;c_cflag |= CS8;
// Enable RTS/CTS hardware flow control
tty-&gt;c_cflag &amp;= CRTSCTS;
// Enables software flow control
tty-&gt;c_iflag &amp;= (IXON | IXOFF | IXANY);

tty-&gt;c_lflag &amp;= ~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); //-&gt; default 10, 2
//setPrintDensity(1, 2); //-&gt; for URL-A3 V3.0 -&gt; 1, 4

return file_descriptor;

}

  1. Finally, the printer's manual talks about flow control.

enter image description here

Needing help urgently to complete my prototype. Any guidance would be greatly appreciated!

mqmarathon
  • 11
  • 4
  • XON/XOFF are control codes embedded in the data ... they are not hardware pins – jsotola Oct 26 '22 at 02:11
  • @jsotola Does it mean I can use any GPIO pins for XON/XOFF control codes? I am learning this while making it. I am sorry the question is rudimentary. – mqmarathon Oct 26 '22 at 02:20
  • you are not understanding ... the codes are sent by the printer to the computer as data ... it's like somebody dictating a shopping list and you are trying to write it down ... you ask them to stop, do that you can catch up ... then you ask them to start again – jsotola Oct 26 '22 at 02:27
  • @jsotol Yes, the codes from the printer would be sent to Rpi, which I understand because that's flow control. I want to know how to enable it and connect the printer and Rpi since it doesn't look like I can connect the DTR pin from the printer to any GPIO pin on my Pi. Thank you. – mqmarathon Oct 26 '22 at 02:39
  • you said that the printer is already connected RX(GPIO 14) and TX(GPIO15) – jsotola Oct 26 '22 at 02:44
  • @jsotol Okay, that was the cause of the confusion... So there are 5 pins on the printer. RX, TX, DTR, GND, VIN. The printer's RX and TX are connected to Pi's TX and RX respectively. From my understanding, this is to send data from the Pi to the printer. DTR is separate from this, which confuses me because.. shouldn't I be able to do the flow control with the RX&TX connection without having this DTR connected? Thanks again. – mqmarathon Oct 26 '22 at 02:53
  • the DTR on the printer may need to be tied either high or low – jsotola Oct 26 '22 at 15:42

1 Answers1

0

"DTR isn't supported with Rpi" only BCM2711 (Pi4 et al) supports DTR, although there maybe a software serial which does.

"for the XON/XOFF serial interface, which pin should I use" XON/XOFF is a software protocol - it DOES NOT use any pins.

I looked at the 4 printers mentioned on the Adafruit site and NONE of them use flow control and list connection details. (In typical Adafruit style the "documentation" is poorly organised and somewhat vague.)

To use serial enable in raspi-config and open /dev/serial0 See How do I make serial work on the Raspberry Pi3 or later for more detail.

I suggest you try something simpler e.g. loopback for initial testing.

Milliways
  • 59,890
  • 31
  • 101
  • 209
  • I will do some research on a software serial with Rpi Zero 2(RP3A0) and Pi OS. However, XON/XOFF still confuses me since I understand it's a software protocol. I assume you said "XON/XOFF does not use any pins" because I already mentioned the printer and Pi talk to each other through RX/TX. If so, does it mean I can use XON/XOFF without any other additional line between the printer and Pi? Thank you so much. – mqmarathon Oct 26 '22 at 03:22
  • XON/XOFF is a standard protocol and usable on any serial interface including the Pi. It was old 50 years ago, but unless the "printer" supports it is meaningless. This "question" is not Pi specific - indeed I don't even see any serial code in the code fragments you listed. – Milliways Oct 26 '22 at 03:56