簡體   English   中英

如何連接 Zebra 打印機 ZD220 或 Gc420t 並打印?

[英]How to Connect to Zebra Printer ZD220 or Gc420t and print?

我手頭的任務被證明是困難的。 過去幾天我一直在做這件事。
我需要將 Mercado Livre(有點像亞馬遜)的貨件標簽從我的系統直接打印到我的斑馬打印機上。 我的系統在 PHP 中,我從他們的 API 獲得標簽。
問題是,我這輩子都無法讓我的電腦連接到打印機。

在我的工作中,打印機通過以太網電纜連接到連接到我的本地網絡的桌面,並且它是共享的。
我可以從我的筆記本電腦上打印標簽,所以我知道有一條路線。 我如何找到它?

我試過的

基本上,我可以在互聯網上找到的所有內容。
我試過使用robgridley/zebra PHP ZPL Builder,以及我能找到的其他幾個,但沒有成功。

我嘗試了以下代碼:

當我嘗試這個時,

    error_reporting(E_ALL);
    
    /* Get the port for the service. */ $port = 9100;
    
    /* Get the IP address for the target host. */ $host = "xxx.xxx.xx.xx"; //this is the address of the computer that the printer is connected
    
    /* construct the label */ $label = "^XA ^LH0,0 ^FO4,2 ^GB804,798,4 ^FS ^FO4,2 ^GB548,52,4 ^FS ^FO19,12 ^ADN,36,20 ^FDRossi Andrea ^FS ^FO548,2 ^GB260,52,4 ^FS ^FO561,16 ^ADN,36,10 ^FDP/V ^FS ^FO611,16 ^ADN,36,10 ^FD1:3 ^FS ^FO693,16 ^ADN,36,10 ^FD18/12/14 ^FS ^FO20,66 ^GB540,135,80 ^FS ^FO21,70 ^ADN,135,25 ^FR ^FDREGGIO CALABRIA ^FS ^FO720,66 ^GB72,135,80 ^FS ^FO721,70 ^ADN,135,25 ^FR ^FD13 ^FS ^FO4,210 ^GB128,166,4 ^FS ^FO128,210 ^GB680,166,4 ^FS ^FO136,220 ^ADN,36,15 ^FDProva Rossi ^FS ^FO136,276 ^ADN,36,10 ^FDCONTRADA RAVAGNESE GRAZIELLA,8 ^FS ^FO15,243 ^ADN,108,50 ^FDE5 ^FS ^FO136,338 ^ADN,36,14 ^FDReggio Calabria ^FS ^FO580,338 ^ADN,36,15 ^FDRC ^FS ^FO632,210 ^GB176,166,4 ^FS ^FO660,220 ^AAN,18,5 ^FDCALABRIA SICILIA ^FS ^FO695,246 ^ADN,18,5 ^FDCSN ^FS ^FO632, 273 ^GB176,103,4 ^FS ^FO640,280 ^ADN,18,5 ^FDPeso Kg ^FS ^FO680,318 ^ADN,36,20 ^FD4.1 ^FS ^FO632, 372 ^GB176,48,4 ^FS ^FO640,378 ^ADN,18,5 ^FDPorto: ^FS ^FO658,398 ^ADN,18,10 ^FDFRANCO ^FS ^FO4,372 ^GB804,48,4 ^FS ^FO25,383 ^ADN,36,10 ^FDNote: ^FS ^FO88,383 ^ADN,36,10 ^FDProva Zaffignani ^FS ^FO640,650 ^XGE:GLSMINI.GRF,1,1 ^FS ^FO606,718 ^GB202,82,4 ^FS ^FO4,718 ^GB606,82,4 ^FS ^FO240,718 ^GB144,80,40 ^FS ^FO242,724 ^ADN,96,25 ^FR ^FD1-25 ^FS ^FO50,422 ^BY3 ^BCN,260,N,N,N,A ^FVWW540006975010RC ^FS ^FO50,685 ^ADN,27,15 ^FDWW 540006975 01 0 RC 01 ^FS ^XZ";
    
    $socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP); echo "<pre>"; echo "Socket: " . $socket; if ($socket === false) {
        echo "socket_create() failed: reason: " . socket_strerror(socket_last_error()) . "\n"; } else {
        echo "\nOK. \n"; }
    
    echo "Attempting to connect to '$host' on port '$port'..."; $result = socket_connect($socket, $host, $port);
    
    echo $result; if ($result === false) {
        echo "socket_connect() failed.\nReason: ($result) " . socket_strerror(socket_last_error($socket)) . "\n"; } else {
        echo "OK.\n"; }
    
    socket_write($socket, $label, strlen($label)); socket_close($socket);

我得到這個回應

Socket: Resource id #2
OK. 
Attempting to connect to 'xxx.xxx.xx.xx' on port '9100'...

Warning:  socket_connect(): unable to connect [110]: Connection timed out in /xxx/xxx/xxxxxx/xxxxxxxxxxx/xxxxxxxxxxxxxx/xxxxxxxxxxxx on line 38

socket_connect() failed.
Reason: () Connection timed out


Warning:  socket_write(): unable to write to socket [32]: Broken pipe in /xxx/xxx/xxxxxx/xxxxxxxxxxx/xxxxxxxxxxxxxx/xxxxxxxxxxxx on line 47

我知道端口 445 是打開的(通過所述計算機上的 netstat)。
所以我把端口改成445,發送命令打印。

Socket: Resource id #2
OK. 
Attempting to connect to 'xxx.xxx.xx.xx' on port '445'...1OK.

不幸的是,沒有打印任何內容。
由於某種原因,以下代碼根本不返回任何內容。

<?php
$fp = fsockopen("xxx.xxx.xx.xx", 445, $errno, $errstr, 30);
if (!$fp) {
    echo "$errstr ($errno)
\n";
} else {
    $out = "GET / HTTP/1.1\r\n";
    $out .= "Host: xxx.xxx.xx.xx\r\n";
    $out .= "Connection: Close\r\n\r\n";
    fwrite($fp, $out);
    while (!feof($fp)) {
        echo fgets($fp, 128);
    }
    fclose($fp);
}


echo "</pre>";

我嘗試了很多其他代碼,但基本上是一樣的(socket_create 和 fsockopen)。
我已經嘗試使用我的打印機名稱執行 exec:

exec("lpr -P '\\xxx.xxx.xx.xx\ZDesigner ZD220-203dpi ZPL' -r 'README.txt');

exec("lp -d '\\xxx.xxx.xx.xx\ZDesigner ZD220-203dpi ZPL' -r 'README.txt');

在我寫這篇文章的時候,我感到很大膽,並用這段代碼測試了從 1 到 20000 的 20000 個端口:

<?php
$host = 'xxx.xxx.xx.xx';

for($i = 1; $i <= 20000; $i++) {
    $connection = @fsockopen($host, $i);

    if (is_resource($connection)) {
        echo '<h2>' . $host . ':' . $i . ' ' . '(' . getservbyport($i, 'tcp') . ') is open.</h2>' . "\n";

        fclose($connection);
    } else {
    echo '<h2>' . $host . ':' . $i . ' is not responding.</h2>' . "\n";
    }
}

那是一個愚蠢的舉動,因為它花了很多時間才把我已經知道的東西還給了我。 有些端口是開放的,但 9100 不是。 我在防火牆上打開了端口 23(用於 telnet)、515 和 9100,但這並沒有讓我到任何地方,它仍然顯示連接超時。 當我在桌面上完全停用 windows 防火牆時,它說連接被拒絕。 當我嘗試通過它的設置向打印機發送命令時,它給了我 CM:OpenPrinter Access Denied。
Zebra 還有一個設置實用程序工具,它沒有給我這個錯誤,但今天它是:

新錯誤

另外,當我發送 ~WL 時,它說沒有安裝網絡,這沒關系,我知道,但是有一些方法,否則我將無法打印任何東西。

我需要的:

有什么方法可以通過 PHP 訪問打印機?
我真的需要它來工作。 感謝您抽出時間來閱讀。
我已經盡力提出這個問題,我希望我最終沒有感到困惑。

我們使用fputs($fp, $out)代替fwrite($fp, $out) ,其中$out是字符串中的一些 EZPL/ZPL 代碼。

$fp = fsockopen($printer['ip'], $printer['port'], $errno, $errstr, 10);
if (!$fp) {
    // code if no connection found
} else {
    $out = "some EZPL code as string";
    fputs($fp, $out);
    fclose($fp);
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM