首页 文章

Arduino序列阅读

提问于
浏览
4

我正在使用网络控制的流动站,并使用串行端口与Arduino进行通信 . 我写了一些只使用 fwrite() 的PHP,并将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">
</td> <td></td> <td> <input type="submit" value="Go" name="rcmd">
</td> </tr> <tr> <td></td> <td><br><br><br><br><br> </td> <td></td> </tr> </table> </form> </body> </html>

4 回答

  • 1

    如果它是C,那么在两个测试中你都有 assignment 而不是 comparison ,所以两者都是 true ,所以所有的写操作都是每次都完成的 . 编译具有高警告级别(如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.尝试在PHP中将其更改为 '1' 代码(或C代码中的 1 . )

    这里有关于Controlling the Serial Port with PHP的一些文章的链接 - 我用谷歌搜索,不知道它的质量 - 但是不足以将整数写入"com1" - 这是我的域名,所以祝你好运:)

  • 0

    正如Nikolai所说,看起来你在“if”语句中进行赋值(=)而不是比较(==) .

    一些C程序员遇到的一个好习惯是将rvalues放在比较的左侧,这样如果你不小心使用赋值运算符而不是比较运算符,编译器就会产生错误:

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

    无论您使用什么编译器标志或警告级别,这都有效,因为分配到右值是非法的 .

    我应该补充说,如果你需要比较两个左值,这个“安全网”不起作用 .

  • 0

    可能为时已晚,但我认为你的问题是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

    机甲

  • 1

    我找到了你的答案寻找其他的东西,无论如何我只是面对(我猜)你遇到的同样的问题 .

    如果你还没有解决它,我认为问题不是你的代码,而是arduino板上的自动重置机制 . 即:每次在串行端口上 Build 新连接时,arduino板都会复位,这允许在通过串行端口对其进行编程时加载新固件 .

    要验证这一点,请尝试使用 setup() 功能中的LED闪烁,如果每次加载页面时它都闪烁,这证实了我的论文 .

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

    我通过在5V和RESET之间插入120欧姆电阻解决了问题 . 每次要将新固件上传到电路板时,请记住将其删除 .

相关问题