电机
42电机(42BYGH34-401A),最高速度居然是300 每分钟,卖家说的, 测试A+,A-时,将2根线短接,转动电机时会有明显的滞涩感,B+,B-一样,
编码器
HN3806-AB-600N ,有AB两相接24V电源,A,B两相直接连PLC I0.6,I0.7 ,这里使用PLC的向导,启用HC1
步进控制器
TB6600 带光耦的,控制信号3.3-24 可以直接连PLC 不用接电阻很方便,拨码开关把电流设置成0.5,细分设置成1600,也就是1600个脉冲转一圈,至于一圈移动多少毫米这个要看螺杆机构的参数(一般供应商提供)
这里是8mm
MAP 0 使用HC0, 并使用Q0.0 作为脉冲输出Q0.2方向,I0.0参考点输入
按键位反转
由于无法实现按位反转,所以这里使用了字节
添加自复位功能
完成程序
//是PLC相对地址除以2
//比方VD240, 从&VB100开始, 那么相对字节是140,除以2后是70,
//因为一个保持寄存器对应2个字节
_Monitor.SendCmd(new ReadHoldRegisterCmd(1, 70, 4));
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using System.Threading; using EMDLX.Server.PLC_Core; using PLC.Util; namespace EMDLX.UI { public partial class frmModbusMonitor : Form { private SynchronizationContext SyncCtx = null; private Boolean IsRunning = false; private TCPModbusMonitor _Monitor = new TCPModbusMonitor("192.168.1.243", 502); public frmModbusMonitor() { InitializeComponent(); SyncCtx = SynchronizationContext.Current ; btnServerSwitch.Text = "启动服务"; _Monitor.ReceivedData += new EventHandler<ReceivedEventArgs>(_Monitor_ReceivedData); #region 刷新读数 var timer = new System.Windows.Forms.Timer(); timer.Interval = 500 * 1; timer.Tick += (s, e) => { try { timer.Stop(); //是PLC相对地址除以2 //比方VD240, 从&VB100开始, 那么相对字节是140,除以2后是70, //因为一个保持寄存器对应2个字节 _Monitor.SendCmd(new ReadHoldRegisterCmd(1, 70, 12)); } catch (Exception ex) { Console.WriteLine(ex.Message); } finally { timer.Start(); } }; timer.Start(); #endregion } protected override void OnClosed(EventArgs e) { try { _Monitor.Stop(); } catch { } base.OnClosed(e); } void _Monitor_ReceivedData(object sender, ReceivedEventArgs e) { if (e.Cmd is ReadHoldRegisterCmdResult) { var cmd = e.Cmd as ReadHoldRegisterCmdResult; var hc1 = cmd.GetInt32(0); var pos = cmd.GetFloat(6); var frequency = cmd.GetFloat(2); var fLimit = cmd.GetRegister(8).HighByte; var rLimit = cmd.GetRegister(9).LowByte; var homeLimit = cmd.GetRegister(9).HighByte; var motorDirection = cmd.GetRegister(10).LowByte; var motorEN = cmd.GetRegister(10).HighByte; var showMsg = string.Format("HC1:{0},位置:{1},频率:{2}",hc1,pos,frequency); showMsg += Environment.NewLine + Environment.NewLine; showMsg += string.Format("前限位:{0},后限位{1},参考点:{2},电机转向:{3},是否脱机:{4}", fLimit, rLimit, homeLimit,motorDirection,motorEN); SyncCtx.Post(o => { txtMsg.Text = showMsg; }, null); } } private void btnServerSwitch_Click(object sender, EventArgs e) { try { if (btnServerSwitch.Text == "启动服务") { _Monitor.Start(); btnServerSwitch.Text = "停止服务"; txtTips.Text = "服务启动:" + DateTime.Now; IsRunning = true; } else { _Monitor.Stop(); btnServerSwitch.Text = "启动服务"; txtTips.Text = "服务关闭"; IsRunning = false; } } catch (Exception ex) { MessageBox.Show(ex.Message, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error); try { btnServerSwitch.Text = "启动服务"; _Monitor.Stop(); IsRunning = false; } catch { } } } private void frmModbusMonitor_Load(object sender, EventArgs e) { } private void btnHome_Click(object sender, EventArgs e) { _Monitor.SendCmd(new WriteSingleHoldRegisterCmd(1, 25, 0x01, 0x00)); } private void checkBox1_CheckedChanged(object sender, EventArgs e) { if (checkBox1.Checked) { //电脱机 _Monitor.SendCmd(new WriteSingleHoldRegisterCmd(1, 22, 0x01, 0x00)); } else { //电脱机 _Monitor.SendCmd(new WriteSingleHoldRegisterCmd(1, 22, 0, 0)); } } private void btnMA_Click(object sender, EventArgs e) { try { #region 设置位置 float pos = float.Parse(txtPos.Text); var cmd = new WriteHoldRegisterCmd(1, 15,2,TCPCommandBase.FloatGetBytes(pos)); _Monitor.SendCmd(cmd); #endregion //等待100毫秒 Thread.Sleep(50); #region 启动绝对定位 var cmd1 = new WriteSingleHoldRegisterCmd(1, 26, 0x01, 0x00); // Console.WriteLine(cmd1.CmdHexString); _Monitor.SendCmd(cmd1); #endregion } catch (Exception ex) { MessageBox.Show(ex.Message); } } private void btnMR_Click(object sender, EventArgs e) { try { #region 设置位置 float pos = float.Parse(txtPos.Text); var cmd = new WriteHoldRegisterCmd(1, 15, 2, TCPCommandBase.FloatGetBytes(pos)); _Monitor.SendCmd(cmd); #endregion //等待100毫秒 Thread.Sleep(50); #region 设置方向 byte setDirectionLowByte=0x00; if (ckbDirection.Checked) { setDirectionLowByte = 0x01; } _Monitor.SendCmd(new WriteSingleHoldRegisterCmd(1, 30, setDirectionLowByte, 0x00)); #endregion Thread.Sleep(50); #region 启动相对定位 var cmd1 = new WriteSingleHoldRegisterCmd(1, 27, 0x01, 0x00); _Monitor.SendCmd(cmd1); #endregion } catch (Exception ex) { MessageBox.Show(ex.Message); } } private void btnM_Click(object sender, EventArgs e) { try { #region 设置方向 byte setDirectionLowByte = 0x00; if (ckbDirection.Checked) { setDirectionLowByte = 0x01; } _Monitor.SendCmd(new WriteSingleHoldRegisterCmd(1, 30, setDirectionLowByte, 0x00)); #endregion Thread.Sleep(50); #region 启动相对定位 var cmd1 = new WriteSingleHoldRegisterCmd(1, 29, 0x01, 0x00); _Monitor.SendCmd(cmd1); #endregion } catch (Exception ex) { MessageBox.Show(ex.Message); } } private void btnStop_Click(object sender, EventArgs e) { _Monitor.SendCmd(new WriteSingleHoldRegisterCmd(1, 28, 0x01, 0x00)); } private void btnClearCounter_Click(object sender, EventArgs e) { _Monitor.SendCmd(new WriteSingleHoldRegisterCmd(1, 69, 0x01, 0x00)); } } }
完整代码在后台文件管理PLC运动控制与艾莫讯