Unity使用串口需要先进入playersettings里面设置.Net2.0
using UnityEngine;
using System.Collections;
using System.IO.Ports;
using System;
using System.Collections.Generic;
using System.Threading;
using System.IO;
using UnityEngine.UI;
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位数据
SerialPort sp = null;//串口控制
Thread dataReceiveThread;
//发送
string message = "";
string filename;
//byte[] message=new byte[8];
void Start( )
{
filename = Application.dataPath + @"\StreamingAssets\配置文件.txt";
portName = ReadALine(filename, 1);
baudRate = int.Parse(ReadALine(filename, 3));
OpenPort();//打开串口
dataReceiveThread = new Thread(new ThreadStart(DataReceiveFunction));//数据接收线程
dataReceiveThread.Start();
WriteData(strToToHexByte("FE 0F 00 00 00 08 01 FF F1 D1"));
}
void Update()
{
//string str = "";
//for (int i = 0; i < data.Length; i++)
//{
// str += data[i].ToString() + " ";
//}
//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(byte[] bys)
{
if (sp.IsOpen)
{
sp.Write(bys,0,bys.Length);
}
}
void OnApplicationQuit()
{
ClosePort();
}
void DataReceiveFunction()//数据接收功能
{
byte[] buffer = new byte[128];
int bytes = 0;
//定义协议
int flag0 = 0xFF;
int flag1 = 0xAA;
int index = 0;//用于记录此时的数据次序
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;//理论上不应该会进入此判断,但是由于传输的误码,导致数据的丢失,使得标志位与数据个数出错
data[index] = buffer[i];//将数据存入data中
index++;
}
}
}
catch (Exception ex)
{
if (ex.GetType() != typeof(ThreadAbortException))
{
}
}
}
Thread.Sleep(10);
}
}
public void Btn(int i )//
{
string by=null;
if (i == 0)
{
by = "FE 05 00 00 FF 00 98 35";
Debug.Log(by);
}
if (i == 1)
{
by = "FE 05 00 01 FF 00 C9 F5";
}
if (i == 2)
{
by = "FE 05 00 02 FF 00 39 F5";
}
if (i == 3)
{
by = "FE 05 00 03 FF 00 68 35";
}
if (i == 4)
{
by = "FE 05 00 04 FF 00 D9 F4";
}
if (i == 5)
{
by = "FE 05 00 05 FF 00 88 34";
}
if (i == 6)
{
by = "FE 05 00 06 FF 00 78 34";
}
if (i == 7)
{
by = "FE 05 00 07 FF 00 29 F4";
}
//if (i == 8)//全开
//{
// by = "FE 0F 00 00 00 08 01 FF F1 D1";
//}
WriteData(strToToHexByte("FE 0F 00 00 00 08 01 00 B1 91"));
StartCoroutine(Wirte(strToToHexByte(by)));
}
public void CloseD(int i)
{
WriteData(strToToHexByte("FE 0F 00 00 00 08 01 FF F1 D1"));//全开
}
public string ReadALine(string FileName, int linenumber) // 参数1:打开的文件(路径+文件命),参数2:读取的行数
{
string[] strs = File.ReadAllLines(FileName);
if (linenumber == 0)
{
return "0";
}
else
{
return strs[linenumber];
}
}
private static byte[] strToToHexByte(string hexString)
{
hexString = hexString.Replace(" ", "");
if ((hexString.Length % 2) != 0)
hexString += " ";
byte[] returnBytes = new byte[hexString.Length / 2];
for (int i = 0; i < returnBytes.Length; i++)
returnBytes[i] = Convert.ToByte(hexString.Substring(i * 2, 2), 16);
return returnBytes;
}
IEnumerator Wirte( byte[] bsy)
{
yield return new WaitForSeconds(0.5f);
WriteData(bsy);
}
}