I have developed an app client that sends a string/comand by socket for another pc app server and the app server sends a string for the arduino over the serial port.
The problem is: how can I send bytes for arduino?
Sorry my bad english
Update: The code, it works! ;)
See the explanation in the code.
The code of my C# app server that sends a String over the serial port:
using System;
using System.Windows.Forms;
//
using System.Threading;
using System.IO;
using System.IO.Ports;
pulic class senddata(){
private void Form1_Load(object sender, System.EventArgs e)
{
//Define a Porta Serial
serialPort1.PortName = textBox2.Text;
serialPort1.BaudRate = 9600;
serialPort1.Open();
}
private void button1_Click(object sender, System.EventArgs e)
{
int cmd = 1;
byte[] b = BitConverter.GetBytes(cmd);
serialPort1.Write(b, 0, 4);
}
}
The C code of the arduino:
#include <Servo.h>
Servo servo;
void setup()
{
servo.attach(9);
Serial.begin(9600);
servo.write(0);
}
void loop()
{
if(Serial.available())
{
int cmd = Serial.read();
if (cmd> 0) {
servo.write(cmd);
}
}
}
For you to understand my problem better I put this code. Because the value of servo goes from 0 to 180, and this way don't work.
'
in my example. If you surround the value in quotes it will be treated as a character. – Oli Glaser Nov 27 '11 at 17:34