2015-05-08 70 views
1

我的C#代碼發送一個信號給伺服電機的arduino開始運行,但是當我運行我的代碼時,沒有來自arduino的響應,並且伺服電機沒有啓動。如果我在Arduino IDE中使用串行監視器,arduino和伺服電機完美工作。哪裏不對?Arduino的代碼不會讀取來自C#的輸入

這裏是我的代碼:

string dir = " "; 
     string distance; 
     string result = " "; 

     string port1 = "COM12"; 
     int baudRatePort1 = 9600; 
     SerialPort arduino1 = new SerialPort(port1, baudRatePort1); 

     string port2 = "COM8"; 
     int baudRatePort2 = 115200; 
     SerialPort arduino2 = new SerialPort(port2, baudRatePort2); 

     Console.WriteLine("Enter Direction:\n"); 
     dir = Console.ReadLine(); 


     if (dir == "S") 
     { 
      Console.WriteLine("Enter Distance:\n"); 
      distance = Console.ReadLine(); 
      arduino2.Open(); 
      if (arduino2.IsOpen) 
      { 
       try 
       { 
        arduino2.WriteLine(distance.ToString()); 
        result = arduino2.ReadLine(); 
        string[] words = result.Split('\r'); 

        foreach (string word in words) 
        { 
         result = word; 
         break; 
        } 
        arduino1.Open(); 
        if (arduino1.IsOpen) 
        { 
         try 
         { 
          arduino1.WriteLine("S"); 
         } 
         catch (Exception e) 
         { 
          Console.WriteLine("Arduino connecting Servo is in problem"); 
         } 
        } 
        while (result != distance) 
        { 
         if (result != "ON") 
         { 
          result = arduino2.ReadLine(); 
          Console.WriteLine(result); 

          words = result.Split('\r'); 

          foreach (string word in words) 
          { 
           result = word; 
           break; 
          } 
          result = arduino1.ReadLine(); 
          Console.WriteLine(result); 
         } 
         else 
         { 
          Console.WriteLine("Please Excuse Me!"); 
         } 

        } 
        arduino1.WriteLine("P"); 
        arduino1.Close(); 
        arduino2.Close(); 
        Console.WriteLine("Finish"); 
       } 
       catch (Exception e) 
       { 
        Console.WriteLine("Arduino connecting LRF is in problem does not open"); 
       } 
      } 
     } 
     else if(dir == "R") 
     { 
      arduino1.Open(); 

      if(arduino1.IsOpen) 
      { 
       try 
       { 
        arduino1.WriteLine("R"); 
        result = arduino1.ReadLine(); 
        string[] words = result.Split('\r'); 

        foreach (string word in words) 
        { 
         result = word; 
         break; 
        } 
        while (result != "F") 
        { 
         result = arduino1.ReadLine(); 
         words = result.Split('\r'); 

         foreach (string word in words) 
         { 
          result = word; 
          break; 
         } 
        } 
        arduino1.WriteLine("P"); 
        arduino1.Close(); 
        Console.WriteLine("Finish"); 
       } 
       catch(Exception e) 
       { 
        Console.WriteLine("Problem in arduino connecting servo"); 
       } 
      } 
     } 
     else if (dir == "L") 
     { 
      arduino1.Open(); 

      if (arduino1.IsOpen) 
      { 
       try 
       { 
        arduino1.WriteLine("L"); 
        result = arduino1.ReadLine(); 
        string[] words = result.Split('\r'); 

        foreach (string word in words) 
        { 
         result = word; 
         break; 
        } 
        while (result != "F") 
        { 
         result = arduino1.ReadLine(); 
         words = result.Split('\r'); 

         foreach (string word in words) 
         { 
          result = word; 
          break; 
         } 
        } 
        arduino1.WriteLine("P"); 
        arduino1.Close(); 
        Console.WriteLine("Finish"); 
       } 
       catch (Exception e) 
       { 
        Console.WriteLine("Problem in arduino connecting servo"); 
       } 
      } 
     } 

端口2是激光測距儀,其完美的作品。

對於Arduino的代碼,這是在端口1:

#include <Servo.h> 
    #include <Wire.h> 
    #include <MPU6050.h> 

    MPU6050 mpu; 

    Servo servoLeft; 
    Servo servoRight; 

    unsigned long timer = 0; 
    float timeStep = 0.01; 

    //Pitch, Roll, Yaw values 
    float yaw = 0; 

    char dir; //direction to be travelled 
    int hold = 0; 
    int gyroVal = 0; 

    void setup() 
    { 
    Serial.begin(9600); 
    servoRight.attach(9); //BLUE SERVO 
    servoLeft.attach(8); //YELLOW SERVO 

    while(!mpu.begin(MPU6050_SCALE_2000DPS, MPU6050_RANGE_2G)) 
    { 
     Serial.println("Could not find a valid MPU6050 sensor, check wiring!"); 
     delay(500); 
    } 

    // Calibrate gyroscope. The calibration must be at rest. 
    // If you don't want calibrate, comment this line. 
    mpu.calibrateGyro(); 

    // Set threshold sensivty. Default 3. 
    // If you don't want use threshold, comment this line or set 0. 
    mpu.setThreshold(3); 
    } 

    void loop() 
    { 
    timer = millis(); 
    int gyroYaw = readGyro(); 
    //Serial.println(dir); 
    while(Serial.available()>0) 
    { 
     dir = Serial.read(); 
    } 

    Stop(); 
    Serial.println(dir); 
    if(dir == 'S') 
    { 

    // Serial.print(gyroYaw); 
     //Serial.print (hold); 
     // Serial.println(); 
     //delay(1000); 
     if(gyroYaw >= 1) 
     { 
     adjustToRight(); 
     // Serial.println("Adjust Right"); 
     // Serial.print (hold); 
     //Serial.println(); 
     //delay(1000); 
     //Forward(); 
     /*if(gyroYaw=1) 
     { 
      hold = 1; 
     } 
     else if(gyroYaw<=-0) 
     { 
      hold = 2; 
     } 
     else 
     { 
      hold = 0; 
     }*/ 
     } 
     else if(gyroYaw<=-1) 
     { 
     //Serial.println("Adjust Left"); 
     adjustToLeft(); 
     } 
     else 
     { 
     //Serial.println("forward"); 
     Forward(); 
     } 

    /* if(hold = 1) 
     { 
     Serial.print(gyroYaw); 
     Serial.print (hold); 
     Serial.println(); 
     delay(1000); 
     //adjustToLeft(); 
     if(gyroYaw == 0) 
     { 
      hold = 0; 
     } 
     } 

     if(hold = 2) 
     { 
     Serial.print(gyroYaw); 
     Serial.print (hold); 
     Serial.println(); 
     delay(1000); 
     // adjustToRight(); 
     if(gyroYaw == 0) 
     { 
      hold = 0; 
     } 
     }*/ 
    } 
    else if(dir == 'R') 
    { 
     TurnRight(); 
    } 
    else if(dir == 'L') 
    { 
     TurnLeft(); 
    } 
    else 
    { 
     Stop(); 
    } 
    } 
    void Stop() 
    { 
    servoRight.write(0); 
    servoLeft.write(0); 
    } 
    void Forward() 
    { 
    servoRight.write(50); 
    servoLeft.write(96.9); 
    } 
    void TurnRight() 
    { 
     servoLeft.write(170); 
     servoRight.write(180); 
     delay(2200); 
     Stop(); 
     Serial.println("F"); 
    } 
    void TurnLeft() 
    { 
    servoLeft.write(80); 
    servoRight.write(30); 
    delay(2200); 
    Stop(); 
    Serial.println("F"); 
    } 
    void adjustToRight() 
    { 
    servoRight.write(100); 
    servoLeft.write(100); 
    //delay(1500); 
    } 
    void adjustToLeft() 
    { 
    servoRight.write(50); 
    servoLeft.write(70); 
    } 

    int readGyro() 
    { 
    // Read normalized values 
    Vector norm = mpu.readNormalizeGyro(); 

    // Calculate Pitch, Roll and Yaw 
    yaw = yaw + norm.ZAxis * timeStep; 

    //Serial.println(yaw); 
    // Wait to full timeStep period 
    delay((timeStep*1000) - (millis() - timer)); 
    return ((int)yaw); 
    } 

謝謝!

+0

試圖找出它是否將字符寫入串口(在arduino1.writeline後面寫一個寫字符),並檢查問題是與串口還是程序的通信。 – frarugi87

+0

你確定你的Arduino已連接到COM12嗎? COM8未設置爲9600波特,但Arduino板是。 –

回答

2

我認爲如果我們將代碼分解爲Arduino和C#之間的簡單雙向通信問題,將會更容易回答您的問題。

爲了Arduino和C#之間的穩定通信,有幾個庫可用,它們簡化了通信,例如SolidSoils4ArduinoCmdMessenger,並且還有其他全面的示例。此外,在stackoverflow上已經有一些類似的問題(例如Recieve and send data from arduino to c# and vice versa)。

我希望這可以幫助您檢查和改進您的代碼並整合您的電機控制。

0

您正在發送以\ r \ n - >結尾的utf16字符串,因此您要發送4個字節的數據。您在arduino網站上的 您在while循環中讀取數據,並只取最後一個字節。所以你總是閱讀\ n