终端一体化运控平台
Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

JuicerHelper.cs 3.8 KiB

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