예제 #1
1
 /**
  * Execute the console command.
  *
  * @return mixed
  */
 public function fire()
 {
     while (true) {
         $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($this->argument('device'));
         // We can change the baud rate, parity, length, stop bits, flow control
         $serial->confBaudRate(9600);
         $serial->confParity("none");
         $serial->confCharacterLength(8);
         $serial->confStopBits(1);
         $serial->confFlowControl("none");
         $serial->deviceOpen();
         $read = '';
         $theResult = '';
         $start = microtime(true);
         while ($read == '' && microtime(true) <= $start + 0.5) {
             $read = $serial->readPort();
             if ($read != '') {
                 $theResult .= $read;
                 $read = '';
             }
         }
         $theResult = trim($theResult);
         if (!$theResult) {
             continue;
         }
         echo $theResult . "\n";
         $line = explode("\n", $theResult);
         foreach ($line as $l) {
             $l = trim($l);
             $watt_array = explode(" ", $l);
             $unitId = 1;
             if ($watt_array[0] == "watt" && isset($watt_array[1]) && $watt_array[1] != false) {
                 Watts::addWatt($unitId, $watt_array[1]);
                 //funtion sa models/Watts.php
                 Units::lessCredit($unitId, $watt_array[1]);
             }
         }
         $unitId = 1;
         if (Units::hasCredit($unitId)) {
             $serial->sendMessage("on");
         } else {
             $serial->sendMessage("off");
         }
         $serial->deviceClose();
     }
 }
 /**
  * 
  * @param string $message Message to sent to a client.
  * @return boolean True on success, false - otherwise.
  */
 public function sendMessage($message)
 {
     $this->_logger->log(__METHOD__, array('message' => $message));
     if ($this->_serial->_dState !== PhpSerial::SERIAL_DEVICE_OPENED) {
         $this->_logger->log(__METHOD__ . ' Serial port is not opened', array('port' => $this->_serial->_device));
         $this->_errors[] = 'Serial port ' . $this->_serial->_device . ' is not opened';
         return false;
     }
     $this->_serial->sendMessage($message);
     return true;
 }
예제 #3
1
<?php

require 'vendor/autoload.php';
$sensor = intval($_POST['sensor']);
$val = intval($_POST['value']);
$serial = new PhpSerial();
$serial->deviceSet("/dev/ttyACM0");
$serial->confBaudRate(57600);
$serial->confParity("none");
$serial->confCharacterLength(10);
$serial->confStopBits(1);
$serial->confFlowControl("none");
$serial->deviceOpen();
$byte = $val << 8;
$byte |= $sensor;
$serial->sendMessage($byte);
$serial->deviceClose();
예제 #4
0
 /**
  * Sends seprate command to COM port and wait for response.
  * 
  * @param string $command Command to send.
  * @param string $output Returns response here (optional).
  * @param float $timeout Timeout for waiting response (optional).
  * @return boolean True if there were no errors, false - otherwise.
  */
 protected function sendCommand($command, &$output = null, $timeout = 1)
 {
     $this->_logger->log(__METHOD__, array('command' => $command));
     $this->_serial->sendMessage($command, $timeout);
     $response = $this->_serial->readPort();
     if ($response === false) {
         $this->_errors[] = 'Error during send command: ' . $command;
         return false;
     }
     $output = $response;
     return true;
 }
예제 #5
0
파일: sms.php 프로젝트: kabacs/SMS.server
function enviarSMS($nr_tlm, $sms)
{
    $serial = new PhpSerial();
    $serial->deviceSet("COM6");
    //choose COM port
    $serial->confBaudRate(9600);
    //Choose Bitrate
    $serial->deviceOpen('w+');
    //Open COM port
    $serial->sendMessage("AT" . chr(13));
    //Check if is acessible
    //read response and check if it can continue
    $codigo_at = $serial->readPort();
    $verificar = substr($codigo_at, -4);
    if (substr($verificar, 0, 2) == 'OK') {
        $serial->sendMessage("AT+CMGF=1" . chr(13));
        //Prepare to SMS
        //read response and check if it may continue
        $codigo_at = $serial->readPort();
        $verificar = substr($codigo_at, -4);
        if (substr($verificar, 0, 2) == 'OK') {
            $serial->sendMessage('AT+CMGS="' . $nr_tlm . '"' . chr(13));
            //send destination number
            $serial->sendMessage($sms . chr(26));
            //send SMS text
            sleep(5);
            //read response and check if the sms was sent
            $codigo_at = $serial->readSMS();
            $verificar = substr($codigo_at, -4);
            if (substr($verificar, 0, 2) == 'OK') {
                $resposta = "OK";
            } else {
                $resposta = "NOK";
            }
        } else {
            $resposta = "NOK";
        }
    } else {
        $resposta = "NOK";
    }
    $serial->deviceClose();
    //close COM port - NEVER FORGET THIS STEP!
    return $resposta;
}
예제 #6
0
 public function test_AtAndCpinCommands()
 {
     $connector = new PhpSerial();
     $connector->deviceSet('COM1');
     $connector->confBaudRate(9600);
     $connector->confParity('none');
     $connector->confFlowControl('rts/cts');
     $connector->confCharacterLength(8);
     $connector->confStopBits(1);
     $connector->deviceOpen('r+b');
     stream_set_timeout($connector->_dHandle, 2);
     $connector->sendMessage("AT+CPIN?\r\n");
     $result = $connector->readPort();
     $connector->deviceClose();
     $result = explode("\r\n", $result);
     $this->assertEquals(4, count($result));
     $this->assertEquals('AT+CPIN?', $result[0]);
     $this->assertEmpty($result[1]);
     $this->assertEquals('+CPIN: READY', $result[2]);
     $this->assertEmpty($result[3]);
 }
예제 #7
0
include 'PhpSerial.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("COM1");
// We can change the baud rate, parity, length, stop bits, flow control
$serial->confBaudRate(2400);
$serial->confParity("none");
$serial->confCharacterLength(8);
$serial->confStopBits(1);
$serial->confFlowControl("none");
// 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(2400);
// etc...
//
//
/* Notes from Jim :
> Also, one last thing that would be good to document, maybe in example.php:
>  The actual device to be opened caused me a lot of confusion, I was
> attempting to open a tty.* device on my system and was having no luck at
> all, until I found that I should actually be opening a cu.* device instead!
>  The following link was very helpful in figuring this out, my USB/Serial
예제 #8
0
 // 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("COM1");
 $serial->deviceSet("/dev/cu.usbserial-FTDY7ID6");
 // We can change the baud rate, parity, length, stop bits, flow control
 $serial->confBaudRate(2400);
 $serial->confParity("none");
 $serial->confCharacterLength(8);
 $serial->confStopBits(1);
 $serial->confFlowControl("none");
 // Then we need to open it
 $serial->deviceOpen();
 // To write into
 $serial->sendMessage("I" . $the_input);
 // Or to read from
 $read = '';
 $theResult = '';
 $start = microtime_float();
 while ($read == '' && microtime_float() <= $start + 0.5) {
     $read = $serial->readPort();
     if ($read != '') {
         $theResult .= $read;
         $read = '';
     }
 }
 // If you want to change the configuration, the device must be closed
 $serial->deviceClose();
 // etc...
 echo "Read data: " . $theResult . "<br>";
예제 #9
0
         break;
     case 3:
         $out .= $XB4S . $XB4M;
         break;
 }
 $out .= pack("n", 0x0);
 $out .= $outstr;
 $sum = 0;
 //echo "Strlen " . strlen($out) . " reqlen " . $reqlen . " [" . bin2hex($out) . "]\n";
 //return;
 for ($i = 3; $i < $reqlen + 3; $i++) {
     $sum += ord($out[$i]);
 }
 $sum = 0xff - ($sum & 0xff);
 $out .= pack("C", $sum);
 $serial->sendMessage($out);
 //echo "Sending " . bin2hex($out) . "\n";
 // wait for reply
 $inbuf = '';
 $done = false;
 $timestart = time();
 do {
     $datain = $serial->readPort();
     if ($datain) {
         $inbuf .= $datain;
     }
     //if ($datain) echo bin2hex($datain) . " ";
     $len = strlen($inbuf);
     if ($len == 0) {
         continue;
     }
<?php

require 'vendor/autoload.php';
set_time_limit(60 * 5);
$serial = new PhpSerial();
$serial->deviceSet("/dev/ttyACM0");
$serial->confBaudRate(57600);
$serial->confParity("none");
$serial->confCharacterLength(32);
$serial->confStopBits(1);
$serial->confFlowControl("none");
$source = file_get_contents(isset($_FILES['image']['tmp_name']) ? $_FILES['image']['tmp_name'] : 'descarga.png');
$serial->deviceOpen();
$index = 0;
$len = strlen($source);
$nFrames = ceil($len / 28);
$curFrame = 1;
$start = true;
$MASK = 255;
echo $nFrames . "\n";
while ($index < $len) {
    $aux = substr($source, $index, 28);
    $aux = chr($nFrames >> 8 & $MASK) . chr($nFrames & $MASK) . chr($curFrame >> 8 & $MASK) . chr($curFrame & $MASK) . $aux;
    echo $curFrame . "\n";
    $serial->sendMessage($aux);
    while ($serial->readPort() == "") {
    }
    $index += 28;
    $curFrame++;
}
$serial->deviceClose();
예제 #11
0
 protected function _transmit($message, $fetch_answer = false)
 {
     $this->last_message = $message;
     $this->last_raw_message = $this->_buildRawMessage($this->last_message);
     $this->last_answer = '';
     $this->last_raw_answer = '';
     // start the serial connection
     $serial = $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($this->serial_port);
     // We can change the baud rate, parity, length, stop bits, flow control
     // http://forum.mysensors.org/topic/340/pidome-domotica-home-automation
     $serial->confBaudRate(115000);
     $serial->confParity("none");
     $serial->confCharacterLength(8);
     $serial->confStopBits(1);
     $serial->confFlowControl("none");
     // ?
     // Then we need to open it
     $serial->deviceOpen();
     // we may have to sleep before arduino restart ?
     // http://stackoverflow.com/questions/13114275/php-serial-port-data-return-from-arduino
     //sleep($this->arduino_boot_time);
     // We may need to return if nothing happens for $timeout seconds
     if ($fetch_answer) {
         $timeout = $this->socket_timeout_answer;
     } else {
         $timeout = $this->socket_timeout_message;
     }
     stream_set_timeout($serial->_dHandle, $timeout);
     // send message
     $serial->sendMessage($this->last_raw_message . "\n");
     if ($fetch_answer) {
         $line = '';
         $until_t = time() + $timeout;
         while (true) {
             if (!$serial->_dHandle or time() > $until_t) {
                 break;
             }
             $line = $serial->readPort();
             if ($this->last_answer = $this->_filterAnswer($line)) {
                 $this->last_raw_answer = trim($line);
                 $out = $this->last_answer['payload'];
                 break;
             }
         }
     } else {
         $out = true;
     }
     $serial->deviceClose();
     return $out;
 }
예제 #12
0
파일: servo.php 프로젝트: hck509/arduino
<?php

include 'lib/serial/src/PhpSerial.php';
$serial = new PhpSerial();
$serial->deviceSet("COM11");
//$serial->confBaudRate(9600);
//$serial->confParity("even");
//$serial->confCharacterLength(7);
//$serial->confStopBits(1);
//$serial->confFlowControl("rts/cts");
$serial->deviceOpen('w+');
sleep(5);
// We may need to return if nothing happens for 20 seconds
stream_set_timeout($serial->_dHandle, 20);
for ($i = 0; $i < 100; ++$i) {
    $serial->sendMessage("30,50;");
    var_dump($serial->readPort());
    sleep(0.03);
}
$serial->deviceClose();
예제 #13
0
파일: sms.php 프로젝트: altanai/PHP-Serial
<?php

include 'PhpSerial.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("COM4");
// 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", 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();