0
votes

I am creating an application which prints text through a POS printer.

The prints works fine. But for POS printer there are control commands to do certain functions like : paper cut, cashdraw open etc.. For eg:

Function: Partial cut Code:

  • ASCII———-> ESC i
  • Hex ————-> 1B 69
  • Decimal—-> 27 105

When I try to send command 27 105 it just prints on paper instead of performing action.. I’m not exactly sure how to send it… Can someone suggest how to write to the socket… #include "lanprinterui.h" #include "ui_lanprinterui.h"

LanPrinterUI::LanPrinterUI(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::LanPrinterUI)
{
    ui->setupUi(this);

    m_TextInput = ui->textEdit;
    m_pSocket   = new QTcpSocket();
    m_pSocket->connectToHost("192.168.1.20", 9100);
    m_ConnectStatus = true;

    QObject::connect(ui->pushButton, SIGNAL(clicked()), this, SLOT(print()));
}

LanPrinterUI::~LanPrinterUI()
{
    delete ui;
    delete m_pSocket;
}

void LanPrinterUI::print()
{
    const int Timeout = 5 * 1000;

    if (!m_ConnectStatus)
    {
        m_pSocket->connectToHost("192.168.1.20", 9100);
    }

    if (!m_pSocket->waitForConnected(Timeout))
    {
        //sent error
        qDebug ("error in waitForConnected()");
        qDebug (qPrintable(m_pSocket->errorString()));
        m_ConnectStatus = false;
        return;
    }

    m_ConnectStatus = true;
    QByteArray block;
    QDataStream out(&block, QIODevice::WriteOnly);
    out.setVersion(QDataStream::Qt_4_0);
    out << m_TextInput->toPlainText();
    out << '\n';
    m_pSocket->write(block);
}
1

1 Answers

2
votes

What you want is

out << "\n\x1Bi";

That is, you want, after the '\n' char, to send the '\x1B' char (also known as "escape") and the 'i' char. You could also have written this as:

out << '\n' << "\x1b\x69";

or:

out << '\n' << char(27) << char(105);

or:

out << '\n' << char(0x1b) << char(0x69);

(or any other valid combination)

Another, better idea, would be to put in your class:

class LanPrinterUI {
private:
  static const QString PARTIAL_PAPER_CUT = "\x1bi";
  static const QString CASHDRAWER_OPEN = "\x1b....";
//...
};

And then you would just:

out << '\n' << PARTIAL_PAPER_CUT;

(which would be much better than hardcoded constants)