raul.suarez.marin
Posts: 7
Joined: Thu Jul 19, 2012 11:33 am

C# and Serial comunicaction

Wed Sep 05, 2012 8:12 am

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

User avatar
panik
Posts: 369
Joined: Fri Sep 23, 2011 12:29 pm
Location: Netherlands

Re: C# and Serial comunicaction

Fri Sep 07, 2012 6:34 pm

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.

6677
Posts: 382
Joined: Wed Mar 14, 2012 9:23 pm

Re: C# and Serial comunicaction

Fri Sep 07, 2012 10:46 pm

It is just a case of the arduino not being able to cope with such a high baud rate as said above.

raul.suarez.marin
Posts: 7
Joined: Thu Jul 19, 2012 11:33 am

Re: C# and Serial comunicaction

Sat Sep 08, 2012 7:13 am

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

Return to “Other languages”

Who is online

Users browsing this forum: No registered users and 0 guests