当前位置: 首页 > 知识库问答 >
问题:

从Arduino返回PHP串行端口数据

后源
2023-03-14

我想知道是否有一种方法可以通过PHP来完成对串行端口的读取——有效:——

在练习Arduino技能时,我设计了一个简单的LED开关示意图。它通过在串行监视器中输入on或off来工作。

下一步,我把一个网页放在一起,作为一个GUI界面,点击一个链接并执行上面的开关功能。这个基于网络的GUI通过PHP工作。我使用PHP串行类与Arduino使用的串行端口进行交互。

问题是我需要找到一种从串口获取反馈的方法。使用Arduino IDE串行监视器,我可以看到响应于每个串行输入的打印消息,我需要在PHP代码中检索相同的反馈。

PHP串行类提供了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}";
                }
        }
    }
?>

共有3个答案

宰父远
2023-03-14

当串行端口上没有数据时,您可能正在尝试读取。您需要实现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();
?>
陈琪
2023-03-14

/dev/ttyUSB0与用户root一起运行,如果您正在使用apache,请尝试将chown/dev/ttyUSB0连接到apache,或者用户已登录。

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

然后再试一次,或者尝试从ubuntu注销并以root用户身份登录。

纪勇军
2023-03-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);

你只需要记住一件事。Arduino将在每个连接上重新启动。如果你不知道这会让你困惑。例如,如果你连接(fopen)并立即发送数据,Arduino会错过它,因为它正在启动(需要一两秒钟)。尝试睡眠,给它一些时间。如果你想禁用重启,使用10uF电容器从GRD到RST。

祝你好运

注:您可以使用“屏幕”进行故障排除

screen /dev/ttyACM0 9600

关于使用Arduino设置PHP的帖子http://systemsarchitect.net/connecting-php-with-arduino-via-serial-port-on-linux/还有一个http://systemsarchitect.net/arduino-and-php-serial-communication-with-a-protocol/.

 类似资料:
  • 我想知道是否有一种方法可以通过PHP来完成对串行端口的读取——有效:—— 在练习Arduino技能时,我设计了一个简单的LED开关示意图。它通过在串行监视器中输入on或off来工作。 下一步,我把一个网页放在一起,作为一个GUI界面,点击一个链接并执行上面的开关功能。这个基于网络的GUI通过PHP工作。我使用PHP串行类与Arduino使用的串行端口进行交互。 问题是我需要找到一种从串口获取反馈的

  • 我试图做一些非常简单的东西,按钮计数器,有一个按钮在我的HTML-php当我点击它我发送数据'按'到我的arduino和它使led闪烁,到目前为止,还不错,但然后我想发送信号,当我在arduino中按下按钮,我想让我的php读取字符串"点击次数: N"的串行端口,N是从arduio的推送计数。 这里的问题是,在arduino串行我可以看到他字符串,一切正常,但在PHP我做

  • 问题内容: 我有一个Java程序,必须读取Arduino发送的信息。我从这里获取了Java代码。现在,我不太了解它是如何工作的,但是我尝试对其进行修改,并且得到了以下信息: 我创建一个对象串行COM口,我需要在主程序,然后我使用和当我需要它。 效果很好,Arduino获取数据并将其显示在LCD显示屏中。问题是。程序运行时,它会不断从串行端口读取数据(大约每40毫秒一次),但这并不意味着Arduin

  • 我试图让Arduino在串口读取字符“s”时触发一个中继。该字符“s”由python根据从屏幕读取的图像发送。 我的问题是arduino似乎无法从串行端口读取数据,因为它从不执行if条件。我的猜测是这两者之间存在某种死锁(这就是为什么我将ardu.close()放在函数foo中) 这是我的arduino代码: 这是我的python代码:

  • 问题内容: 我以下列方式从打开的串行端口读取某些数据时遇到麻烦。我已经多次使用此代码实例,并且一切正常,但是现在,由于某种原因,我无法弄清楚,我完全无法从串行端口读取任何内容。 我能够写,并且在另一端正确接收了所有消息,但是从未收到答复(正确发送)(不,电缆都还好;)) 我用来打开串行端口的代码如下: 端口初始化后,我通过简单的write命令向其中写入一些内容。 hCom是文件描述符(没关系),并

  • 我在一个项目中,我想通过串行通信发送传感器数据从Arduino到PHP。 不幸的是,我无法读取PHP中的串行端口。然而,另一个方向(PHP到Arduino)工作得很好。我使用的是php_系列。班来自Rémy Sanchez的php,由Rizwan Kassim修改。我依赖于readPort()函数。 我在Mac OS X上使用Arduino UNO和Apache WAMP-Server。我应该实现