}
}
function errorhandler($errno, $errstr, $errfile, $errline)
{
    throw new Exception(basename($errfile) . '[' . $errline . '] (errno:' . $errno . ') -- ' . $errstr);
}
set_error_handler('errorhandler');
$s = new phpSerial();
try {
    if (!$s->confPort('/dev/ttyAMA0', 9600)) {
        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);