MotorYPort.cs 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. using System;
  2. using System.Threading;
  3. using SHJX.Service.Common.Extend;
  4. using SHJX.Service.Common.ReadXML;
  5. using SHJX.Service.Model.Control;
  6. using SHJX.Service.ServerClient.Interface;
  7. using SHJX.Service.ServerClient.TempController;
  8. using SHJX.Service.Common.Logging;
  9. using Microsoft.Extensions.Logging;
  10. namespace SHJX.Service.ServerClient.RS485Control
  11. {
  12. /// <summary>
  13. /// Y轴操作
  14. /// </summary>
  15. public class MotorYPort : SerialPortImp, PortControlImp
  16. {
  17. private static readonly ILogger logger = LogFactory.BuildLogger(typeof(MotorYPort));
  18. /// <summary>
  19. /// Y轴
  20. /// </summary>
  21. /// <param name="log">日志</param>
  22. /// <param name="config">配置</param>
  23. ///
  24. public MotorYPort( ReadConfigUtil config)
  25. : base( config)
  26. {
  27. config.GetSpeed.ForEach(item=> {
  28. if (item.NodeName == "MotorY")
  29. speedY = item.Speed;
  30. });
  31. }
  32. private double speedY = 0;
  33. public object Read(PortArgs args)
  34. {
  35. object obj = args.ReadWay switch
  36. {
  37. "OriginalPosition" => ReadOriginalPosition(args.NodeId),
  38. _ => JudgeStorage(args.NodeId),
  39. };
  40. return obj;
  41. }
  42. private bool ReadOriginalPosition(byte NodeId)
  43. {
  44. var res = JudgeStorage(NodeId);
  45. if (res is null or { Length: 0 }) return false;
  46. return res[7].Equals('0');
  47. }
  48. public bool Write(PortArgs args)
  49. {
  50. var res = args.WriteWay switch
  51. {
  52. WriteWay.Move => yMove(args),
  53. WriteWay.GoBack => yMotorGoBack(args),
  54. WriteWay.Stop => MotorStop(args.NodeId),
  55. WriteWay.Speed => MotorSpeed(args.NodeId, args.Distance),
  56. WriteWay.AcSpeed => MotorAcSpeed(args.NodeId, args.Distance),
  57. WriteWay.DeSpeed => MotorDeSpeed(args.NodeId, args.Distance),
  58. _ => throw new ArgumentNullException(args.WriteWay.GetEnumDesc<WriteWay>()),
  59. };
  60. return res;
  61. }
  62. private bool yMove(PortArgs args)
  63. {
  64. bool res = true;
  65. //var judgeRes = JudgeStorage(3);
  66. //if (judgeRes is null || judgeRes.Length < 1)
  67. // return false;
  68. //if (!judgeRes[3 - 1].Equals('0'))
  69. //{
  70. // res = MotorGoBack(3, 3);
  71. //}
  72. return res && Move(args.NodeId, args.Distance, 50, speedY);
  73. }
  74. private bool yMotorGoBack(PortArgs args)
  75. {
  76. bool res = true;
  77. //var judgeRes = JudgeStorage(3);
  78. //if (judgeRes is null || judgeRes.Length < 1)
  79. // return false;
  80. //if (!judgeRes[3 - 1].Equals('0'))
  81. //{
  82. // res = MotorGoBack(3, 3);
  83. //}
  84. return res && MotorGoBack(args.NodeId, 8);
  85. }
  86. }
  87. }