Search code examples
c#iostepper

Problem sending string to Applied Motion Stepper Motor Controller via C#


I'm working on writing a simple program to move a stepper motor in C#. I have some past experience in C++, but have decided to transition over, as I'd have to reteach myself a bit of C++.

Previously, I've controlled the motor (using an Applied Motion ST5 Stepper Controller) with serial commands via PuTTY. My thought is that I could just send the same commands using C# by opening the correct COM Port (which works fine, as it crashes when I feed in a nonworking port number), and sending a string over. However, when I send a string of the same command that I had been using via serial terminal (FL1000, followed by a carriage return, it tells the motor to move 1000 steps clockwise), the motor does nothing. WriteLine should be the correct thing to use here, as it sends the string then a return, correct?

Does anybody see any glaring mistakes that would make my string not make it to the controller?

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO.Ports;

namespace ConsoleApp3
{
    class Program
    {
        static SerialPort comPort;
        static void Main()
        {
            //These values in the declared serial port match what my device manager says.
            comPort = new SerialPort("COM6", 9600, Parity.None, 8, StopBits.One);
            comPort.ReadTimeout = 5000;
            comPort.WriteTimeout = 5000;
            comPort.Open();
            //Pauses for a moment so that I can see the console otuput.
            System.Threading.Thread.Sleep(TimeSpan.FromSeconds(3));
            string command = "FL1000";
            Console.WriteLine("Moving Motor...");
            //Tells the controller to move the motor 1000 steps clockwise
            comPort.WriteLine(command);
            //confirms that the code made it past the comPort writeline
            Console.Write("Command Sent");
            //Pauses for a moment so that I can see the console output.
            System.Threading.Thread.Sleep(TimeSpan.FromSeconds(3));
        }

    }
}

I expect that this would move the motor 1000 steps. The only results I see are that my "markers" appear on the console. The program exits without error.

Thank you in advance!


Solution

  • Your command does not contain a carriage return or newline. The motor is looking for one of these to know that the command is complete.

    I haven't worked with their ST5 line of motors, but other products they carry require the command to be terminated with a carriage return. Try changing your message to:

    string command = "FL1000\r";