C# Arduino serial communication Serial.Read() dont enought

I have been reading about image processing data using C#. I need to send to Arduino object coordinates. I sent data about x coordinate with the code I wrote below, but I still could not send y coordinates, because I don't know how Arduino will separate x and y coordinates. Is there a method to send data from 2 different channels?

if (serialok == true) {
  int second =0;
  int offset=300;
  second = offset - Math.Abs(objectX);
  map =(float) 0.85 * second;
  buffer[0] = (byte)Math.Abs((int)map);
  serialPort1.Write(buffer, 0, 1);

This is how I read the above code from Arduino.

if(Serial.available()>0) {
  inbyte=Serial.read();
}
servo1.write(map(inbyte,0,255,0,180));
delay(15);

I made some improvements in the code on C#

int offset2 = 300;
 second2 = offset2 - Math.Abs(objectY);
 map2 = (float)0.85 * second2;   
buffer2[0] = (byte)Math.Abs((int)map2);
//serialPort1.Write(buffer2, 0, 1);
                             
                             
                             
string veri = buffer[0] + "#" + buffer2[0];
serialPort1.Write(veri);
 Console.WriteLine(veri);

I'm trying to send 2 strings by combining 2 data with sing of "#"

how can I seperate in arduino for incoming string value , I have to seperate before and after the sign "#" and write servo.write

I realy need to help because I can not think anymore.everything is mixed.

Have a look at Robin2's serial handling tutorial thread.

Thank you very much for your reply , I looked second and 4. examples but I could not set it for myself .While arduino is connected to c #, I can not open the serial monitor on arduino .In this case i can not control the output . my head is very mixed, I need a clue

Why do you need the Serial monitor.

What you need to do is write code to handle this stuff. Maybe you send <x=3, y=5> or maybe you just send <3,5> and your program knows that the x always comes first. Either way, since you are writing both ends of the transmission, you can write it to send whatever you want in whatever format you want.

Have a look at the examples in Serial Input Basics - simple reliable ways to receive data. There is also a parse example to illustrate how to extract numbers from the received text.

...R

Here my c# code I am sending like this “123#433” and I am trying to read this data , You can see my arduino code below it’s not working clearly servo motor turning freak.

                   int second = 0;
                            int offset = 300;
                            second = offset - Math.Abs(objectX);
                            map = (float)0.85 * second;
                            buffer[0] = (byte)Math.Abs((int)map);
                           
                            int second2 = 0;
                            int offset2 = 300;
                            second2 = offset2 - Math.Abs(objectY);
                            map2 = (float)0.85 * second2;
                            buffer2[0] = (byte)Math.Abs((int)map2);
     
                           string veri = buffer[0] + "#" + buffer2[0];
                            serialPort1.Write(veri);
#include <Servo.h>

Servo servo1;


const byte charnumarasi = 32;
char gelenchar[charnumarasi];   
int i ;
boolean newData = false;

int datanumarasi = 0;          

void setup() {
   Serial.begin(9600);
     servo1.attach(2);
   

   for (i = 0; i < 180; i++) {
   servo1.write(i);
   delay(150);
 }
 servo1.write(90);
}

void loop() {
   isaret();
   numarayigoster();
}

void isaret() {
   static byte index = 0;
   char isaret = '#';
   char alinanveri;
   
   if (Serial.available() > 0) {
       alinanveri = Serial.read();

       if (alinanveri != isaret) {
           gelenchar[index] = alinanveri;
           index++;
           if (index >= charnumarasi) {
               index = charnumarasi - 1;

                
           }
       }
       else {
           gelenchar[index] = '\0'; // stringi kapat
           index = 0;
           newData = true;
       }
   }
}

void numarayigoster() {
   if (newData == true) {
       datanumarasi = 0;             
       datanumarasi = atoi(gelenchar); // yeni olan
       // new for this version

     servo1.write(map( gelenchar[datanumarasi], 0, 255, 0, 180));
       
       Serial.println(gelenchar);
      
       Serial.println(datanumarasi);    // yeni olan
       newData = false;
   }
}

Hold on. You read 123 into datanumerasi, then you use it to access the 123rd character in gelenchar? Is your buffer even that long?

Your Arduino code in Reply #5 looks like it is a conversion of my Serial Input Basics into another language (which is fine) but if you are sending "123#433" you should not be using the # character as the end marker. That would work if you were sending "123#433#" as it would see it as two complete messages.

When you send "123#433" you should look for the real end-marker - the character that is sent after the 433, which is probably a carriage-return or line-feed. And when you have received the "123#433" you should use the parse example to split it into two numbers using the # character as the delimiter. My example code assumes a comma is used as a delimiter but you could change that. Or you could get your C# program to send a comma in place of the #

...R

Thank you Robin You really explained it very well , I just changed conversion float to integer. if anyone finding servo control with parsing data your code is working very good.As you said, I made the decisive statement in c # .Thank you again

// Example 5 - Receive with start- and end-markers combined with parsing
#include <Servo.h>

Servo servo1;
Servo servo2;
const byte numChars = 32;
char receivedChars[numChars];
char tempChars[numChars];        // temporary array for use when parsing

     // variables to hold the parsed data
char messageFromPC[numChars] = {0};
int integerFromPC = 0;
float floatFromPC = 0.0;

boolean newData = false;

//============

void setup() {
   Serial.begin(9600);

   servo1.attach(2);
   servo2.attach(4);
  
}

//============

void loop() {
   recvWithStartEndMarkers();
   if (newData == true) {
       strcpy(tempChars, receivedChars);
           // this temporary copy is necessary to protect the original data
           //   because strtok() used in parseData() replaces the commas with \0
       parseData();
       showParsedData();
       newData = false;
   }
}

//============

void recvWithStartEndMarkers() {
   static boolean recvInProgress = false;
   static byte ndx = 0;
   char startMarker = '<';
   char endMarker = '>';
   char rc;

   while (Serial.available() > 0 && newData == false) {
       rc = Serial.read();

       if (recvInProgress == true) {
           if (rc != endMarker) {
               receivedChars[ndx] = rc;
               ndx++;
               if (ndx >= numChars) {
                   ndx = numChars - 1;
               }
           }
           else {
               receivedChars[ndx] = '\0'; // terminate the string
               recvInProgress = false;
               ndx = 0;
               newData = true;
           }
       }

       else if (rc == startMarker) {
           recvInProgress = true;
       }
   }
}

//============

void parseData() {      // split the data into its parts

   char * strtokIndx; // this is used by strtok() as an index

   strtokIndx = strtok(tempChars,",");      // get the first part - the string
   strcpy(messageFromPC, strtokIndx); // copy it to messageFromPC

   strtokIndx = strtok(NULL, ","); // this continues where the previous call left off
   integerFromPC = atoi(strtokIndx);     // convert this part to an integer
servo1.write(map(integerFromPC, 0, 255, 0, 180));


   strtokIndx = strtok(NULL, ",");
   floatFromPC = atoi(strtokIndx);     // convert this part to a float
servo2.write(map(floatFromPC, 0, 255, 0, 180));
}

//============

void showParsedData() {

}