Arduino resets after receiving data from c#

Hello,
I am trying to write a simple program that send data from C# code to arduino.
and arduino send data back to c#.
via serial port.

If i open the connection and close it and than open it again. the arduino DOES NOT resets it self.
I'm receiving the data in the c# program and all working fine.
But when i try to send data from c# to arduino. The arduino gets the data,send it back for 1 time only and than reset itself for some reason.

can you please help me with that ?

The Arduino code:

float  x = 0;

void setup() {
  Serial.begin(9600);
}

void loop() {
     if (  Serial.available())
     {
          x = Serial.parseFloat();
     }
     x = x +1;
     Serial.println(x);
     delay(500);
}

The c# code:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO.Ports;


namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        // Global variables
        public SerialPort serialPort1 = new SerialPort();

        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            getAvailablePorts();        // Check wich ports are available
            button2.Enabled = true;     // open port
            button3.Enabled = false;    // close port
            button1.Enabled = false;    // send
            ovalShape1.FillColor = Color.Red;
        }

        void connection (int status)        // Set labels and buttons
        {
            if (status == 1)
            {
                label2.Text = "Connetion Status: Ok";
                ovalShape1.FillColor = Color.Green;
                button2.Enabled = false;        // open port
                button3.Enabled = true;         // close
                button1.Enabled = true;         // send
            }
            else if (status == 0)
            {
                label2.Text = "Connetion Status: Disconnected";
                ovalShape1.FillColor = Color.Red;
                button2.Enabled = true;     // open
                button3.Enabled = false;    // close
                button1.Enabled = false;    // send
            }
        }
        void getAvailablePorts()
        {
            SerialPort serialPort1 = new SerialPort();
            String[] ports = SerialPort.GetPortNames();
            comboBox1.Items.AddRange(ports);
        }

        private void button1_Click(object sender, EventArgs e)      // Button for Sending data via Serial Port
        {
            try
            {
                if (textBox1.Text == "")
                {
                    MessageBox.Show("Please write a number in the Text-Box ", "test");
                }
                else
                {
                    string str = textBox1.Text;
                    serialPort1.WriteLine( str );
                }
            }
            catch(UnauthorizedAccessException)
            {
                MessageBox.Show("Error sendng data");
            }
        }

        private void button2_Click(object sender, EventArgs e)      // Button for openning port
        {
            try
            {
                if (comboBox1.Text == "")
                {
                    MessageBox.Show("Please choose Port", "test");
                }
                else
                {
                    serialPort1.PortName = comboBox1.Text;
                    serialPort1.BaudRate = 9600;
                    serialPort1.DtrEnable = false;
                    connection(1);      // connection is open
                    serialPort1.Open();
                    timer1.Start();     // starting timer for reading data
                }
            }
            catch (UnauthorizedAccessException)
            {
                MessageBox.Show("Error opennig port", "");
                ovalShape1.FillColor = Color.Green;
            }
        }

        private void button3_Click(object sender, EventArgs e)      // Button for closing Port
        {
            try
            {
                timer1.Stop();
                if (serialPort1.IsOpen)
                    serialPort1.Close();
                connection(0);
            }
            catch (UnauthorizedAccessException)
            {
                MessageBox.Show("Error closing port", "");
            }
        }

        private void timer1_Tick(object sender, EventArgs e)
        {

            if (serialPort1.ReadExisting() != "")
            {
                string[] str = serialPort1.ReadLine().Split('\r');
                label3.Text = "Seted value is: " + str[0];
            }
        }
    }
}

If you want see how the c# program looks: [attached]

Capture.JPG

Your PC program should open the serial port and allow time for the Arduino to reset and then it should keep the port open until it is completely finished with the Arduino.

If you have something in setup() like Serial.println("Arduino is ready"); your C# program can wait until it receives that as evidence that the Arduino has finished its reset process.

...R

                    string str = textBox1.Text;
                    serialPort1.WriteLine( str );

Why do you need to make a copy of the text, in a variable that goes out of scope as soon as you've written it to the serial port?

        private void button2_Click(object sender, EventArgs e)      // Button for openning port

You can, and you REALLY should, give meaningful names to objects. button2 is really a stupid name for a button. It does not give a clue as to what the button is supposed to do.

        private void timer1_Tick(object sender, EventArgs e)

WTH? There are ways, using the Serial class methods, to register an event handler, which gets called when serial data arrives. Using a timer and hoping that there is something to read is just wrong.

  1. At first i thought something is wrong here. i maid everything as simple as possible to eliminate other problem options. So i splited this two commands. But you are right its not suppose to be like that after i sove the main problem

  2. The name of the objects is how the Visual-Studio builds it. Any way you have a simple description for every function in the code.

  3. Again i thought that the event handler causes the arduino resets, so i used something else. You can see in the timer_tick that if there something in the serial than it will read it. it wont wait for serial data to come in.

  4. Opening the port in C# wont make the arduino reset. I tried opening and closing the port several times it didn't reset.

The main problem is that after i send some simple float number from C# to arduino. The arduino gets that number, sends its back for one time and reset itself.
why does it reset itself ?

qwerty124:
4. Opening the port in C# wont make the arduino reset. I tried opening and closing the port several times it didn't reset.

Maybe you need to change this

serialPort1.DtrEnable = false;

to

serialPort1.DtrEnable = true;

There may also be a similar setting for RTS (I can never remember which is the appropriate one).

What Arduino board are you using?

I can't see anything in your Arduino code that could be causing a problem. That's why I think you need to get your C# code to cause the Arduino to reset in an orderly way.

And another important question is "how do you know the Arduino is resetting when it should not?" A good way to prove that is to put some code in setup() to blink the onboard LED in a specific pattern.

...R

  1. Opening the port in C# wont make the arduino reset. I tried opening and closing the port several times it didn't reset.

Your proof of this assertion is conspicuously absent.

thank for the replay.
I will try this code:

serialPort1.DtrEnable = true;

As you can see in the arduino code i have a variable called X which is set at the beginning as x=0.
In the loop function:
X=X+1 every 500 (milisec)

the data that received in the serial port in the arduino code is set to this variable (X) and is printed back to the serial:

void loop() {
     if (  Serial.available())
     {
          x = Serial.parseFloat();
     }
     x = x +1;
     Serial.println(x);
     delay(500);
}

what is see in the c# code when reading from serial is at first the values are increasing from 0.
When i send (from c#) a value for example of: (62.35).
The next value that is i read (in c#) from the serial is: 63.35.
And the next one is 0, and starting to increase.

As i said before, i have an option in c# open and close the port , as many times as i want. when doing that i read the values as they suppose to be, increasing by 1 each 500 (milisec).
only after send a data it seems that arduino reset itself.

i will try to put in the setup function (in arduino) a line that will set (X=10.1), for example.
to see if after the reset it will start from this number or from 0, for some unexplained reason.

qwerty124:
As i said before, i have an option in c# open and close the port , as many times as i want. when doing that i read the values as they suppose to be, increasing by 1 each 500 (milisec).
only after send a data it seems that arduino reset itself.

That all suggests to me there is something in your code for sending data that is causing the reset.

As I suggested earlier. my approach to debugging this problem would start with getting the C# program to reset the Arduino reliably.

And you have still not said what Arduino you are using. They do not all behave the same way.

...R

@Robin2 And what if he wants to continue the count or whatever without resetting the arduino?

I'm courious about this thread because I plan on doing something like that in the future and I want to make sure I actually can. so I understand that he made it to work (sort of) but I know that I definitely wouldn't want my arduino to reset each time I want to pass something to it to work with.

If you know which arduino model won't reset please post it here. I'd like to know.

Thanks.

but I know that I definitely wouldn't want my arduino to reset each time I want to pass something to it to work with.

What I prefer is that opening the serial port resets the Arduino, so that it is in a known configuration. Then, as long as my PC application leaves the port open, it can talk to the Arduino, and the Arduino can talk to it.

If you prefer that opening the serial port not reset the Arduino, search for "Arduino prevent reset serial".

I have arduino uno.
Its important for the arduino to keep running after it get my data from C#, otherwise whats the point of sending information to arduino via port.

From my point of view and by debugging the code (arduino and C#) as i see it the only thing that causing arduino to reset is:

serialPort1.WriteLine( str );

I tried to change this line, nothing new, same problem:

serialPort1.DtrEnable = true;

Is there other options beside adding capacitor to the board to prevent it from reset?

Sagirokach:
@Robin2 And what if he wants to continue the count or whatever without resetting the arduino?

I am hoping he will get to the stage where his C# program causes the Arduino to reset when the serial port is first opened and that he will then keep the serial port open to prevent any further resets.

Of course there could be a very different scenario in which the Arduino is running 24/7 and a PC needs to pick up data from it from time to time without disturbing it. I don't believe that is what the OP is trying to do and it would require a different approach.

...R

After doing a lot of test i got the conclusion that the arduiono DOESN'T reset itself after receiving my data.

I placed at the end of the setup function:

x=10;

If the arduino did reset itself it would be 10 (ten). But its equal to 0 (zero).
But when i send some information through the Serial-Monitor (in arduino), the code is working fine.
So this is defiantly a problem with the C# code:

string str = textBox1.Text;
serialPort1.WriteLine( str );

So what is the problem ?

SOLVED
But i need an explanation.

As said, i i was using the Serial-Monitor in the arduino. the code was working fine.
but when i sent the data trough the c# code the receiving variable was set to 0 (zero) for some reason, or some error data but the counting was staring from 0 (zero);

I edited the arduino code to this:

float  x = 0;
String str;

void setup() {
  pinMode(13, OUTPUT);
  Serial.begin(9600);
  str = "abc";
  x = 100.0;
}

void loop() {
  if (  Serial.available())
  {
    x = Serial.parseFloat();
    str = Serial.readString();          //     <<<------ THIS WAS ADDED
  }
  x = x +1;
  Serial.println(x);
  delay(500);
}

After that all was working fine, in the Serial-Monitor and C# code.
Can someone please explain it to me ?

Do you KNOW what parseFloat() removes from the serial stream? Do you know what it does NOT remove from the serial stream?

Do you know what adding the readString() call really caused to happen?

You should not use functions that you do not understand. You CAN read the code for the parseFloat() and readString() methods to learn what they actually do.

Have a look at the examples in Serial Input Basics - simple reliable ways to receive data. There is also a parse example.

...R