通过 C# 将字符串发送到 Applied Motion 步进电机控制器时出现问题

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

我正在用 C# 编写一个简单的程序来移动步进电机。我过去有一些 C++ 经验,但我决定过渡过来,因为我必须重新自学一些 C++。

以前,我通过 PuTTY 使用串行命令控制电机(使用 Applied Motion ST5 步进控制器)。我的想法是,我可以通过打开正确的 COM 端口(工作正常,因为当我输入一个非工作端口号时它会崩溃)并发送一个字符串来使用 C# 发送相同的命令。然而,当我通过串行终端(FL1000,后跟一个马车 return,它告诉电机顺时针移动 1000 步)发送一串相同的命令时,电机什么都不做。 WriteLine 应该是在这里使用的正确方法,因为它发送字符串然后是 return,对吗?

有没有人发现任何明显的错误会使我的字符串无法到达控制器?

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));
        }

    }
}

我预计这会使电机移动 1000 步。我看到的唯一结果是我的 "markers" 出现在控制台上。程序无错退出。

提前致谢!

您的命令不包含回车 return 或换行符。电机正在寻找其中之一以知道命令已完成。

我没有使用过他们的 ST5 系列电机,但他们携带的其他产品需要用马车终止命令 return。尝试将您的消息更改为:

字符串命令="FL1000\r";