123 lines
4.0 KiB
C++
123 lines
4.0 KiB
C++
#include "posprinter.h"
|
|
|
|
#include <exception>
|
|
#include <iostream>
|
|
#include <sstream>
|
|
#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()
|
|
{
|
|
int retValue;
|
|
|
|
retValue = libusb_init(&contextPtr_);
|
|
if (retValue < 0) {
|
|
throw std::runtime_error("Init error");
|
|
}
|
|
|
|
#if defined(_WIN32)
|
|
libusb_set_option(contextPtr_, LIBUSB_OPTION_USE_USBDK);
|
|
#endif
|
|
|
|
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;
|
|
}
|
|
}
|
|
}
|
|
|
|
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 printer interface" << std::endl;
|
|
throw std::runtime_error("Cannot claim printer interface");
|
|
}
|
|
|
|
libusb_free_device_list(devList, 1);
|
|
}
|
|
|
|
PosPrinter::~PosPrinter()
|
|
{
|
|
int retValue;
|
|
retValue = libusb_release_interface(devicePtr_, 0); // release the claimed interface
|
|
if (retValue != 0) {
|
|
std::cout << "Cannot release printer interface" << std::endl;
|
|
} else {
|
|
std::cout << "Printer interface released" << std::endl;
|
|
}
|
|
|
|
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"
|
|
<< u8"Beachten Sie, dass nicht\nalle Modelle Strichcodes\nausdrucken können.";
|
|
commandStream << Command::FEED;
|
|
printHeader();
|
|
write(commandStream.str());
|
|
} |