CH4BPQ.cs 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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. namespace Ips.DevAlgorithm
  8. {
  9. /// <summary>
  10. /// 4通道变频器
  11. /// </summary>
  12. public class CH4BPQ
  13. {
  14. readonly string rem_on = "<REM_ON\r";
  15. readonly string cfreq = "<CF{0}_{1}\r";
  16. readonly int tryCount = 3; //控制失败后的尝试次数
  17. SerialPortDevice _device;
  18. public CH4BPQ(SerialPortDevice device)
  19. {
  20. _device = device;
  21. }
  22. /// <summary>
  23. /// 打开远控
  24. /// </summary>
  25. /// <param name="errmsg"></param>
  26. /// <returns></returns>
  27. bool RemOn(ref String errmsg)
  28. {
  29. bool result;
  30. if (_device.SendCommand(rem_on, ref errmsg))
  31. {
  32. string result2 = _device.GetOutString();
  33. result = result2.Contains("REM_ON");
  34. }
  35. else
  36. {
  37. result = false;
  38. }
  39. return result;
  40. }
  41. /// <summary>
  42. /// 设置一个频点
  43. /// </summary>
  44. /// <param name="ch"></param>
  45. /// <param name="freq"></param>
  46. /// <param name="errmsg"></param>
  47. /// <returns></returns>
  48. public bool SetFreq(int ch, double freq, ref String errmsg)
  49. {
  50. String cmd = String.Format(cfreq, ch, freq.ToString("0000.000"));
  51. bool result = false;
  52. for (int idx = 0; idx < tryCount; ++idx)
  53. {
  54. if (_device.SendCommand(cmd, ref errmsg))
  55. {
  56. string result2 = _device.GetOutString();
  57. result = result2.Contains("CF");
  58. }
  59. else
  60. {
  61. result = false;
  62. errmsg = $"{cmd}:{errmsg}";
  63. }
  64. if (result)
  65. {
  66. break;
  67. }
  68. //控制失败 继续尝试
  69. Thread.Sleep(500);
  70. }
  71. return result;
  72. }
  73. public bool SetFreqs(double[] freqs, ref String errmsg)
  74. {
  75. if (freqs.Length == 0)
  76. {
  77. return false;
  78. }
  79. if (!RemOn(ref errmsg))
  80. {
  81. return false;
  82. }
  83. Thread.Sleep(100);
  84. bool ret = true;
  85. for (int idx = 0; idx < freqs.Length; ++idx)
  86. {
  87. if (freqs[idx] <= 0.01) continue;
  88. if (!SetFreq(idx + 1, freqs[idx], ref errmsg))
  89. {
  90. ret = false;
  91. }
  92. }
  93. return ret;
  94. }
  95. }
  96. }