1

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()
{
    for (int i = 0;i<4;i++)
    {
        Serial.print(Serial.read());
    } 
    Serial.println(); 
    delay(500);
}
5
  • It seems likely that the reading and writing threads aren't firing at the same frequency and are getting out of synch. Commented Dec 14, 2013 at 20:40
  • i don't think so, the reading and writing threads are completely independet. also, the values on the arduino are wrong too Commented Dec 14, 2013 at 20:58
  • I guess you're just overloading your external Hardware. You're sending 1000 as fast as the serial port allows, not even using any handshaking. That's probably just to much for the arduino board. Commented Dec 14, 2013 at 21:10
  • why is it 1000 times faster than allowed? i thought 115200 is the limit? EDIT: just tried it with 9600, no difference. also, the wrong message has always the same rythm: first fault after 16 messages and always 2 0's to much Commented Dec 14, 2013 at 21:19
  • @user2422196 If I were in your position I would say C# Send message. Is the message sent? Okay now wait for it to come back.... When it comes back then you are allowed to send another message. Commented Dec 16, 2013 at 13:20

1 Answer 1

2

I think the problem is caused by your 500 ms delay in the Arduino loop.

The PC is sending characters as fast as it can.

However the the Arduino receives 4 characters and then delays 1/2 a second (during the delay the PC could send 62,000 characters).

For a while the Arduino serial receive routine puts the characters into a buffer. But after it receives a few correctly the buffer is full and starts overwriting old characters with the new characters received.

To verify this is the problem, remove the line delay(500); from your Arduino code and add a delay to the PC after the line: p.Write(data, 0, data.Count());

Also, how does Console.ReadLine(); ever get executed since if is after your while(true) infinite loop?

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.