终端一体化运控平台
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

171 rivejä
5.9 KiB

  1. using BPASmartClient.MorkCL.Model.Control;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7. namespace BPASmartClient.MorkCL.Server
  8. {
  9. /// <summary>
  10. /// 机器人服务类
  11. /// </summary>
  12. internal class RobotServer : RobotSet, IModbus
  13. {
  14. public RobotServer()
  15. {
  16. //MyModbus = new ModbusTcp();
  17. }
  18. public void Init(string IP = "", int Port = 502, string PortName = "")
  19. {
  20. //MyModbus = new ModbusTcp();
  21. CommHelper.CreateModbusTcp(IP, Port).OnSuccess(S =>
  22. {
  23. MyModbus = S.Content;
  24. TaskManage.GetInstance.StartLong(new Action(() =>
  25. {
  26. MyModbus.ReadCoil("GM600".ToModbusAdd(), 9).OnSuccess(s =>
  27. {
  28. for (byte i = 0; i < 3; i++)
  29. {
  30. MaterialPouringRequest[i] = s.Content[i * 3];
  31. MaterialPouringComplete[i] = s.Content[i * 3 + 1];
  32. DiningOutRequest[i] = s.Content[i * 3 + 2];
  33. }
  34. });
  35. Thread.Sleep(20);
  36. MyModbus.ReadCoil("GM620".ToModbusAdd()).OnSuccess((b) => { Home = b.Content; });
  37. Thread.Sleep(20);
  38. MyModbus.ReadCoil("GM630".ToModbusAdd(), 12).OnSuccess(s =>
  39. {
  40. for (byte i = 0; i < s.Content.Length; i++)
  41. WarehousingComplete[i] = s.Content[i];
  42. });
  43. Thread.Sleep(20);
  44. MyModbus.ReadHoldRegister<ushort>("GI5".ToModbusAdd(), 1).OnSuccess(s => { TaskFeedback = (RobotMainTask)s.Content; });
  45. IsIdle = (TaskFeedback == RobotMainTask.无任务 && Home);
  46. Thread.Sleep(20);
  47. MyModbus.ReadCoil("GM130".ToModbusAdd()).OnSuccess((b) => { RemoteMode = b.Content; });
  48. Thread.Sleep(20);
  49. MyModbus.ReadCoil("GM650".ToModbusAdd()).OnSuccess((b) => { AtCleanLoc = b.Content; });
  50. Thread.Sleep(20);
  51. }), $"机器人服务-{IP}", true);
  52. });
  53. //MyModbus.WithModbusTcp(IP, Port).UseConnected(() =>
  54. //{
  55. // ((ModbusTcp)MyModbus).master.Transport.ReadTimeout = 3000;
  56. // ((ModbusTcp)MyModbus).master.Transport.WriteTimeout = 3000;
  57. // TaskManage.GetInstance.StartLong(new Action(() =>
  58. // {
  59. // MyModbus.Read<bool[]>("GM600".ToModbusAdd(), 9).OnSuccess(s =>
  60. // {
  61. // for (byte i = 0; i < 3; i++)
  62. // {
  63. // MaterialPouringRequest[i] = s[i * 3];
  64. // MaterialPouringComplete[i] = s[i * 3 + 1];
  65. // DiningOutRequest[i] = s[i * 3 + 2];
  66. // }
  67. // });
  68. // Thread.Sleep(20);
  69. // MyModbus.Read<bool>("GM620".ToModbusAdd()).OnSuccess((b) => { Home = b; });
  70. // Thread.Sleep(20);
  71. // MyModbus.Read<bool[]>("GM630".ToModbusAdd(), 12).OnSuccess(s =>
  72. // {
  73. // for (byte i = 0; i < s.Length; i++)
  74. // WarehousingComplete[i] = s[i];
  75. // });
  76. // Thread.Sleep(20);
  77. // MyModbus.Read<ushort>("GI5".ToModbusAdd(), 1).OnSuccess(s => { TaskFeedback = (RobotMainTask)s; });
  78. // IsIdle = (TaskFeedback == RobotMainTask.无任务 && Home);
  79. // Thread.Sleep(20);
  80. // MyModbus.Read<bool>("GM130".ToModbusAdd(), 1).OnSuccess((b) => { RemoteMode = b; });
  81. // Thread.Sleep(20);
  82. // MyModbus.Read<bool>("GM650".ToModbusAdd(), 1).OnSuccess((b) => { AtCleanLoc = b; });
  83. // Thread.Sleep(20);
  84. // }), $"机器人服务-{IP}", true);
  85. //});
  86. }
  87. public void WriteValue<T>(string address, T value)
  88. {
  89. throw new NotImplementedException();
  90. }
  91. private void RobotInit()
  92. {
  93. if (EnableState && ProgramRuning) return;
  94. //if (!CR_Data.Home)
  95. //{
  96. // Noticer.GetInstance.Show("煮面机器人初始化失败,机器人不在原点");
  97. // return;
  98. //}
  99. while (!EnableState || !ProgramRuning)
  100. {
  101. //复位
  102. RobotReset = true;
  103. Thread.Sleep(1000);
  104. RobotReset = false;
  105. Thread.Sleep(500);
  106. if (!EnableState)
  107. {
  108. //上使能
  109. RobotEnable = true;
  110. Thread.Sleep(1000);
  111. RobotEnable = false;
  112. Thread.Sleep(500);
  113. }
  114. //启动
  115. RobotStart = true;
  116. Thread.Sleep(1000);
  117. RobotStart = false;
  118. Thread.Sleep(1000);
  119. }
  120. }
  121. public bool InitVariable()
  122. {
  123. if (MyModbus is not null && MyModbus.IsConnected())
  124. {
  125. try
  126. {
  127. MyModbus.WriteHoldRegister<ushort>("GI0".ToModbusAdd(), 0);
  128. MyModbus.WriteHoldRegister<ushort>("GI1".ToModbusAdd(), 0);
  129. for (int i = 0; i <= 8; i++)
  130. {
  131. MyModbus.WriteCoil(("GM" + (500 + i).ToString()).ToModbusAdd(), false);
  132. }
  133. return true;
  134. }
  135. catch (Exception)
  136. {
  137. return false;
  138. }
  139. }
  140. else
  141. return false;
  142. }
  143. }
  144. }