终端一体化运控平台
Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.

MCUSerialHelper.cs 5.2 KiB

2 lat temu
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using System.IO;
  7. using System.IO.Ports;
  8. using System.Threading;
  9. using System.Collections.Concurrent;
  10. using BPASmartClient.Message;
  11. namespace BPASmartClient.SerialPort
  12. {
  13. public class MCUSerialHelper
  14. {
  15. //private volatile static MCUSerialHelper _Instance;
  16. //public static MCUSerialHelper GetInstance => _Instance ?? (_Instance = new MCUSerialHelper());
  17. //private MCUSerialHelper() { }
  18. private System.IO.Ports.SerialPort comPort = new System.IO.Ports.SerialPort();
  19. public bool IsOpen => comPort.IsOpen;
  20. public bool Open(string portName, int baudRate)
  21. {
  22. while (!System.IO.Ports.SerialPort.GetPortNames().Contains(portName))
  23. {
  24. Thread.Sleep(1000);
  25. }
  26. while (!comPort.IsOpen)
  27. {
  28. comPort.PortName = portName;
  29. comPort.BaudRate = baudRate;
  30. comPort.DataBits = 8;
  31. comPort.Parity = Parity.None;
  32. comPort.StopBits = StopBits.One;
  33. comPort.ReadTimeout = 1000;
  34. comPort.WriteTimeout = 1000;
  35. //comPort.RtsEnable = true; //设置为 true后会读取不到数据
  36. //comPort.DtrEnable = true;//获取或设置一个值,该值在串行通信过程中启用数据终端就绪 (DTR) 信号。
  37. //comPort.RtsEnable = true;//获取或设置一个值,该值指示在串行通信中是否启用请求发送 (RTS) 信号
  38. try
  39. {
  40. comPort.Open();
  41. }
  42. catch (Exception ex)
  43. {
  44. MessageLog.GetInstance.ShowEx(ex.ToString());
  45. Thread.Sleep(5000);
  46. }
  47. }
  48. MessageLog.GetInstance.Show($"{portName} 串口打开成功");
  49. return comPort.IsOpen;
  50. }
  51. private static readonly object OutPutLock = new object();
  52. /// <summary>
  53. /// 单片机输出端口控制
  54. /// </summary>
  55. /// <param name="index">通道号 1 - 8 </param>
  56. /// <param name="value">控制值</param>
  57. public void OutputControl(byte index, bool value)
  58. {
  59. lock (OutPutLock)
  60. {
  61. byte NumValue = (byte)(value ? 0x01 : 0x00);
  62. byte[] buffers = new byte[6] { 0xCC, 0x01, 0x01, index, NumValue, 0xDD };
  63. if (IsOpen) comPort.Write(buffers, 0, buffers.Length);
  64. }
  65. }
  66. /// <summary>
  67. /// 舵机控制
  68. /// </summary>
  69. /// <param name="index">通道号 1 - 8</param>
  70. /// <param name="value">舵机位置 0 - 180</param>
  71. public void ServoControl(byte index, byte value)
  72. {
  73. byte[] buffers = new byte[6] { 0xCC, 0x01, 0x02, index, value, 0xDD };
  74. if (IsOpen) comPort.Write(buffers, 0, buffers.Length);
  75. }
  76. /// <summary>
  77. /// 获取单片机输入端口状态
  78. /// </summary>
  79. /// <param name="index"></param>
  80. /// <returns>0:无意义 1:有信号 2:无信号 3:信号不正确</returns>
  81. public int GetInputStatus(byte index)
  82. {
  83. if (index <= 0 || index > 8) return 0;
  84. byte[] buffers = new byte[6] { 0xCC, 0x01, 0x03, index, 0x00, 0xDD };
  85. if (IsOpen)
  86. {
  87. comPort.Write(buffers, 0, buffers.Length);
  88. DateTime dt = DateTime.Now;
  89. List<byte> receive = new List<byte>();
  90. while (true)
  91. {
  92. byte[] re = new byte[comPort.BytesToRead];
  93. comPort.Read(re, 0, re.Length);
  94. if (re.Contains<byte>(0xcc) && re.Contains<byte>(0xDD)) receive.AddRange(re);
  95. comPort.DiscardInBuffer();
  96. if (receive.Contains(0xcc) && receive.Contains(0xdd)) break;
  97. if (DateTime.Now.Subtract(dt).TotalSeconds >= 2) break;
  98. Thread.Sleep(1);
  99. }
  100. if (receive != null)
  101. {
  102. int Reindex = Array.FindIndex(receive.ToArray(), p => p == 0xcc);
  103. if (Reindex < receive.Count && Reindex >= 0)
  104. {
  105. var res = receive.GetRange(Array.FindIndex(receive.ToArray(), p => p == 0xcc), 6);
  106. if (res.ElementAt(2) == 0x03)
  107. {
  108. if (res != null && res.Count() == 6 && res.ElementAt(4) == 0x01)
  109. {
  110. return 1;
  111. }
  112. else if (res != null && res.Count() == 6 && res.ElementAt(4) == 0x00)
  113. {
  114. return 2;
  115. }
  116. }
  117. else
  118. {
  119. return 3;
  120. }
  121. }
  122. else
  123. {
  124. return 0;
  125. }
  126. }
  127. }
  128. return 0;
  129. }
  130. }
  131. }