0

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!

RMundorf
  • 3
  • 1
  • There's about a dozen things that can go wrong with that serial port call. If the stepper motor hasn't moved even once using your C# code, that's where I would put your focus (getting it to move once). – Robert Harvey Dec 31 '18 at 19:49

1 Answers1

0

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";

Guest
  • 16
  • Sorry, I should have read more thoroughly. Try changing the newline behavior of the serial port. comPort.Out.NewLine = "\r"; Otherwise add an explicit carriage return and use comPort.Write – Guest Dec 31 '18 at 20:10
  • My issue was a lack of understanding of the "writeline". It does not include a carriage return, apparently. Your suggested string command fixed it! – RMundorf Dec 31 '18 at 20:12
  • @RMundorf `WriteLine` outputs the current line terminator, which by default is `\r\n`. So it was there, but not at the end. You may want to change it so `WriteLine` for your case(s) outputs `\r` instead. – Kit Dec 31 '18 at 20:17