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", , Parity.None, , 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[];
string sbReadline2str = string.Empty;
if (sp.IsOpen) sp.Read(buf, , );
if (buf.Length == )
{
return;
}
if (buf != null)
{ for (int i = ; 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 > )
{
List<byte> cmd = DataList[]; StringBuilder sb = new StringBuilder();
for (int i = ; i < cmd.Count; i++)
{
sb.Append(cmd[i] + " ");
} Debug.Log(sb.ToString()); DataList.RemoveAt();
} }
}
catch (Exception ex)
{
Debug.Log(ex);
}
}
private void DealData()
{
while (queueDataPool.Count != )
{
for (int i = ; i < queueDataPool.Count; i++)
{
strOutPool += queueDataPool.Dequeue();
if (strOutPool.Length == )
{
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()))
{
byte[] buffer = { 0xAA, 0x00, 0x22, 0x00, 0x00, 0x22, 0x8E };
sp.Write(buffer, , buffer.Length);
} }
}

最新文章

  1. 常用str函数
  2. 学习mongo系列(七)aggregate() ,$group() 管道
  3. android.view.WindowLeaked
  4. HDU 1087 Super Jumping! Jumping! Jumping
  5. 开发自己的cordova插件
  6. mongodb 教程一
  7. 一条insert语句批量插入多条记录
  8. (转)iOS消息推送机制中pem文件的生成
  9. ASPxGridView-为每行添加序号
  10. 拷贝构造函数,深拷贝,大约delete和default相关业务,explicit,给定初始类,构造函数和析构函数,成员函数和内联函数,关于记忆储存,默认参数,静态功能和正常功能,const功能,朋友
  11. 使用Net.Mail、CDO组件、JMail组件三种方式发送邮件
  12. 关于sql语句between and的边界问题
  13. “玲珑杯”ACM比赛 Round #4 B Best couple
  14. url重定向或者重写
  15. Android Remote Views
  16. cpp实验二
  17. linux内存源码分析 - 内存池
  18. win10默认壁纸位置
  19. Selenium:利用select模块处理下拉框
  20. 用spring tool suite插件创建spring boot项目时报An internal error occurred during: &quot;Building UI model&quot;. com/google/common/

热门文章

  1. sql server 的变量
  2. xitong
  3. DevExpress v18.1新版亮点——Windows 10 UWP篇
  4. 打开WORD文档出错提示
  5. workflow
  6. Linux内核参数优化记录
  7. apache中的RewriteCond、RewriteRule
  8. Redis (error) NOAUTH Authentication required.解决方法
  9. django页面导出excel
  10. webbench-1.5_hacking