| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310 |
- 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();
- /// <summary>
- /// 电机返回原点
- /// </summary>
- /// <param name="typeName"></param>
- /// <returns></returns>
- 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;
- }
- }
- /// <summary>
- /// 温度读取
- /// </summary>
- /// <param name="typeName"></param>
- /// <returns></returns>
- public static Dictionary<string, double> HeatRead(this string typeName)
- {
- return PortProducerFactory.Factory(WriteWay.Read).SetName(typeName).SetType(ArgType.HeatRead).Execute() as Dictionary<string, double>;
- }
- /// <summary>
- /// 电机读取
- /// </summary>
- /// <param name="typeName"></param>
- /// <param name="register"></param>
- /// <returns></returns>
- public static char[] MotorRead(this string typeName)
- {
- lock (obj_locker)
- {
- return PortProducerFactory.Factory(WriteWay.Read).SetName(typeName).SetType(ArgType.Read).Execute() as char[];
- }
- }
- /// <summary>
- /// 液体电机移动
- /// </summary>
- /// <param name="name"></param>
- /// <param name="value"></param>
- /// <returns></returns>
- 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;
- }
- /// <summary>
- /// 运动电机的移动
- /// </summary>
- /// <param name="name"></param>
- /// <param name="value"></param>
- /// <returns></returns>
- 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;
- }
- /// <summary>
- /// 打开寄存器
- /// </summary>
- /// <param name="name"></param>
- /// <returns></returns>
- 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;
- }
- }
- /// <summary>
- /// 关闭寄存器
- /// </summary>
- /// <param name="name"></param>
- /// <returns></returns>
- 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;
- }
- }
- /// <summary>
- /// 电机停止
- /// </summary>
- /// <param name="name"></param>
- /// <returns></returns>
- public static bool MotorStop(this string name)
- {
- return (bool)PortProducerFactory.Factory(WriteWay.Write).SetName(name).SetType(ArgType.Stop).Execute();
- }
- /// <summary>
- /// 速度写入
- /// </summary>
- /// <param name="name"></param>
- /// <param name="value"></param>
- /// <returns></returns>
- public static bool WriteSpeed(this string name, object value)
- {
- return (bool)PortProducerFactory.Factory(WriteWay.Write).SetName(name).SetType(ArgType.Speed).SetValue(value).Execute();
- }
- /// <summary>
- /// 写入加速度
- /// </summary>
- /// <param name="name"></param>
- /// <param name="value"></param>
- /// <returns></returns>
- public static bool WriteAcSpeed(this string name, object value)
- {
- return (bool)PortProducerFactory.Factory(WriteWay.Write).SetName(name).SetType(ArgType.AcSpeed).SetValue(value).Execute();
- }
- /// <summary>
- /// 写入减速度
- /// </summary>
- /// <param name="name"></param>
- /// <param name="value"></param>
- /// <returns></returns>
- public static bool WriteDeSpeed(this string name, object value)
- {
- return (bool)PortProducerFactory.Factory(WriteWay.Write).SetName(name).SetType(ArgType.DeSpeed).SetValue(value).Execute();
- }
- /// <summary>
- /// 写入温度
- /// </summary>
- /// <param name="name"></param>
- /// <param name="value"></param>
- /// <returns></returns>
- public static bool WriteTemperature(this string name, object value)
- {
- return (bool)PortProducerFactory.Factory(WriteWay.Write).SetName(name).SetType(ArgType.Temp).SetValue(value).Execute();
- }
- /// <summary>
- /// 写入比例
- /// </summary>
- /// <param name="name"></param>
- /// <param name="value"></param>
- /// <returns></returns>
- public static bool WriteRatio(this string name, object value)
- {
- return (bool)PortProducerFactory.Factory(WriteWay.Write).SetName(name).SetType(ArgType.Ratio).SetValue(value).Execute();
- }
- /// <summary>
- /// 写入积分
- /// </summary>
- /// <param name="name"></param>
- /// <param name="value"></param>
- /// <returns></returns>
- public static bool WriteIntegral(this string name, object value)
- {
- return (bool)PortProducerFactory.Factory(WriteWay.Write).SetName(name).SetType(ArgType.Integral).SetValue(value).Execute();
- }
- /// <summary>
- /// 写入微分
- /// </summary>
- /// <param name="name"></param>
- /// <param name="value"></param>
- /// <returns></returns>
- public static bool WriteDifferential(this string name, object value)
- {
- return (bool)PortProducerFactory.Factory(WriteWay.Write).SetName(name).SetType(ArgType.Differential).SetValue(value).Execute();
- }
- /// <summary>
- /// 写入积分默认值
- /// </summary>
- /// <param name="name"></param>
- /// <param name="value"></param>
- /// <returns></returns>
- public static bool WriteIntegralDeafult(this string name, object value)
- {
- return (bool)PortProducerFactory.Factory(WriteWay.Write).SetName(name).SetType(ArgType.IntegralDeafult).SetValue(value).Execute();
- }
- /// <summary>
- /// 写入Pid
- /// </summary>
- /// <param name="name"></param>
- /// <returns></returns>
- 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();
- }
- }
- }
|