Get TM-T20II Epson ASB status through UDP socket
/*
* $status = {
* "status": "online" | "offline",
* "cover": "open" | "closed", (opt)
* "feeding": "feeding" | "not", (opt)
* "autocutterError": "error" | "not", (opt)
* "unrecoverableError": "error" | "not", (opt)
* "recoverableError": "error" | "not", (opt)
* "paper": "paperEnd" | "paperPresent" (opt)
* }
* See https://www.epson-biz.com/modules/pos/index.php?page=single_doc&cid=1600
*/
function getStatus($printerIp, $debug = false) {
$status = array();
$printerPort = 3289;
$socket = socket_create(AF_INET, SOCK_DGRAM, SOL_UDP);
if ($socket === false) {
//echo "socket_create() failed: reason: " . socket_strerror(socket_last_error()) . "\n";
$status["status"] = "offline";
return $status;
} else {
$timeout = array('sec' => 10, 'usec' => 0);
socket_set_option($socket, SOL_SOCKET, SO_RCVTIMEO, $timeout);
$outputString = "EPSON";
$outputString .= "Q"; // PacketType (Q)
$outputString .= "\x03"; // DeviceType(3)
$outputString .= "\x00"; // DeviceNumber(0)
$outputString .= "\x00"; // Function(0010h)
$outputString .= "\x10"; // Function(0010h)
$outputString .= "\x00"; // Result
$outputString .= "\x00";
$outputString .= "\x00"; // parameter length Length
$outputString .= "\x00";
$len = strlen($outputString);
if (false !== socket_sendto($socket, $outputString, $len, 0, $printerIp, $printerPort)) {
if (false !== ($bytes = socket_recv($socket, $buf, 2048, MSG_WAITALL))) {
//echo "Read $bytes bytes from socket_recv(). Closing socket...";
//if (($buf[10] == "\x00") && ($buf[11] == "\x00")) {
if ($debug) {
for ($i = 0; $i < 27; $i++) {
echo $i . ": " . bin2hex($buf[$i]) . "<br>";
echo $i . ": " . str_pad(decbin(ord($buf[$i])), 8, '0', STR_PAD_LEFT) . "<br>";
}
echo "<br>";
}
if (ord($buf[15]) & pow(2, 3)) {
$statusValue = "offline";
} else {
$statusValue = "online";
}
$status["status"] = $statusValue;
if (ord($buf[15]) & pow(2, 5)) {
$cover = "open";
} else {
$cover = "closed";
}
$status["cover"] = $cover;
if (ord($buf[15]) & pow(2, 6)) {
$feeding = "feeding";
} else {
$feeding = "not";
}
$status["feeding"] = $feeding;
if (ord($buf[16]) & pow(2, 3)) {
$autocutter = "error";
} else {
$autocutter = "not";
}
$status["autocutterError"] = $autocutter;
if (ord($buf[16]) & pow(2, 5)) {
$unrecoverableError = "error";
} else {
$unrecoverableError = "not";
}
$status["unrecoverableError"] = $unrecoverableError;
if (ord($buf[16]) & pow(2, 6)) {
$recoverableError = "error";
} else {
$recoverableError = "not";
}
$status["recoverableError"] = $recoverableError;
if ((ord($buf[17]) & pow(2, 2)) &&
(ord($buf[17]) & pow(2, 3))) {
$paperStatus = "paperEnd";
} else {
$paperStatus = "paperPresent";
}
$status["paper"] = $paperStatus;
//}
} else {
//echo "socket_recv() failed; reason: " . socket_strerror(socket_last_error($socket)) . "\n";
$status["status"] = "offline";
}
} else {
//echo "socket_sendto() failed";
$status["status"] = "offline";
}
socket_close($socket);
return $status;
}