Non puoi selezionare più di 25 argomenti Gli argomenti devono iniziare con una lettera o un numero, possono includere trattini ('-') e possono essere lunghi fino a 35 caratteri.
 
 

113 righe
3.7 KiB

  1. using HBLConsole.Service;
  2. using System;
  3. using System.Collections.Concurrent;
  4. using System.Collections.Generic;
  5. using System.IO.Ports;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Threading;
  9. using System.Threading.Tasks;
  10. namespace HBLConsole.Communication
  11. {
  12. public class JuicerHelper
  13. {
  14. private volatile static JuicerHelper _Instance;
  15. public static JuicerHelper GetInstance => _Instance ?? (_Instance = new JuicerHelper());
  16. private JuicerHelper() { }
  17. private SerialPort comPort = new SerialPort();
  18. ConcurrentQueue<byte[]> SerialMessages = new ConcurrentQueue<byte[]>();
  19. public bool IsOpen => comPort.IsOpen;
  20. public bool Open(string portName, int baudRate)
  21. {
  22. while (!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.DataReceived += ComPort_DataReceived;
  36. comPort.RtsEnable = true;
  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. /// <summary>
  51. /// 开始制作
  52. /// </summary>
  53. /// <param name="aisle"></param>
  54. /// <returns></returns>
  55. public bool StartCook(byte aisle)
  56. {
  57. byte sum = (byte)(0x21 + aisle);
  58. byte checksum = (byte)~sum;
  59. checksum++;
  60. byte[] buffers = new byte[6] { 0x7E, 0x03, 0x00, 0x21, aisle, checksum };
  61. if (comPort.IsOpen)
  62. {
  63. comPort.Write(buffers, 0, buffers.Length);
  64. while (comPort.BytesToRead < 6)
  65. {
  66. Thread.Sleep(1);
  67. }
  68. byte[] receive = new byte[comPort.BytesToRead];
  69. comPort.Read(receive, 0, receive.Length);
  70. if (receive.Length == 6)
  71. return receive[4] == 1;
  72. }
  73. return false;
  74. }
  75. /// <summary>
  76. /// 获取果汁机状态
  77. /// </summary>
  78. /// <param name="index"></param>
  79. /// <returns></returns>
  80. public int[] GetDeviceStatus()
  81. {
  82. List<int> res = new List<int>();
  83. byte[] buffers = new byte[5] { 0x7E, 0x02, 0x00, 0x13, 0xED };
  84. if (comPort.IsOpen)
  85. {
  86. comPort.Write(buffers, 0, buffers.Length);
  87. DateTime newDate = DateTime.Now;
  88. while (comPort.BytesToRead < 33)
  89. {
  90. Thread.Sleep(1);
  91. if (DateTime.Now.Subtract(newDate).TotalSeconds >= 2) break;
  92. }
  93. byte[] receive = new byte[comPort.BytesToRead];
  94. comPort.Read(receive, 0, receive.Length);
  95. if (receive.Length == 33)
  96. {
  97. res.Add(receive.Skip(24).Take(4).ToArray().BytesToInt());
  98. res.Add(receive.Skip(28).Take(4).ToArray().BytesToInt());
  99. }
  100. return res.ToArray();
  101. }
  102. return default;
  103. }
  104. }
  105. }