| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183 |
- 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;
- }
- /// <summary>
- /// 数据下发
- /// </summary>
- /// <param name="value"></param>
- /// <param name="port"></param>
- /// <returns></returns>
- 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);
- }
- /// <summary>
- /// 电机停止
- /// </summary>
- /// <param name="nodeId"></param>
- /// <returns></returns>
- public bool MotorStop(byte nodeId)
- {
- var value = $"{nodeId}ST\r";
- return SendFormat(value, nodeId);
- }
- /// <summary>
- /// 电机移动
- /// </summary>
- /// <param name="args"></param>
- /// <returns></returns>
- 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;
- }
- /// <summary>
- /// 寄存器操作
- /// </summary>
- 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;
- }
- /// <summary>
- /// 检查寄存器
- /// </summary>
- /// <param name="nodeId"></param>
- /// <returns></returns>
- 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];
- }
- /// <summary>
- /// 返回原点
- /// </summary>
- /// <param name="nodeId"></param>
- /// <param name="type"></param>
- /// <returns></returns>
- 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;
- }
- /// <summary>
- /// 设置速度
- /// </summary>
- /// <param name="nodeId"></param>
- /// <param name="speed"></param>
- /// <returns></returns>
- protected bool MotorSpeed(byte nodeId, double speed)
- {
- var value = $"{nodeId}VE{speed}\r";
- return SendFormat(value, nodeId);
- }
- /// <summary>
- /// 设置加速度
- /// </summary>
- /// <param name="nodeId"></param>
- /// <param name="speed"></param>
- /// <returns></returns>
- protected bool MotorAcSpeed(byte nodeId, double speed)
- {
- var value = $"{nodeId}AC{speed}\r";
- return SendFormat(value, nodeId);
- }
- /// <summary>
- /// 设置减速度
- /// </summary>
- /// <param name="nodeId"></param>
- /// <param name="speed"></param>
- /// <returns></returns>
- protected bool MotorDeSpeed(byte nodeId, double speed)
- {
- var value = $"{nodeId}DE{speed}\r";
- return SendFormat(value, nodeId);
- }
- /// <summary>
- /// 移动
- /// </summary>
- /// <param name="nodeId"></param>
- /// <param name="distance"></param>
- /// <returns></returns>
- public bool Move(byte nodeId, long distance, int interval, Double speed)
- {
- bool res = false;
- res = MotorMove(nodeId, distance);
- return res;
- }
- }
- }
|