2009-05-22 136 views
4

我正在使用網絡控制流動站,並使用串行端口與Arduino進行通信。我寫了一些只使用fwrite()的PHP,並將一個ASCII 1或一個ASCII 2寫入串口。 Arduino正在聽該端口,並根據聽到的內容做一些事情。我知道我的PHP正在工作,因爲每當我告訴它發送東西時,Arduino都會收到它。這裏是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> 

回答

1

如果是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代碼(我不是一個專家),它看起來,你是作爲一個字符,這是不ASCII 49寫入二進制1,但ASCII 1(SOH),同對於2.嘗試將其更改爲在PHP代碼'1'

這裏的一些文章的鏈接上Controlling the Serial Port with PHP(C代碼或1) - 我用Google搜索,沒有質量的想法 - 但看起來並不像它足以只是寫入一個整數到「com1」 - 這是我的域名,所以祝你好運:)

+0

我改變了這一點,現在led燈根本不亮。這意味着它不是正確讀取ascii 1或2嗎? – blueintegral 2009-05-22 18:17:01

+0

您可以記錄/打印您收到的內容嗎? 也發佈你的PHP代碼 - 這將有助於縮小問題的範圍。 – 2009-05-22 18:20:16

+0

如果我嘗試使用內置於arduino環境中的串行監視器,PHP會抱怨com端口無法寫入, – blueintegral 2009-05-22 18:32:51

1

正如尼古拉提到,它看起來像你正在做的任務(=)鼠她比你的「if」語句中的比較(==)。

一個好習慣,一些C程序員進入是把右值就比較的左側,這樣編譯器會產生一個錯誤,如果你不小心使用了賦值運算符,而不是比較操作:

 
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.

要修復,您可以更改序列號。可條件是

if (Serial.available() == 2) 

,然後做一些類似下面的轉換爲數字:

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

另一種選擇是使用TextFinder庫 - 我已經在我的網站上寫了一個簡短的教程http://mechariusprojects.com/thunderbolt/?p=60

機甲

0

,我發現你的答案尋找其他的東西,反正我只是面對(我猜)你有同樣的問題。

如果你還沒有解決它,我認爲問題不是你的代碼,而是arduino板上的自動重置機制。 也就是說:每次在串口上建立一個新的連接時,arduino板都會復位,這允許在通過串口編程時加載新的固件。

要驗證這一點,請嘗試在setup()函數中閃爍LED,如果每次加載頁面時閃爍,這都證實了我的論點。

看一看這裏:http://www.arduino.cc/playground/Main/DisablingAutoResetOnSerialConnection

我通過堅持+ 5V和RESET之間的120歐姆的電阻來解決。只要記住每次你想上傳一個新的固件到你的主板時就把它刪除。