RP203 Thermal Printer
-
I'm Using Wiring pi library to control my thermal printer and barcode with Multiplexer in that my barcode was working without issues but my printer was not working I given my code printer code for your reference by using this code I can get output in console like this Data sent to printer. Waiting for acknowledgment...
Timeout waiting for printer acknowledgment.
Serial port for the printer is closed. but there is no response in my printer#include "mainwindow.h" #include "ui_mainwindow.h" #include <QtSerialPort/QSerialPort> #include <wiringPi.h> #include <wiringSerial.h> #include <QDebug> #include <unistd.h> #include <QDebug> #include <wiringPi.h> // Define GPIO pins for mux control #define MUX_S0_PIN 21 // Mux S0 pin #define MUX_S1_PIN 22 // Mux S1 pin MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent) , ui(new Ui::MainWindow) { ui->setupUi(this); // Initialize WiringPi if (wiringPiSetup() == -1) { qDebug() << "Failed to initialize WiringPi."; // Handle the error appropriately } wiringPiSetup(); pinMode(MUX_S0_PIN, OUTPUT); pinMode(MUX_S1_PIN, OUTPUT); // Set MUX_S0_PIN to low digitalWrite(MUX_S0_PIN,LOW); // Set MUX_S1_PIN to high digitalWrite(MUX_S1_PIN, LOW); printer.setPortName("/dev/ttyS0"); printer.setBaudRate(QSerialPort::Baud9600); // Set the baud rate printer.setDataBits(QSerialPort::Data8); // Set data bits printer.setParity(QSerialPort::NoParity); // Set parity printer.setStopBits(QSerialPort::OneStop); // Set stop bits printer.setFlowControl(QSerialPort::NoFlowControl); // Set flow control // Attempt to open the serial port for the printer // Attempt to open the serial port for the printer if (!printer.open(QIODevice::ReadWrite)) { qDebug() << "Failed to open serial port for the printer: " << printer.errorString(); } } MainWindow::~MainWindow() { delete ui; } void MainWindow::on_print_clicked() { // Check if the serial port is open if (!printer.isOpen()) { qDebug() << "Serial port for the printer is not open. Aborting print."; return; } // Send a simple text message to the printer when the button is clicked QByteArray command; command.append(char(0x1B)); // ESC character command.append(char(0x61)); // Set text alignment command command.append(char(0x01)); // 0x01 for center alignment command.append("Hello, Printer!\n"); // Text to print // Send the ESC/POS command to the printer using QSerialPort printer.write(command); printer.waitForBytesWritten(5000); // Wait for data to be written (adjust the timeout as needed) qDebug() << "Data sent to printer. Waiting for acknowledgment..."; // Read any response from the printer (if necessary) QByteArray response; if (printer.waitForReadyRead(5000)) { // Wait for data to be available for reading response = printer.readAll(); // Read the response from the printer qDebug() << "Printer acknowledgment received:" << response; } else { qDebug() << "Timeout waiting for printer acknowledgment."; } // Close the serial port when done printer.close(); qDebug() << "Serial port for the printer is closed."; }
-
@Thananjeyan
A couple of points to address in your code. Perhaps won't solve your issue, but just in case:-
Whatever
wiringPiSetup()
, presumably you are not supposed to call it twice? -
Check the return result from
printer.waitForBytesWritten(5000)
. -
Be aware that you
printer.close()
inon_print_clicked()
but you onlyprinter.open()
inMainWindow
constructor. So you won't be able to print more than once.
-