throw new Exception("confPort() failed");
    }
    if (!$s->deviceOpen()) {
        throw new Exception("deviceOpen() failed");
    }
    if (!$s->confBlocking(false)) {
        throw new Exception("confBlocking() failed");
    }
} catch (Exception $e) {
    print "exception: {$e}\n";
    die;
}
$print_after_read = false;
$keystring = "";
do {
    $r = array($s->getFilehandle());
    $w = null;
    $e = null;
    $ready_count = stream_select($r, $w, $e, NULL);
    if ($ready_count !== false && $ready_count > 0) {
        $keylen = $s->readPort($keystring);
        if ($print_after_read) {
            print "readPort() returned {$keylen} bytes\n";
        }
        if (strlen($keystring) >= 16) {
            try {
                $key = new rfid_key($keystring);
                print "received valid rfid key: {$key}\n";
                $keystring = "";
                // consume the string after success
            } catch (Exception $e) {