TFLH/Model/MotionControl/Card.cs
2026-08-06 09:50:46 +08:00

346 lines
8.8 KiB
C#
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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
{
/// <summary>
/// 运动控制卡
/// </summary>
public class Card : NotificationObject
{
#region 控制卡信息
/// <summary>
/// ID
/// </summary>
public int ID { get; set; }
private ushort cardID;
/// <summary>
/// 控制卡ID
/// </summary>
public ushort CardID
{
get { return cardID; }
set
{
cardID = value;
this.RaisePropertyChanged("CardID");
}
}
private string cardName;
/// <summary>
/// 卡名称
/// </summary>
public string CardName
{
get { return cardName; }
set
{
cardName = value;
this.RaisePropertyChanged("CardName");
}
}
private ushort interfaceType;
/// <summary>
/// 接口类型 链接类型:1-串口,2-网口
/// </summary>
public ushort InterfaceType
{
get { return interfaceType; }
set
{
interfaceType = value;
this.RaisePropertyChanged("InterfaceType");
}
}
private string pconnectString;
/// <summary>
/// 连接字符 对应与控制器的IP 地址或相应的COM 口
/// </summary>
public string PconnectString
{
get { return pconnectString; }
set
{
pconnectString = value;
this.RaisePropertyChanged("PconnectString");
}
}
private uint baud;
/// <summary>
/// 波特率
/// </summary>
public uint Baud
{
get { return baud; }
set
{
baud = value;
this.RaisePropertyChanged("Baud");
}
}
private uint totalAxis;
/// <summary>
/// 控制卡的轴数
/// </summary>
public uint TotalAxis
{
get { return totalAxis; }
set
{
totalAxis = value;
this.RaisePropertyChanged("TotalAxis");
}
}
private string notes;
/// <summary>
/// 备注
/// </summary>
public string Notes
{
get { return notes; }
set
{
notes = value;
this.RaisePropertyChanged("Notes");
}
}
#endregion
#region Io输入信号
private ObservableCollection<InputSignal> ioInput;
/// <summary>
/// Io输入信号
/// </summary>
public ObservableCollection<InputSignal> IoInput
{
get { return ioInput; }
set
{
ioInput = value;
this.RaisePropertyChanged("IoInput");
}
}
#endregion
#region Io输出信号
private ObservableCollection<OutputSignal> ioOutput;
/// <summary>
/// Io输出信号
/// </summary>
public ObservableCollection<OutputSignal> IoOutput
{
get { return ioOutput; }
set
{
ioOutput = value;
this.RaisePropertyChanged("IoOutput");
}
}
#endregion
#region 轴集合
private ObservableCollection<Shaft> shaftList;
/// <summary>
/// 轴集合
/// </summary>
public ObservableCollection<Shaft> 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 = "-";
}
/// <summary>
/// 返回模拟量
/// </summary>
/// <param name="card"></param>
/// <param name="chan"></param>
/// <param name="aVal_Low"></param>
/// <param name="aVal_High"></param>
/// <param name="dVal_Low"></param>
/// <param name="dVal_High"></param>
/// <returns></returns>
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 获取轴数量
/// <summary>
/// 获取轴数量
/// </summary>
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 连接控制器
/// <summary>
/// 连接控制器
/// </summary>
/// <returns></returns>
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 关闭控制器
/// <summary>
/// 关闭控制器
/// </summary>
public void close()
{
if (InterfaceType == 0)
{
return;
}
LTSMC.smc_board_close(CardID);
}
#endregion
#region 所有轴紧急停止
/// <summary>
/// 所有轴紧急停止
/// </summary>
/// <param name="cardid">卡ID</param>
public void MoveFunction_Estop()
{
LTSMC.smc_emg_stop(this.CardID);
}
#endregion
}
}