qt:如何将控制命令发送到POS打印机?

时间:2014-05-21 10:40:28

标签: c++ qt printing

我正在创建一个通过POS打印机打印文本的应用程序。

打印效果很好。但对于POS打印机,有控制命令可以执行某些功能,例如:剪纸,提取现金等。例如:

功能:部分切割 代码:

  • ASCII ----> ESC i
  • Hex -----> 1B 69
  • 十进制 - > 27 105

当我尝试发送命令27 105时,它只是在纸上打印而不是执行动作..我不确定如何发送它......有人可以建议如何写入套接字......     #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 个答案:

答案 0 :(得分:2)

你想要的是

out << "\n\x1Bi";

也就是说,您希望在'\n' char之后发送'\x1B' char(也称为“escape”)和'i' char。您也可以将其写为:

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

或:

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

或:

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

(或任何其他有效组合)

另一个更好的想法是加入你的班级:

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

然后你就会:

out << '\n' << PARTIAL_PAPER_CUT;

比硬编码常量更好