c – Arduino序列阅读

我正在使用网络控制的漫游器并使用串行端口与
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,那么你在两个测试中都有赋值而不是比较,所以两者都是真的,所以所有的写操作都是每次都完成的.编译具有高警告级别(如GCC中的-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编写为char,它不是ASCII 49,而是ASCII 1(soh),同样适用于2.尝试将其更改为“1”在PHP代码中(或C代码中的1)

这里有一篇关于Controlling the Serial Port with PHP的文章的链接 – 我用Google搜索,不知道它的质量 – 但看起来不足以将整数写入“com1” – 这超出了我的域名,所以祝你好运:)

点赞