【问题标题】:Running c-program through PHP (raspberry Pi)通过 PHP(树莓派)运行 c 程序
【发布时间】:2016-04-18 11:24:46
【问题描述】:

在我的第三个 if 语句中,当我按下按钮时,我想在树莓派上运行一个 C 程序。我不知道该怎么做。

现在 C 程序只会点亮一个 LED,如果我只运行编译后的程序,它就可以工作,但我不知道如何通过 PHP 来实现。

我在某处读过使用“exec”,但我不知道该怎么做(我对编程比较陌生)。

<html>
<head>
</head>
<body>


<form method=GET action="index.php">

<input name="button" type="submit" value="Turnon">
<input name="button" type="submit" value="Turnoff">
<input name="button" type="submit" value="ON">
<input name="button" type="submit" value="OFF">

</form>


<?php



if ($_GET["button"] == "Turnon")
   {
    system ( "gpio mode 28 out" );
    system ( "gpio write 28 1" );
   };

if ($_GET["button"] == "Turnoff")
   {
    system ( "gpio mode 28 out" );
    system ( "gpio write 28 0" );
   };

if ($_GET["button"] == "ON")
   {
    system("sudo /home/pi/var/www/blink.exe >/dev/null 2>/dev/null & ");
   };

if ($_GET["button"] == "OFF")
   {
    system ( "gpio mode 28 out" );
    system ( "gpio write 28 0" );
   };

?>


</body>
</html>

【问题讨论】:

    标签: php c raspberry-pi exec


    【解决方案1】:

    你检查过 PHP 中的shellexec() 命令吗?

    【讨论】:

    • 正如我所说,我对编程很陌生,所以我真的不知道如何使用它。
    【解决方案2】:

    来自 PHP docs

    string exec ( string $command [, array &$output [, int &$return_var ]] )
    

    在您的代码中:

    <?php
    exec("gpio mode 28 out", $output);
    print_r($output);
    

    $output 包含脚本的结果

    【讨论】:

    • 最后我不需要LED来点亮吗?我只需要能够在树莓上运行一个 c 程序(通过树莓上的 php)。 LED程序只是为了测试它是否有效。我实际上需要写入显示器,这就是我需要 c 程序的原因。如果你能用你发给我的东西做到这一点,我很抱歉,但我不明白
    • 您说您不知道如何使用 exec,exec 就像一个终端(命令提示符),您可以在其中执行任何脚本或命令。 exec 函数将该命令的输出存储在 $output 变量中。
    • 运行一个c程序,它是一个.exe格式。 exec("path_to_exe_file/file.exe", $output)
    • 还是不行。按下按钮时 LED 不会亮起。
    • 没关系...我不得不更改 /etc/sudoer 文件,现在它可以工作了(虽然我使用的是系统而不是 exec)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-07
    • 1970-01-01
    • 2018-03-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多