|
|
|
|
using CSZTM;
|
|
|
|
|
using GTN;
|
|
|
|
|
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.GugaoEcat
|
|
|
|
|
{
|
|
|
|
|
public class ZtmCardManager : ICardManager
|
|
|
|
|
{
|
|
|
|
|
Dictionary<short, short> mcDic = new Dictionary<short, short>();
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 执行底层接口的返回值
|
|
|
|
|
/// </summary>
|
|
|
|
|
private short apiResult = 0;
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 保存一共有多少张卡
|
|
|
|
|
/// </summary>
|
|
|
|
|
Dictionary<ushort,ICard> cardDic= new Dictionary<ushort,ICard>();
|
|
|
|
|
private ZtmCardManager() {
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static ZtmCardManager instance;
|
|
|
|
|
public static ZtmCardManager Instance
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
if(instance==null)
|
|
|
|
|
{
|
|
|
|
|
instance = new ZtmCardManager();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return instance;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 添加一张IO卡
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
public override IIOCard AddIoCard(int cardID,string name,string vender,bool isEthercat,ushort inNum,ushort outNum,int slaveID)
|
|
|
|
|
{
|
|
|
|
|
ZtmIOCard ioCard = new ZtmIOCard();
|
|
|
|
|
string filename = $"config/motion/io/ztm{cardID}.xml";
|
|
|
|
|
if (!File.Exists(filename))
|
|
|
|
|
{
|
|
|
|
|
ioCard.CardID = (ushort)cardID;
|
|
|
|
|
//ioCard.MC = mcDic[(short)cardID];
|
|
|
|
|
ioCard.Name = name;
|
|
|
|
|
ioCard.Vender = vender;
|
|
|
|
|
ioCard.IsEtherCat = isEthercat;
|
|
|
|
|
ioCard.Init((short)cardID, inNum, outNum, (ushort)slaveID);
|
|
|
|
|
bool suc = XmlSerializerHelper.Instance.Serialize<ZtmIOCard>(filename, ioCard);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
XmlSerializerHelper.Instance.Deserialize<ZtmIOCard>(filename, out ioCard);
|
|
|
|
|
if (mcDic.ContainsKey((short)cardID))
|
|
|
|
|
{
|
|
|
|
|
if (ioCard != null)
|
|
|
|
|
{
|
|
|
|
|
ioCard.SetMc(mcDic[(short)cardID]);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return ioCard;
|
|
|
|
|
}
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 同一类卡统一初始化
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
public override ErrorCode Init()
|
|
|
|
|
{
|
|
|
|
|
int connectCount = 1;
|
|
|
|
|
SqliteHelper db = new SqliteHelper();
|
|
|
|
|
DataTable dt = db.GetDataTable("select * from motioncard where vender='ztm' and enable=1 order by id asc");
|
|
|
|
|
if(dt!=null&&dt.Rows.Count>0)
|
|
|
|
|
{
|
|
|
|
|
foreach(DataRow dr in dt.Rows)
|
|
|
|
|
{
|
|
|
|
|
connectCount = 1;
|
|
|
|
|
short mc = -1;
|
|
|
|
|
string ip = dr["ipaddr"].ToString();
|
|
|
|
|
while(true && connectCount<=1)
|
|
|
|
|
{
|
|
|
|
|
apiResult = ZTM.ZT_Open(ip.ToCharArray(), ref mc, 2000);
|
|
|
|
|
if (apiResult != 0)
|
|
|
|
|
{
|
|
|
|
|
connectCount++;
|
|
|
|
|
LogHelper.MoveLog($"rs motion card load fail ip={ip},ret={apiResult}",null);
|
|
|
|
|
MessageQueue.Instance.Warn($"rs motion card load fail ip={ip},ret={apiResult}");
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
//apiResult = ZTM.ZT_GetConnectionStatus(ip.ToCharArray(), ref mc);
|
|
|
|
|
//if (1 != apiResult)
|
|
|
|
|
//{
|
|
|
|
|
// return ErrorCode.CardInitFail;
|
|
|
|
|
//}
|
|
|
|
|
LogHelper.MoveLog($"rs motion card load success ip={ip},mc={mc}");
|
|
|
|
|
MessageQueue.Instance.Insert($"rs motion card load success ip={ip},mc={mc}");
|
|
|
|
|
mcDic.Add(short.Parse(dr["cardid"].ToString()), mc);
|
|
|
|
|
Thread.Sleep(100);
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
IsInitialized = true;
|
|
|
|
|
LogHelper.Debug("rs motion card init ok");
|
|
|
|
|
return ErrorCode.Ok;
|
|
|
|
|
}
|
|
|
|
|
public ICard GetCard(short cardID)
|
|
|
|
|
{
|
|
|
|
|
ICard card;
|
|
|
|
|
if(cardDic.ContainsKey((ushort)cardID)) return cardDic[(ushort)cardID];
|
|
|
|
|
if(mcDic.ContainsKey(cardID))
|
|
|
|
|
{
|
|
|
|
|
card = new ZtmCard((ushort)cardID, (ushort)mcDic[cardID], 10, $"ztm{cardID}.xml");
|
|
|
|
|
LogHelper.Debug($"cardid={cardID} mc={mcDic[cardID]} ");
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
card = new ZtmCard((ushort)cardID, 0, 10, $"ztm{cardID}.xml");
|
|
|
|
|
LogHelper.Debug($"cardid={cardID} ");
|
|
|
|
|
}
|
|
|
|
|
card.Init();
|
|
|
|
|
cardDic.Add((ushort)cardID, card);
|
|
|
|
|
return card;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|