当前位置:   article > 正文

自动化上位机开发C#100例:如何用面向对象的方式封装雷赛运动控制卡EtherCAT总线卡(C#代码)

自动化上位机开发C#100例:如何用面向对象的方式封装雷赛运动控制卡EtherCAT总线卡(C#代码)

自动化上位机开发C#100例:雷赛运动控制卡EtherCAT总线卡C#封装类


在这里插入图片描述

LTDMC.dll下载

最新的雷赛运动控制卡SDK,LTDMC.dll下载:
https://download.csdn.net/download/Mr_Wei_/88847834

LTDMC.cs LTDMC.dll C#调用封装下载

最新的雷赛运动控制卡SDK,LTDMC.cs带中文注释 下载:
https://download.csdn.net/download/Mr_Wei_/88847839

ICard.cs 运动控制卡接口

using System.Collections.Generic;

namespace iSystem
{
    public interface ICard
    {
        /// <summary>  
        /// 卡ID  
        /// </summary> 
        int CardID { get; set; }

        /// <summary>  
        /// 第几张卡  
        /// </summary> 
        int CardNo { get; set; }

        /// <summary>  
        /// 卡名称  
        /// </summary> 
        string CardName { get; set; }

        /// <summary>  
        /// 卡类型 0:2410 1:总线  
        /// </summary> 
        CardType CardType { get; set; }

        /// <summary>  
        /// 是否使用  
        /// </summary> 
        bool IsUsed { get; set; }

        /// <summary>  
        /// 是否初始化完成  
        /// </summary> 
        bool InitSucced { get; set; }

        /// <summary>
        /// 初始化
        /// </summary>
        /// <returns></returns>
        int Init();

        /// <summary>
        /// 关闭
        /// </summary>
        /// <returns></returns>
        int UnInit();

        /// <summary>
        /// 软复位
        /// </summary>
        /// <returns></returns>
        int SoftReset();

        /// <summary>
        /// 硬复位
        /// </summary>
        /// <returns></returns>
        int Reset();

        /// <summary>
        /// 获取总线卡错误代码
        /// </summary>
        /// <returns></returns>
        int GetErrCode();

        /// <summary>
        /// 清除总线卡错误代码
        /// </summary>
        /// <returns></returns>
        int ClearErrCode();

        /// <summary>
        /// 多轴插补
        /// </summary>
        /// <param name="lstMotor">电机列表</param>
        /// <param name="lstDist">目标位置</param>
        /// <param name="mode">移动模式</param>
        /// <returns></returns>
        bool MultMove(List<Motor> lstMotor, List<double> lstDist, int mode = 0, ushort crd = 0);

        /// <summary>
        /// 检测多轴运动是否停止
        /// </summary>
        /// <param name="crd"></param>
        /// <returns>0停止 1未停止</returns>
        int CheckMultMoveDone(ushort crd = 0);

        /// <summary>
        /// 等多轴运动停止
        /// </summary>
        /// <param name="OutTime">等待时间</param>
        /// <param name="crd"></param>
        /// <returns>是否超时</returns>
        bool WaitMultMoveDone(int OutTime, ushort crd = 0);
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97

Card.cs 运动控制卡抽象类

using System.Collections.Generic;

namespace iSystem
{
    public partial class Card : ICard
    {
        #region Properties

        /// <summary>  
        /// 卡ID  
        /// </summary> 
        public int CardID { get; set; } = 1;

        /// <summary>  
        /// 第几张卡  
        /// </summary> 
        public int CardNo { get; set; } = 0;

        /// <summary>  
        /// 卡名称  
        /// </summary> 
        public string CardName { get; set; } = "卡1";

        /// <summary>  
        /// 卡类型 0:2410 1:总线  
        /// </summary> 
        public CardType CardType { get; set; } = CardType.LS2410;

        /// <summary>  
        /// 是否使用  
        /// </summary> 
        public bool IsUsed { get; set; } = true;
        /// <summary>  
        /// 是否初始化完成  
        /// </summary> 
        public bool InitSucced { get; set; } = false;

        #endregion

        #region Constructors

        public Card()
        {

        }
        public Card(int cardID)
        {
            CardID = cardID;
        }

        public Card(Card model)
        {
            CardID = model.CardID;
            CardName = model.CardName;
            CardNo = model.CardNo;
            CardType = model.CardType;
            IsUsed = model.IsUsed;
            InitSucced = model.InitSucced;
        }

        #endregion

        #region Methods


        /// <summary>
        /// 初始化
        /// </summary>
        /// <returns></returns>
        public virtual int Init()
        {
            //int aa = Program.g_MachineType;
            return 0;
        }

        /// <summary>
        /// 关闭
        /// </summary>
        /// <returns></returns>
        public virtual int UnInit()
        {
            return 0;
        }
        /// <summary>
        /// 软复位
        /// </summary>
        /// <returns></returns>
        public virtual int SoftReset()
        {
            return 0;
        }

        /// <summary>
        /// 硬复位
        /// </summary>
        /// <returns></returns>
        public virtual int Reset()
        {
            return 0;
        }
        /// <summary>
        /// 获取总线卡错误代码
        /// </summary>
        /// <returns></returns>
        public virtual int GetErrCode()
        {
            return 0;
        }
        /// <summary>
        /// 清除总线卡错误代码
        /// </summary>
        /// <returns></returns>
        public virtual int ClearErrCode()
        {
            return 0;
        }
        /// <summary>
        /// 多轴插补
        /// </summary>
        /// <param name="lstMotor">电机列表</param>
        /// <param name="lstDist">目标位置</param>
        /// <param name="mode">移动模式</param>
        /// <returns></returns>
        public virtual bool MultMove(List<Motor> lstMotor, List<double> lstDist, int mode = 0, ushort crd = 0)
        {
            return false;
        }
        /// <summary>
        /// 检测多轴运动是否停止
        /// </summary>
        /// <param name="crd"></param>
        /// <returns>0停止 1未停止</returns>
        public virtual int CheckMultMoveDone(ushort crd = 0)
        {
            return 1;
        }
        /// <summary>
        /// 等多轴运动停止
        /// </summary>
        /// <param name="OutTime">等待时间</param>
        /// <param name="crd"></param>
        /// <returns>是否超时</returns>
        public virtual bool WaitMultMoveDone(int OutTime, ushort crd = 0)
        {
            bool result = false;
            return result;
        }


        #endregion
    }
}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133
  • 134
  • 135
  • 136
  • 137
  • 138
  • 139
  • 140
  • 141
  • 142
  • 143
  • 144
  • 145
  • 146
  • 147
  • 148
  • 149
  • 150
  • 151
  • 152
  • 153

CardLTDMC.cs 雷赛运动控制卡EtherCAT总线卡实现类

using System;
using System.Windows.Forms;

namespace iSystem
{
    public class CardLTDMC : Card
    {

        public CardLTDMC() : base()
        {
            //CardName = "DMC2410";
        }
        public CardLTDMC(int cardID) : base(cardID)
        {
            this.CardID = cardID;
        }

        public CardLTDMC(Card model) : base(model)
        {

        }
        /// <summary>
        /// 初始化电机卡
        /// </summary>
        /// <returns>返回卡数</returns>
        public override int Init()
        {
            int result = 0;
            InitSucced = false;
            try
            {
                result = LTDMC.dmc_board_init();
                ushort _num = 0;
                ushort[] cardids = new ushort[8];
                uint[] cardtypes = new uint[8];
                short res = LTDMC.dmc_get_CardInfList(ref _num, cardtypes, cardids);
                if (result > 0)
                    InitSucced = true;
            }
            catch (Exception ex)
            {
                string msg = "电机卡初始化失败!" + ex;
                MessageBox.Show(msg);
                result = 0;
                //throw;
            }

            return result;
        }

        /// <summary>
        /// 关闭电机卡
        /// </summary>
        /// <returns></returns>
        public override int UnInit()
        {
            int result = 1;
            try
            {
                LTDMC.dmc_board_close();
                InitSucced = false;
            }
            catch (System.Exception ex)
            {
                string msg = "电机卡卸载失败!" + ex;
                MessageBox.Show(msg);
                result = 0;
            }
            return result;
        }
        public override int SoftReset()
        {
            int result = 1;
            result = LTDMC.dmc_soft_reset((ushort)CardNo);
            //UnInit();
            //for (int i = 0; i < 15; i++)//总线卡软件复位耗时15s左右
            //{
            //    Application.DoEvents();
            //    Thread.Sleep(1000);
            //}
            //Init();

            return result;
        }

        /// <summary>
        /// 获取总线卡错误
        /// </summary>
        /// <returns>0正常,其它值出错</returns>
        public override int GetErrCode()
        {
            ushort errCode = 0;
            LTDMC.nmc_get_errcode((ushort)CardNo, 2, ref errCode);
            return errCode;
        }
        /// <summary>
        /// 清除总线卡错误代码
        /// </summary>
        /// <returns></returns>
        public override int ClearErrCode()
        {
            short errCode = 0;
            errCode = LTDMC.nmc_clear_errcode((ushort)CardNo, 2);
            return errCode;
        }




    }
}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112

CardList.cs 总线卡列表封装


using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace iSystem
{
    public class CardList
    {
        private static readonly string ConfigPath = $@"{GlobalData.ConfigDir}card.json";

        public static Dictionary<int, Card> Devs = new Dictionary<int, Card>();
        public static ushort CardID=0;

        /// <summary>
        /// 初始化文件
        /// </summary>
        public static void InitJsonFile()
        {
            //Devs.Add(0, new CardDMC2410(0));
            //Devs.Add(1, new CardDMC2410(1));
            //Devs.Add(2, new CardDMC2410(2));
            //Devs.Add(3, new CardDMC2410(3));
            Devs.Add(0, new CardLTDMC(0));
            Save();
        }

        /// <summary>
        /// 加载
        /// </summary>
        public static void Load()
        {
            if (!File.Exists(ConfigPath))
            {
                InitJsonFile();
            }
            Devs = Loader.LoadFromJson<Dictionary<int, Card>>(ConfigPath);

        }

        /// <summary>
        /// 初始化电机卡
        /// </summary>
        /// <returns>返回卡数</returns>
        public static void Init()
        {
            IsInitSucceed = false;
            try
            {
                int num = LTDMC.dmc_board_init();
                if (num <= 0 || num > 8)
                {
                    //UIHelper.ShowError("电机卡初始化失败!");
                }
                ushort _num = 0;
                ushort[] cardids = new ushort[8];
                uint[] cardtypes = new uint[8];
                short res = LTDMC.dmc_get_CardInfList(ref _num, cardtypes, cardids);
                if (num > 0)
                {
                    IsInitSucceed = true;
                    CardID = cardids[0];
                }
            }
            catch (Exception ex)
            {
                string msg = "电机卡初始化失败!" + ex.Message;
                UIHelper.ShowErrorForm(msg);
            }

        }

        /// <summary>
        /// 硬复位
        /// </summary>
        /// <returns></returns>
        public static async Task<bool> Reset()
        {
            Logger.Info("总线卡硬件复位进行中……");
            LTDMC.dmc_board_reset();
            LTDMC.dmc_board_close();

            for (int i = 15; i > 0; i--)//总线卡硬件复位耗时15s左右
            {
                Application.DoEvents();
                await Task.Delay(1000);
                Logger.Info($"总线卡硬复位……{i}s");
            }
            //MotorListForm.Instance.HideWaitForm();


            LTDMC.dmc_board_init();
            Logger.Info("总线卡硬件复位完成,请确认总线状态");

            ushort errcode = 0;
            LTDMC.nmc_get_errcode(CardID, 2, ref errcode);
            if (errcode == 0)
            {
                Logger.Info("EtherCAT总线正常");
                return true;
            }
            else
            {
                Logger.Error($"EtherCAT总线出错 errcode:{errcode}");
                return false;
            }
        }

        /// <summary>
        /// 软复位
        /// </summary>
        /// <returns></returns>
        public static async Task<bool> SoftReset()
        {
            Logger.Info("总线卡软件复位进行中……");
            LTDMC.dmc_soft_reset(CardID);
            LTDMC.dmc_board_close();
            ushort errcode = 0;
            for (int i = 15; i > 0; i--)//总线卡软复位耗时15s左右
            {
                Application.DoEvents();
                await Task.Delay(1000);
                Logger.Info($"总线卡软复位……{i}s");
                
                //LTDMC.nmc_get_errcode(CardID, 2, ref errcode);
                //if (errcode == 0)
                //{
                //    break;
                //}
            }

            LTDMC.dmc_board_init();
            Logger.Info("总线卡软复位完成,请确认总线状态");

            LTDMC.nmc_get_errcode(CardID, 2, ref errcode);
            if (errcode == 0)
            {
                Logger.Info("EtherCAT总线正常");
                return true;
            }
            else
            {
                Logger.Error($"EtherCAT总线出错:{errcode}");
                return false;
            }
        }

        /// <summary>
        /// 获取总线状态
        /// </summary>
        /// <returns></returns>
        public static bool GetStatus()
        {
            ushort errcode = 0;
            LTDMC.nmc_get_errcode(0, 2, ref errcode);
            return errcode == 0;
        }

        /// <summary>
        /// 直线插补相对运动
        /// </summary>
        public static bool LineOffset(IMotor aMotor, IMotor bMotor, double aDist, double bDist)
        {
            if (!CardList.IsInitSucceed)
                return false;

            ushort[] AxistList = new[] { aMotor.AxisNo, bMotor.AxisNo };
            double[] Target_Pos = new double[] { aDist, bDist };
            ushort mode = 0;
            ushort crd = 0;

            LTDMC.dmc_set_vector_profile_unit(aMotor.CardNo, 0, aMotor.MinSpeed, aMotor.MaxSpeed, aMotor.AccTime, aMotor.DecTime, 100);
            LTDMC.dmc_line_unit(aMotor.CardNo, crd, 2, AxistList, Target_Pos, mode);

            while (LTDMC.dmc_check_done_multicoor(aMotor.CardNo, crd) == 0)
            {
                Application.DoEvents();
            }
            return true;
        }

        /// <summary>
        /// 直线插补绝对运动
        /// </summary>
        public static bool LineTo(IMotor aMotor, IMotor bMotor, double aDist, double bDist)
        {
            if (!CardList.IsInitSucceed)
                return false;

            ushort[] AxistList = new[] { aMotor.AxisNo, bMotor.AxisNo };
            double[] Target_Pos = new double[] { aDist, bDist };
            ushort mode = 1;
            ushort crd = 0;

            LTDMC.dmc_set_vector_profile_unit(aMotor.CardNo, 0, aMotor.MinSpeed, aMotor.MaxSpeed, aMotor.AccTime, aMotor.DecTime, 100);
            LTDMC.dmc_line_unit(aMotor.CardNo, crd, 2, AxistList, Target_Pos, mode);

            //while (LTDMC.dmc_check_done_multicoor(aMotor.CardNo, crd) == 0)
            //{
            //    Application.DoEvents();
            //}
            return true;
        }

        /// <summary>
        /// 检测插值运动是否停止
        /// </summary>
        public static bool CheckLineDone()
        {
            while (LTDMC.dmc_check_done_multicoor(0,0) == 0)
            {
                Application.DoEvents();
            }

            return (LTDMC.dmc_check_done_multicoor(0, 0) == 1);
        }

        /// <summary>
        /// 等多轴运动停止
        /// </summary>
        public bool WaitLineDone(int outTime)
        {
            return HiTimer.WaitCondition(CheckLineDone, outTime);
        }

        /// <summary>
        /// 紧急停止所有轴
        /// </summary>
        public void EmergentStop()
        {
            LTDMC.dmc_emg_stop(0);
        }

        /// <summary>
        /// 保存
        /// </summary>
        public static void Save()
        {
            Loader.SaveToJson(Devs, ConfigPath);
        }

        /// <summary>
        /// 卡初始化是否成功
        /// </summary>
        public static bool IsInitSucceed { get; set; }

        public static Card GetByKey(int key)
        {
            return Devs.ContainsKey(key) ? Devs[key] : null;
        }

        public static Card GetByIndex(int index)
        {
            return Devs[index];
        }

        public static int Count => Devs.Count();

        /// <summary>
        /// 卡0
        /// </summary>
        public static Card Card0 => Devs[0];

        / <summary>
        / 卡1
        / </summary>
        //public static Card DMC24101 => Devs[1];

        / <summary>
        / 卡2
        / </summary>
        //public static Card DMC24102 => Devs[2];

        / <summary>
        / 卡3
        / </summary>
        //public static Card DMC24103 => Devs[3];

    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133
  • 134
  • 135
  • 136
  • 137
  • 138
  • 139
  • 140
  • 141
  • 142
  • 143
  • 144
  • 145
  • 146
  • 147
  • 148
  • 149
  • 150
  • 151
  • 152
  • 153
  • 154
  • 155
  • 156
  • 157
  • 158
  • 159
  • 160
  • 161
  • 162
  • 163
  • 164
  • 165
  • 166
  • 167
  • 168
  • 169
  • 170
  • 171
  • 172
  • 173
  • 174
  • 175
  • 176
  • 177
  • 178
  • 179
  • 180
  • 181
  • 182
  • 183
  • 184
  • 185
  • 186
  • 187
  • 188
  • 189
  • 190
  • 191
  • 192
  • 193
  • 194
  • 195
  • 196
  • 197
  • 198
  • 199
  • 200
  • 201
  • 202
  • 203
  • 204
  • 205
  • 206
  • 207
  • 208
  • 209
  • 210
  • 211
  • 212
  • 213
  • 214
  • 215
  • 216
  • 217
  • 218
  • 219
  • 220
  • 221
  • 222
  • 223
  • 224
  • 225
  • 226
  • 227
  • 228
  • 229
  • 230
  • 231
  • 232
  • 233
  • 234
  • 235
  • 236
  • 237
  • 238
  • 239
  • 240
  • 241
  • 242
  • 243
  • 244
  • 245
  • 246
  • 247
  • 248
  • 249
  • 250
  • 251
  • 252
  • 253
  • 254
  • 255
  • 256
  • 257
  • 258
  • 259
  • 260
  • 261
  • 262
  • 263
  • 264
  • 265
  • 266
  • 267
  • 268
  • 269
  • 270
  • 271
  • 272
  • 273
  • 274
  • 275
  • 276
  • 277
  • 278
  • 279
  • 280
  • 281
  • 282
  • 283
  • 284

在这里插入图片描述

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/AllinToyou/article/detail/134805
推荐阅读
相关标签
  

闽ICP备14008679号