zoukankan      html  css  js  c++  java
  • 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);
            }
    
        }
    }
  • 相关阅读:
    2653 区间xor
    c++位运算
    洛谷P1233 木棍加工
    c++背包问题
    FOC基本调试方法[转]
    用于下载AD官网登录账号:User name: fuxin918@fuxin918.com Passeword: s6c0W1w8
    STC10F10XE定时器中断输出10KHz的方波程序
    学习DSP(三)安装C2833x/C2823x C/C++ 头文件和外设示例-压缩包
    学DSP(二):目标芯片28335,GO!
    学DSP(一):开始
  • 原文地址:https://www.cnblogs.com/WilliamJiang/p/5632270.html
Copyright © 2011-2022 走看看