• 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);
            }
    
        }
    }
  • 相关阅读:
    主攻ASP.NET.4.5.1 MVC5.0之重生:创建UIHelper通用自定义分页和选择开关与PagesHelper和IsSelect简单用法
    主攻ASP.NET.4.5.1 MVC5.0之重生:系统角色与权限(二)
    @MarkFan 口语练习录音 20140423 [风雨哈佛路.Homeless To Harvard口语录音]
    主攻ASP.NET.4.5.1 MVC5.0之重生:系统角色与权限(一)
    离乡与理想 小样
    主攻ASP.NET.4.5.1 MVC5.0之重生:空地搭建一个包含 Ninject框架 项目
    震撼全世界的一块墓碑
    @MarkFan 口语练习录音 20140415 [MDL演讲口语录音]
    PHP预编译处理技术简介
    事物控制之保存点
  • 原文地址:https://www.cnblogs.com/WilliamJiang/p/5632270.html
Copyright © 2020-2023  润新知