Pressing on serial monitor on arduino editor Re-runs the setup() function. How to Re-runs the setup() function from c#?
I build software with c# that communicates with Arduino but when I run the software arduino has already started work because is USB connected. How can I reboot the arduino board from c#?
Same way. Close and open the serial connection.
You can also do it in code. Have the arduino wait for a start command from the C# application.
Thank you, Are there other options? is it possible to call the function? because close and open the serial connection little problematic for me.
in
aarg:
You can also do it in code. Have the arduino wait for a start command from the C# application.
on c# i send "Start" to the serial and in the arduino i put the code :
if(Serial.readString() == "Start"){
RunOnce();
}
I can save it all if i run the function RunOnce(); from the Setup function.
Then don't reboot it. You can call the setup function again, but what does that do for you? It won't remove any characters from the PC end of the serial connection.
SurferTim:
Then don't reboot it. You can call the setup function again, but what does that do for you? It won't remove any characters from the PC end of the serial connection.
How can call the setup function again???
What is it in setup() that you want/need to run again ?
UKHeliBob's question is a good one. It won't reset any global variables, but this is how.
if(Serial.readString() == "Start"){
setup();
}
i need to call a function RunOnce(); This function needs to runs only once and it should write to serial.write 2-3 times (The function contains 2-3 serial.write commands).
SurferTim:
UKHeliBob's question is a good one. It won't reset any global variables, but this is how.if(Serial.readString() == "Start"){
setup();
}
It is similar to my code and in this case I prefer to call my function and not to the SETUP function.
There is no way to call a setup function from C# or reboot the arduino board?
maordany:
i need to call a function RunOnce(); This function needs to runs only once and it should write to serial.write 2-3 times (The function contains 2-3 serial.write commands).
Then it isn't "run only once". It is "run on command" and could be run several times.
There is no way to call a setup function from C# or reboot the arduino board?
Yes to both. If you want to call the setup function on command, I showed you how to do that. If you want a hard reset, close and open the serial connection.
maordany:
i need to call a function RunOnce(); This function needs to runs only once and it should write to serial.write 2-3 times (The function contains 2-3 serial.write commands).
I think you need to describe what you need to achieve more clearly so that we can suggest the best way to do that.
It sounds like you just need to send an appropriate command from your C# program and when the Arduino recognizes that command it will call the appropriate function. It does not sound like there is any need to reset the Arduino or to make setup() run a second time.
If you are concerned that a function might be called several times when you only want it to run once it is easy to include a variable that records the fact that it has run and prevents it from running again even if it is called many times.
...R
SurferTim:
If you want a hard reset, close and open the serial connection.
Or toggle DTR, which resets the Arduino....
Regards,
Ray L.
My code runs once because I send "Start" only once
if(Serial.readString() == "Start"){
RunOnce();
}
i need to send serial.write 2-3 times only once. My problem is I can not get commands from setup. If I write a serial.write in loop its work but if i write serial.write in setup its dont work.
C#
private void timer_Tick(object sender, EventArgs e)
{
try
{
if (!serialPort.IsOpen)
{
serialPort.BaudRate = 9600;
serialPort.PortName = "COM3";
serialPort.Open();
Thread.Sleep(1500);
serialPort.Write("Start");
}
}
catch (Exception)
{
}
}
if i write serialPort.Write("Start"); its work but if not its dont work.
Thank you all. I added the command
serialPort.DtrEnable = true;
in c# and its work gooooooooooooooood
Your solution sounds clumsy. Why don't you just put the code you gave, ie
if(Serial.readString() == "Start"){
RunOnce();
}
in the loop() function ?
maordany:
Thank you all. I added the command
serialPort.DtrEnable = true;
in c# and its work gooooooooooooooood
I don't know how to reconcile that statement with this one from Reply #3
because close and open the serial connection little problematic for me.
It was because of your Reply #3 that I did not mention DtrEnable ....
...R