Page 1 of 1

C# and Serial comunicaction

Posted: Wed Sep 05, 2012 8:12 am
by raul.suarez.marin
Hi there.

I'm working on a project where I have to use my arduino as ADQ. Part of my code is the following.

Code: Select all

SerialPort comport = new SerialPort();
comport.DtrEnable = true;
comport.RtsEnable = true;
            try
            {
                comport.PortName = "/dev/ttyUSB0";
                comport.BaudRate = Convert.ToInt32(115200);
                comport.Open();
            }
            catch (Exception c)
            {
                Console.WriteLine(c);
                return;
            }
while (true)
            {
                int i = 0;
                sw_error = new StreamWriter("fallos.txt", true);
                sw1 = new StreamWriter("salida_01.txt", false);
                sw2 = new StreamWriter("salida_02.txt", false);
                while (i < (500+fallos))
                {
                    string data = comport.ReadLine();
                    Console.WriteLine(data);
                    if (comprobar_errores(data))
                        errores(sw_error, sw1, data);
                    else
                        sw1.WriteLine(data);
                    i++;
                }
                sw1.Close();
                sw_error.Close();
            }
Everything works fine when I set

Code: Select all

comport.BaudRate = Convert.ToInt32(9600);
or

Code: Select all

comport.BaudRate = Convert.ToInt32(38400);
BUT if I set

Code: Select all

comport.BaudRate = Convert.ToInt32(57600);
or

Code: Select all

comport.BaudRate = Convert.ToInt32(115200);
I get some errors in the transmision.

I've been looking for it in some forums but still can't find anything. The same code works great on Windows, so I guess is not Arduino's problem nor program's problem.

Thanks for your time and sorry for my English.

Best regards,
Raúl Suárez

Re: C# and Serial comunicaction

Posted: Fri Sep 07, 2012 6:34 pm
by panik
I'm actually surprised it works on Windows with these baudrates, but I've also been having varying degrees of success on different OS'es, and in different situations. For example, I'm able to communicate with the raspberry pi on 115K2, with an 8MHz crystal with a USB-serial adapter I did with the LUFA library and an AT90USB162. However, I can't log in with it. Too much garbage.

If you look at the ATmega328p datasheet (http://www.atmel.com/Images/doc8161.pdf) starting on page 200, you'll see the 'error rate' on baudrate vs frequency. The Arduino uses a 16Mhz crystal. That means for 57k6 and 115k2 an error-rate of respectively 2.1% and -3.5%. The other baudrates you mention have 0.2%. I'm being told anything over 2% (or under -2%) is too much [citation needed].

Whenever I'm using the UART on an MCU heavily, I'll make sure to add the correct crystal. I stocked up on 11.0592MHz crystals, and those never failed in any situation.

This might have something to do with it, but I hope someone more knowledgeable can give a definitive answer.

Re: C# and Serial comunicaction

Posted: Fri Sep 07, 2012 10:46 pm
by 6677
It is just a case of the arduino not being able to cope with such a high baud rate as said above.

Re: C# and Serial comunicaction

Posted: Sat Sep 08, 2012 7:13 am
by raul.suarez.marin
Yeah, I've already solved it.

It was problem of Arduino. My testing program was something like:

Code: Select all

void loop {
Serial.print(analogRead(0));
Serial.print(",");
Serial.println(analogRead(1));
}
Messages were going too fast to the Raspberry. For improving this comunication I just had to add

Code: Select all

delay(1); //or delay(2)
for increasing stability.
Now, there are only 1 or 2 wrong transmissions at the begining of comunication, but we consider it transitory so it's no problem.

So I can say that my problem is solved.

Thank you very much for your answers.

Best regards,
Raúl Suárez