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; using System.Threading.Tasks; namespace Rs.Motion.GugaoPulse { /// /// 固高脉冲卡 /// public class GugaoPulseCardManager : ICardManager { /// /// 执行底层接口的返回值 /// private short apiResult = 0; /// /// 保存一共有多少张卡 /// Dictionary cardDic= new Dictionary(); private GugaoPulseCardManager() { } private static GugaoPulseCardManager instance; public static GugaoPulseCardManager Instance { get { if(instance==null) { instance = new GugaoPulseCardManager(); } return instance; } } public override IIOCard AddIoCard(int cardID, string name, string vender, bool isEthercat, ushort inNum, ushort outNum, int slaveID) { string filename = string.Empty; GLinkIOCard ioCard = new GLinkIOCard(); if(vender== "GugaoPulseExt") { //先打开卡 apiResult = mc_pulse.GT_OpenExtMdl((short)cardID, "gts.dll"); if(apiResult== 0) { mc_pulse.GT_LoadExtConfig((short)cardID, "ExtModule.cfg"); LogHelper.Debug($"打开扩展卡{slaveID-1}成功"); } else { LogHelper.Debug($"打开扩展卡{slaveID - 1}失败"); } filename = $"config/motion/io/gugaoext{cardID}.xml"; } else { filename = $"config/motion/io/gugao{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(filename, ioCard); } else XmlSerializerHelper.Instance.Deserialize(filename, out ioCard); if (ioCard != null) { //ztmIOCard.SetMc(0); //ioCards.Add(ztmIOCard); //Add(ztmIOCard); } return ioCard; } /// /// 同一类卡统一初始化 /// /// public override ErrorCode Init() { SqliteHelper db = new SqliteHelper(); DataTable dt = db.GetDataTable("select * from motioncard where vender='gugao' 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) { //ICard card = new GugaoPulseCard(ushort.Parse(row["cardid"].ToString()), 8, $"gugaopulse{row["cardid"]}.xml"); //card.Init(); //cardDic.Add(ushort.Parse(row["cardid"].ToString()), card); //打开运动控制卡 apiResult = mc_pulse.GT_Open(short.Parse(row["cardid"].ToString()), 0, 0); if (apiResult != 0) { MessageQueue.Instance.Warn($"mc_pulse.GT_Open({short.Parse(row["cardid"].ToString())}, 0, 1);ret={apiResult}"); return ErrorCode.CardInitFail; } else { //加载配置文件 short cardid = short.Parse(row["cardid"].ToString()); if (File.Exists($"gugao{cardid}.cfg")) { apiResult = mc_pulse.GT_LoadConfig(short.Parse(row["cardid"].ToString()), $"gugao{cardid}.cfg"); LogHelper.Debug($"加载固高配置文件gugao{cardid},ret={apiResult}"); } MessageQueue.Instance.Insert($"mc_pulse.GT_Open({short.Parse(row["cardid"].ToString())}, 0, 1);ret={apiResult}"); } //清楚报警和限位 apiResult = mc_pulse.GT_ClrSts(short.Parse(row["cardid"].ToString()), 1, 8); if (apiResult != 0) { return ErrorCode.Fail; } else { LogHelper.Debug($"{apiResult} = mc_pulse.GT_ClrSts({short.Parse(row["cardid"].ToString())}, 1, 8);"); } short value = 32767;// * 5 / 10; apiResult = mc_pulse.GT_SetDac(short.Parse(row["cardid"].ToString()), 11, ref value, 1); MessageQueue.Instance.Insert($"GT_SetDac ret={apiResult}"); } } 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 GugaoPulseCard(cardID, 8, $"gugaopulse{cardID}.xml"); card.Init(); if(!cardDic.ContainsKey((ushort)cardID)) { cardDic.Add((ushort)cardID, card); } return card; //if (cardDic.ContainsKey(cardID)) // return cardDic[cardID]; //return null; } public ErrorCode ComparePulse(short CardId,ushort channel, bool dp = true,short HcmpPulseWidth=100) { short value = (short)channel; if (value != 0) { value = (short)(1 << (channel - 1)); } apiResult = mc_pulse.GT_ComparePulse(CardId, (short)(1 << (channel - 1)), (short)(dp ? 1 : 0), HcmpPulseWidth); if (apiResult != 0) { return ErrorCode.Fail; } return ErrorCode.Ok; } } }