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

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

文章目录

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

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 运动控制卡接口

csharp 复制代码
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);
    }
}

Card.cs 运动控制卡抽象类

csharp 复制代码
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
    }
}

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

csharp 复制代码
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;
        }




    }
}

CardList.cs 总线卡列表封装

csharp 复制代码
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];

    }
}
相关推荐
离歌漠35 分钟前
C#调用C++ DLL方法之C++/CLI(托管C++)
c++·c#·clr
sukalot42 分钟前
windows C#-异步返回类型(下)
windows·c#
飞人博尔特的摄影师1 小时前
WPF绑定Bind方法合集,实时更新
visualstudio·c#·wpf·xaml·maui·xamarin·技巧
菜鸟小贤贤2 小时前
mac安装Pytest、Allure、brew
python·macos·自动化·pytest
冷眼Σ(-᷅_-᷄๑)3 小时前
WPF异步UI交互功能的实现方法
c#·wpf
CV大法好7 小时前
刘铁猛C#入门 027 抽象和开闭原则
开发语言·c#
离歌漠8 小时前
C#调用C++ DLL方法之P/Invoke
c++·c#·p/invoke
公子小六9 小时前
在WPF程序中实现PropertyGrid功能
windows·microsoft·c#·.net·wpf
dangoxiba9 小时前
[Unity Demo]从零开始制作空洞骑士Hollow Knight第二十集:制作专门渲染HUD的相机HUD Camera和画布HUD Canvas
游戏·unity·c#·游戏引擎·playmaker