From f71f257eb0cbffc264119ca98a59cc8e1e2ea4fc Mon Sep 17 00:00:00 2001 From: Martin Brodbeck Date: Mon, 6 Aug 2018 09:59:06 +0200 Subject: [PATCH 1/3] successfully claim printer interace --- src/gui/CMakeLists.txt | 2 +- src/gui/settingsdialog.cpp | 6 ++++ src/printer/CMakeLists.txt | 2 +- src/printer/posprinter.cpp | 62 +++++++++++++++++++++++++------------- src/printer/posprinter.h | 16 +++++++--- 5 files changed, 60 insertions(+), 28 deletions(-) diff --git a/src/gui/CMakeLists.txt b/src/gui/CMakeLists.txt index dd02999..244d481 100644 --- a/src/gui/CMakeLists.txt +++ b/src/gui/CMakeLists.txt @@ -29,7 +29,7 @@ set(GUI_SOURCES add_executable(kima2 ${GUI_SOURCES}) target_include_directories(kima2 PRIVATE ${PROJECT_BINARY_DIR}) -target_link_libraries(kima2 core Qt5::Widgets Qt5::PrintSupport stdc++fs) +target_link_libraries(kima2 core printer Qt5::Widgets Qt5::PrintSupport stdc++fs) if(WIN32) set_target_properties(kima2 PROPERTIES LINK_FLAGS "-mwindows") endif(WIN32) diff --git a/src/gui/settingsdialog.cpp b/src/gui/settingsdialog.cpp index af62279..c891909 100644 --- a/src/gui/settingsdialog.cpp +++ b/src/gui/settingsdialog.cpp @@ -1,6 +1,7 @@ #include "settingsdialog.h" #include +#include #include #include @@ -19,6 +20,11 @@ SettingsDialog::SettingsDialog(QWidget* parent, Qt::WindowFlags f) : QDialog(par ui_.posPrinterDeviceEdit->setText(posPrinterDevice); ui_.feePercentSpinBox->setValue(feeInPercent); ui_.maxFeeSpinBox->setValue(maxFeeInEuro); + + connect(ui_.testPosPrinterButton, &QPushButton::clicked, this, [](){ + //PosPrinter::initialize({0, 0}); + PosPrinter printer; + }); } void SettingsDialog::accept() diff --git a/src/printer/CMakeLists.txt b/src/printer/CMakeLists.txt index 26f56be..4f698d7 100644 --- a/src/printer/CMakeLists.txt +++ b/src/printer/CMakeLists.txt @@ -5,5 +5,5 @@ set(PRINTER_SOURCES ) add_library(printer STATIC ${PRINTER_SOURCES}) -target_link_libraries(printer ${LibUSB_LIBRARY}) +target_link_libraries(printer ${LIBUSB_1_LIBRARIES}) target_include_directories(printer PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}) \ No newline at end of file diff --git a/src/printer/posprinter.cpp b/src/printer/posprinter.cpp index ff50d66..8ac8dfe 100644 --- a/src/printer/posprinter.cpp +++ b/src/printer/posprinter.cpp @@ -4,9 +4,7 @@ #include #include -PosPrinter::PosPrinter() : PosPrinter(std::pair{0x0456, 0x0808}) {} - -PosPrinter::PosPrinter(std::pair vendorModelId) +PosPrinter::PosPrinter() { int retValue; @@ -15,17 +13,50 @@ PosPrinter::PosPrinter(std::pair vendorModelId) throw std::runtime_error("Init error"); } - // libusb_set_debug(contextPtr_, 3); // set verbosity level to 3, as suggested in the - // documentation + libusb_device** devList; + int devCount = libusb_get_device_list(contextPtr_, &devList); + if (devCount <= 0) { + libusb_exit(contextPtr_); + throw std::runtime_error("Could not receive device list"); + } + int numDevice = -1; + for (int i = 0; i < devCount; ++i) { + libusb_device_descriptor desc; + libusb_get_device_descriptor(devList[i], &desc); + for (const auto& supported : supportedPrinters_.models) { + if (desc.idVendor == supported.first && desc.idProduct == supported.second) { + numDevice = i; + break; + } + } + } - devicePtr_ = - libusb_open_device_with_vid_pid(contextPtr_, vendorModelId.first, - vendorModelId.second); // these are vendorID and productID - if (devicePtr_ == NULL) { + if (numDevice < 0) { + libusb_exit(contextPtr_); + return; + } + + retValue = libusb_open(devList[numDevice], &devicePtr_); // these are vendorID and productID + if (retValue != 0) { + libusb_free_device_list(devList, 1); + libusb_exit(contextPtr_); throw std::runtime_error("Cannot open printer device"); } - // ... + if (libusb_kernel_driver_active(devicePtr_, 0) == 1) { // find out if kernel driver is attached + std::cout << "Kernel Driver Active" << std::endl; + if (libusb_detach_kernel_driver(devicePtr_, 0) == 0) // detach it + std::cout << "Kernel Driver Detached!" << std::endl; + } + + retValue = libusb_claim_interface( + devicePtr_, 0); // claim interface 0 (the first) of device (mine had jsut 1) + if (retValue < 0) { + std::cout << "Cannot Claim Interface" << std::endl; + throw std::runtime_error("Cannot claim interface"); + } + + libusb_free_device_list(devList, 1); } PosPrinter::~PosPrinter() @@ -40,15 +71,4 @@ PosPrinter::~PosPrinter() libusb_close(devicePtr_); // close the device we opened libusb_exit(contextPtr_); // close the session - - delete instance_; } - -void PosPrinter::initialize(std::pair vendorModelId) -{ - if (!instance_) { - instance_ = new PosPrinter(vendorModelId); - } -} - -PosPrinter* PosPrinter::getInstance() { return instance_; } diff --git a/src/printer/posprinter.h b/src/printer/posprinter.h index 73fd6fe..33dfb19 100644 --- a/src/printer/posprinter.h +++ b/src/printer/posprinter.h @@ -5,18 +5,24 @@ #include +struct SupportedPrinters { + std::array, 2> models{ + // {Vendor ID, Model ID} + std::make_pair(0x0456, 0x0808), + std::make_pair(0x0416, 0x5011), + }; +}; + class PosPrinter { public: - static void initialize(std::pair vendorModelIds); - static PosPrinter* getInstance(); - private: PosPrinter(); - PosPrinter(std::pair vendorModelId); ~PosPrinter(); - static PosPrinter* instance_; + + private: libusb_context* contextPtr_{}; libusb_device_handle* devicePtr_{}; + SupportedPrinters supportedPrinters_; }; #endif \ No newline at end of file From 77040e9f278c630ad2a14fff82b80b6172bea3e0 Mon Sep 17 00:00:00 2001 From: Martin Brodbeck Date: Mon, 6 Aug 2018 10:00:22 +0200 Subject: [PATCH 2/3] code cleanup --- src/printer/posprinter.cpp | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/printer/posprinter.cpp b/src/printer/posprinter.cpp index 8ac8dfe..dcd242d 100644 --- a/src/printer/posprinter.cpp +++ b/src/printer/posprinter.cpp @@ -44,16 +44,16 @@ PosPrinter::PosPrinter() } if (libusb_kernel_driver_active(devicePtr_, 0) == 1) { // find out if kernel driver is attached - std::cout << "Kernel Driver Active" << std::endl; + std::cout << "Kernel driver active" << std::endl; if (libusb_detach_kernel_driver(devicePtr_, 0) == 0) // detach it - std::cout << "Kernel Driver Detached!" << std::endl; + std::cout << "Kernel driver detached!" << std::endl; } retValue = libusb_claim_interface( devicePtr_, 0); // claim interface 0 (the first) of device (mine had jsut 1) if (retValue < 0) { - std::cout << "Cannot Claim Interface" << std::endl; - throw std::runtime_error("Cannot claim interface"); + std::cout << "Cannot claim printer interface" << std::endl; + throw std::runtime_error("Cannot claim printer interface"); } libusb_free_device_list(devList, 1); @@ -64,9 +64,9 @@ PosPrinter::~PosPrinter() int retValue; retValue = libusb_release_interface(devicePtr_, 0); // release the claimed interface if (retValue != 0) { - std::cout << "Cannot Release Interface" << std::endl; + std::cout << "Cannot release printer interface" << std::endl; } else { - std::cout << "Released Interface" << std::endl; + std::cout << "Printer interface released" << std::endl; } libusb_close(devicePtr_); // close the device we opened From a34fd9aefd4f2da7b99f29030df697ebe9cfa404 Mon Sep 17 00:00:00 2001 From: Martin Brodbeck Date: Mon, 6 Aug 2018 12:52:45 +0200 Subject: [PATCH 3/3] first steps in printing via libusb --- src/gui/settingsdialog.cpp | 1 + src/printer/posprinter.cpp | 45 ++++++++++++++++++++++++++++++++++++++ src/printer/posprinter.h | 13 +++++++++++ 3 files changed, 59 insertions(+) diff --git a/src/gui/settingsdialog.cpp b/src/gui/settingsdialog.cpp index c891909..398a22a 100644 --- a/src/gui/settingsdialog.cpp +++ b/src/gui/settingsdialog.cpp @@ -24,6 +24,7 @@ SettingsDialog::SettingsDialog(QWidget* parent, Qt::WindowFlags f) : QDialog(par connect(ui_.testPosPrinterButton, &QPushButton::clicked, this, [](){ //PosPrinter::initialize({0, 0}); PosPrinter printer; + printer.printTest(); }); } diff --git a/src/printer/posprinter.cpp b/src/printer/posprinter.cpp index dcd242d..c174806 100644 --- a/src/printer/posprinter.cpp +++ b/src/printer/posprinter.cpp @@ -2,8 +2,17 @@ #include #include +#include #include +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() { int retValue; @@ -72,3 +81,39 @@ PosPrinter::~PosPrinter() libusb_close(devicePtr_); // close the device we opened 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()); +} \ No newline at end of file diff --git a/src/printer/posprinter.h b/src/printer/posprinter.h index 33dfb19..ffdf166 100644 --- a/src/printer/posprinter.h +++ b/src/printer/posprinter.h @@ -18,6 +18,19 @@ class PosPrinter public: 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: libusb_context* contextPtr_{};