# How can send bytes for arduino?

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

• Note the lack of ' 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
• Stackoverflow question? – Konsalik Nov 27 '11 at 21:44

Why do you need it to be sent as a string?

Just send a byte value and treat it as so in the Arduino. The serialPort1.Write method will likely have different overloads for various formats including byte.

If you need to represent a value >255 then you can send it over multiple bytes and concatenate into e.g. an int at the Arduino.

NOTE - you seem to have changed the code in your question since the below was written, but I'll leave it there as the concept is still valid.

EDIT

At the moment you are sending in ASCII format from the PC, so 0 - 0x30, 1 = 0x31 etc. To send in plain binary check the overloads for the method.

Try putting something like:

private void button1_Click(object sender, System.EventArgs e)
{
// Say we want to send 23456 (0x5BA0)
//serialPort1.Write(200);  // 200 is "send value" command
//serialPort1.Write(91);  // 100 (0x5B) is your upper byte value
//serialPort1.Write(160);  // 160 (0xA0) is your lower byte value

// this uses the serialPort1.write overload correctly
// 1st byte is command, 2nd and 3rd are upper and lower bytes of int
byte[] data = new byte[] {200, 91, 160};
serialPort1.Write(data, 0, 3);  // 0 is the offset and 3 is the size of array

}


and see if it is recieved correctly at the Arduino (do not read it as a string, e.g. if incoming byte == 200)
Something like the below should work - you will have to adjust accordingly to make it do whatever it is you are trying to do, this is just an example of transferring bytes:

    if (incomingByte == 200) {

upper_byte = (unsigned char)Serial.read();
lower_byte = (unsigned char)Serial.read();
// full_value should be 23456
int full_value = (upper_byte << 8) + lower_byte; // reconstruct int
}


Notes:
You can write code to split an int into the two bytes (I just put an example "test value")
Again, there may be overloads for this. Also you can use any multiple byte receive capability in the Serial.read routine.

• Ok... Yes I already thougth in send bytesdo but how can do? – FredVaz Nov 27 '11 at 16:23
• @FredVaz - see edits, I tried to give a rough example. – Oli Glaser Nov 27 '11 at 16:48
• thanks... but see the code i put now, i don't understant how i can define a value with two digits or more. – FredVaz Nov 27 '11 at 17:01
• I can't put this way serialPort1.Write(200) because serialPort1.Write(String), and if i put 200 not work. – FredVaz Nov 27 '11 at 17:04
• I updated the code to send an int - it should work okay, let me know if not. Again this is a quick example to get the idea across, you will need to adapt it as necessary. There are many ways to do this, understanding the different formats is the main thing. All data is sent in binary, it's how the software sees it that makes the difference. For example 1 in ASCII is 0x31 as opposed to 0x01 in "plain binary" format. – Oli Glaser Nov 27 '11 at 17:22

Oli's answer might be the better solution for your overall problem, but to answer the question, how to generate the string representation of an int in C, have a look at the function sprintf().

It's hard to understand exactly what you're asking due to the bad english and complex nature of the problem. I'm also not 100% sure that electronics.stackexchange is where this question belongs. Even so, here goes...

To convert a single ASCII character into an integer value, do this:

char c = whatever; int i; i = c - '0'; // C is an ASCII value between '0' and '9'.
//I will be an integer value between 0 and 9.

To do the opposite:

c = '0' + i;

If these simple things don't work for you, then look at the standard functions of: atoi(), itoa(), sprintf(), and others.

• Sorry my bad english... but a reformeled a question. How can i send bytes? – FredVaz Nov 27 '11 at 16:38