我的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);
}
謝謝!
試圖找出它是否將字符寫入串口(在arduino1.writeline後面寫一個寫字符),並檢查問題是與串口還是程序的通信。 – frarugi87
你確定你的Arduino已連接到COM12嗎? COM8未設置爲9600波特,但Arduino板是。 –