it would be nice to get some help....
trying to control two servo motors by c# 2008
It connects to arduino uno board, see rx, tx lights flashing......
problem:
how to send a string = "030a" to arduino????

arduino code:
#include <Servo.h>
Servo servoX; // cria objecto servo
Servo servoY; // cria objecto servo
int posX = 0; // variavel para guardar posicao do servo
int posY = 0; // variavel para guardar posicao do servo
char buffer[4];
int received;
void setup()
{
servoX.attach(10); // indica que o servo esta conectado no pin9 do arduino
servoY.attach(11); // indica que o servo esta conectado no pin9 do arduino
Serial.begin(9600); //Inicia a porta serial
servoX.write(070); //Posicao inicial x, valores de alinhamento
servoY.write(020); //Posicao inicial y
received = 0;
}
void loop()
{
if(Serial.available()) //Verifica se possui entrada de dados na entrada Serial
{
// Serial.println(buffer);
char c = Serial.read(); // Se o "c" for igual ao caracter "A" (97 na tabela ASCII)
if(c == 97)
{
Serial.println(buffer);
int numero = atoi(buffer);
servoX.write(numero);
received = 0;
}
if(c == 98) // Se o "c" for igual ao caracter "B" (98 na tabela ASCII)
{
Serial.println(buffer);
int numero = atoi(buffer);
if(numero >= 15) // Se servo Y for maior que 15 graus(15 e o minimo de angulo sem forcar motor)
{
servoY.write(numero);
received = 0;
}
received = 0;
}
if(c != 97 && c != 98)
{
buffer[received++] = c;
}
Serial.flush();
}
}
in serial monitor "040a" works, servo "a" moves 40
C# code
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.IO.Ports;
namespace botaoServoteste
{
public partial class Form1 : Form
{
int posicao = 60;
//byte[] data = new byte[1];
public Form1()
{
InitializeComponent();
//Decalracoes iniciais para comunicacao com o arduino
sp.PortName = "COM3"; // porta com
sp.BaudRate = 9600; // Taxa de velocidade
sp.DtrEnable = true;
}
private void button1_Click(object sender, EventArgs e)
{
sp.Open();
posicao += 10;
string temp = "0" + posicao.ToString()+"a";
string teste = temp;
sp.Write(temp);
// sp.Write(new byte[] { Convert.ToByte(teste) }, 0, 1);
sp.Close();
}
somebody knows who to do it????
sorry for the english...