php - PHP串行端口数据从Arduino返回

  显示原文与译文双语对照的内容
0 0

我不知道可以通过某种方法来完成读取串口通过PHP,工作方式:)

在我Arduino练习技能,我开发了一个简单LED开/关草图。 它的工作原理就是上输入 或关闭在串口监视器。

下一步,我做好了网页,以作为GUI界面单击链接并执行上面的打开和关闭函数。 这个基于web的GUI工作通过PHP 。 我使用 PHP SERIAL 类来与串口交互我Arduino正在使用。

出现问题我需要找到一种途径获得回复从串行端口。 使用Arduino IDE串行监视,我看见我打印好的消息以响应我的串行输入我需要检索相同的反馈在我PHP代码。

在PHP Serial类提供了 readPort( ) 函数但我不返回数据。

UPDATE[ 2] :

Arduino :

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}";
                }
        }
    }
?>
时间:原作者:3个回答

0 0

/dev/ttyUSB0以root用户运行,如果你使用Apache试试chown /dev/ttyUSB0 Apache或用户已登录。

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

从ubuntu和用户的身份登录,然后重试,或者试一试注销用户。

原作者:
0 0

那是你最好时要读取串口上的数据不。 需要实现 JavaScript 代码能够调用 PHP 代码可以定期读取。

有数据时,应该处理它。

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();
?>
原作者:
...