我正在使用TI-Luminary Micro的Stellaris Stepper motor RDK。如果需要,请将我转到右侧论坛。我正在使用USB串行接口将设备与使用C#开发的应用程序连接起来。目前我很难从我的应用程序发送控制命令。我能够开始&通过发送以下命令停止步进电机。但我想设定目标步数/秒。我无法理解如何使用所需的目标步骤形成命令控制数据包。所以对此有任何帮助表示赞赏。
代码:
//To startMotor:
byte[] StartMotorRequest = new byte[] {0xff,0x07,0x13,0x14,0x00,0xcf,0xff,
0x04,0x30,0xcd,0xff,0x09,0x13,0x08,0x00,0xd0,0x30,0x0e,0xcf };
_serialPort.Write(StartMotorRequest, 0, StartMotorRequest.Length);
//To StopMotor:
byte[] StopMotorRequest = new byte[] { 0xff,0x04,0x31,0xcc};
_serialPort.Write(StopMotorRequest, 0, StopMotorRequest.Length);
谢谢,
Balaji.R
答案 0 :(得分:0)
以下是使用USB串行接口将步进电机与应用程序连接的简单教程。
{tag} {length} {command} {optional command data byte(s)} {checksum}
所以没有数据字节的0x01命令将按如下方式发送:
0xff 0x04 0x01 0xfc
使用预定义的目标步/秒启动电机
CMD_RUN = 0x30 TAG_CMD = 0xff
描述: 根据当前参数集启动电机运行(如果尚未运行)。
命令:
TAG_CMD 0x04 CMD_RUN {checksum}
CMD_SET_PARAM_VALUE
描述: 设置参数的值。对于值大于单个字节的参数,不需要提供参数值的所有字节;未提供的值字节(即,更重要的字节)被视为传输了零。如果提供的参数值超出了所需的字节数,则忽略额外的字节。
命令:
TAG_CMD {length} CMD_SET_PARAM_VALUE {param} {value} [{value} ...] {校验}
PARAM_TARGET_POS 0x08
描述: 指定电机的目标位置。
PARAM_USE_ONBOARD_UI 0x1e
描述: 指定板载用户界面是处于活动状态还是非活动状态。
C#中的示例代码为Start&使用目标步骤/秒停止预装电机
private void InitializeSerialPort()
{
try
{
String ComPortName = ComPortCB.Text;
ComPortName = ComPortName.Trim();
_serialPort = new SerialPort(ComPortName, 115200, Parity.None, 8, StopBits.One);
_serialPort.Handshake = Handshake.None;
_serialPort.WriteTimeout = 500;
_serialPort.ReadTimeout = 500;
_serialPort.Open();
_serialPort.Close();
_SerialPortInitialization = true;
}
catch (NullReferenceException excpt)
{
MessageBox.Show(excpt.Message);
}
}
public static void StartStepperMotor()
{
try
{
if (!_serialPort.IsOpen)
_serialPort.Open();
byte[] StartMotorRequest = new byte[] { 0xff, 0x04, 0x30, 0xcd };
byte[] SetParamTargetPosition = new byte[] { 0xFF, 0x09, 0x13, 0x08, 0x00, 0x50, 0xC3, 0x00, 0xCA };
byte[] GetDataItems;
GetDataItems = new byte[] { 0xff, 0x06, 0x13, 0x1e, 0x00, 0xca };
_serialPort.Write(GetDataItems, 0, GetDataItems.Length);
_serialPort.Write(StartMotorRequest, 0, StartMotorRequest.Length);
_serialPort.Write(SetParamTargetPosition, 0, SetParamTargetPosition.Length);
//_serialPort.Close();
}
catch (Exception ex)
{
MessageBox.Show("Error opening/writing to serial port :: " + ex.Message, "Error!");
}
}
public static void StopStepperMotor()
{
try
{
if (!_serialPort.IsOpen)
_serialPort.Open();
byte[] StopMotorRequest = new byte[] { 0xff, 0x04, 0x31, 0xcc };
_serialPort.Write(StopMotorRequest, 0, StopMotorRequest.Length);
//_serialPort.Close();
}
catch (Exception ex)
{
MessageBox.Show("Error opening/writing to serial port :: " + ex.Message, "Error!");
}
}
private void RotateStepperMotor(int RequiredAngle)
{
try
{
if (!_serialPort.IsOpen)
_serialPort.Open();
ushort Steps = RequiredAngle;
byte TargetPositionLSB = (byte)(Steps & 0xFFu);
byte TargetPositionMSB = (byte)((Steps >> 8) & 0xFFu);
byte CheckSumByte;
CheckSumByte = (byte)(0 - (0xFF + 0x09 + 0x13 + 0x08 + TargetPositionLSB + TargetPositionMSB));
byte[] GetDataItems;
GetDataItems = new byte[] { 0xff, 0x06, 0x13, 0x1e, 0x00, 0xca };
_serialPort.Write(GetDataItems, 0, GetDataItems.Length);
byte[] StartMotorRequest = new byte[] { 0xff, 0x04, 0x30, 0xcd };
byte[] SetParamTargetPosition = new byte[] { 0xFF, 0x09, 0x13, 0x08, 0x00, TargetPositionLSB, TargetPositionMSB, 0x00, CheckSumByte };
_serialPort.Write(StartMotorRequest, 0, StartMotorRequest.Length);
_serialPort.Write(SetParamTargetPosition, 0, SetParamTargetPosition.Length);
_serialPort.Close();
}
catch (Exception ex)
{
MessageBox.Show("Error opening/writing to serial port :: " + ex.Message, "Error!");
}
}