Unity3D SerialPort处理

using UnityEngine;
using System.Collections;
using System;
using System.Threading;
using System.Collections.Generic;
using System.ComponentModel;
using System.IO.Ports;
using System.Text.RegularExpressions;
using System.Text;


public class UnitySerialPort : MonoBehaviour
{


    private SerialPort sp;
    private Queue queueDataPool;
    private Thread tPort;
    private Thread tPortDeal;
    private string strOutPool = string.Empty;
    string finalstring = string.Empty;
    string tempstring = string.Empty;

    byte flag0 = 0xAA;//start sign
    byte flag1 = 0x8E;//end sign

    List<Byte> cmdList = null;//catch pre sign

    List<List<Byte>> DataList = new List<List<byte>>();//catch all sign 

    // Use this for initialization 
    void Start()
    {
        sp = new SerialPort("COM3", 115200, Parity.None, 8, StopBits.One);
        if (!sp.IsOpen)
        {
            sp.Open();
        }
        tPort = new Thread(DealData);
        tPort.Start();
        tPortDeal = new Thread(ReceiveData);
        tPortDeal.Start();
    }


    // Update is called once per frame 
    void Update()
    {
        if (!tPortDeal.IsAlive)
        {
            tPortDeal = new Thread(ReceiveData);
            tPortDeal.Start();
        }
        if (!tPort.IsAlive)
        {
            tPort = new Thread(DealData);
            tPort.Start();
        }
    }


    private void ReceiveData()
    {
        try
        {
            Byte[] buf = new Byte[1];
            string sbReadline2str = string.Empty;
            if (sp.IsOpen) sp.Read(buf, 0, 1);
            if (buf.Length == 0)
            {
                return;
            }
            if (buf != null)
            {

                for (int i = 0; i < buf.Length; i++)
                {
                    if (buf[i] == flag0)
                    {
                        cmdList = new List<Byte>();
                        cmdList.Add(buf[i]);
                    }
                    else if (buf[i] == flag1)
                    {
                        cmdList.Add(buf[i]);
                        DataList.Add(cmdList);
                    }
                    else
                    {
                        cmdList.Add(buf[i]);
                    }
                }

                if (DataList.Count > 0)
                {
                    List<byte> cmd = DataList[0];

                    StringBuilder sb = new StringBuilder();
                    for (int i = 0; i < cmd.Count; i++)
                    {
                        sb.Append(cmd[i] + " ");
                    }

                    Debug.Log(sb.ToString());

                    DataList.RemoveAt(0);
                }

            }
        }
        catch (Exception ex)
        {
            Debug.Log(ex);
        }
    }
    private void DealData()
    {
        while (queueDataPool.Count != 0)
        {
            for (int i = 0; i < queueDataPool.Count; i++)
            {
                strOutPool += queueDataPool.Dequeue();
                if (strOutPool.Length == 16)
                {
                    Debug.Log(strOutPool);
                    strOutPool = string.Empty;
                }
            }

        }
    }

    private void SendSerialPortData(string data)
    {
        if (sp.IsOpen)
        {
            sp.WriteLine(data);
        }
    }

    void OnApplicationQuit()
    {
        sp.Close();
    }

    void OnGUI()
    {
        if (GUILayout.Button("Send Data", GUILayout.Height(30)))
        {
            byte[] buffer = { 0xAA, 0x00, 0x22, 0x00, 0x00, 0x22, 0x8E };
            sp.Write(buffer, 0, buffer.Length);
        }

    }
}

 

posted @ 2016-07-01 10:42  William Jiang  阅读(3240)  评论(2编辑  收藏  举报