免费视频淫片aa毛片_日韩高清在线亚洲专区vr_日韩大片免费观看视频播放_亚洲欧美国产精品完整版
打開APP
未登錄
開通VIP,暢享免費電子書等14項超值服
開通VIP
首頁
好書
留言交流
下載APP
聯(lián)系客服
unity基礎(chǔ)開發(fā)--unity串口通訊
有趣的永
>《待分類》
2017.06.04
關(guān)注
[csharp]
view plain
copy
using UnityEngine;
using System.Collections;
using System.IO.Ports;
using System;
using System.Collections.Generic;
using System.Threading;
public class PortControl : MonoBehaviour {
public GUIText gui;
//定義基本信息
public string portName = "COM2";
public int baudRate = 9600;
public Parity parity = Parity.None;
public int dataBits = 8;
public StopBits stopBits = StopBits.One;
int[] data = new int[6];//用于存儲6位數(shù)據(jù)
SerialPort sp = null;
Thread dataReceiveThread;
//發(fā)送
string message = "";
//byte[] message=new byte[8];
void Start()
{
OpenPort();
dataReceiveThread = new Thread(new ThreadStart(DataReceiveFunction));
dataReceiveThread.Start();
}
void Update()
{
string str = "";
for(int i=0; i<data.Length; i++)
{
str += data[i].ToString() + " ";
}
gui.text = str;
Debug.Log(str);
}
public void OpenPort()
{
sp = new SerialPort(portName, baudRate, parity, dataBits, stopBits);
sp.ReadTimeout = 400;
try
{
sp.Open();
}
catch(Exception ex)
{
Debug.Log(ex.Message);
}
}
public void ClosePort()
{
try
{
sp.Close();
}
catch(Exception ex)
{
Debug.Log(ex.Message);
}
}
public void WriteData(string dataStr)
{
if(sp.IsOpen)
{
sp.Write(dataStr);
}
}
void OnApplicationQuit()
{
ClosePort ();
}
void DataReceiveFunction()
{
byte[] buffer = new byte[128];
int bytes = 0;
//定義協(xié)議
int flag0 = 0xFF;
int flag1 = 0xAA;
int index = 0;//用于記錄此時的數(shù)據(jù)次序
while (true)
{
if (sp != null && sp.IsOpen)
{
try
{
bytes = sp.Read(buffer, 0, buffer.Length);
for(int i=0; i<bytes; i++)
{
if(buffer[i]==flag0 || buffer[i]==flag1)
{
index = 0;//次序歸0
continue;
}
else
{
if(index>=data.Length) index = data.Length-1;//理論上不應(yīng)該會進入此判斷,但是由于傳輸?shù)恼`碼,導(dǎo)致數(shù)據(jù)的丟失,使得標志位與數(shù)據(jù)個數(shù)出錯
data[index] = buffer[i];//將數(shù)據(jù)存入data中
index++;
}
}
}
catch (Exception ex)
{
if (ex.GetType() != typeof(ThreadAbortException))
{
Debug.Log(ex.Message);
}
}
}
Thread.Sleep(10);
}
}
void OnGUI()
{
message = GUILayout.TextField(message);
if(GUILayout.Button("Send Message"))
{
WriteData(message);
}
string by= "XX AA 03 31 20 51 00 00";
if (GUILayout.Button("Send",GUILayout.Height(50)))
{
WriteData(by);
}
}
}
本站僅提供存儲服務(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ù)
首頁
萬象
文化
人生
生活
健康
教育
職場
理財
娛樂
藝術(shù)
上網(wǎng)
留言交流
回頂部
聯(lián)系我們
分享
收藏
點擊這里,查看已保存的文章
導(dǎo)長圖
關(guān)注
一鍵復(fù)制
下載文章
綁定賬號成功
后續(xù)可登錄賬號暢享VIP特權(quán)!
如果VIP功能使用有故障,
可點擊這里聯(lián)系客服!
聯(lián)系客服
微信登錄中...
請勿關(guān)閉此頁面
先別劃走!
送你5元優(yōu)惠券,購買VIP限時立減!
5
元
優(yōu)惠券
優(yōu)惠券還有
10:00
過期
馬上使用
×