Mike42\Escpos\PrintConnectors\PrintConnector::read PHP Method

read() public method

Read data from the printer.
public read ( string $len ) : Data
$len string Length of data to read.
return Data read from the printer, or false where reading is not possible.
    public function read($len);

Usage Example

Example #1
0
 /**
  * @param int $type The type of status to request. May be any of the Printer::STATUS_* constants
  * @return stdClass Class containing requested status, or null if either no status was received, or your print connector is unable to read from the printer.
  */
 function getPrinterStatus($type = self::STATUS_PRINTER)
 {
     self::validateIntegerMulti($type, array(array(1, 4), array(6, 8)), __FUNCTION__);
     // Determine which flags we are looking for
     $statusFlags = array(self::STATUS_PRINTER => array(4 => "pulseHigh", 8 => "offline", 32 => "waitingForOnlineRecovery", 64 => "feedButtonPressed"), self::STATUS_OFFLINE_CAUSE => array(4 => "coverOpen", 8 => "paperManualFeed", 32 => "paperEnd", 64 => "errorOccurred"), self::STATUS_ERROR_CAUSE => array(4 => "recoverableError", 8 => "autocutterError", 32 => "unrecoverableError", 64 => "autorecoverableError"), self::STATUS_PAPER_ROLL => array(4 => "paperNearEnd", 32 => "paperNotPresent"), self::STATUS_INK_A => array(4 => "inkNearEnd", 8 => "inkEnd", 32 => "inkNotPresent", 64 => "cleaning"), self::STATUS_INK_B => array(4 => "inkNearEnd", 8 => "inkEnd", 32 => "inkNotPresent"), self::STATUS_PEELER => array(4 => "labelWaitingForRemoval", 32 => "labelPaperNotDetected"));
     $flags = $statusFlags[$type];
     // Clear any previous statuses which haven't been read yet
     $f = $this->connector->read(1);
     // Make request
     $reqC = chr($type);
     switch ($type) {
         // Special cases: These are two-character requests
         case self::STATUS_INK_A:
             $reqC = chr(7) . chr(1);
             break;
         case self::STATUS_INK_B:
             $reqC = chr(7) . chr(2);
             break;
         case self::STATUS_PEELER:
             $reqC = chr(8) . chr(3);
             break;
     }
     $this->connector->write(self::DLE . self::EOT . $reqC);
     // Wait for single-character response
     $f = $this->connector->read(1);
     $i = 0;
     while (($f === false || strlen($f) == 0) && $i < 50000) {
         usleep(100);
         $f = $this->connector->read(1);
         $i++;
     }
     if ($f === false || strlen($f) == 0) {
         // Timeout
         return null;
     }
     $ret = new \stdClass();
     foreach ($flags as $num => $name) {
         $ret->{$name} = (ord($f) & $num) != 0;
     }
     return $ret;
 }