How to send bytes for arduino

arduinocserial

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.

Best Answer

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.