Arduino code does not read input from C# -


my c# code sends signal arduino servo motor start running, when run code there no response arduino , servo motor not start. if use serial monitor in arduino ide, arduino , servo motor works perfectly. wrong?

here code:

 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 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 in problem 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");                 }             }         } 

port 2 laser range finder works perfectly.

for arduino code in port1:

  #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 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 valid mpu6050 sensor, check wiring!");       delay(500);     }      // calibrate gyroscope. calibration must @ rest.     // if don't want calibrate, comment line.     mpu.calibrategyro();      // set threshold sensivty. default 3.     // if don't want use threshold, comment 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 , yaw     yaw = yaw + norm.zaxis * timestep;      //serial.println(yaw);     // wait full timestep period     delay((timestep*1000) - (millis() - timer));     return ((int)yaw);   } 

thank you!

i think easier answer question if break down code simple bidirectional communication problem between arduino , c#.

for stable communication between arduino , c# there several libraries available, simplifies communication, such solidsoils4arduino , cmdmessenger , have additionally comprehensive examples. furthermore, there similar questions (e.g recieve , send data arduino c# , vice versa) on stackoverflow.

i hope helps check , improve code , integrate motor control.


Comments