簡體   English   中英

Arduino串行讀取

[英]Arduino serial reading

我正在研究網絡控制的流動站,並且正在使用串行端口與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 ,因此每次都進行所有寫操作。 以高警告級別進行編譯(如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 )。

這是一些有關使用PHP控制串行端口的文章的鏈接-我用谷歌搜索,不知道它的質量-但看起來只向“ com1”中寫入一個整數就不夠了-這超出了我的領域,祝您好運:)

如Nikolai所述,您似乎在“ if”語句中執行賦值(=)而不是比較(==)。

一些C程序員習慣的一個好習慣是將rvalues放在比較的左側,這樣,如果您不小心使用賦值運算符而不是比較運算符,則編譯器將生成錯誤:

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閃爍,如果每次加載頁面時LED閃爍,則表明我的論斷。

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

我通過在+ 5V和RESET之間插入120歐姆電阻來解決。 只要記住每次要將新固件上傳到板上時都將其刪除即可。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM