微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Unity3D SerialPort处理

原文链接http://www.cnblogs.com/WilliamJiang/p/5632270.html
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);
        }

    }
}

 

转载于:https://www.cnblogs.com/WilliamJiang/p/5632270.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐