using SHJX.Service.Control.Extends;
using SHJX.Service.Control.Common.Assets;
namespace SHJX.Service.Control.Execute
{
public static class PortExecute
{
private static readonly ILogger logger = LogFactory.BuildLogger(typeof(PortExecute));
//public static bool AxisZdownOrUp = false;//z轴上下状态
private static readonly object obj_locker = new();
///
/// 电机返回原点
///
///
///
public static bool MotorGoBack(this string typeName)
{
int register = ConfigInstance.GetPortRegister(typeName);
bool res;
res = WriteGobackData();
double executedTime = 0;
do
{
/* Mark:
* 暂时只针对Z轴使用
*/
if (typeName.Equals(EquipmentNames.AxisZ) && executedTime > 15)
{
logger.LogInformation($"{EquipmentNames.AxisZ}在{executedTime}秒后还未返回原点,重新写入返回原点数据!");
WriteGobackData();
executedTime = 0;
}
char[] judgeRes = typeName.MotorRead();
if (judgeRes is null or { Length: 0 })
{
continue;
}
res = judgeRes[register -1].Equals('0');
Thread.Sleep(200);
executedTime += 0.2;
} while (!res);
return res;
bool WriteGobackData()
{
bool gobackRes;
int defeatCount = 0;
do
{
gobackRes = (bool)PortProducerFactory.Factory(WriteWay.Write).SetName(typeName).SetType(ArgType.GoBack).SetValue(register).Execute();
if (!gobackRes)
{
defeatCount++;
logger.LogInformation($"第{defeatCount}失败,等待1s后重试!");
Thread.Sleep(1000);
}
} while (!gobackRes);
return gobackRes;
}
}
///
/// 温度读取
///
///
///
public static Dictionary HeatRead(this string typeName)
{
return PortProducerFactory.Factory(WriteWay.Read).SetName(typeName).SetType(ArgType.HeatRead).Execute() as Dictionary;
}
///
/// 电机读取
///
///
///
///
public static char[] MotorRead(this string typeName)
{
lock (obj_locker)
{
return PortProducerFactory.Factory(WriteWay.Read).SetName(typeName).SetType(ArgType.Read).Execute() as char[];
}
}
///
/// 液体电机移动
///
///
///
///
public static bool LiquidMotorMove(this string name, object value)
{
bool res;
int defeatCount = 0;
do
{
res = (bool)PortProducerFactory.Factory(WriteWay.Write).SetName(name).SetType(ArgType.Move).SetValue(value).Execute();
if (res)
{
Thread.Sleep(Math.Abs(Convert.ToInt32(value) / 20000 * SpeedSleep.Calculation(name)));
}
else
{
defeatCount++;
logger.LogInformation($"第{defeatCount}失败,等待3s后重试!");
Thread.Sleep(1000);
}
} while (!res);
return res;
}
///
/// 运动电机的移动
///
///
///
///
public static bool MotorMove(this string name, object value)
{
bool res;
int defeatCount = 0;
do
{
res = (bool)PortProducerFactory.Factory(WriteWay.Write).SetName(name).SetType(ArgType.Move).SetValue(value).Execute();
if (res)
{
break;
}
else
{
defeatCount++;
logger.LogInformation($"第{defeatCount}失败,等待3s后重试!");
Thread.Sleep(3000);
}
} while (!res);
return res;
}
///
/// 打开寄存器
///
///
///
public static bool RegisterOpen(this string registerName)
{
lock (obj_locker)
{
int register = ConfigInstance.GetPortRegister(registerName);
int defeatCount = 0;
bool res;
do
{
res = (bool)PortProducerFactory.Factory(WriteWay.Write).SetName(registerName).SetType(ArgType.Open).SetValue(register).Execute();
logger.LogDebug($"写寄存器{registerName}" + "打开****************" + register.ToString());
if (!res)
{
defeatCount++;
logger.LogDebug($"第{defeatCount}失败,等待1s后重试!");
Thread.Sleep(1000);
}
} while (!res);
return res;
}
}
///
/// 关闭寄存器
///
///
///
public static bool RegisterClose(this string name)
{
lock (obj_locker)
{
int register = ConfigInstance.GetPortRegister(name);
bool res;
int defeatCount = 0;
do
{
res = (bool)PortProducerFactory.Factory(WriteWay.Write).SetName(name).SetType(ArgType.Close).SetValue(register).Execute();
logger.LogDebug($"写寄存器{name}" + "关闭*****************" + register.ToString());
if (!res)
{
defeatCount++;
logger.LogDebug($"第{defeatCount}失败,等待1s后重试!");
Thread.Sleep(1000);
}
} while (!res);
return res;
}
}
///
/// 电机停止
///
///
///
public static bool MotorStop(this string name)
{
return (bool)PortProducerFactory.Factory(WriteWay.Write).SetName(name).SetType(ArgType.Stop).Execute();
}
///
/// 速度写入
///
///
///
///
public static bool WriteSpeed(this string name, object value)
{
return (bool)PortProducerFactory.Factory(WriteWay.Write).SetName(name).SetType(ArgType.Speed).SetValue(value).Execute();
}
///
/// 写入加速度
///
///
///
///
public static bool WriteAcSpeed(this string name, object value)
{
return (bool)PortProducerFactory.Factory(WriteWay.Write).SetName(name).SetType(ArgType.AcSpeed).SetValue(value).Execute();
}
///
/// 写入减速度
///
///
///
///
public static bool WriteDeSpeed(this string name, object value)
{
return (bool)PortProducerFactory.Factory(WriteWay.Write).SetName(name).SetType(ArgType.DeSpeed).SetValue(value).Execute();
}
///
/// 写入温度
///
///
///
///
public static bool WriteTemperature(this string name, object value)
{
return (bool)PortProducerFactory.Factory(WriteWay.Write).SetName(name).SetType(ArgType.Temp).SetValue(value).Execute();
}
///
/// 写入比例
///
///
///
///
public static bool WriteRatio(this string name, object value)
{
return (bool)PortProducerFactory.Factory(WriteWay.Write).SetName(name).SetType(ArgType.Ratio).SetValue(value).Execute();
}
///
/// 写入积分
///
///
///
///
public static bool WriteIntegral(this string name, object value)
{
return (bool)PortProducerFactory.Factory(WriteWay.Write).SetName(name).SetType(ArgType.Integral).SetValue(value).Execute();
}
///
/// 写入微分
///
///
///
///
public static bool WriteDifferential(this string name, object value)
{
return (bool)PortProducerFactory.Factory(WriteWay.Write).SetName(name).SetType(ArgType.Differential).SetValue(value).Execute();
}
///
/// 写入积分默认值
///
///
///
///
public static bool WriteIntegralDeafult(this string name, object value)
{
return (bool)PortProducerFactory.Factory(WriteWay.Write).SetName(name).SetType(ArgType.IntegralDeafult).SetValue(value).Execute();
}
///
/// 写入Pid
///
///
///
public static bool WritePid(this string name)
{
PID pid = DataManagerInstance.GetPid();
return (bool)PortProducerFactory.Factory(WriteWay.Write).SetName(name).SetType(ArgType.Pid).SetValue(pid).Execute();
}
public static ushort[] HumitureRead()
{
return (ushort[])PortProducerFactory.Factory(WriteWay.Read).SetType(ArgType.Humiture).Execute();
}
}
}