MotorXPort.cs 2.9 KB

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