unity 串口编程

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 SerialPortReciever : MonoBehaviour
{

    private SerialPort sp;
    private Queue<string> queueDataPool;
    private Thread tPort;
    private Thread tPortDeal;
    private string strOutPool = string.Empty;
    string finalstring = string.Empty;
    string tempstring = string.Empty;
    // Use this for initialization
    void Start()
    {
        queueDataPool = new Queue<string>();

        sp = new SerialPort("COM3", 9600, Parity.None, 8, 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[1];
            string sbReadline2str = string.Empty;
            if (sp.IsOpen) sp.Read(buf, 0, 1);
            if (buf.Length == 0)
            {
                return;
            }
            if (buf != null)
            {
                for (int i = 0; i < buf.Length; i++)
                {
                    sbReadline2str += buf[i].ToString("X2");
                    queueDataPool.Enqueue(sbReadline2str);
                }
            }
        }
        catch (Exception ex)
        {
            Debug.Log(ex);
        }
    }
    private void DealData()
        //处理数据
    {
        while (queueDataPool.Count != 0)
        {
            for (int i = 0; i < queueDataPool.Count; i++)
            {
                strOutPool+= queueDataPool.Dequeue();
                if(strOutPool.Length == 16)
                                {
                                        Debug.Log(strOutPool);       //可以打印到控制台
                                        strOutPool=string.Empty;
                                }
            }

        }
    }

    private void SendSerialPortData(string data)
        //发送数据
        {
                if(sp.IsOpen)
                {
                        sp.WriteLine(data);
            Debug.Log(data );
        }
        }



    public void ButtonClick()
    {

        //实际串口代码是   FE 05 00 00 FF 00 98 35
        byte[] buffer = { 0xFE, 0x05, 0x00, 0x00, 0xFF, 0x00, 0x98, 0x35 };

        sp.Write(buffer, 0, buffer.Length);

    }



    public void ButtonClick1()
    {
        //实际串口是 FE 05 00 00 00 00 D9 C5

        byte[] buffer = { 0xFE, 0x05, 0x00, 0x00, 0x00, 0x00, 0xD9, 0xC5 };

        sp.Write(buffer, 0, buffer.Length);

    }

    void OnApplicationQuit()
    {
        sp.Close();
    }
}

阅读更多
上一篇unity vr位移
下一篇unity 调用摄像头
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

关闭
关闭
关闭