Sending "060a" c# to arduino servo "a"

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???? :0

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...

servoX.write(070); //Posicao inicial x, valores de alinhamento
servoY.write(020);

Why specify angles in octal?

if(c == 97)

I don't remember all the ASCII table
if(c == 'a') is much easier to read.

You're using "atoi" on "buffer", but I don't see where you're terminating your string.

 buffer[received++] = c;
 buffer[received] = '\0';

(need to see if buffer is long enough to accomodate your string)

        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();
        }

Each time the button is clicked, the Arduino is reset, sent some data, and reset again. Is that REALLY what you want?

Opening and closing the serial port resets the Arduino, unless you have modified the Arduino to not reset under those conditions.

Each time the button is clicked, the Arduino is reset, sent some data, and reset again. Is that REALLY what you want?
R- doesn't matter for now, it's only for testing.

Look the arduino code works fine, if you open the Serial Monitor in Arduino and insert the values.
ex="030a" the servo 'a' will turn 30.

The problem is sending string value like this one "030a" in c#. If you knows who to do it Help will be well appreciated.

After this problem is fix i will fine tune the arduino code.... :relaxed:

Again: somebody knows who to do it????

The point that PaulS is making, is that while the Arduino is resetting, it is missing the serial data you are attempting to send it.

I understand and thank you PaulS, but if i comment out like

 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();

The problem still remains.......

I understand and thank you PaulS, but if i comment out like

The Open() still resets the Arduino. You need separate buttons to Open() and Close() the serial port.

The Arduino can echo what is sent. C# can have callback methods that get called when serial data is received. You can, in that callback, write the data to the Console, to see what the Arduino received.

There does not, though, appear to be anything wrong with the sending code.

Ok, guys finally fixed the code, thank you for all of the help. For all other ones looking for code to control 2 servos with C# .
NOTE: This is only for one but from here is a piece of cake!!!!!

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;
            sp.Open();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            
            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();
        }
   }
}

The arduino code can be copyed from the first post and fine tuned with AWOL replay
Thanks PaulS and dxw00d for the info in open() and close() serial port.

Screen print is the arduino serial Monitor sending the string. For c# is only a form and a button.