1
0

SerialPortUtilityProManager.cs 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using System;
  5. using SerialPortUtility;
  6. using UnityEngine.Events;
  7. namespace FordBroncoToproofAssemble
  8. {
  9. public class SerialPortUtilityProManager : MonoBehaviour
  10. {
  11. public static SerialPortUtilityProManager Instance;
  12. private SerialPortUtilityPro serialPortUtilityPro;
  13. private event UnityAction<object> OnReciveMessage;
  14. // ==============================
  15. private void Awake()
  16. {
  17. Instance = this;
  18. serialPortUtilityPro = FindAnyObjectByType<SerialPortUtilityPro>();
  19. }
  20. private void Update()
  21. {
  22. if (Input.GetKeyDown(KeyCode.Q)) // 秒表正计时
  23. {
  24. SendMessage2Device(TimerCommandStorage.Start);
  25. }
  26. if (Input.GetKeyDown(KeyCode.W)) // 暂停
  27. {
  28. SendMessage2Device(TimerCommandStorage.Pause);
  29. }
  30. if (Input.GetKeyDown(KeyCode.E)) // 重置
  31. {
  32. SendMessage2Device(TimerCommandStorage.Reset);
  33. }
  34. if (Input.GetKeyDown(KeyCode.A)) // 返回值
  35. {
  36. SendMessage2Device(TimerCommandStorage.GetTime);
  37. }
  38. }
  39. // ==============================
  40. public void AddEventListener(UnityAction<object> unityAction)
  41. {
  42. OnReciveMessage += unityAction;
  43. return;
  44. }
  45. public void RemoveEventListener(UnityAction<object> unityAction)
  46. {
  47. OnReciveMessage -= unityAction;
  48. return;
  49. }
  50. // ==============================
  51. // 发包
  52. /// <summary>
  53. /// 发送信号给设备
  54. /// </summary>
  55. /// <param name="value">是否带0x都可以</param>
  56. public void SendMessage2Device(string value)
  57. {
  58. byte[] data = OutMessageProcessing(value);
  59. serialPortUtilityPro.Write(data);
  60. return;
  61. }
  62. /// <summary>
  63. /// 发出数据包处理
  64. /// </summary>
  65. /// <param name="value"></param>
  66. /// <returns></returns>
  67. private byte[] OutMessageProcessing(string value)
  68. {
  69. string[] valueSlices = value.Replace("0x", "").Split(' '); // 去0x // 分割
  70. byte[] bytes = new byte[valueSlices.Length];
  71. for (int i = 0; i < bytes.Length; i++)
  72. {
  73. bytes[i] = Convert.ToByte(Convert.ToInt32(valueSlices[i], 16));
  74. }
  75. return bytes;
  76. }
  77. // ==============================
  78. // 收包
  79. /// <summary>
  80. /// 读二进制流
  81. /// 配合SerialPortUtilityPro使用
  82. /// </summary>
  83. /// <param name="byteData"></param>
  84. public void ReadBinaryStreaming(object byteData)
  85. {
  86. string stringRawData = BitConverter.ToString((byte[])byteData); // 比特流翻译
  87. InMessageProcessing(stringRawData);
  88. return;
  89. }
  90. private void InMessageProcessing(string value)
  91. {
  92. string[] dataSlices = value.Split('-'); // 数据切片
  93. // 在此处理/过滤数据
  94. if (dataSlices.Length < 14) // 以长度判断
  95. {
  96. return;
  97. }
  98. string result = string.Empty;
  99. for (int i = 6; i < 12; i++)
  100. {
  101. result += Convert.ToChar(Convert.ToInt32(dataSlices[i], 16)) - '0'; // Ascii16转10转char转int
  102. }
  103. // 广播订阅
  104. if (OnReciveMessage != null)
  105. {
  106. OnReciveMessage(result);
  107. return;
  108. }
  109. return;
  110. }
  111. }
  112. }