using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO.Ports;
using System.Threading;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
SerialPort sp = new SerialPort();
sp.PortName = "COM1";
sp.BaudRate = 9600;
sp.Open();
if (sp.IsOpen)
{
sp.Write("ENA;");
Thread.Sleep(1000);
sp.Write("POS;");
Thread.Sleep(1000);
string msgPos = sp.ReadExisting();
Console.WriteLine(msgPos);
sp.Write("OFF;");
sp.Close();
Console.ReadKey();
}
}
}
}
以上是获取步进电机当前位置的代码。下面是我的输出的链接。但我无法将其格式化为数字。
这是输出:我的输出图片