first steps in printing via libusb

This commit is contained in:
Martin Brodbeck 2018-08-06 12:52:45 +02:00
parent 77040e9f27
commit a34fd9aefd
3 changed files with 59 additions and 0 deletions

View file

@ -24,6 +24,7 @@ SettingsDialog::SettingsDialog(QWidget* parent, Qt::WindowFlags f) : QDialog(par
connect(ui_.testPosPrinterButton, &QPushButton::clicked, this, [](){ connect(ui_.testPosPrinterButton, &QPushButton::clicked, this, [](){
//PosPrinter::initialize({0, 0}); //PosPrinter::initialize({0, 0});
PosPrinter printer; PosPrinter printer;
printer.printTest();
}); });
} }

View file

@ -2,8 +2,17 @@
#include <exception> #include <exception>
#include <iostream> #include <iostream>
#include <sstream>
#include <string> #include <string>
const std::string PosPrinter::Command::RESET = {0x1b, 0x40};
const std::string PosPrinter::Command::ENCODING = {'\x1b', '\x74', 16};
const std::string PosPrinter::Command::CENTERING = {'\x1b', '\x61', '\x01'};
const std::string PosPrinter::Command::FONT_SIZE_BIG = {'\x1b', '\x21', '\x10'};
const std::string PosPrinter::Command::FONT_SIZE_NORMAL = {'\x1b', '\x21', '\x00'};
const std::string PosPrinter::Command::LEFT_ALIGN = {'\x1b', '\x61', '\x00'};
const std::string PosPrinter::Command::FEED = {0x1b, 0x64, 0x03};
PosPrinter::PosPrinter() PosPrinter::PosPrinter()
{ {
int retValue; int retValue;
@ -72,3 +81,39 @@ PosPrinter::~PosPrinter()
libusb_close(devicePtr_); // close the device we opened libusb_close(devicePtr_); // close the device we opened
libusb_exit(contextPtr_); // close the session libusb_exit(contextPtr_); // close the session
} }
void PosPrinter::write(const std::string& text)
{
if (devicePtr_ == NULL)
return;
int length = text.length();
int actual{0};
int retValue = libusb_bulk_transfer(devicePtr_, (0x03 | LIBUSB_ENDPOINT_OUT),
(unsigned char*)text.c_str(), length, &actual, 10000);
if (retValue != 0 || actual != length)
std::cerr << "Write Error" << std::endl;
}
void PosPrinter::printHeader()
{
std::stringstream commandStream;
commandStream << Command::RESET << Command::ENCODING << Command::CENTERING
<< Command::FONT_SIZE_BIG;
commandStream << "Kindersachenmarkt\nDettingen\n\n";
commandStream << Command::LEFT_ALIGN << Command::Command::FONT_SIZE_NORMAL;
write(commandStream.str());
}
void PosPrinter::printTest()
{
using namespace std::string_literals;
std::stringstream commandStream;
commandStream << Command::ENCODING;
commandStream << "Der Drucker kann von KIMA2\nangesprochen werden.\n\n"
<< "Beachten Sie, dass nicht\nalle Modelle Strichcodes\nausdrucken können.";
commandStream << Command::FEED;
printHeader();
write(commandStream.str());
}

View file

@ -18,6 +18,19 @@ class PosPrinter
public: public:
PosPrinter(); PosPrinter();
~PosPrinter(); ~PosPrinter();
void write(const std::string& text);
void printHeader();
void printTest();
struct Command {
static const std::string RESET;
static const std::string ENCODING;
static const std::string CENTERING;
static const std::string FONT_SIZE_BIG;
static const std::string FONT_SIZE_NORMAL;
static const std::string LEFT_ALIGN;
static const std::string FEED;
};
private: private:
libusb_context* contextPtr_{}; libusb_context* contextPtr_{};