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

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

文章目录

  • LTDMC.dll下载
  • LTDMC.cs LTDMC.dll C#调用封装下载
  • ICard.cs 运动控制卡接口
  • Card.cs 运动控制卡抽象类
  • CardLTDMC.cs 雷赛运动控制卡EtherCAT总线卡实现类
  • CardList.cs 总线卡列表封装


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

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
    {
        ///   
        /// 卡ID  
        ///  
        int CardID { get; set; }

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

        ///   
        /// 卡名称  
        ///  
        string CardName { get; set; }

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

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

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

        /// 
        /// 初始化
        /// 
        /// 
        int Init();

        /// 
        /// 关闭
        /// 
        /// 
        int UnInit();

        /// 
        /// 软复位
        /// 
        /// 
        int SoftReset();

        /// 
        /// 硬复位
        /// 
        /// 
        int Reset();

        /// 
        /// 获取总线卡错误代码
        /// 
        /// 
        int GetErrCode();

        /// 
        /// 清除总线卡错误代码
        /// 
        /// 
        int ClearErrCode();

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

        /// 
        /// 检测多轴运动是否停止
        /// 
        /// 
        /// 0停止 1未停止
        int CheckMultMoveDone(ushort crd = 0);

        /// 
        /// 等多轴运动停止
        /// 
        /// 等待时间
        /// 
        /// 是否超时
        bool WaitMultMoveDone(int OutTime, ushort crd = 0);
    }
}

Card.cs 运动控制卡抽象类

using System.Collections.Generic;

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

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

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

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

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

        ///   
        /// 是否使用  
        ///  
        public bool IsUsed { get; set; } = true;
        ///   
        /// 是否初始化完成  
        ///  
        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


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

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

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


        #endregion
    }
}

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)
        {

        }
        /// 
        /// 初始化电机卡
        /// 
        /// 返回卡数
        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;
        }

        /// 
        /// 关闭电机卡
        /// 
        /// 
        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;
        }

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




    }
}

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;

        /// 
        /// 初始化文件
        /// 
        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();
        }

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

        }

        /// 
        /// 初始化电机卡
        /// 
        /// 返回卡数
        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);
            }

        }

        /// 
        /// 硬复位
        /// 
        /// 
        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;
            }
        }

        /// 
        /// 软复位
        /// 
        /// 
        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;
            }
        }

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

        /// 
        /// 直线插补相对运动
        /// 
        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;
        }

        /// 
        /// 直线插补绝对运动
        /// 
        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;
        }

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

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

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

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

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

        /// 
        /// 卡初始化是否成功
        /// 
        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();

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

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

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

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

    }
}

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

你可能感兴趣的:(小白上位机开发100例,自动化,c#,上位机,运动控制)