通过套接字连接从Zebra打印机接收消息

时间:2014-01-16 18:22:18

标签: php sockets printing zebra-printers zpl

我正试图通过php套接字从Zebra QLn220移动打印机获得回复。

与打印机的通信很好,我可以发送配置参数并打印我需要的标签/收据,但我无法从打印机得到任何响应。

// Toy socket connection to Zebra QLn220 mobile printer
// set up socket
$sock = socket_create(AF_INET, SOCK_STREAM, 0);

if (!$sock){
  die("boourns, no socket created");
}
echo "Socket created".PHP_EOL;
$printer_ip = '192.168.0.11';
// connect socket to printer
if(!socket_connect($sock, $printer_ip, 6101)){
  $errorcode = socket_last_error();
  $errormsg  = socket_str($errorcode);
  die("Could not connect: [$errorcode] $errormsg\n");
}
// this is actually a CPCL set up command, not ZPL, but I can't get a response regardless of language
$get_mac = '! U1 getvar "wlan.mac_addr"'

$success = socket_send($sock, $get_mac, strlen($out), 0);
$result  = socket_read($sock, 1024);
echo $result.PHP_EOL;

发送套接字有效,但我最终等待永远等待,并且在调用socket_read时没有响应。

关于如何从套接字读取,或者打印机根本不响应我尝试过的任何请求,我可能会关闭?

已通过套接字成功传输的消息的其他示例,因此您可以看到我不仅仅是在大吼大叫:

$m1 = "^XA".
      "^FO20,50".
      "^A0N50,50".
      "^FDHello, World!".
      "^FS".
      "^XZ";

$m2 = "^XA".
      "^FO15,60".
      "^BCN,75,Y,N,N".
      "^FDFront Shelf-2^FS".
      "^XZ";

$m3 = "^XA".
      "^POI".
      "^FO50,50".
      "^ADN,36,20". // height, width of characters. min is 18,10
      "^FDInverted label^FS".
      "^XZ";

$m4 = "^XA".
      "^FO15,15".
      "^FDONLY ONE LABEL?!?!^FS".
      "^XZ";

$m5 = "~JC".  //  set media sensor calibration
      "^XA".
      "^JUS". //  Configuration update. SAVE settings
      "^XZ";

1 个答案:

答案 0 :(得分:3)

每个SGD命令都需要从一个新行开始,并以\r\n

终止

所以在这一行添加\r\n

$get_mac = '\r\n! U1 getvar "wlan.mac_addr"\r\n'