using Community;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Model.MotionControl
{
///
/// 运动控制卡
///
public class Card : NotificationObject
{
#region 控制卡信息
///
/// ID
///
public int ID { get; set; }
private ushort cardID;
///
/// 控制卡ID
///
public ushort CardID
{
get { return cardID; }
set
{
cardID = value;
this.RaisePropertyChanged("CardID");
}
}
private string cardName;
///
/// 卡名称
///
public string CardName
{
get { return cardName; }
set
{
cardName = value;
this.RaisePropertyChanged("CardName");
}
}
private ushort interfaceType;
///
/// 接口类型 链接类型:1-串口,2-网口
///
public ushort InterfaceType
{
get { return interfaceType; }
set
{
interfaceType = value;
this.RaisePropertyChanged("InterfaceType");
}
}
private string pconnectString;
///
/// 连接字符 对应与控制器的IP 地址或相应的COM 口
///
public string PconnectString
{
get { return pconnectString; }
set
{
pconnectString = value;
this.RaisePropertyChanged("PconnectString");
}
}
private uint baud;
///
/// 波特率
///
public uint Baud
{
get { return baud; }
set
{
baud = value;
this.RaisePropertyChanged("Baud");
}
}
private uint totalAxis;
///
/// 控制卡的轴数
///
public uint TotalAxis
{
get { return totalAxis; }
set
{
totalAxis = value;
this.RaisePropertyChanged("TotalAxis");
}
}
private string notes;
///
/// 备注
///
public string Notes
{
get { return notes; }
set
{
notes = value;
this.RaisePropertyChanged("Notes");
}
}
#endregion
#region Io输入信号
private ObservableCollection ioInput;
///
/// Io输入信号
///
public ObservableCollection IoInput
{
get { return ioInput; }
set
{
ioInput = value;
this.RaisePropertyChanged("IoInput");
}
}
#endregion
#region Io输出信号
private ObservableCollection ioOutput;
///
/// Io输出信号
///
public ObservableCollection IoOutput
{
get { return ioOutput; }
set
{
ioOutput = value;
this.RaisePropertyChanged("IoOutput");
}
}
#endregion
#region 轴集合
private ObservableCollection shaftList;
///
/// 轴集合
///
public ObservableCollection ShaftList
{
get { return shaftList; }
set
{
shaftList = value;
if (value != null && value.Count > 0)
{
foreach (Shaft sf in value)
{
if (InterfaceType > 1)
{
sf.LanControl = true;
sf.Initialize();
}
}
}
this.RaisePropertyChanged("ShaftList");
}
}
#endregion
public Card()
{
Baud = 115200;
Notes = "-";
}
///
/// 返回模拟量
///
///
///
///
///
///
///
///
public string GetAD(ushort card, ushort chan, double aVal_Low, double aVal_High, double dVal_Low, double dVal_High)
{
//实际输出模拟量-最低输出模拟值底数=实际模拟输出
double adVal = LTSMC.smc_get_ain(card, chan) - aVal_Low;
//最大模拟上限-最小模拟下限=有效模拟区间
double atemp = aVal_High - aVal_Low;
//实际模拟输出/有效模拟区间=模拟输出指数坐标比例
double vtemp = adVal / atemp;
//实模拟输出指数坐标比例*(最大数字上限-最小数字下限)=实际输出数字值
double dTemp = vtemp * (dVal_High - dVal_Low);
return dTemp.ToString("f3");
}
public double Get_ain(ushort channel)
{
//实际输出模拟量-最低输出模拟值底数=实际模拟输出
return LTSMC.smc_get_ain(this.CardID, channel);
}
#region 获取轴数量
///
/// 获取轴数量
///
public bool GetCradInfo()
{
try
{
//获取卡轴数
if (InterfaceType == 0)
{
uint axisNumber = 0;
LTDMC.dmc_get_total_axes(this.CardID, ref axisNumber);
this.TotalAxis = axisNumber;
}
else
{
uint a = 0;
LTSMC.smc_get_total_axes(CardID, ref a);
this.TotalAxis = a;
}
if (this.TotalAxis < 1 || TotalAxis > 8)
{
return false;
}
else
{
return true;
}
}
catch (Exception)
{
return false;
}
}
#endregion
#region 连接控制器
///
/// 连接控制器
///
///
public bool Connect()
{
try
{
if (InterfaceType == 0)
{
int num = LTDMC.dmc_board_init(); //初始化控制卡
if (num < 1 || num > 8)
{
CardID = 100;
return true;
}
else
{
CardID = 0;
return false;
}
}
else
{
short a = LTSMC.smc_board_init(CardID, this.InterfaceType, this.PconnectString, this.Baud);
if (a == 0)
{
if (ShaftList != null)
{
foreach (Shaft sf in ShaftList)
{
sf.Initialize();
}
}
return true;
}
else
{
return false;
}
}
}
catch (Exception)
{
return false;
}
}
#endregion
#region 关闭控制器
///
/// 关闭控制器
///
public void close()
{
if (InterfaceType == 0)
{
return;
}
LTSMC.smc_board_close(CardID);
}
#endregion
#region 所有轴紧急停止
///
/// 所有轴紧急停止
///
/// 卡ID
public void MoveFunction_Estop()
{
LTSMC.smc_emg_stop(this.CardID);
}
#endregion
}
}