Question

I'm trying to get a response from a Zebra QLn220 mobile printer via a php socket.

Communication to the printer is fine, I'm able to send config params and print the labels/receipts I need, but I can't get any response from the printer.

// 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;

Sending over the socket works, but I end up waiting forever and getting no response when socket_read is called.

I might be off when it comes to how to read from the socket, or perhaps the printer simply doesn't respond to any of the requests I've tried?

Other examples of messages that have been successfully transmitted over the socket, so you can see that I'm not just yelling into an abyss:

$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";
Was it helpful?

Solution

Every SGD command needs to start on a new line and be terminated with a \r\n

so add a \r\n to this line :

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top