0

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 Answer 1

2

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)

Sign up to request clarification or add additional context in comments.

5 Comments

Thanks Massa.. It worked.. so \n\x can be used before all the command correct?
@Abin: I edited it. Sorry that my initial response was too terse, I was in a hurry.
Thanks for the edit, but out << '\n' << char(27) << char(105); or: out << '\n' << char(0x1b) << char(0x69); didn't work for me.. other works good and well
may i know if anywhere it is defined that \n\x should be used... ?
the sequence "\n\x1bi" means "the newline" (it may be char(10), char(13), or char(13) followed by char(10), depending on the platform) followed by char(27) (the "escape" char, 0x1b in hexadecimal), followed by char(105)... it's unsettling that it didn't work for some reason for you. (follow this link to see a live test at coliru, showing that they are all the same)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.