从Arduino的PHP串口数据返回 [英] PHP serial port data return from Arduino

查看:398
本文介绍了从Arduino的PHP串口数据返回的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不知道是否有做到通过PHP阅读我的串行端口的方式 - 即作品: - )

在练习我的Arduino的技能,我开发​​了一个简单的LED ON / OFF草图。它的工作原理通过输入关闭在串行监视器。

下一步,我把一个网页,作为一个GUI界面点击一个链接以及打开和关闭上述功能执行。这种基于Web的GUI通过PHP的作品。
我使用的是 PHP SERIAL 类与我的Arduino是使用串行端口进行交互。

的问题是我需要找到从串口获得反馈的一种方式。使用Arduino的IDE串口监视器,我可以看到我打印的消息回应我的每一个串行输入的,我需要找回我的PHP code中的相同的反馈。

PHP的串行类提供的 readPort()的功能,但我不回我的数据。

UPDATE [2]:

ARDUINO:

  const int的greenPin = 2;
const int的bluePin = 3;
const int的redPin = 4;INT currentPin = 0; //当前引脚被褪
INT亮度= 0; //当前的亮度水平无效设置(){
  Serial.begin(9600);  pinMode(greenPin,OUTPUT);
  pinMode(bluePin,OUTPUT);
  pinMode(redPin,OUTPUT);
}空隙环(){
  //如果在缓冲区中的所有串行数据,读一个字节
  如果(Serial.available()大于0){
    INT inByte = Serial.read();      //通过仅响应的值R,G,B,或0'9'
      如果(inByte =='R')
        currentPin = redPin;      如果(inByte =='G')
        currentPin = greenPin;      如果(inByte =='B')
        currentPin = bluePin;      如果(inByte&GT ='0'和;&放大器; inByte&下; ='9'){
        //传入字节的值映射到analogRead的范围()命令
        亮度=地图(inByte,'0','9',0,255);
        //设置当前引脚当前的亮度:
        analogWrite(currentPin,亮度);
      }      Serial.print(当前PIN:);
      Serial.println(currentPin);      Serial.print(亮度);
      Serial.println(亮度);  } //关闭串口检查}

PHP / HTML:

 < PHP
    要求(php_serial.class.php);
//包括(php_serial.class.php);    //让我们开始类
    $串行=新phpSerial();    //首先,我们必须指定设备。这适用于Linux和Windows(如果
    //你的Linux串口设备是/ dev / ttyS0来为COM1等)
    $ serial-> deviceSet(的/ dev / ttyACM0);    //为9600-8-N-1(无流量控制)
    $ serial-> confBaudRate(9600); //波特率:9600
    $ serial-> confParity(无); //校验(这是N中的8-N-1)
    $ serial-> confCharacterLength(8); //字符长度(这是8,在8-N-1)
    $ serial-> confStopBits(1); //停止位(这是1,在8-N-1)
    $ serial-> confFlowControl(无);    //然后,我们需要打开它
    $ serial-> deviceOpen();    //读取数据
    $读= $ serial-> readPort();
打印< pre>中;
的print_r($读取);
打印< / pre>中;    //打印出的数据
    回声$读取;
        //打印EXEC(回声r9g9b9'>的/ dev / ttyACM0);
    打印响应(1):{$读}&L​​T; BR>< BR>中;    //如果要更改配置,必须将设备关闭。
    $ serial-> deviceClose();
?>< PHP
    如果(使用isset($ _ REQUEST ['LED']))
        响应();
?><形式的行动='的index.php'方法=POST>
    <选择一个id ='领导'的名字='LED'>
        <选项ID ='零'> - < /选项>
        <选项ID ='红'> RED< /选项>
        <选项ID ='绿色'>绿色< /选项>
        <选项ID ='蓝'>蓝色和LT; /选项>
        <选项ID ='所有'> ALL< /选项>
    < /选择>
    <输入类型=提交值=SET>
< /表及GT;< PHP
    打印你好,地球人!;    响应函数(){
        $ CMDString =;
        $执行= FALSE;        如果(使用isset($ _ REQUEST ['LED'])){
                开关($ _REQUEST ['LED']){
                    案红:
                        $ CMDString ='R9';
                        $执行= TRUE;        EXEC(回声r9g0b0'>的/ dev / ttyACM0);                打印< BR>实测值:{$ _REQUEST ['LED']};
                        打破;
                    案件绿色:
                        $ CMDString ='G9';
                        $执行= TRUE;
                打印< BR>实测值:{$ _REQUEST ['LED']};
                        打破;
                    案蓝色:
                        $ CMDString ='B9';
                        $执行= TRUE;
                打印< BR>实测值:{$ _REQUEST ['LED']};
                        打破;
                    案ALL:
                        $ CMDString ='r9g9b9';
                        $执行= TRUE;
                打印< BR>实测值:{$ _REQUEST ['LED']};
                        打破;
                    默认:
                        打印EXEC(回声r0g0b0'>的/ dev / ttyACM0);
                        $执行= FALSE;
                        打破;
                }                如果($执行){
                    打印EXEC(回声'{$ CMDString}'>的/ dev / ttyACM0);
                    打印< BR>< BR>执行:{$ CMDString};
                }
        }
    }
?>


解决方案

我假设你在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

  $计划生育=的fopen(的/ dev / ttyACM0,W +);
如果(!$ FP){
        回声错误;模具();
}FWRITE($ FP,$ _ SERVER ['argv']作为[1] 0×00);
回声FREAD($ FP,10);FCLOSE($ FP);

有是你必须记住的只有一件事。 的Arduino将重新启动每个连接上。如果你不知道它会迷惑你。例如,如果你连接(FOPEN),并立即发送数据,因为它的启动(这需要一两秒钟)的Arduino会错过它。实验与睡眠给它一些时间。如果要禁用重启从GRD使用10uF的电容RST。

好运气

PS。你可以用画面

排查

 屏幕的/ dev / ttyACM0 9600

有关与Arduino的<一个设置PHP帖子href=\"http://systemsarchitect.net/connecting-php-with-arduino-via-serial-port-on-linux/\">http://systemsarchitect.net/connecting-php-with-arduino-via-serial-port-on-linux/还有一此<一个href=\"http://systemsarchitect.net/arduino-and-php-serial-communication-with-a-protocol/\">http://systemsarchitect.net/arduino-and-php-serial-communication-with-a-protocol/.

I wonder if there is a way to accomplish reading my serial port via PHP - that works :-)

In practising my Arduino skills, I developed a simple LED ON/OFF sketch. It works by entering on or off in the serial monitor.

Next step, I put together a webpage to act as an GUI interface to click a link and perform the on and off function above. This webbased GUI works via PHP. I am using the PHP SERIAL class to interact with the serial port my Arduino is using.

The issue is I need to find a way of getting feedback from the serial port. Using the Arduino IDE serial monitor, I can see my printed messages in response to each of my serial input and I need to retrieve the same feedback in my PHP code.

The PHP Serial class offers a readPort() function but I does not return my data.

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}";
                }
        }
    }
?>

解决方案

I assume you work on linux.

First setup your serial port:

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

Then you can use good old fashion fread/fwrite

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

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

fclose($fp);

There is only one thing you have to remember. Arduino will restart on every connection. If you don't know that It will confuse you. For instance if you connect (fopen) and instantly send data Arduino will miss it because it's booting (which takes a sec or two). Experiment with sleep to give it some time. If you want to disable restarting use 10uF capacitor from GRD to RST.

Good luck

ps. you can troubleshoot with "screen"

screen /dev/ttyACM0 9600

Post about setting PHP with Arduino http://systemsarchitect.net/connecting-php-with-arduino-via-serial-port-on-linux/ and one more here http://systemsarchitect.net/arduino-and-php-serial-communication-with-a-protocol/.

这篇关于从Arduino的PHP串口数据返回的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆