|
|
|
|
using csLTDMC;
|
|
|
|
|
using GTN;
|
|
|
|
|
using gts;
|
|
|
|
|
using Rs.DataAccess;
|
|
|
|
|
using Rs.Framework;
|
|
|
|
|
using Rs.Motion.Base;
|
|
|
|
|
using Rs.Motion.GugaoEcat;
|
|
|
|
|
using Rs.Motion.GugaoPulse;
|
|
|
|
|
using Rs.Motion.Leisai;
|
|
|
|
|
using Rs.Motion.Ztm;
|
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.ComponentModel.Design;
|
|
|
|
|
using System.Data;
|
|
|
|
|
using System.IO;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Text;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
|
|
|
|
namespace Rs.Motion
|
|
|
|
|
{
|
|
|
|
|
public class IoCardFactory
|
|
|
|
|
{
|
|
|
|
|
public static IIOCard CreateIoCard(string vender,DataRow dr)
|
|
|
|
|
{
|
|
|
|
|
switch (vender)
|
|
|
|
|
{
|
|
|
|
|
case "ztm":
|
|
|
|
|
return ZtmCardManager.Instance.AddIoCard(
|
|
|
|
|
int.Parse(dr["CardID"].ToString()),
|
|
|
|
|
dr["CardName"].ToString(),
|
|
|
|
|
dr["vender"].ToString(),
|
|
|
|
|
false,
|
|
|
|
|
ushort.Parse(dr["InNum"].ToString()),
|
|
|
|
|
ushort.Parse(dr["OutNum"].ToString()),
|
|
|
|
|
int.Parse(dr["SlaveID"].ToString()
|
|
|
|
|
));
|
|
|
|
|
case "GugaoPulseExt":
|
|
|
|
|
return GugaoPulseCardManager.Instance.AddIoCard(
|
|
|
|
|
int.Parse(dr["CardID"].ToString()),
|
|
|
|
|
dr["CardName"].ToString(),
|
|
|
|
|
dr["vender"].ToString(),
|
|
|
|
|
false,
|
|
|
|
|
ushort.Parse(dr["InNum"].ToString()),
|
|
|
|
|
ushort.Parse(dr["OutNum"].ToString()),
|
|
|
|
|
int.Parse(dr["SlaveID"].ToString()
|
|
|
|
|
));
|
|
|
|
|
case "GugaoPulse":
|
|
|
|
|
return GugaoPulseCardManager.Instance.AddIoCard(
|
|
|
|
|
int.Parse(dr["CardID"].ToString()),
|
|
|
|
|
dr["CardName"].ToString(),
|
|
|
|
|
dr["vender"].ToString(),
|
|
|
|
|
false,
|
|
|
|
|
ushort.Parse(dr["InNum"].ToString()),
|
|
|
|
|
ushort.Parse(dr["OutNum"].ToString()),
|
|
|
|
|
int.Parse(dr["SlaveID"].ToString()
|
|
|
|
|
));
|
|
|
|
|
case "GugaoEcat":
|
|
|
|
|
return GugaoCardManager.Instance.AddIoCard(
|
|
|
|
|
int.Parse(dr["CardID"].ToString()),
|
|
|
|
|
dr["CardName"].ToString(),
|
|
|
|
|
dr["vender"].ToString(),
|
|
|
|
|
dr["CardName"].ToString().IndexOf("GLink")>=0?false:true,
|
|
|
|
|
ushort.Parse(dr["InNum"].ToString()),
|
|
|
|
|
ushort.Parse(dr["OutNum"].ToString()),
|
|
|
|
|
int.Parse(dr["SlaveID"].ToString()
|
|
|
|
|
));
|
|
|
|
|
case "LeisaiPulse":
|
|
|
|
|
case "LeisaiPulseEx":
|
|
|
|
|
return LeisaiCardManager.Instance.AddIoCard(
|
|
|
|
|
int.Parse(dr["CardID"].ToString()),
|
|
|
|
|
dr["CardName"].ToString(),
|
|
|
|
|
|
|
|
|
|
dr["vender"].ToString(),
|
|
|
|
|
false,
|
|
|
|
|
ushort.Parse(dr["InNum"].ToString()),
|
|
|
|
|
ushort.Parse(dr["OutNum"].ToString()),
|
|
|
|
|
int.Parse(dr["SlaveID"].ToString()
|
|
|
|
|
));
|
|
|
|
|
|
|
|
|
|
default:return null;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
public class IoManager
|
|
|
|
|
{
|
|
|
|
|
private short apiResult = 0;
|
|
|
|
|
List<IIO> ioList = new List<IIO>();
|
|
|
|
|
Dictionary<string, IIO> ioDic = new Dictionary<string, IIO>();
|
|
|
|
|
private List<IIOCard> ioCards = new List<IIOCard>();
|
|
|
|
|
private IoManager() { }
|
|
|
|
|
|
|
|
|
|
private static IoManager instance;
|
|
|
|
|
public static IoManager Instance
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
if(instance==null)
|
|
|
|
|
instance = new IoManager();
|
|
|
|
|
return instance;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public List<IIOCard> GetCards()
|
|
|
|
|
{
|
|
|
|
|
return ioCards;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public IIOCard GetCard(ushort cardID,string vender="ztm")
|
|
|
|
|
{
|
|
|
|
|
var cards = ioCards.Where(ic => ic.CardID == cardID && ic.Vender.ToLower()==vender);
|
|
|
|
|
if(cards.Count()>0)
|
|
|
|
|
{
|
|
|
|
|
return cards.First();
|
|
|
|
|
}
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void Init()
|
|
|
|
|
{
|
|
|
|
|
IIOCard iIOCard = null;
|
|
|
|
|
SqliteHelper db = new SqliteHelper();
|
|
|
|
|
DataTable dt = db.GetDataTable("select * from io where enable=1 order by sortindex asc");
|
|
|
|
|
if(dt!=null && dt.Rows.Count>0)
|
|
|
|
|
{
|
|
|
|
|
foreach(DataRow dr in dt.Rows)
|
|
|
|
|
{
|
|
|
|
|
iIOCard = IoCardFactory.CreateIoCard(dr["vender"].ToString(), dr);
|
|
|
|
|
if (iIOCard != null)
|
|
|
|
|
{
|
|
|
|
|
Add(iIOCard);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public IIO GetIO(String ioName)
|
|
|
|
|
{
|
|
|
|
|
if (ioDic.ContainsKey(ioName))
|
|
|
|
|
return ioDic[ioName];
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Add(IIOCard card)
|
|
|
|
|
{
|
|
|
|
|
ioCards.Add(card);
|
|
|
|
|
foreach (IIO eCatIO in card.DOut)
|
|
|
|
|
{
|
|
|
|
|
eCatIO.Card = card;
|
|
|
|
|
if (ioDic.ContainsKey(eCatIO.Name))
|
|
|
|
|
continue;
|
|
|
|
|
ioDic.Add(eCatIO.Name, eCatIO);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
foreach (IIO eCatIO in card.DIn)
|
|
|
|
|
{
|
|
|
|
|
eCatIO.Card = card;
|
|
|
|
|
if (ioDic.ContainsKey(eCatIO.Name))
|
|
|
|
|
continue;
|
|
|
|
|
ioDic.Add(eCatIO.Name, eCatIO);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public short ReadIn(string name)
|
|
|
|
|
{
|
|
|
|
|
if(ioDic.ContainsKey(name))
|
|
|
|
|
{
|
|
|
|
|
IIO io = ioDic[name];
|
|
|
|
|
if(io.Card.Vender=="GugaoEcat")
|
|
|
|
|
{
|
|
|
|
|
if(io.Card.IsEtherCat)
|
|
|
|
|
{
|
|
|
|
|
ECatIO eio = (ECatIO)io;
|
|
|
|
|
apiResult = mc_ecat.GTN_EcatIOBitReadInput(eio.Core, eio.SlaveNo, eio.Offset, eio.Index, out byte pValue);
|
|
|
|
|
if (apiResult == 0)
|
|
|
|
|
return pValue;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
ECatIO eio = (ECatIO)io;
|
|
|
|
|
apiResult = mc_ecat.GTN_GetDi(1, mc_ecat.MC_GPI, out Int32 value);
|
|
|
|
|
if (apiResult == 0)
|
|
|
|
|
{
|
|
|
|
|
byte pValue = (byte)(~(value >> eio.Index) & 0x01);
|
|
|
|
|
return pValue;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else if(io.Card.Vender == "GugaoPulse")
|
|
|
|
|
{
|
|
|
|
|
GLinkIO eio = (GLinkIO)io;
|
|
|
|
|
apiResult = mc_pulse.GT_GetDi((short)io.CardID, mc_pulse.MC_GPI, out int value);
|
|
|
|
|
if (apiResult == 0)
|
|
|
|
|
{
|
|
|
|
|
byte pValue = (byte)(~(value >> eio.Index) & 0x01);
|
|
|
|
|
if (eio.IsReverse)
|
|
|
|
|
{
|
|
|
|
|
return (short)((~pValue) & 0x01);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
return (short)pValue;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else if(io.Card.Vender== "GugaoPulseExt")
|
|
|
|
|
{
|
|
|
|
|
GLinkIO eio = (GLinkIO)io;
|
|
|
|
|
apiResult = mc_pulse.GT_GetExtIoValue((short)io.CardID,(short)(io.Card.SlaveID-1), out ushort value);
|
|
|
|
|
if (apiResult == 0)
|
|
|
|
|
{
|
|
|
|
|
byte pValue = (byte)(~(value >> eio.Index) & 0x01);
|
|
|
|
|
if (eio.IsReverse)
|
|
|
|
|
{
|
|
|
|
|
return (short)((~pValue) & 0x01);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
return (short)pValue;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else if (io.Card.Vender.ToLower() == "ztm")
|
|
|
|
|
{
|
|
|
|
|
ZtmIO zio = (ZtmIO)io;
|
|
|
|
|
ushort data = 0;
|
|
|
|
|
apiResult = CSZTM.ZTM.ZT_GetInBit(zio.mc,(short)zio.SlaveNo,(short)zio.Index, ref data);//.GTN_GetDi(1, mc_ecat.MC_GPI, out Int32 value);
|
|
|
|
|
if (apiResult == 0)
|
|
|
|
|
{
|
|
|
|
|
//byte pValue = (byte)(~(value >> eio.Index) & 0x01);
|
|
|
|
|
|
|
|
|
|
if(zio.IsReverse)
|
|
|
|
|
{
|
|
|
|
|
return (short)((~data) & 0x01);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
return (short)data;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else if (io.Card.Vender == "LeisaiPulse")
|
|
|
|
|
{
|
|
|
|
|
LeisaiIO eio = (LeisaiIO)io;
|
|
|
|
|
apiResult = LTDMC.dmc_read_inbit((ushort)io.CardID, eio.Index);
|
|
|
|
|
if (eio.IsReverse)
|
|
|
|
|
{
|
|
|
|
|
return (short)((~apiResult) & 0x01);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
return (short)apiResult;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else if (io.Card.Vender == "LeisaiPulseEx")
|
|
|
|
|
{
|
|
|
|
|
LeisaiIO eio = (LeisaiIO)io;
|
|
|
|
|
ushort value = 0;
|
|
|
|
|
apiResult = LTDMC.nmc_read_inbit((ushort)io.CardID, eio.SlaveNo, eio.Index,ref value);
|
|
|
|
|
if(apiResult==0)
|
|
|
|
|
{
|
|
|
|
|
if (eio.IsReverse)
|
|
|
|
|
{
|
|
|
|
|
return (short)((~value) & 0x01);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
return (short)value;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public short ReadOut(string name)
|
|
|
|
|
{
|
|
|
|
|
if (ioDic.ContainsKey(name))
|
|
|
|
|
{
|
|
|
|
|
IIO io = ioDic[name];
|
|
|
|
|
if (io.Card.Vender == "GugaoEcat")
|
|
|
|
|
{
|
|
|
|
|
if(io.Card.IsEtherCat)
|
|
|
|
|
{
|
|
|
|
|
ECatIO eio = (ECatIO)io;
|
|
|
|
|
apiResult = mc_ecat.GTN_EcatIOBitReadOutput(eio.Core, eio.SlaveNo, eio.Offset, eio.Index, out byte pValue);
|
|
|
|
|
if (apiResult == 0)
|
|
|
|
|
{
|
|
|
|
|
if (io.IsReverse)
|
|
|
|
|
{
|
|
|
|
|
return (short)((~pValue) & 0x01);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
return pValue;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
ECatIO eio = (ECatIO)io;
|
|
|
|
|
apiResult = mc_ecat.GTN_GetDo(1, mc_ecat.MC_GPO, out Int32 value);
|
|
|
|
|
if (apiResult == 0)
|
|
|
|
|
{
|
|
|
|
|
byte pValue = (byte)(~(value >> eio.Index) & 0x01);
|
|
|
|
|
if(io.IsReverse)
|
|
|
|
|
{
|
|
|
|
|
return (short)((~pValue) & 0x01);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
return pValue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else if (io.Card.Vender == "GugaoPulse")
|
|
|
|
|
{
|
|
|
|
|
GLinkIO eio = (GLinkIO)io;
|
|
|
|
|
apiResult = mc_pulse.GT_GetDo(io.CardID, mc_pulse.MC_GPO, out Int32 value);
|
|
|
|
|
if (apiResult == 0)
|
|
|
|
|
{
|
|
|
|
|
byte pValue = (byte)((value >> eio.Index) & 0x01);
|
|
|
|
|
if (io.IsReverse)
|
|
|
|
|
{
|
|
|
|
|
return (short)((~pValue) & 0x01);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
return (short)pValue;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else if (io.Card.Vender == "GugaoPulseExt")
|
|
|
|
|
{
|
|
|
|
|
GLinkIO eio = (GLinkIO)io;
|
|
|
|
|
apiResult = mc_pulse.GT_GetExtDoValue(io.CardID, (short)(io.Card.SlaveID-1), out ushort value);
|
|
|
|
|
if (apiResult == 0)
|
|
|
|
|
{
|
|
|
|
|
byte pValue = (byte)((value >> eio.Index) & 0x01);
|
|
|
|
|
if (io.IsReverse)
|
|
|
|
|
{
|
|
|
|
|
return (short)((~pValue) & 0x01);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
return (short)pValue;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else if (io.Card.Vender.ToLower() == "ztm")
|
|
|
|
|
{
|
|
|
|
|
ZtmIO zio = (ZtmIO)io;
|
|
|
|
|
ushort data = 0;
|
|
|
|
|
apiResult = CSZTM.ZTM.ZT_GetOutBit(zio.mc,(short)zio.SlaveNo,(short)zio.Index,ref data);// mc_ecat.GTN_GetDo(1, mc_ecat.MC_GPO, out Int32 value);
|
|
|
|
|
if (apiResult == 0)
|
|
|
|
|
{
|
|
|
|
|
if (zio.IsReverse)
|
|
|
|
|
{
|
|
|
|
|
return (short)((~data) & 0x01);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
return (short)data;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else if (io.Card.Vender == "LeisaiPulse")
|
|
|
|
|
{
|
|
|
|
|
LeisaiIO eio = (LeisaiIO)io;
|
|
|
|
|
apiResult = LTDMC.dmc_read_outbit((ushort)io.CardID, eio.Index);
|
|
|
|
|
if (eio.IsReverse)
|
|
|
|
|
{
|
|
|
|
|
return (short)((~apiResult) & 0x01);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
return (short)apiResult;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else if (io.Card.Vender == "LeisaiPulseEx")
|
|
|
|
|
{
|
|
|
|
|
LeisaiIO eio = (LeisaiIO)io;
|
|
|
|
|
ushort ioValue = 0;
|
|
|
|
|
apiResult = LTDMC.nmc_read_outbit((ushort)io.CardID,eio.SlaveNo, eio.Index,ref ioValue);
|
|
|
|
|
if(apiResult == 0)
|
|
|
|
|
{
|
|
|
|
|
if (eio.IsReverse)
|
|
|
|
|
{
|
|
|
|
|
return (short)((~ioValue) & 0x01);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
return (short)ioValue;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public short WriteOut(string name,byte value)
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
//mc_ecat.GTN_SetDoBit(1, mc_ecat.MC_GPO, 1, 0);
|
|
|
|
|
|
|
|
|
|
//mc_ecat.GTN_GetDo(1, mc_ecat.MC_GPO, out Int32 aaa);
|
|
|
|
|
|
|
|
|
|
//mc_ecat.GTN_GetDi(1, mc_ecat.MC_GPI, out int ppp);
|
|
|
|
|
|
|
|
|
|
if (ioDic.ContainsKey(name))
|
|
|
|
|
{
|
|
|
|
|
IIO io = ioDic[name];
|
|
|
|
|
if(io.IsReverse)
|
|
|
|
|
{
|
|
|
|
|
value = (byte)(~(value & 0x01) & 0x01);//取反最后一位
|
|
|
|
|
}
|
|
|
|
|
if(io.Card.Vender=="GugaoEcat")
|
|
|
|
|
{
|
|
|
|
|
if(io.Card.IsEtherCat)
|
|
|
|
|
{
|
|
|
|
|
ECatIO eio = (ECatIO)io;
|
|
|
|
|
apiResult = mc_ecat.GTN_EcatIOBitWriteOutput(eio.Core, eio.SlaveNo, eio.Offset, (short)eio.Index, value);
|
|
|
|
|
if (apiResult == 0)
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
ECatIO gio = (ECatIO)io;
|
|
|
|
|
mc_ecat.GTN_SetDoBit(1, mc_ecat.MC_GPO, (short)(gio.Index + 1), value);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else if (io.Card.Vender == "GugaoPulse")
|
|
|
|
|
{
|
|
|
|
|
GLinkIO gio = (GLinkIO)io;
|
|
|
|
|
apiResult = mc_pulse.GT_SetDoBit(gio.CardID, mc_pulse.MC_GPO, (short)(gio.Index + 1), value);
|
|
|
|
|
}
|
|
|
|
|
else if (io.Card.Vender == "GugaoPulseExt")
|
|
|
|
|
{
|
|
|
|
|
GLinkIO gio = (GLinkIO)io;
|
|
|
|
|
apiResult = mc_pulse.GT_SetExtIoBit(gio.CardID,(short)(io.Card.SlaveID-1), (short)(gio.Index), value);
|
|
|
|
|
}
|
|
|
|
|
else if(io.Card.Vender.ToLower()=="ztm")
|
|
|
|
|
{
|
|
|
|
|
ZtmIO ztm = (ZtmIO)io;
|
|
|
|
|
short ret = CSZTM.ZTM.ZT_SetOutBit(ztm.mc,(short) ztm.SlaveNo, (short)ztm.Index, value);
|
|
|
|
|
}
|
|
|
|
|
else if (io.Card.Vender == "LeisaiPulse")
|
|
|
|
|
{
|
|
|
|
|
LeisaiIO gio = (LeisaiIO)io;
|
|
|
|
|
LTDMC.dmc_write_outbit((ushort)gio.CardID, gio.Index, value);
|
|
|
|
|
}
|
|
|
|
|
else if (io.Card.Vender == "LeisaiPulseEx")
|
|
|
|
|
{
|
|
|
|
|
LeisaiIO gio = (LeisaiIO)io;
|
|
|
|
|
LTDMC.nmc_write_outbit((ushort)gio.CardID,gio.SlaveNo, gio.Index, value);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void Add(string name,IIO io)
|
|
|
|
|
{
|
|
|
|
|
if(!ioDic.ContainsKey(name))
|
|
|
|
|
{
|
|
|
|
|
ioDic.Add(name, io);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|