终端一体化运控平台
Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

MCUSerialHelper.cs 5.2 KiB

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