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.
 
 

94 lines
2.7 KiB

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading;
  6. using System.Threading.Tasks;
  7. using HBLConsole.Service;
  8. using Lebai.SDK;
  9. using Lebai.SDK.Dtos;
  10. using Robotc;
  11. namespace HBLConsole.Communication
  12. {
  13. public class LebaiHelper
  14. {
  15. private volatile static LebaiHelper _Instance;
  16. public static LebaiHelper GetInstance => _Instance ?? (_Instance = new LebaiHelper());
  17. private LebaiHelper() { }
  18. private LebaiRobotClient client;
  19. private RobotData robotData;
  20. public void Connect(string ip)
  21. {
  22. bool ErrorFlag = false;
  23. while (robotData == null)
  24. {
  25. try
  26. {
  27. client = new LebaiRobotClient(ip);
  28. robotData = client.GetRobotData().Result;
  29. }
  30. catch (Exception ex)
  31. {
  32. if (!ErrorFlag)
  33. {
  34. MessageLog.GetInstance.Show(ex.ToString());
  35. ErrorFlag = true;
  36. }
  37. Thread.Sleep(3000);
  38. }
  39. }
  40. MessageLog.GetInstance.Show("乐百机器人连接成功!");
  41. GetSpeed();
  42. //ActionOperate.GetInstance.Send($"{GVL.GeneralConfig.DeviceType.ToString()}/ConnectOk");
  43. }
  44. public void GetSpeed()
  45. {
  46. //client.StartSys().GetAwaiter();
  47. var aa = client.GetActualTcpSpeed().GetAwaiter();
  48. }
  49. /// <summary>
  50. /// 获取信号量
  51. /// </summary>
  52. /// <param name="index"></param>
  53. /// <returns></returns>
  54. public SignalResult GetValueAsync(int index = 0)
  55. {
  56. if (robotData == null) return default(SignalResult);
  57. SignalValue signalValue = new SignalValue();
  58. signalValue.Index = index;
  59. return client?.GetSignal(signalValue).Result;
  60. }
  61. /// <summary>
  62. /// 设置信号量
  63. /// </summary>
  64. /// <param name="index"></param>
  65. /// <param name="value"></param>
  66. /// <returns></returns>
  67. public SignalResult SetValue(int value, int index = 0)
  68. {
  69. if (robotData == null) return default(SignalResult);
  70. SignalValue signalValue = new SignalValue();
  71. signalValue.Index = index;
  72. signalValue.Value = value;
  73. return client.SetSignal(signalValue).Result;
  74. }
  75. /// <summary>
  76. /// 运行指定的场景
  77. /// </summary>
  78. /// <param name="id"></param>
  79. public void Scene(int id)
  80. {
  81. if (robotData == null) return;
  82. client?.RunScene(id);
  83. }
  84. }
  85. }