C# and Arduino Auto detect Com Port for Stepper motor control

Hello All,

I am trying to control a stepper motor through arduino using C#. The program works perfectly fine at the first instance. But when i launch the program at the second instance it fails.

I have tried to integrate the autodetect function (http://playground.arduino.cc/Csharp/SerialCommsCSharp) and stepper control code together. But there is some issue in the code which lets it run perfectly only once. Kindly help me to solve the issue.

autodetect_v3.ino (5.39 KB)

You state, "...But when i launch the program at the second instance it fails..."

HOW does it fail ?

You have two calls to "Serial.begin(9600);" in your Setup()
Could that be causing the problem?

Whatever about your other problems this is not going to work

while (Serial.available()==5)

perhaps it should be IF rather than WHILE

If there is an autodetect problem it will be on the PC, not on the Arduino. Perhaps your C# program is not releasing the COM port when it shuts down.

...R
Serial Input Basics
Stepper Motor Basics

Hello…

Thank you very much for the inputs. Even with the if condition this doesnt seem to resolve the issue.

I think you are right that the serial port is not being released by the C# code, because when disconnect the USB cable and reconnect it the code works perfectly fine when i launch and run the application.

Or else I have to press the “Reset” button on the board to make it work.

I didn’t attach the C# code earlier. its attached now. Am I making a mistake in the C# code??

Form1.zip (2.27 KB)

alvanthony:
its attached now.

Where ?

...R

Robin2:
Where ?

...R

its attached now.. Sorry

These two lines may be the problem

serialPort1.Open();
    // SNIP

serialPort1.Close();

You should open the serial port at the start of the program and keep it open until the program is finished. Opening and closing it does not work as the Arduino restarts every time it is opened.

You may also need to set one of DTR CTS etc

...R

Thanks for your suggestions. I tried doing this but it doesn’t help. But I have found a new thing that my C# code works perfectly if i use the default communication protocol code (attached here). This code is just an autodetect code and does not have the stepper motor code in it. C# responds perfectly to this code and there are no issues at all.

But when i integrate the stepper motor code getinput(); it works for the first time and after that it doesnot detect the port automatically and throws an error. So i have press the “reset” button on the arduino to make it work or i have to reconnect the USB cable to make the code work.

c_sharp_test.ino (2.06 KB)

Your Arduino code is only one side of the conversation. If its behaviour affects a C# program it makes little sense without the C# code.

As a general comment it seems a very complex way to do stuff. I have never had a problem establishing communication with this in setup()

void setup() {
   Serial.begin(baudrate);
   Serial.println("Arduino is ready");
   // other stuff
}

...R