213 lines
5.6 KiB
C#
Raw Normal View History

2025-11-25 17:26:38 +08:00
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Data.SqlTypes;
using System.IO.Ports;
using System.Text;
using System.Threading;
//using UnityEditor.PackageManager.UI;
using UnityEngine;
using UnityThreadingUtils;
using ZXKFramework;
public class SensorManager : MonoBehaviour /*MonoSingleton<SensorManager>*/
{
public enum DataType
{
<EFBFBD>ַ<EFBFBD><EFBFBD><EFBFBD>,
<EFBFBD>ֽ<EFBFBD><EFBFBD><EFBFBD>,
}
#region <EFBFBD><EFBFBD><EFBFBD>ڲ<EFBFBD><EFBFBD><EFBFBD>,<EFBFBD><EFBFBD>Ҫ<EFBFBD>޸Ĵ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
[Header("<22><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>")]
public string portName = "COM10";
[Header("<22><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>")]
public int baudRate = 115200;
[Header("<22><>żУ<C5BC><D0A3>")]
private Parity parity = Parity.None;
[Header("<22><><EFBFBD><EFBFBD>λ")]
private int dataBits = 8;
[Header("ֹͣλ")]
private StopBits stopBits = StopBits.One;
SerialPort sp = null;
Thread dataReceiveThread;
[Header("<22><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ݸ<EFBFBD>ʽ")]
public DataType dataType;
#endregion
private Dictionary<string, Sensor> allSensor = new Dictionary<string, Sensor>();
StringBuilder sb = new StringBuilder();
private void Start()
{
sp = new SerialPort(portName, baudRate, parity, dataBits, stopBits);
foreach (Sensor s in GetComponentsInChildren<Sensor>())
{
allSensor.TryAdd(s.GetType().Name, s);
}
switch (dataType)
{
case DataType.<EFBFBD>ֽ<EFBFBD><EFBFBD><EFBFBD>:
dataReceiveThread = new Thread(new ThreadStart(DataReceiveBytesThread));
break;
case DataType.<EFBFBD>ַ<EFBFBD><EFBFBD><EFBFBD>:
dataReceiveThread = new Thread(new ThreadStart(DataReceiveStrThread));
break;
}
OpenPort();
}
/// <summary>
/// <20><><EFBFBD><EFBFBD><EFBFBD>ֽ<EFBFBD><D6BD><EFBFBD>
/// </summary>
private void DataReceiveBytesThread()
{
while (true)
{
if (sp != null && sp.IsOpen)
{
try
{
if (sp.BytesToRead > 0)
{
byte[] buffer = new byte[sp.BytesToRead];
sp.Read(buffer, 0, sp.BytesToRead);
string receivedData = Encoding.Default.GetString(buffer);
//Debug.Log(receivedData);
UnityMainThreadDispatcher.Instance().Enqueue(() =>
{
foreach (var s in allSensor)
{
s.Value.ReceiveData(receivedData);
}
});
}
}
catch (Exception)
{
Debug.Log("<22><>Ϣ<EFBFBD><CFA2><EFBFBD><EFBFBD>ʧ<EFBFBD><CAA7>");
}
}
Thread.Sleep(20);
}
}
/// <summary>
/// <20><><EFBFBD><EFBFBD><EFBFBD>ַ<EFBFBD><D6B7><EFBFBD>
/// </summary>
private void DataReceiveStrThread()
{
while (true)
{
if (sp != null && sp.IsOpen)
{
try
{
if (sp.BytesToRead > 0)
{
sb.Append(sp.ReadExisting());
//this.ColorLog(GDLog.LogColorState.Blue, sp.ReadLine());
UnityMainThreadDispatcher.Instance().Enqueue(() =>
{
foreach (var s in allSensor)
{
s.Value.ReceiveData(sb.ToString());
}
sb.Clear();
});
}
}
catch (Exception)
{
Debug.Log("<22><>Ϣ<EFBFBD><CFA2><EFBFBD><EFBFBD>ʧ<EFBFBD><CAA7>");
}
}
Thread.Sleep(20);
}
}
/// <summary>
/// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϣ
/// </summary>
/// <param name="dataSend"></param>
public void SendFunction(string str)
{
try
{
byte[] dataSend = Encoding.ASCII.GetBytes(str);
if (sp != null && sp.IsOpen)
{
if (dataSend != null && dataSend.Length > 0)
{
sp.Write(dataSend, 0, dataSend.Length);
Debug.Log("<22><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϣ<EFBFBD>ɹ<EFBFBD><C9B9><EFBFBD>" + str);
}
}
}
catch (Exception)
{
Debug.Log("<22><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϣʧ<CFA2>ܣ<EFBFBD>" + str);
}
}
//<2F><>ȡָ<C8A1><D6B8><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
public T GetSensor<T>() where T : Sensor
{
try
{
string name = typeof(T).Name;
if (!allSensor.ContainsKey(name))
{
allSensor.TryAdd(name, GetComponentInChildren<T>());
allSensor[name].Init(this);
}
//Debug.Log("<22><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ȡ<EFBFBD>ɹ<EFBFBD>");
return allSensor[name] as T;
}
catch (Exception)
{
Debug.Log("<22><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ȡʧ<C8A1><CAA7>");
return null;
}
}
#region <EFBFBD><EFBFBD><EFBFBD>ڿ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ر<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
//<2F>򿪴<EFBFBD><F2BFAAB4><EFBFBD>
public void OpenPort()
{
try
{
if (!sp.IsOpen)
{
sp.Open();
dataReceiveThread.Start();
Debug.Log("<22><><EFBFBD>ڴ򿪳ɹ<F2BFAAB3>");
}
}
catch (Exception)
{
Debug.Log("<22><><EFBFBD>ڴ<EFBFBD><DAB4><EFBFBD>ʧ<EFBFBD><CAA7>");
}
}
//<2F>رմ<D8B1><D5B4><EFBFBD>
public void ClosePort()
{
try
{
sp.Close();
dataReceiveThread.Abort();
Debug.Log("<22><><EFBFBD>ڹر<DAB9>");
}
catch (Exception)
{
Debug.Log("<22><><EFBFBD>ڹر<DAB9>ʧ<EFBFBD><CAA7>");
}
}
#endregion
#region Unity <EFBFBD>˳<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
private void OnApplicationQuit()
{
ClosePort();
}
private void OnDisable()
{
this.Log("Ϊʲô<CAB2>ر<EFBFBD><D8B1>ˣ<EFBFBD>" + gameObject.name);
ClosePort();
}
#endregion
}