我有一個Arduino與伺服器(Pin 9,5.5v和Ground)連接起來,它將在Arduino上運行任何ol測試;但是,當我發送一個串行命令來移動它時,沒有任何反應。 rx燈閃爍,所以我知道Arduino正在獲取信息。我認爲這個問題是在我的字節轉換。C#串行命令移動Arduino伺服X度?
碼時間:
的Arduino代碼:
#include <Servo.h>
Servo myservo; // create servo object to control a servo
// a maximum of eight servo objects can be created
void setup()
{
myservo.attach(9);
// attaches the servo on pin 9 to the servo object and sets the rotation to 0
myservo.write(0);
}
int pos1= 0;
int pos2= 0;
int pos3= 0;
int totalMove = 0;
void loop()
{
if (Serial.available() > 0 && totalMove > 0)
{
pos1 = Serial.read() - '0';
// pos2 = Serial.read() - '0';
// pos3 = Serial.read() - '0';
// totalMove = ((pos3) + (pos2*10) + pos1*100);
myservo.write(pos1);
}
}
你看其他POS持有人,因爲evenutally我想能夠發送大於9的值,但是現在我只需要獲得它迴應:)
C#代碼:
public void moveServo()
{
if (!serialPort1.IsOpen)
{
Console.WriteLine("Oops");
serialPort1.Open();
return;
}
serialPort1.DtrEnable = true;
serialPort1.DataReceived +=
new System.IO.Ports.SerialDataReceivedEventHandler(
serialPort1_DataReceived);
serialPort1.Write(new byte[] {57}, 0, 1);
}
任何想法?
http://arduino.cc/playground/Learning/SingleServoExample –
你如何實例化和初始化C#代碼中的'serialPort1'對象? –
serialPort1 = new System.IO.Ports.SerialPort(components); serialPort1.PortName =「COM4」; serialPort1.BaudRate = 9600; serialPort1。打開(); 像那樣 –