unity3d串口通信,双线程一收一处理
来源:互联网 发布:python 推荐系统包 编辑:程序博客网 时间:2024/04/28 22:50
using UnityEngine;using System.Collections;using System.Collections.Generic;//队列using System.IO;using System.IO.Ports;using System.Threading;//线程using System;using System.Text;//16进制转换public class com : MonoBehaviour { SerialPort myport; public string ComNum; public int irate; bool isSend = false; public StringBuilder builder; public string myDataReceived=""; public string myButton="打开串口 "; public Thread readthread; public Thread dealdatathread; Queue<string> datapool; public float wheelnum; public float steerangle; string stroutpool = string.Empty; int readnum = 0; float zero; public float outputangle=0; public float outputvelocity=0; float time = 5; string num = "5"; public GUIStyle style0;// Use this for initialization void Start () { datapool = new Queue<string>(); myport = new SerialPort(ComNum, irate, Parity.None, 8, StopBits.One); if(!myport.IsOpen) myport.Open(); readthread = new Thread(read); dealdatathread = new Thread(dealdata); readthread.Start(); dealdatathread.Start();}// Update is called once per framevoid Update () {/* if (Input.GetKeyDown(KeyCode.Space)) { Debug.Log("yes"); if (readthread.IsAlive) readthread.Abort(); if (dealdatathread.IsAlive) dealdatathread.Abort(); if (myport.IsOpen) myport.Close();myport = new SerialPort(ComNum, irate, Parity.None, 8, StopBits.One); if(!myport.IsOpen) myport.Open(); readthread = new Thread(read); dealdatathread = new Thread(dealdata); readthread.Start(); dealdatathread.Start();datapool = new Queue<string>();}*/ if(time>=0) time -= Time.deltaTime; if (time > 4) num = "5"; else if (time > 3) num = "4"; else if (time > 2) num = "3"; else if (time > 1) num = "2"; else num = "1"; if(!readthread.IsAlive) { readthread = new Thread(read); readthread.Start(); } if (!dealdatathread.IsAlive) { dealdatathread = new Thread(dealdata); dealdatathread.Start(); } } void read() { try { string myData = string.Empty; //builder = new StringBuilder(); byte[] buffer = new byte[1]; myport.Read(buffer, 0,1); if (buffer.Length == 0) { return; } else { foreach (byte b in buffer) { myData += b.ToString("X2"); datapool.Enqueue(myData); // builder.Append(b.ToString("X2")+" "); } } } catch(TimeoutException) { //Debug.Log("error"); } } void dealdata() { /* Byte[] buffer = new Byte[10]; myport.Read(buffer, 0, myport.ReadBufferSize); // myDataReceived = Encoding.ASCII.GetString(buffer); int index = myDataReceived.IndexOf("起始符"); if (index < 5) { if (myDataReceived[index + 5].Equals("/截止符")) { } } */ while (datapool.Count != 0) { for (int i = 0; i < datapool.Count; i++) { stroutpool += datapool.Dequeue(); if (stroutpool.Length == 16) { if (stroutpool.StartsWith("2101") && stroutpool.EndsWith("00FFFF")) { readnum++; wheelnum = Int16.Parse(stroutpool[4].ToString(), System.Globalization.NumberStyles.AllowHexSpecifier) * 16 + Int16.Parse(stroutpool[5].ToString(), System.Globalization.NumberStyles.AllowHexSpecifier); steerangle = Int16.Parse(stroutpool[6].ToString(), System.Globalization.NumberStyles.AllowHexSpecifier) * 16 + Int16.Parse(stroutpool[7].ToString(), System.Globalization.NumberStyles.AllowHexSpecifier) + Int16.Parse(stroutpool[9].ToString(), System.Globalization.NumberStyles.AllowHexSpecifier) * 16 * 16; if (readnum <= 10) { zero += steerangle; } if (readnum == 10) { zero = zero / 10; } if (readnum > 10) { // Debug.Log("zero:"+zero); outputangle = (steerangle - zero) / 2.822f; outputangle = Mathf.Clamp(outputangle, -90, 90); outputvelocity = (float)(wheelnum * 2/ 16 * 2 * Math.PI * 2.7); //Debug.Log("总的字符串:" + stroutpool); //Debug.Log("16进制杠数:" + stroutpool[4] + stroutpool[5]); // Debug.Log("10进制杠数:" + wheelnum);*/ // Debug.Log("16进制角度:" + stroutpool[9] + stroutpool[6] + stroutpool[7]); //Debug.Log("十进制速度:" + outputvelocity); //Debug.Log("十进制角度:" + outputangle); } } stroutpool = string.Empty; } } } } void OnGUI() { if (time > 0) { GUI.Box(new Rect(Screen.width / 2 - 50, Screen.height / 2 - 100, 100, 100), " "); GUI.Label(new Rect(Screen.width / 2 - 100, Screen.height / 2 - 100, 200, 200), num, style0); } } void OnApplicationQuit() { try { readthread.Abort(); dealdatathread.Abort(); myport.Close(); } catch { return; } }}