using System; using System.Linq; using System.Threading; using SHJX.Service.Common.ReadXML; using SHJX.Service.ServerClient.RS485; using SHJX.Service.Common.Logging; using Microsoft.Extensions.Logging; namespace SHJX.Service.ServerClient.TempController { public abstract class SerialPortImp { private readonly ReadConfigUtil _config; private static readonly ILogger logger = LogFactory.BuildLogger(typeof(SerialPortImp)); protected SerialPortImp( ReadConfigUtil config) { _config = config; } /// /// 数据下发 /// /// /// /// protected virtual bool SendFormat(string value, object port) { if (!Rs485Client.IsValid) return false; var res = Rs485Client.SendData(value, '\r'); return string.Concat(port, '%', '\r').Equals(res); } /// /// 电机停止 /// /// /// public bool MotorStop(byte nodeId) { var value = $"{nodeId}ST\r"; return SendFormat(value, nodeId); } /// /// 电机移动 /// /// /// protected bool MotorMove(params object[] args) { string value = string.Format("{0}FL{1}\r", args); var res = SendFormat(value, args[0]); logger.LogInformation($"写入电机移动,端口:{args[0]},数据:{value},写入:{(res ? "成功" : "失败")}"); return res; } /// /// 寄存器操作 /// public bool MotorStorage(byte nodeId, int address, string way) { var value = $"{nodeId}SO{address}{way}\r"; var res = SendFormat(value, nodeId); logger.LogInformation($"写入寄存器数据,端口:{nodeId},地址:{address},数据:{value},写入:{(res ? "成功" : "失败")}"); return res; } /// /// 检查寄存器 /// /// /// public char[] JudgeStorage(byte nodeId) { var value = $"{nodeId}IS\r"; var res = Rs485Client.SendData(value, '\r'); var chars = res.Replace($"{nodeId}IS=", string.Empty).Replace("\r", string.Empty).Reverse().ToArray(); return chars; } public string JudgeXYZMove(byte nodeId) { var value = $"{nodeId}SC\r"; if (!Rs485Client.IsValid) return ""; var res = Rs485Client.SendData(value, '\r'); return res.Split('=')[1]; } /// /// 返回原点 /// /// /// /// public bool MotorGoBack(byte nodeId, int type) { var value = $"{nodeId}FS{type}L\r"; var res = SendFormat(value, nodeId); logger.LogInformation($"写入返回原点数据,端口:{nodeId},数据:{value},写入:{(res ? "成功" : "失败")}"); if (!res) return res; do { var judgeRes = JudgeStorage(nodeId); if (judgeRes is null || judgeRes.Length < 1) { res = false; continue; } res = judgeRes[type - 1].Equals('0'); } while (!res); Thread.Sleep(500); return res; } public bool MotorGoBackDissolvePort(byte nodeId, int type) //消解出来的时和液体硫酸亚铁胺单独写回原点,消解位因为不支持FS 回原点命令,出来只能用SH回原点,进去 的话可以用FS回原点 { var value = $"{nodeId}SH{type}L\r"; var res = SendFormat(value, nodeId); logger.LogInformation($"写入返回原点数据,端口:{nodeId},数据:{value},写入:{(res ? "成功" : "失败")}"); if (!res) return res; do { var judgeRes = JudgeStorage(nodeId); if (judgeRes is null || judgeRes.Length < 1) { res = false; continue; } res = judgeRes[type - 1].Equals('0'); } while (!res); Thread.Sleep(500); return res; } /// /// 设置速度 /// /// /// /// protected bool MotorSpeed(byte nodeId, double speed) { var value = $"{nodeId}VE{speed}\r"; return SendFormat(value, nodeId); } /// /// 设置加速度 /// /// /// /// protected bool MotorAcSpeed(byte nodeId, double speed) { var value = $"{nodeId}AC{speed}\r"; return SendFormat(value, nodeId); } /// /// 设置减速度 /// /// /// /// protected bool MotorDeSpeed(byte nodeId, double speed) { var value = $"{nodeId}DE{speed}\r"; return SendFormat(value, nodeId); } /// /// 移动 /// /// /// /// public bool Move(byte nodeId, long distance, int interval, Double speed) { bool res = false; res = MotorMove(nodeId, distance); return res; } } }