You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
131 lines
3.9 KiB
C#
131 lines
3.9 KiB
C#
using GTN;
|
|
using Rs.Framework;
|
|
using Rs.Motion.Base;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Diagnostics;
|
|
using System.IO;
|
|
using System.Linq;
|
|
using System.Net.NetworkInformation;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace Rs.Motion.GugaoEcat
|
|
{
|
|
public class GugaoCardManager : ICardManager
|
|
{
|
|
/// <summary>
|
|
/// 执行底层接口的返回值
|
|
/// </summary>
|
|
private short apiResult = 0;
|
|
private short core = 1;
|
|
/// <summary>
|
|
/// 保存一共有多少张卡
|
|
/// </summary>
|
|
Dictionary<ushort,ICard> cardDic= new Dictionary<ushort,ICard>();
|
|
private GugaoCardManager() {
|
|
|
|
}
|
|
|
|
private static GugaoCardManager instance;
|
|
public static GugaoCardManager Instance
|
|
{
|
|
get
|
|
{
|
|
if(instance==null)
|
|
{
|
|
instance = new GugaoCardManager();
|
|
}
|
|
|
|
return instance;
|
|
}
|
|
}
|
|
|
|
public override IIOCard AddIoCard(int cardID, string name, string vender, bool isEthercat, ushort inNum, ushort outNum, int slaveID)
|
|
{
|
|
ECatIOCard ioCard = new ECatIOCard();
|
|
string filename = $"config/motion/io/gugao_ecat{slaveID}.xml";
|
|
if (!File.Exists(filename))
|
|
{
|
|
ioCard.CardID = (ushort)cardID;
|
|
ioCard.Name = name;
|
|
ioCard.Vender = vender;
|
|
ioCard.IsEtherCat = isEthercat;
|
|
ioCard.Init((short)cardID,inNum, outNum, (ushort)slaveID);
|
|
bool suc = XmlSerializerHelper.Instance.Serialize<ECatIOCard>(filename, ioCard);
|
|
}
|
|
else
|
|
XmlSerializerHelper.Instance.Deserialize<ECatIOCard>(filename, out ioCard);
|
|
if (ioCard != null)
|
|
{
|
|
//ztmIOCard.SetMc(0);
|
|
//ioCards.Add(ztmIOCard);
|
|
//Add(ztmIOCard);
|
|
}
|
|
return ioCard;
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
/// 同一类卡统一初始化
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
public override ErrorCode Init()
|
|
{
|
|
Stopwatch timer = new Stopwatch();
|
|
if (IsInitialized)
|
|
return ErrorCode.CardAlready;
|
|
short eStatus = 0;
|
|
apiResult = mc_ecat.GTN_Open(mc_ecat.CHANNEL_PCIE, 0);//打开控制卡
|
|
if (apiResult != 0)
|
|
{
|
|
return ErrorCode.CardInitFail;
|
|
}
|
|
apiResult = mc_ecat.GTN_InitEcatComm(core);//初始化EtherCat网络
|
|
if (apiResult != 0)
|
|
{
|
|
return ErrorCode.CardInitFail;
|
|
}
|
|
timer.Restart();
|
|
do
|
|
{
|
|
mc_ecat.GTN_IsEcatReady(core, out eStatus);
|
|
} while (eStatus != 1 && timer.ElapsedMilliseconds<=5000);//等待EtherCAT总线初始化成功
|
|
if(timer.ElapsedMilliseconds>5000)
|
|
{
|
|
timer.Stop();
|
|
return ErrorCode.CardInitFail;
|
|
}
|
|
apiResult = mc_ecat.GTN_StartEcatComm(core);
|
|
if (apiResult != 0)
|
|
{
|
|
return ErrorCode.CardInitFail;
|
|
}
|
|
apiResult = mc_ecat.GTN_ClrSts(core, 1, 32);//清除轴状态
|
|
if (apiResult != 0)
|
|
{
|
|
return ErrorCode.CardInitFail;
|
|
}
|
|
IsInitialized = true;
|
|
return ErrorCode.Ok;
|
|
}
|
|
|
|
public ICard GetCard(ushort cardID)
|
|
{
|
|
if(cardDic.ContainsKey(cardID)) return cardDic[cardID];
|
|
else
|
|
{
|
|
if(cardID==0)
|
|
{
|
|
ICard card = new GugaoCard(cardID, 32, "gugao0.xml");
|
|
card.Init();
|
|
cardDic.Add(cardID, card);
|
|
return card;
|
|
}
|
|
}
|
|
return null;
|
|
}
|
|
|
|
}
|
|
}
|