免费视频淫片aa毛片_日韩高清在线亚洲专区vr_日韩大片免费观看视频播放_亚洲欧美国产精品完整版

打開APP
userphoto
未登錄

開通VIP,暢享免費電子書等14項超值服

開通VIP
unity基礎(chǔ)開發(fā)--unity串口通訊
[csharp] view plain copy
  1. using UnityEngine;  
  2. using System.Collections;  
  3. using System.IO.Ports;  
  4. using System;  
  5. using System.Collections.Generic;  
  6. using System.Threading;  
  7.   
  8. public class PortControl : MonoBehaviour {  
  9.     public GUIText gui;  
  10.     //定義基本信息  
  11.     public string portName = "COM2";  
  12.     public int baudRate = 9600;  
  13.     public Parity parity = Parity.None;  
  14.     public int dataBits = 8;  
  15.     public StopBits stopBits = StopBits.One;  
  16.       
  17.     int[] data = new int[6];//用于存儲6位數(shù)據(jù)  
  18.     SerialPort sp = null;  
  19.     Thread dataReceiveThread;  
  20.     //發(fā)送  
  21.     string message = "";  
  22.     //byte[] message=new byte[8];  
  23.     void Start()  
  24.     {  
  25.         OpenPort();  
  26.         dataReceiveThread = new Thread(new ThreadStart(DataReceiveFunction));  
  27.         dataReceiveThread.Start();  
  28.     }  
  29.       
  30.     void Update()  
  31.     {  
  32.         string str = "";  
  33.         for(int i=0; i<data.Length; i++)  
  34.         {  
  35.             str += data[i].ToString() + " ";  
  36.         }  
  37.         gui.text = str;  
  38.         Debug.Log(str);  
  39.     }  
  40.       
  41.     public void OpenPort()  
  42.     {  
  43.         sp = new SerialPort(portName, baudRate, parity, dataBits, stopBits);  
  44.         sp.ReadTimeout = 400;  
  45.         try  
  46.         {  
  47.             sp.Open();  
  48.         }  
  49.         catch(Exception ex)  
  50.         {  
  51.             Debug.Log(ex.Message);  
  52.         }  
  53.     }  
  54.       
  55.     public void ClosePort()  
  56.     {  
  57.         try  
  58.         {  
  59.             sp.Close();  
  60.         }  
  61.         catch(Exception ex)  
  62.         {  
  63.             Debug.Log(ex.Message);  
  64.         }  
  65.     }  
  66.   
  67.     public void WriteData(string dataStr)  
  68.     {  
  69.         if(sp.IsOpen)  
  70.         {  
  71.             sp.Write(dataStr);  
  72.         }  
  73.   
  74.     }  
  75.       
  76.   
  77.     void OnApplicationQuit()  
  78.     {  
  79.         ClosePort ();  
  80.     }  
  81.       
  82.       
  83.     void DataReceiveFunction()  
  84.     {  
  85.         byte[] buffer = new byte[128];  
  86.         int bytes = 0;  
  87.         //定義協(xié)議  
  88.         int flag0 = 0xFF;  
  89.         int flag1 = 0xAA;  
  90.         int index = 0;//用于記錄此時的數(shù)據(jù)次序  
  91.         while (true)  
  92.         {  
  93.             if (sp != null && sp.IsOpen)  
  94.             {  
  95.                 try  
  96.                 {  
  97.                     bytes = sp.Read(buffer, 0, buffer.Length);  
  98.                     for(int i=0; i<bytes; i++)  
  99.                     {  
  100.   
  101.                         if(buffer[i]==flag0 || buffer[i]==flag1)  
  102.                         {  
  103.                             index = 0;//次序歸0   
  104.                             continue;  
  105.                         }  
  106.                         else  
  107.                         {  
  108.                             if(index>=data.Length)   index = data.Length-1;//理論上不應(yīng)該會進入此判斷,但是由于傳輸?shù)恼`碼,導(dǎo)致數(shù)據(jù)的丟失,使得標志位與數(shù)據(jù)個數(shù)出錯  
  109.                             data[index] = buffer[i];//將數(shù)據(jù)存入data中  
  110.                             index++;  
  111.                         }  
  112.   
  113.                     }  
  114.                 }  
  115.                 catch (Exception ex)  
  116.                 {  
  117.                     if (ex.GetType() != typeof(ThreadAbortException))  
  118.                     {  
  119.                         Debug.Log(ex.Message);  
  120.                     }  
  121.                 }  
  122.             }  
  123.             Thread.Sleep(10);  
  124.         }  
  125.     }  
  126.   
  127.   
  128.     void OnGUI()  
  129.     {  
  130.         message = GUILayout.TextField(message);  
  131.         if(GUILayout.Button("Send Message"))  
  132.         {  
  133.             WriteData(message);  
  134.         }  
  135.         string by=  "XX AA 03 31 20 51 00 00";  
  136.         if (GUILayout.Button("Send",GUILayout.Height(50)))  
  137.         {  
  138.             WriteData(by);  
  139.         }  
  140.     }  
  141.   
  142. }  

本站僅提供存儲服務(wù),所有內(nèi)容均由用戶發(fā)布,如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請點擊舉報。
打開APP,閱讀全文并永久保存 查看更多類似文章
猜你喜歡
類似文章
Vuforia SDK
C# SerialportHelper 串口通信 實例 | sql語句大全
巧用asp.net 過濾所有的Response請求并替換部分內(nèi)容,徹底解決MVC虛擬路徑問題.
c#(Socket)異步套接字代碼示例
[腳本]Unity3d之json解析研究
XMPP——Smack[4]狀態(tài),心情,頭像更改
更多類似文章 >>
生活服務(wù)
分享 收藏 導(dǎo)長圖 關(guān)注 下載文章
綁定賬號成功
后續(xù)可登錄賬號暢享VIP特權(quán)!
如果VIP功能使用有故障,
可點擊這里聯(lián)系客服!

聯(lián)系客服