一、PortControl脚本(硬件串口数据)
using UnityEngine;
using System.Collections;
using System.IO.Ports;
using System;
using System.Collections.Generic;
using System.Threading;
using System.Text;
using UnityEngine.UI;
public class PortControl : MonoBehaviour
{
public static PortControl Instance;
#region 定义串口属性
public string portName = "COM5";
public int baudRate = 115200;
public Parity parity = Parity.None;
public int dataBits = 8;
public StopBits stopBits = StopBits.One;
public SerialPort sp = null;
public Thread dataReceiveThread;
public Thread dataSendThread;
public List<byte> listReceive = new List<byte>();
public Queue<byte[]> recBuffer = new Queue<byte[]>();
public Queue<byte[]> sendBuffer = new Queue<byte[]>();
char[] strchar = new char[100];
string str;
byte[] buffer = new byte[1024];
byte[] receiveBuffer = new byte[1024];
int receivePosition;
#endregion
void Start()
{
Instance = this;
dataReceiveThread = new Thread(new ThreadStart(DataReceiveFunction));
dataReceiveThread.Start();
dataSendThread = new Thread(new ThreadStart(WriteData));
dataSendThread.Start();
}
#region 创建串口,并打开串口
public void OpenPort()
{
sp = new SerialPort(portName, baudRate, parity, dataBits, stopBits);
sp.ReadTimeout =800;
try
{
sp.Open