15

PHP経由でシリアルポートの読み取りを達成する方法があるかどうか疑問に思います-それは機能します:-)

Arduino のスキルを練習する中で、簡単な LED のオン/オフのスケッチを作成しました。シリアルモニタにonまたはoffを入力することで動作します。

次のステップでは、リンクをクリックして上記のオンとオフの機能を実行するための GUI インターフェイスとして機能する Web ページを作成します。この Web ベースの GUI は PHP 経由で動作します。PHP SERIALクラスを使用して、Arduino が使用しているシリアル ポートとやり取りしています。

問題は、シリアル ポートからフィードバックを取得する方法を見つける必要があることです。Arduino IDE シリアル モニターを使用すると、各シリアル入力に応答して印刷されたメッセージを確認できます。また、PHP コードで同じフィードバックを取得する必要があります。

PHP Serial クラスにはreadPort()関数がありますが、データが返されません。

更新[2]:

アルディーノ:

const int greenPin = 2;
const int bluePin = 3;
const int redPin = 4;

int currentPin = 0; //current pin to be faded
int brightness = 0; //current brightness level

void setup(){
  Serial.begin(9600);

  pinMode(greenPin, OUTPUT);
  pinMode(bluePin, OUTPUT);
  pinMode(redPin, OUTPUT);
}

void loop(){
  //if there's any serial data in the buffer, read a byte
  if( Serial.available() > 0 ){
    int inByte = Serial.read();

      //respond only to the values 'r', 'g', 'b', or '0' through '9'
      if(inByte == 'r')
        currentPin = redPin;

      if(inByte == 'g')
        currentPin = greenPin;

      if(inByte == 'b')
        currentPin = bluePin;

      if(inByte >= '0' && inByte <= '9'){
        //map the incoming byte value to the range of the analogRead() command
        brightness = map(inByte, '0', '9', 0, 255);
        //set the current pin to the current brightness:
        analogWrite(currentPin, brightness);
      }

      Serial.print("Current Pin : ");
      Serial.println(currentPin);

      Serial.print("Brightness : ");
      Serial.println(brightness);

  }//close serial check

}

PHP/HTML:

<?php
    require("php_serial.class.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("/dev/ttyACM0");

    // Set for 9600-8-N-1 (no flow control)
    $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");

    // Then we need to open it
    $serial->deviceOpen();

    // Read data
    $read = $serial->readPort();
print "<pre>";
print_r($read);
print "</pre>";

    // Print out the data
    echo $read;
        // print exec("echo 'r9g9b9' > /dev/ttyACM0");
    print "RESPONSE(1): {$read}<br><br>";

    // If you want to change the configuration, the device must be closed.
    $serial->deviceClose();
?>

<?php
    if( isset($_REQUEST['LED']) )
        response();
?>

<form action='index.php' method='POST'>
    <select id='led' name='LED'>
        <option id='nil'>-</option>
        <option id='red'>RED</option>
        <option id='green'>GREEN</option>
        <option id='blue'>BLUE</option>
        <option id='all'>ALL</option>
    </select>
    <input type='submit' value='SET'>
</form>

<?php
    print "Hi, Earthlings!";

    function response(){
        $CMDString = "";
        $execute   = false;

        if( isset($_REQUEST['LED']) ){
                switch ($_REQUEST['LED']) {
                    case 'RED':
                        $CMDString = 'r9';
                        $execute = true;

        exec("echo 'r9g0b0' > /dev/ttyACM0");

                print "<br>FOUND: {$_REQUEST['LED']}";
                        break;
                    case 'GREEN':
                        $CMDString = 'g9';
                        $execute = true;
                print "<br>FOUND: {$_REQUEST['LED']}";
                        break;
                    case 'BLUE':
                        $CMDString = 'b9';
                        $execute = true;
                print "<br>FOUND: {$_REQUEST['LED']}";
                        break;
                    case 'ALL':
                        $CMDString = 'r9g9b9';
                        $execute = true;
                print "<br>FOUND: {$_REQUEST['LED']}";
                        break;
                    default:
                        print exec("echo 'r0g0b0' > /dev/ttyACM0");
                        $execute = false;
                        break;
                }

                if($execute){
                    print exec("echo '{$CMDString}' > /dev/ttyACM0");
                    print "<br><br>executing: {$CMDString}";
                }
        }
    }
?>
4

3 に答える 3

14

Linuxで作業していると仮定します。

最初にシリアルポートをセットアップします:

stty -F /dev/ttyACM0 cs8 9600 ignbrk -brkint -imaxbel -opost -onlcr -isig -icanon -iexten -echo -echoe -echok -echoctl -echoke noflsh -ixon -crtscts

次に、古き良きファッションの fread/fwrite を使用できます

$fp =fopen("/dev/ttyACM0", "w+");
if( !$fp) {
        echo "Error";die();
}

fwrite($fp, $_SERVER['argv'][1] . 0x00);
echo fread($fp, 10);

fclose($fp);

覚えておかなければならないことは 1 つだけです。Arduino は接続ごとに再起動します。それを知らないと混乱します。たとえば、接続 (fopen) してすぐにデータを送信すると、Arduino は起動中 (1 ~ 2 秒かかる) であるため、データを受信できません。睡眠を試して、時間を与えてください。再起動を無効にしたい場合は、GRD から RST に 10uF のコンデンサを使用してください。

幸運を

ps。「画面」でトラブルシューティングできます

screen /dev/ttyACM0 9600

Arduino での PHP の設定に関する投稿http://systemsarchitect.net/connecting-php-with-arduino-via-serial-port-on-linux/と、もう 1つここhttp://systemsarchitect.net/arduino-and-php-プロトコル付きシリアル通信/ .

于 2013-01-26T19:53:37.620 に答える
3

/dev/ttyUSB0 はユーザー root で実行されます。Apache を使用している場合は、chown /dev/ttyUSB0 を apache に送信するか、ユーザーがログインしている場合。

$ sudo chown apache2:apache2 /dev/ttyACM0
OR
$ sudo chown yourusername:yourusername /dev/ttyACM0

もう一度やり直すか、ubuntu からログアウトして root ユーザーとしてログインしてみてください。

于 2012-12-24T00:26:35.490 に答える
2

シリアルポートにデータがない場合、おそらく読み取ろうとしています。定期的に読み取ることができるPHPコードを呼び出すには、JavaScriptコードを実装する必要があります。

データを取得したら、それを処理する必要があります。

readPort()私にとってはうまくいきました。ボーレート、パリティなどが適切に調整されていれば、シリアルポートの読み取りに問題はないはずです。

Arduino用ライブラリの使用例です。少し前に私のために働いた:

<?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("/dev/ttyUSB0");

    // Set for 9600-8-N-1 (no flow control)
    $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");

    // Then we need to open it
    $serial->deviceOpen();

    // Read data
    $read = $serial->readPort();

    // Print out the data
    echo $read;

    // If you want to change the configuration, the device must be closed.
    $serial->deviceClose();
?>
于 2012-10-29T09:11:33.390 に答える