SYNCHRONIZATION PROBLEM

Dear All,

I have about 10.000 integer values (range : -200 / +200) on c#. I would like to send them to Arduino Uno.

I wrote a simple c# code (this code also display Arduino Serial Monitor, so i can check the results)

 // piece of code that sends data to Arduino

                int a = 0;    
                foreach (var results in s)
                {
                    string ID = results.Sample.ToString() + '\0';

                    // if (a == 20) { MessageBox.Show(ID);     a = 0; }
                    char[] buff = ID.ToCharArray();
                    serialPort1.Write(buff, 0, ID.Length);

                 }

And i wrote a simpe Arduino code

// piece of code that sends data to Arduino

                 void loop()

                 {
                    while (Serial.available() > 0)  // do while serial data is available

                    {

                        char buffer[10]; 
                        Serial.readBytesUntil('\0', buffer, 5);                 

                        int i = atoi( buffer );

                        Serial.println( i );
                    }

                 }

When i send the datas pieces by pieces (20's in parts), the codes send and read datas correctly.

However, it does not see my job. When i send all the data at a time, the Arduino code does not read the datas correctly (synchronization problem).

I should send 2 column data(each of which contains 10.000 integer values).

How can i solve the synchronization problem?

The Arduino has a "small" (measured in bytes and not kilo or mega bytes) serial buffer. The buffer is probably filling up and dropping the remaining bytes.

                        Serial.readBytesUntil('\0', buffer, 5);

Your sending code is not sending a NULL. How is the Arduino supposed to know when to quit reading?

PaulS:
Your sending code is not sending a NULL. How is the Arduino supposed to know when to quit reading?

Doesn't this code at the sender null-terminate each value?

string ID = results.Sample.ToString() + '\0';

Values ??are divided according to '\0'.

string ID = results.Sample.ToString() + '\0';

The below code checks that serial data is available.

while (Serial.available() > 0)  // do while serial data is available

PeterH:
Doesn't this code at the sender null-terminate each value?

string ID = results.Sample.ToString() + '\0';

.NET strings can contain null characters. That C# statement appends a null character to the end of the string returned by results.Sample.ToString().

Doesn't this code at the sender null-terminate each value?

Yes, it does. But, when the string is sent, only the characters up to the NULL are sent.

I'd recommend using \n instead of \0.

The below code checks that serial data is available.

Yes, it does. It checks that at least one character is available. But that doesn't mean that a complete packet is available.

PaulS:
Yes, it does. But, when the string is sent, only the characters up to the NULL are sent.

Good point. :*

Yes, it does. But, when the string is sent, only the characters up to the NULL are sent.

I'd recommend using \n instead of \0.

I could not understand exactly what is the difference between \n & \0?

By the way, i add a delay command in the c# program and now i could read one column's data correctly.
However, it is not enough fast and also i will read two column data.
When i add the second column's data, this solution will not be sufficient.

So I need to find a different solution...

I could not understand exactly what is the difference between \n & \0?

\n is a carriage return. It can be embedded in the serial stream and will be send.

\0 is a string terminator. It is NOT actually sent to the serial port. It defines when to stop sending data.

I have about 10.000 integer values (range : -200 / +200) on c#. I would like to send them to Arduino Uno.

For what purpose? The Arduino can't store that many values.

For what purpose? The Arduino can't store that many values.

Firstly, i should transfer the two columns data from c# program to Arduino's ports.
I would like to display & check the transfer process.
Therefore, I also display the Arduino Serial Monitor with c# application.

I would like to display & check the transfer process.
Therefore, I also display the Arduino Serial Monitor with c# application.

A reasonable thing to do. But, you said that the process is too slow, which implies that there is more to the program than you have revealed so far.

The question remains what do you expect that Arduino to do with 10000 values?