function sendCommand($n) { $serial = new phpSerial(); $serial->deviceSet("/dev/ttyUSB0"); $serial->confBaudRate(9600); $serial->confParity("none"); $serial->confCharacterLength(8); $serial->confStopBits(1); $serial->confFlowControl("none"); $serial->deviceOpen(); $serial->sendMessage($n); // $read = $serial->readPort(); $serial->deviceClose(); }
function init() { $this->port = $serial = new phpSerial(); $p = DylosReaderConf::$serialPort; $this->log("Opening {$p}"); $serial->deviceSet($p); $serial->confBaudRate(9600); $serial->confParity("none"); $serial->confCharacterLength(8); $serial->confStopBits(1); $serial->confFlowControl("none"); // Then we need to open it $serial->deviceOpen(); }
function setupSerial($device) { $serial = new phpSerial(); $serial->deviceSet($device); $serial->confBaudRate(9600); //Baud rate: 9600 $serial->confParity("none"); //Parity (this is the "N" in "8-N-1") $serial->confCharacterLength(8); //Character length (this is the "8" in "8-N-1") $serial->confStopBits(1); //Stop bits (this is the "1" in "8-N-1") $serial->confFlowControl("none"); $serial->deviceOpen(); return $serial; }
<?php include "php_serial.class.php"; // Let's start the class $serial = new phpSerial(); // First we must specify the device. This works on both linux and windows (if // your linux serial device is /dev/ttyS0 for COM1, etc) $serial->deviceSet("COM2"); // Then we need to open it $serial->deviceOpen(); // To write into $serial->sendMessage("Hello !"); // Or to read from //$read = $serial->readPort(); // If you want to change the configuration, the device must be closed $serial->deviceClose(); // We can change the baud rate $serial->confBaudRate(9600); // etc...
<?php include "php_serial.class.php"; // Let's start the class $serial = new phpSerial(); // First we must specify the device. This works on both linux and windows (if // your linux serial device is /dev/ttyS0 for COM1, etc) // If you are using Windows, make sure you disable FIFO from the modem's // Device Manager properties pane (Advanced >> Advanced Port Settings...) $serial->deviceSet("/dev/ttyUSB1"); // Then we need to open it $serial->deviceOpen('w+'); // We may need to return if nothing happens for 10 seconds stream_set_timeout($serial->_dHandle, 10); // We can change the baud rate $serial->confBaudRate(9600); // SMS inbox query - mode command and list command $serial->sendMessage("AT\n\r", 1); var_dump($serial->readPort()); $serial->sendMessage("AT+CMGF=1\n\r", 1); var_dump($serial->readPort()); $serial->sendMessage("AT+CMGL=\"ALL\"\n\r", 2); var_dump($serial->readPort()); // If you want to change the configuration, the device must be closed $serial->deviceClose();
<?php include "php_serial.class.php"; // // Let's start the class // $serial = new phpSerial(); // // First we must specify the device. // Comm settings assumed correct or use Mode to correct // $serial->deviceSet("COM1"); // // Then we need to open it // $serial->deviceOpen("w+"); // All relays off //$serial->sendMessage(sprintf("#050000%c",13),0); $serial->sendMessage(sprintf("#050000\r\n"), 0); usleep(200000); // // If you want to change the configuration, the device must be closed // $serial->deviceClose(); //
} else { throw new RFID_BadKey_Exception("complete 16 bytes expected. received key: {$key}"); } } } 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;
function rdssend($songName, $artistName, $datePassed) { // The shared memory segment you stored the date in $segment_id = 881; // You have to attach to the shared memory segment first $shm = shm_attach($segment_id, PHP_INT_SIZE, 0600); // Then get the date currently stored in the shared memory segment $dateStored = shm_get_var($shm, 1); // Everytime the RDS sender script gets called the Logbook updates the // time saved in the shared memory so we know if we should leave this script if ($datePassed != $dateStored) { // Detach the shared memory segment and exit shm_detach($shm); } // Declare the new Com port $COM = new phpSerial(); // Set the serial device "/dev/ttyS0" for linux, "COM1" for windows if (substr(PHP_OS, 0, 3) == 'WIN') { $COM->deviceSet("COM1"); } else { $COM->deviceSet("/dev/ttyS0"); } /* I suppose PHP doesn't have permissions to execute the mode command, but it's fine because we are using the defaults anyway // Set the baud rate, parity, length, stop bits, flow control $COM->confBaudRate(9600); $COM->confParity("none"); $COM->confCharacterLength(8); $COM->confStopBits(1); $COM->confFlowControl("none"); */ // Remove the bad words and make it all uppercase $artistName = censor($artistName); $songName = censor($songName); // if your song name is the password, we'll keep on sending until Now Playing gets updated if ($songName == PASSWORD) { // See how long the string is so we can see if we need to shorten it $artistNameLength = strlen($artistName); // if the string is longer than the DPS_MAX, get rid of the " THE "s if ($artistNameLength > DPS_MAX) { $artistName = str_replace(" THE ", " ", $artistName); $artistNameLength = strlen($artistName); } // if it's still too long, just cut it down to size. if ($artistNameLength > DPS_MAX) { $artistName = substr($artistName, 0, DPS_MAX); $artistNameLength = strlen($artistName); } // Make the DPS output // The Inovonics 730 requires a carriage return at the end of every string $dpsOut = "DPS=" . $artistName . chr(13); // if the string is longer than the TEXT_MAX, get rid of the " THE "s. if ($artistNameLength > TEXT_MAX) { $artistName = str_replace(" THE ", " ", $artistName); $artistNameLength = strlen($artistName); } // if it's still too long, just cut it down to size. if ($artistNameLength > TEXT_MAX) { $artistName = substr($artistName, 0, TEXT_MAX); } // Make the RT output $rtOut = "TEXT=" . $artistName . chr(13); // Get the date stored again to see if it's been updated $dateStored = shm_get_var($shm, 1); // if the stored date hasn't changed, send the output every three minutes while ($datePassed == $dateStored) { // Open the COM port $COM->deviceOpen(); // Send the strings $COM->sendMessage($dpsOut); $COM->sendMessage($rtOut); // Close the port when you're done $COM->deviceClose(); sleep(180); // Grab the stored date again $dateStored = shm_get_var($shm, 1); } // Detach from the shared memory segment shm_detach($shm); $fs = fopen('test_output.txt', 'w'); fwrite($fs, $dpsOut); fwrite($fs, $rtOut); fclose($fs); } elseif ($songName == "LIVE SESSION" || $songName == "LIVE SESSIONS") { $dpsOut = "LIVE SESSION WITH " . $artistName . " ON " . STATION_NAME; $dpsLen = strlen($dpsOut); $rtOut = $artistName . " LIVE ON " . STATION_NAME; $rtLen = strlen($rtOut); $stationLen = strlen(STATION_NAME) - 1; // if it's too long we'll drop the station name if ($dpsLen > DPS_MAX) { $dpsOut = "LIVE SESSION WITH " . $artistName; $dpsLen = strlen($dpsOut); } // if it's still too long then we'll drop the " THE "s. if ($dpsLen > DPS_MAX) { $dpsOut = str_replace(" THE ", " ", $dpsOut); $dpsLen = strlen($dpsOut); } // And if it's still too long, we'll just cut it short if ($dpsLen > DPS_MAX) { $dpsOut = substr($dpsOut, 0, DPS_MAX); } // Put it in the format the Inovonics 730 likes $dpsOut = "DPS=" . $dpsOut . chr(13); // Now for the Radio Text, except to make it fun, we need to know // the length of the artist name when we go back to calculate the RT+ // if it's too long drop the " THE "s. if ($rtLen > TEXT_MAX) { $artistName = str_replace(" THE ", " ", $artistName); $rtOut = $artistName . " LIVE ON " . STATION_NAME; $rtLen = strlen($rtOut); } // if it's still too long we cut the artist name down to size if ($rtLen > TEXT_MAX) { // The longest the artist name can be is the TEXT_MAX length, // minus the length of the STATION_NAME, plus the 9 characters // for " LIVE ON " plus 1 because station len is the length minus 1 $artMax = TEXT_MAX - ($stationLen + 10); $artistName = substr($artistName, 0, $artMax); $rtOut = $artistName . " LIVE ON " . STATION_NAME; } // Format the output for the Inovonics 730 $rtOut = "TEXT=" . $rtOut . chr(13); // Let's calculate some RT+ // The count starts at zero $artistNameLength = strlen($artistName) - 1; // This will give the starting position of STATION_NAME $stationStart = $artistNameLength + 10; // This makes it so they are all two digits $artistNameLength = str_pad($artistNameLength, 2, "0", STR_PAD_LEFT); $stationLen = str_pad($stationLen, 2, "0", STR_PAD_LEFT); $stationStart = str_pad($stationStart, 2, "0", STR_PAD_LEFT); // Type,Starting Position,Length, Type,Starting Position,Length $rtpOut = "RTP=04,00," . $artistNameLength . ",31," . $stationStart . "," . $stationLen . chr(13); // Grab the date currently stored in memory $dateStored = shm_get_var($shm, 1); // if it is still the same as the date passed in, send it every three minutes until it no longer is while ($datePassed == $dateStored) { // Open the COM port $COM->deviceOpen(); // Send the strings $COM->sendMessage($dpsOut); $COM->sendMessage($rtOut); $COM->sendMessage($rtpOut); // Close the port when you're done $COM->deviceClose(); sleep(180); // Check the date again $dateStored = shm_get_var($shm, 1); } // Detach from the shared memory and exit shm_detach($shm); $fs = fopen('test_output.txt', 'w'); fwrite($fs, $dpsOut); fwrite($fs, $rtOut); fwrite($fs, $rtpOut); fclose($fs); } else { // Call the trim function to cut them down to size for DPS and make the right string list($songName, $artistName) = shorten($songName, $artistName, DPS_MAX); $dpsOut = "DPS=" . $songName . " BY " . $artistName . " ON " . STATION_NAME . chr(13); // Call the trim function to cut them down to size for RT and make the string list($songName, $artistName) = shorten($songName, $artistName, TEXT_MAX); $rtOut = "TEXT=" . $songName . " BY " . $artistName . chr(13); // Start calculating the RT+ value $artistNameLength = strlen($artistName) - 1; $songNameLength = strlen($songName) - 1; // The starting value of the artist name is the length of the song name, plus 4 for " BY " // and plus one because the length of the song name is one less than it actually is $artistStart = $songNameLength + 5; // Make it so that they are two digit numbers $artistNameLength = str_pad($artistNameLength, 2, "0", STR_PAD_LEFT); $songNameLength = str_pad($songNameLength, 2, "0", STR_PAD_LEFT); $artistStart = str_pad($artistStart, 2, "0", STR_PAD_LEFT); // Make the RT+ output with the right format $rtpOut = "RTP=01,00," . $songNameLength . ",04," . $artistStart . "," . $artistNameLength . chr(13); // Get the stored date from the shared memory segment $dateStored = shm_get_var($shm, 1); // if the passed date is the same as the stored date, go ahead and send it if ($dateStored == $datePassed) { // Open the COM port $COM->deviceOpen(); // Send the DPS String $COM->sendMessage($dpsOut); $COM->sendMessage($rtOut); $COM->sendMessage($rtpOut); // Close the port when you're done $COM->deviceClose(); } // Detach the shared memory segment and exit shm_detach($shm); $fs = fopen('test_output.txt', 'w'); fwrite($fs, $dpsOut); fwrite($fs, $rtOut); fwrite($fs, $rtpOut); fclose($fs); } }
//$argv is an array if ($argc == 0) { error(usage()); } $args = parse_args($argc, $argv); if (isset($args['d'])) { $debug = $args['d']; } elseif (isset($args['debug'])) { $debug = $args['debug']; } else { $debug = 0; } $dmx = new phpSerial(); $dmx->deviceSet(DMX_DEV); $dmx->confBaudRate(SERIAL_BAUD); if (!$dmx->deviceOpen()) { die("Could not open DMX Interface on " . DMX_DEV . "\n"); } $f = 0; if ($sn = dmx_request_serial_number()) { printf("Found DMX Interface with Serial Number: %s\n", $sn); if ($fw = dmx_request_parameters()) { printf("Firmware Version: %s\n", $fw['FW_VER']); printf("DMX Output Break Time: %s x 10.67 = %.02f us\n", $fw['DMX_BR_TIME'], $fw['DMX_BR_TIME'] * 10.67); printf("DMX Mark After Break Time: %s x 10.67 = %.02f us\n", $fw['DMX_MABR_TIME'], $fw['DMX_MABR_TIME'] * 10.67); printf("DMX Output Rate: %s packets/sec\n", $fw['DMX_OUTPUT_RATE']); } $f = 1; } if (!$f) { die("Could not find a DMX interface!\n");
#!/usr/bin/php <?php include "lib/php_serial.class.php"; $s = new phpSerial(); $s->deviceSet("/dev/ttyUSB0"); $s->confBaudRate(9600); $s->deviceOpen(); $s->sendMessage("Hello!"); $r = $s->readPort(); print $r; $s->deviceClose();
function getSerialConnection() { static $serial = null; if (!$serial) { print "Opening serial connection .. "; $serial = new phpSerial(); if ($serial->deviceSet('/dev/ttyACM0')) { $serial->confBaudRate(9600); $serial->deviceOpen(); } else { print "ERROR\n"; exit(1); } print "OK\n"; } return $serial; }