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.

152 lines
5.1 KiB
C#

using GTN;
using gts;
using Rs.DataAccess;
using Rs.Framework;
using Rs.Motion.Base;
using Rs.Motion.Ztm;
using System;
using System.Collections.Generic;
using System.Data;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Net.NetworkInformation;
using System.Text;
using System.Threading.Tasks;
using csLTDMC;
namespace Rs.Motion.Leisai
{
/// <summary>
/// 蕾塞脉冲卡
/// </summary>
public class LeisaiCardManager : ICardManager
{
/// <summary>
/// 执行底层接口的返回值
/// </summary>
private short apiResult = 0;
/// <summary>
/// 保存一共有多少张卡
/// </summary>
Dictionary<ushort,ICard> cardDic= new Dictionary<ushort,ICard>();
private LeisaiCardManager() {
}
private static LeisaiCardManager instance;
public static LeisaiCardManager Instance
{
get
{
if(instance==null)
{
instance = new LeisaiCardManager();
}
return instance;
}
}
public override IIOCard AddIoCard(int cardID, string name, string vender, bool isEthercat, ushort inNum, ushort outNum, int slaveID)
{
string filename = string.Empty;
LeisaiIOCard ioCard = new LeisaiIOCard();
if(vender== "LeisaiPulseEx")
{
//先打开卡
apiResult = LTDMC.nmc_set_connect_state((ushort)cardID, (ushort)slaveID, 1, 0);
if(apiResult== 0)
{
LogHelper.Debug($"打开扩展卡{slaveID-1}成功");
}
else
{
LogHelper.Debug($"打开扩展卡{slaveID - 1}失败");
}
filename = $"config/motion/io/LeisaiPulseEx{cardID}.xml";
}
else
{
filename = $"config/motion/io/leisai{cardID}.xml";
}
//string filename = $"config/motion/io/gugao{cardID}.xml";
if (!File.Exists(filename))
{
ioCard.CardID = (ushort)cardID;
ioCard.Name = name;
ioCard.Vender = vender;
ioCard.IsEtherCat = isEthercat;
ioCard.SlaveID = (ushort)slaveID;
ioCard.Init((short)cardID,inNum, outNum, (ushort)slaveID);
bool suc = XmlSerializerHelper.Instance.Serialize<LeisaiIOCard>(filename, ioCard);
}
else
XmlSerializerHelper.Instance.Deserialize<LeisaiIOCard>(filename, out ioCard);
if (ioCard != null)
{
//ztmIOCard.SetMc(0);
//ioCards.Add(ztmIOCard);
//Add(ztmIOCard);
}
return ioCard;
}
/// <summary>
/// 同一类卡统一初始化
/// </summary>
/// <returns></returns>
public override ErrorCode Init()
{
SqliteHelper db = new SqliteHelper();
DataTable dt = db.GetDataTable("select * from motioncard where vender='leisai' and enable=1 order by id asc");
if(dt!=null&&dt.Rows.Count>0)
{
MessageQueue.Instance.Insert($"have {dt.Rows.Count} card need init");
foreach (DataRow row in dt.Rows)
{
ushort cardid = ushort.Parse(row["cardid"].ToString());
//打开运动控制卡
apiResult = LTDMC.dmc_board_init_onecard(cardid);
if (apiResult <= 0)
{
MessageQueue.Instance.Warn($"{apiResult} = LTDMC.dmc_board_init_onecard({cardid});");
return ErrorCode.CardInitFail;
}
else
{
//加载配置文件
if(File.Exists($"leisai{cardid}.ini"))
{
apiResult = LTDMC.dmc_download_configfile(ushort.Parse(row["cardid"].ToString()), $"leisai{cardid}.ini");
LogHelper.Debug($"加载蕾塞配置文件leisai{cardid},ret={apiResult}");
}
MessageQueue.Instance.Insert($"{apiResult} = LTDMC.dmc_download_configfile({ushort.Parse(row["cardid"].ToString())}, $\"leisai{{cardid}}.ini\");");
}
}
}
else
{
return ErrorCode.CardNotExist;
}
IsInitialized = true;
return ErrorCode.Ok;
}
public ICard GetCard(ushort cardID)
{
ICard card;
if (cardDic.ContainsKey((ushort)cardID)) return cardDic[(ushort)cardID];
card = new LeisaiCard(cardID, 8, $"leisai{cardID}.xml");
card.Init();
if(!cardDic.ContainsKey((ushort)cardID))
{
cardDic.Add((ushort)cardID, card);
}
return card;
}
}
}