我正在开发一个网络控制的流动站,并使用串行端口与 Arduino. 。我写了一些只使用的 PHP fwrite() 并向串行端口写入 ASCII 1 或 ASCII 2。Arduino 正在监听该端口并根据它听到的内容执行操作。我知道我的 PHP 正在工作,因为每当我告诉它发送东西时,Arduino 都会收到它。这是Arduino代码:

//This listens to the serial port (USB) and does stuff based on what it is hearing.

int motor1Pin = 13; //the first motor's port number
int motor2Pin = 12; //the second motor's port number
int usbnumber = 0; //this variable holds what we are currently reading from serial


void setup() { //call this once at the beginning
    pinMode(motor1Pin, OUTPUT);
    //Tell arduino that the motor pins are going to be outputs
    pinMode(motor2Pin, OUTPUT);
    Serial.begin(9600); //start up serial port
}

void loop() { //main loop
    if (Serial.available() > 0) { //if there is anything on the serial port, read it
        usbnumber = Serial.read(); //store it in the usbnumber variable
    }

    if (usbnumber > 0) { //if we read something
        if (usbnumber = 49){
          delay(1000);
          digitalWrite(motor1Pin, LOW);
          digitalWrite(motor2Pin, LOW); //if we read an ASCII 1, stop
        }

        if (usbnumber = 50){
              delay(1000);
              digitalWrite(motor1Pin, HIGH);
              digitalWrite(motor2Pin, HIGH); //if we read an ASCII 2, drive forward
        }

        usbnumber = 0; //reset
    }
}

所以这应该是相当简单的。现在,当我发送 ASCII 1 或 ASCII 2 时,我正在测试的 LED(在引脚 13 上)会亮起并保持亮起状态。但是,如果我发送另一个 ASCII 1 或 2,它会关闭然后重新打开。目标是仅当最后发送的是 ASCII 1 时才打开它,并保持打开状态直到最后发送的是 2。

编辑:这是我的 PHP:

<?php
    $verz="0.0.2";
    $comPort = "com3"; /*change to correct com port */

    if (isset($_POST["rcmd"])) {
        $rcmd = $_POST["rcmd"];
        switch ($rcmd) {
            case Stop:
                $fp =fopen($comPort, "w");
                fwrite($fp, chr(1)); /* this is the number that it will write */
                fclose($fp);


                break;
            case Go:
                $fp =fopen($comPort, "w");
                fwrite($fp, chr(2)); /* this is the number that it will write */
                fclose($fp);
                break;
            default:
                die('???');
        }
    }
?>
<html>
    <head><title>Rover Control</title></head>
    <body>
        <center><h1>Rover Control</h1><b>Version <?php echo $verz; ?></b></center>

        <form method="post" action="<?php echo $PHP_SELF;?>">
            <table border="0">
                <tr>
                    <td></td>
                    <td>

                    </td>
                    <td></td>
                </tr>
                <tr>
                    <td>
                        <input type="submit" value="Stop" name="rcmd"><br/>
                    </td>
                    <td></td>
                    <td>
                        <input type="submit" value="Go" name="rcmd"><br />
                    </td>
                </tr>
                <tr>
                    <td></td>
                    <td><br><br><br><br><br>

                    </td>
                    <td></td>
                </tr>
            </table>
        </form>
    </body>
</html>
有帮助吗?

解决方案

如果是 C 那么你有 任务 代替 比较 在这两个测试中,所以两者都是 true, ,所以每次都会完成所有写入。以高警告级别编译(例如 -Wall -pedantic 在海湾合作委员会)。尝试这个:


int a = 0;
if ( a == 1 ) printf( "a is not one: %d\n", a );
if ( a = 1 ) printf( "a is one: %d\n", a );

从您发布的 PHP 代码(我不是这里的专家)来看,您似乎正在将二进制 1 写为字符,这不是 ASCII 49,而是 ASCII 1 (所以),与 2 相同。尝试将其更改为 '1' 在 PHP 代码中(或 1 在 C 代码中。)

这是一些文章的链接 用PHP控制串口 - 我用谷歌搜索,不知道它的质量 - 但看起来仅仅将一个整数写入“com1”就足够了 - 这超出了我的领域,所以祝你好运:)

其他提示

由于尼古拉提到的,它看起来像你正在做的赋值(=),而不是比较(==)在“if”语句。

有一个良好的习惯,一些C程序员进入是把右值就比较的左侧,这样编译器会产生一个错误,如果你不小心使用了赋值运算符,而不是比较操作:

if (50 == usbnumber) {   // This is okay.
    ...
}

if (50 = usbnumber) {    // The compiler will generate an error here.
    ...
}

这工作的,不管是什么编译标志或警告级别您正在使用,因为分配给右值是非法的。

我要补充一点,这个“安全网”,如果你需要比较两个左值不起作用。

可能为时已晚,但我觉得你的问题是, serial.read()的唯一一次读取一个字符。如果从PC发送“49”,当调用的 usbnumber = serial.read()你会得到“4”的第一环路和“9”的第二回路。这些都不满足条件所以什么也不做并且usbnumber被重置为0。

要修复,则可以更改serial.available条件是

if (Serial.available() == 2)

和然后执行类似下面以转换为数字:

usbnumber = Serial.read() * 10 + Serial.read();

另一种选择是使用TextFinder库 - 我已经在我的网站 HTTP写了一个简短的教程: //mechariusprojects.com/thunderbolt/?p=60

机甲

我发现你的答案寻找其他的东西,反正我只是面对(我猜)你有同样的问题。

如果您还没有解决它,我觉得问题不是你的代码,但在Arduino板自动复位机制。 即:每一个新的连接被建立在串行端口上时,Arduino板复位时,这允许经由串行端口进行编程时它被装载新的固件

要验证这一点,尝试在setup()功能LED闪烁,如果每次加载该页面时闪烁,这证实了我的观点。

看看这里: http://www.arduino.cc/playground/Main/DisablingAutoResetOnSerialConnection

我通过粘贴+ 5V和RESET之间的120欧姆电阻器解决。不过,别忘了你要为新的固件上传到你的板子每次删除它。

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top