ip_adr = $ip; $this->port = $p; $this->timeout = $to; $this->connection = false; } function __destruct() { if ($this->connection != false) fclose($this->connection); } // Connect to the Board. // returns false on error, true on success // note: This call is not necessary. The library will automatically // whenever it needs to connect. function connect() { if ($this->connection != false) return; $this->connection = fsockopen($this->ip_adr,$this->port,$errno,$errstr,$this->timeout); for ($i=0; $i<12 ; $i++) { $this->portstatus[$i] = 'u'; } if ( $this -> connection == false) return false; return true; } // Tear down the connection. function disconnect() { if ($this->connection != false) fclose($this->connection); $this->connection = false; } // Check IO // if cacheok is true, it is ok to returned cached values (that is if // an ouput has been set previously without asking over the network again. Inputs // will always be queried. // portid specified which port to be read. 0..7 are the output ports 8..11 the inputs // ports. function queryio($portid, $cacheok) { if ($portid < 0 || $portid >= 12 ) return false; if ( $this->connection == false) $this->connect(); if ( $this->connection == false) return false; if ( $cacheok == true && $portid < 8 && $this->portstatus[$portid] != 'u') { return $this->portstatus[$portid]; } if ( $portid < 8) { $command = "GETSTATUS\r\n"; if (fwrite($this->connection, $command) == false) return false; $answer = fgets($this->connection); if ( $answer == false) return false; // Anwort ist "Sxxxxxxxx" // für x=0 für low, 1 für high // Portsnummern: S87654321 $answer = trim($answer); // Whitespaces weg (CR, LF am Ende) if (strlen($answer) != 9) return false; // nicht erwartete Anwort $start = substr($answer,0,1); $answer = substr($answer,1); if ( $start != 'S') return false; $i=0; foreach (str_split($answer) as $chr) { $this->portstatus[$i++] = $chr; } return $this->portstatus[$portid]; } // Eingänge, die muss man immer abfragen.... $realport = $portid - 7; $command = "GETPORT ".$realport. "\r\n"; if (fwrite($this->connection, $command) == false) return false; $answer = fgets($this->connection); if ( $answer == false) return false; $answer = trim($answer); // Whitespaces weg (CR, LF am Ende) if ( $answer == '0') { $this->portstatus[$portid] = 0; return 0; } elseif ( $answer == '1') { $this->portstatus[$portid] = 1; return 1; } else { return false; } } // Set the output port to a specified value // portid = port to be set // value = 0 for low, 1 for high function setport($portid, $value) { if ( $portid >= 8 ) return false; if ( $this->connection == false) $this->connect(); if ( $this->connection == false) return false; $command = "SETPORT " . ($portid+1) . "." .$value . "\r\n"; if (fwrite($this->connection, $command) == false) return false; $answer = fgets($this->connection); if ( $answer == false) return false; if ( trim($answer) == "ACK") { $this->portstatus[$portid] = $value; return true; } $this->portstatus[$portid] = 'u'; return false; } // Read the ADC value from the analog inputs. (0...1023) // returns the value on success, false on error. function queryad($portid) { if ($portid > 4) return false; if ( $this->connection == false) $this->connect(); if ( $this->connection == false) return false; $command = "GETADC ". ($portid+1) . "\r\n"; if (fwrite($this->connection, $command) == false) return false; $answer = fgets($this->connection); if ( $answer == false) return false; $answer = trim($answer); if ( !is_numeric($answer)) return false; return (int)$answer; } // Not yet implemented! function writedisplay($string, $line) { return false; } // Not yet implemented! function cleardisplay() { return false; } } ?>