zebra_printer

 avatar
unknown
c_cpp
22 days ago
3.3 kB
8
Indexable
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <string.h>
#include <iostream>
#include <string>

class ZebraPrinterConnection {
private:
    std::string printer_ip;
    int port;
    int buffer_size;
    int timeout_sec;
    int max_retries;

public:
    ZebraPrinterConnection(
        const std::string& ip,
        int port = 9100,
        int buffer_size = 1024,
        int timeout_sec = 5,
        int max_retries = 3
    ) : printer_ip(ip), 
        port(port), 
        buffer_size(buffer_size),
        timeout_sec(timeout_sec),
        max_retries(max_retries) {}

    std::string sendZplCommand(const std::string& zpl_command) {
        for (int attempt = 0; attempt < max_retries; ++attempt) {
            int sock = socket(AF_INET, SOCK_STREAM, 0);
            if (sock < 0) {
                std::cerr << "Błąd tworzenia socketu" << std::endl;
                continue;
            }

            // Ustawienie timeoutu
            struct timeval timeout;
            timeout.tv_sec = timeout_sec;
            timeout.tv_usec = 0;
            setsockopt(sock, SOL_SOCKET, SO_RCVTIMEO, &timeout, sizeof(timeout));
            setsockopt(sock, SOL_SOCKET, SO_SNDTIMEO, &timeout, sizeof(timeout));

            // Konfiguracja adresu
            struct sockaddr_in server_addr;
            memset(&server_addr, 0, sizeof(server_addr));
            server_addr.sin_family = AF_INET;
            server_addr.sin_port = htons(port);
            
            if (inet_pton(AF_INET, printer_ip.c_str(), &server_addr.sin_addr) <= 0) {
                std::cerr << "Błędny adres IP" << std::endl;
                close(sock);
                return "";
            }

            // Próba połączenia
            if (connect(sock, (struct sockaddr*)&server_addr, sizeof(server_addr)) < 0) {
                std::cerr << "Błąd połączenia, próba " << attempt + 1 << "/" 
                         << max_retries << std::endl;
                close(sock);
                sleep(1);
                continue;
            }

            // Wysyłanie komendy
            if (send(sock, zpl_command.c_str(), zpl_command.length(), 0) < 0) {
                std::cerr << "Błąd wysyłania danych" << std::endl;
                close(sock);
                continue;
            }

            // Odbieranie odpowiedzi
            char buffer[1024];
            int bytes_received = recv(sock, buffer, sizeof(buffer) - 1, 0);
            close(sock);

            if (bytes_received > 0) {
                buffer[bytes_received] = '\0';
                std::string response(buffer);
                std::cout << "Odpowiedź od drukarki: " << response << std::endl;
                return response;
            }
        }

        std::cout << "Nie udało się uzyskać odpowiedzi od drukarki po " 
                  << max_retries << " próbach." << std::endl;
        return "";
    }
};

// Przykład użycia
int main() {
    std::string printer_ip = "10.0.10.179";
    std::string zpl_command = "! U1 getvar \"sensor.peeler\"";

    ZebraPrinterConnection printer(printer_ip);
    std::string response = printer.sendZplCommand(zpl_command);

    return 0;
}
Leave a Comment