C# Gui to Arduino Board Serial interface problems

Hi,

I'm trying to connect my C#GUI to my Arduino board and handshake some information back and forth. I've had relatively good success except that for some reason it never works the first time I try to initiate the handshake. I'm unsure why.

On the GUI side I have this to test all ports to find which one the arduino is hooked up to:

 foreach (string port in ports)
                    {

                        if (port == "")
                        {
                            MessageBox.Show("No PORTS OPEN", "NO PORTS", MessageBoxButtons.OK);

                            break;
                        }

                        else
                        {

                            timeoutcheck = 0;
                            try
                            {

                                usb.BaudRate = 38400;
                                usb.PortName = port;
                                //Send message to Port and Wait for response
                                usb.Open();
                                Thread.Sleep(100);
                                usb.Write("A");
                                
                                usb.ReadTimeout = 1000;
                                usb.Read(check, 0, 1);

                                buffer = check[0].ToString();
                                
                            }

                            catch (TimeoutException)
                            {
                                timeoutcheck = 1;
                                usb.Close();
                                Thread.Sleep(100);
                            }


                            if (timeoutcheck == 0 && (check[0] == 'A' ))
                            {
                                MessageBox.Show(buffer, buffer, MessageBoxButtons.OK);
                                break;
                            }
                            else
                            {
                                usb.Close();
                                Thread.Sleep(100);
                                timeoutcheck = 2;
                            }
                        }
                    }

//If handshaking with arduino is initiated it recieves a 'A' and then proceeds to send the rest of the data after this

On The Arduino side I have the following:

 if(Serial.available()){
    
              serial_input[0] = Serial.read();
if(serial_input[0] == 'A'){
             Serial.print("A");
//Then goes on to take in the rest of the data

I can't seem to figure out why it doens't work the first time, but works every time after.

Any thoughts?

Ok so I have noticed that the problem has to be with my GUI somewhere because the Arduino Serial Monitor works perfectly when I use that.

Another thing I have noticed is that the first time I go through (after i adjusted my code to pick it up)
I am actually getting timed out when waiting for the "A" back. But this only happens once and never again.

I can't figure out why

What does not work on the first try? How are you re-trying?

Opening the serial port causes the Arduino to restart. It takes a bit of time after the restart before serial communications is established. Perhaps you simply need to take a longer nap while the Arduino gets ready to reply.

What i mean it doesn't work on first try is, that on the first try I do not recieve a "A" back. Instead i get a read time out exception.

I have the whole start of this handshaking started by a button on my GUI, so whenever I press it my GUI sends a "A" waits for a "A" back from the board. If it does not recieve one in a certain amount of time it times out.

On the arduino side once a "A" is recieved the first thing I do is send one back.

I tried to take a longer nap once opening opening the serial port. It did not fix the problem unfortunately I'm still getting timed out.

The next step, then is to determine whether the issue is that the Arduino doesn't get the value, or doesn't reply to it.

I'd have the Arduino light an LED if there is serial data to read.

If the LED lights up, the Arduino received something.

Perhaps have another LED light up when loop is entered (to tell you that serial communication is ready). If that light has not lit before the timeout occurs, you'd know that you were terminating the wait too soon.

What did you increase the thread sleep time to?

I increased the thread sleep time to 1000.

Unforutnatly I'm using a custom Arduino board so there is no LED on it.

but I do know that it's getting info because the second time I try to send data (after it times out the first time) everything works fine.

I just don't know if theres something i'm missing when I first start that is causing the communication between my GUI and the board to fail.

You know what port finally succeeds. Skip the whole "try every port" mess, and just send a message to the known port. See if that results in a better response.

I'm using C# to talk to an Arduino, using a known port. It succeeds every time.

I have the ReadTimeout set to 5000, though, with the WriteTimeout set to 500.

After you get the known port stuff working, put the try to find the port stuff back.

I switched it right now to the Known Port. Works Perfectly.

BUT unfortunately I need this to be able to search for the port because the application I'm using this on will be on multiple computers, which assign a new port# for serial devices upon installation.

If anyone has a better idea how to scan for which com port the ardunio is on I'm open to suggestions.

I just tested to see what port names are being sent the first time. It's sending COM1 and COM3, and my arduino IS on COM3 so i know for sure it's finding the proper port.

it's either not sending or receiving properly on the first try.

I suppose a way I could avoid this is to try connecting serially twice everytime I try to connect, but thats just bad coding.

It doesn't solve the problem it only avoids it.

I still can't figure this out. Why does running the following code twice work but the first time doesn't either send or receive proper data.

 //Check ports to find the one that board is connected to
                    foreach (string port in ports)
                    {

                        if (port == "")
                        {
                            MessageBox.Show("No PORTS OPEN", "NO PORTS", MessageBoxButtons.OK);
                            break;
                        }

                        else
                        {
                            bob =  0; 
                            timeoutcheck = 0;
                            
                            try
                            {

                                usb.BaudRate = 38400;
                                usb.PortName = port;
                                //Send message to Port and Wait for response
                               // MessageBox.Show(port, port, MessageBoxButtons.OK);
                                usb.Open();
                                Thread.Sleep(100);
                                usb.Write("A");
                                usb.ReadTimeout = 1000;
                                usb.Read(check, 0, 2);

                                buffer = check[0].ToString() + check[1].ToString();
                                
                            }

                            catch (TimeoutException)
                            {
                                timeoutcheck = 1;
                                usb.Close();
                                Thread.Sleep(100);
                                bob = 1;
                                MessageBox.Show(check[0].ToString(), check[0].ToString(), MessageBoxButtons.OK);
                            }


                            if (timeoutcheck == 0 && (check[0] == 'A' || check[1] == 'A'))
                            {
                                //MessageBox.Show(check[0].ToString(), check[0].ToString(), MessageBoxButtons.OK);
                                break;
                            }
                            else if ( bob != 1)
                            {
                                usb.Close();
                                Thread.Sleep(100);
                                timeoutcheck = 2;
                            }
                        }
                    }

Hit there!!
I'm just trying to write a code in C# to read and write to an Arduino serial port and I noticed you have the code posted...is that just partial or pretty much everything need to set it up....I have the console code but i'm trying to build a GUI one this time...if you need the console i can send it to you..I can try debugging on my side and let you know if i figure sth out.

@surefanatic
Every time the serial port is opened, the Arduino resets. It takes a few milliseconds to get serial communication set up. You may not be waiting long enough before trying to read the reply from the Arduino.