【问题标题】:Arduino serial readingArduino 串行读取
【发布时间】:2009-05-22 17:50:05
【问题描述】:

我正在开发一个网络控制的流动站,并使用串行端口与Arduino 通信。我写了一些只使用fwrite() 并将ASCII 1 或ASCII 2 写入串行端口的PHP。 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 serial-port microcontroller arduino


    【解决方案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 相同。尝试将其更改为'1' 在 PHP 代码中(或 1 在 C 代码中。)

    这是Controlling the Serial Port with PHP 上的一些文章的链接 - 我在 Google 上搜索过,不知道它的质量 - 但看起来仅仅将一个整数写入“com1”是不够的 - 这超出了我的域,祝你好运:)

    【讨论】:

    • 我改变了它,现在 LED 根本不亮。这一定意味着它没有读取 ascii 1 或 2 对吗?
    • 您能以某种方式记录/打印您收到的内容吗?同时发布您的 PHP 代码 - 这将有助于缩小问题范围。
    • 如果我尝试使用 arduino 环境中内置的串行监视器,PHP 会抱怨 com 端口无法写入,
    • 还是什么都没有。一定是 1 PHP 发送的任何表示与它在 arduino 中的比较不同。如何判断 PHP 发送的是什么?
    • 我对PHP不够了解,但是尝试将其保存到变量中并在处理发送后将其打印到页面上。
    【解决方案2】:

    正如 Nikolai 所提到的,看起来您在“if”语句中执行的是赋值 (=) 而不是比较 (==)。

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

    if (50 == usbnumber) { // 这没关系。 ... } if (50 = usbnumber) { // 编译器将在此处生成错误。 ... }

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

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

    【讨论】:

    • 是的,这行得通,但由于某种原因,我一直觉得这种表示法是错误的 :)
    • 正如我所说,一些 C 程序员养成的好习惯:P 有些C 程序员永远不会养成这种习惯(即使是好的习惯)。在它不再感觉“错误”之前肯定需要一些时间来适应。
    【解决方案3】:

    可能为时已晚,但我认为您的问题是 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 上写了一个简短的教程

    机甲

    【讨论】:

    • 实际上,只需重新阅读您的问题 - 可能值得做 Serial.println(usbnumber) 看看您得到了什么,但它可能就像检查 usbnumber == 1 或 usbnumber == 一样简单2
    【解决方案4】:

    我在寻找其他东西时找到了你的答案,无论如何我刚刚遇到(我猜)你遇到的同样问题。

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

    要验证这一点,请尝试在 setup() 函数中闪烁 LED,如果每次加载页面时它都会闪烁,这证实了我的论点。

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

    我通过在 +5V 和 RESET 之间粘贴 120 欧姆电阻来解决。每次您想将新固件上传到您的开发板时,请记住将其删除。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-06-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多