unity2017仍不支持SerialPort.DataReceived这个委托

今天测试一下,发现绑定后不会触发方法,还是老老实实手写线程读取处理吧。

版本是unity2017,.net4.6。

具体方法如下

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.IO.Ports;
using System.Threading;
using System;

public class PortClass
{
    public string name;
    public SerialPort serialPort;
    public bool ClosePortFlag;
    private Action> checkRecData;
    private Queue recQueue = new Queue();
    private Queue sendDataQueue;
    private int recBufferSize;//接收缓存区的大小

    /// 
    /// 串口打开的构造函数
    /// 
    /// 
    /// 
    /// 
    /// 
    /// 
    /// 
    /// 
    /// 
    /// 
    public PortClass(string name, int baudRate, int dataBits, int writeTimeout, int readTimeout, int bufferSize,
        bool isRecThread, bool isSendThread, Queue sendQueue, Action> checkAction)
    {
        this.name = name;
        checkRecData = checkAction;
        sendDataQueue = sendQueue;
        recBufferSize = bufferSize;

        serialPort = new SerialPort(name, baudRate, Parity.None, dataBits, StopBits.One)
        {
            WriteTimeout = writeTimeout,
            ReadTimeout = readTimeout
        };
        try
        {
            serialPort.Open();
        }
        catch (Exception e)
        {
            ClosePortFlag = true;
            Debug.Log(e.Message);
            return;
        }
        Debug.Log(name + "打开成功");
        if (isRecThread)
        {
            Thread recThread = new Thread(RecThreadFunc)
            {
                IsBackground = true
            };
            recThread.Start();
        }
        if (isSendThread)
        {
            Thread sendThread = new Thread(SendTheadFunc)
            {
                IsBackground = true
            };
            sendThread.Start();
        }
    }

    private void RecThreadFunc(object obj)
    {
        while (ClosePortFlag == false)
        {
            try
            {
                byte[] receiveBuffer = new byte[recBufferSize];
                //Debug.Log("准备接收");
                int length = serialPort.Read(receiveBuffer, 0, receiveBuffer.Length);
                Debug.Log(length + "  接收到数据");
                if (length > 0)
                {
                    //获取有效数据
                    for (int i = 0; i < length; i++)
                    {
                        //Debug.Log(Convert.ToString(receiveBuffer[i], 16));
                        recQueue.Enqueue(receiveBuffer[i]);
                    }
                    //解包,解包函数存在,就解包,不存在,就将已经接收到的数据清空
                    if (checkRecData != null)
                    {
                        checkRecData(recQueue);
                    }
                    else
                    {
                        recQueue.Clear();
                    }
                    //Debug.Log("解析完毕");
                    Thread.Sleep(0);
                }
            }
            catch (Exception e)
            {
                Debug.Log(e.Message);
            }
        }

        //这里对串口资源进行自动回收,回收前先让线程睡眠30ms,以保证发送线程已经关闭
        //以防发送线程仍在占用串口资源,这样关闭串口时就不会有异常
        Thread.Sleep(30);
        serialPort.Close();
        serialPort.Dispose();
    }
    private void SendTheadFunc(object obj)
    {
        while (ClosePortFlag == false)
        {
            if (sendDataQueue != null && sendDataQueue.Count > 0)
            {
                byte[] tempBytes = sendDataQueue.Dequeue();
                SendData(tempBytes);
            }

            Thread.Sleep(10);//两个包之间的发送间隔为10ms
        }
    }

    private void SendData(byte[] sendBytes)
    {
        serialPort.Write(sendBytes, 0, sendBytes.Length);
    }
}

 

你可能感兴趣的:(unity2017仍不支持SerialPort.DataReceived这个委托)