Problems with fast Serial connection to PC

I’m trying to communicate with my arduino from my pc. I’m sending 1,0,0,0 in a loop and the arduino sends the received data back. However, instead of getting 1,0,0,0 back, this is what I get:

1000
1000
1000
1000
1000
1000
1000
0011
0000
0010
0100
1001
0000
0011
0000
As you can see, it works at the beginning, but starts to get weird after a few messages. Instead of sending 3 0’s it sent 5. Why is that?

Here is my code:

C# Application:

class Program
{
    SerialPort p;

    static void Main(string[] args)
    {
        Program p = new Program();
        p.initialize();
    }
    private void initialize()
    {
        p = new SerialPort("COM3", 115200);
        p.Open();

        byte[] data = { 1, 0, 0, 0 };

        Thread t = new Thread(reading);
        t.Start();

        while(true)
        { 
            p.Write(data, 0, data.Count());
        }
        Console.ReadLine();
    }
    private void reading()
    {
        while (true)
        {
            Console.WriteLine(p.ReadLine());
        }
    }
}

Arduino:

void setup()
{
    Serial.begin(115200);
    delay(5000);
    Serial.println("Program started....");
}
void loop()
{
if (Serial.available > 0)
{
    for (int i = 0;i<4;i++)
    {
        Serial.print(Serial.read());
    } 
    Serial.println(); 
}
}

The next thing i tried is setting a delay in the loop. This works, but the delay needs to be over 300 ms to get a reliable connection. Sadly, 300ms is far too long for my project…
Any Suggestions?

if (Serial.available > 0)
{
    for (int i = 0;i<4;i++)
    {
        Serial.print(Serial.read());
    } 
    Serial.println(); 
}

if there is one character in the receive buffer Serial.available > 0 and the you try to read 4 from the buffer => error!
also fails when there are 2 or 3 chars in the buffer.

try this

if (Serial.available >=4)
{
    for (int i = 0;i<4;i++)
    {
        Serial.print(Serial.read());
    } 
    Serial.println(); 
}

Is this a realistic test - I have had no trouble communicating at 115,200 baud. I think I even tried 1Mbaud successfully - but I wasn’t trying to send stuff back the instant it was received.

…R

I tried it with >= 4, but there was no difference. (if there is no byte available, it returns -1)
Do you have some working code for me?
The weird thing is, that this works:

void loop()
{
  if (Serial.available() >=4)
  {
      for (int i = 0;i<4;i++)
      {
          Serial.print(Serial.read());
      } 
      Serial.println(); 
  }
  delay(300);
}
            while(true)
            {
                p.Write(data, 0, data.Count());
                System.Threading.Thread.Sleep(300);
            }

But withouth the delays, it doesn’t…

@yerst,

my proposed code did only fix the Arduino side.
I assume you have a similar problem at the sending side.
Serial communication is NOT, i repeat NOT instantaneous , it takes aeons of time from the processor point of view.

The delay makes it work as it allows the bytes come in.

        while(true)
        { 
            p.Write(data, 0, data.Count());
        }

This will overflow the receive buffer of the Arduino. Chars will be dropped I assume.
There is no handshake implemented in your code - not in SW not in HW - to align internal buffering.
a small reader- RS232 flow control and handshaking - Lammert Bies -

    private void reading()
    {
        while (true)
        {
            Console.WriteLine(p.ReadLine());
        }
    }

THis part looks ok as readline will wait for \n

Try this

    private void test()
    {
        while (true)
        {
            p.Write(data, 0, data.Count());
            Console.WriteLine(p.ReadLine());
        }
    }

Thanks!
I fixed the problem now, by setting a delay on my pc of 1ms. (no delay on the arduino side)

while(true)
            {
                p.Write(data, 0, data.Count());
                System.Threading.Thread.Sleep(1);
            }

This works nice, and might be enough for my project.
But still, is there a more efficient way to do this? (Sorry, i haven't had time to read the link you sent me, maybe it's in there, then just write "It's in there".)

Sorry, i haven’t had time to read the link you sent me, maybe it’s in there, then just write “It’s in there”.)

OK. It’s in there.