[英]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.