我是靠谱客的博主 敏感白昼,最近开发中收集的这篇文章主要介绍unity串口相关,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

串口读取

public void GetValue(object data)
 {
 var info = data as string;
 if (info == null)
 { 
 return;
 }
 info = info[0].ToString();//得到RFID
 
 }

串口发送和读取(byte类型)(完整程序实例:读取RFID播放音频

using UnityEngine;
using System;
using System.IO.Ports;
using UnityEngine.UI;
using System.Threading;
using System.IO;
using LitJson;
using UnityEngine.Video;
using DG.Tweening;
using System.Collections;

[System.Serializable]
public class RFID
{
    public string port;
    public string[] rfid = new string[5];
    public string[] path = new string[5];
}
public class spSend : MonoBehaviour
{
    public SerialPort Port;
    public RFID RFID = new RFID();
    Thread dataReceiveThread;
    private string _JsonPath;
    private byte[] _SendData = new byte[4] { 0x04, 0x01, 0xDC, 0x1E };
    private int _length = 0;
    private string RfidRead = "";
    public string RfidNow = "";//读取到的RFID 
    public AudioSource Audio;//音频播放
    void Awake()
    {
        _JsonPath = Application.dataPath + "/StreamingAssets/configData.text";
        load();
    }
    void Start()
    {
        Port = new SerialPort(RFID.port, 19200, Parity.None, 8, StopBits.One);
        Port.Open();
        if (Port.IsOpen)
        {
            Debug.Log("串口打开成功");
        }
        else
        {
            Debug.Log("串口打开失败");
        }
        dataReceiveThread = new Thread(new ThreadStart(DataReceiveFunction));//开启线程发送和接收
        dataReceiveThread.Start();
    }
    void OnApplicationQuit()
    {
        Port.Close();
        Application.Quit();
    }
    private void Update()
    {
        if (RfidNow != RfidRead)
        {
            RfidNow = RfidRead;
            if (RfidNow == "")
            {
                Debug.Log("拿走RFID");            
            }
            else
            {
                Debug.Log("接收到RFID:" + RfidNow);
                int i;
                for (i = 0; i < RFID.rfid.Length; i++)
                {
                    if (RfidNow == RFID.rfid[i])
                    {
                        StartCoroutine(LoadAudio(RFID.path[i]));                      
                        Debug.Log("正确的RFID,播放音频:" + RFID.path[i]);
                        break;
                    }
                }
                if (i == RFID.rfid.Length)
                {
                    Debug.Log("没有该RFID");
                }
            }
        }      
    }
    void DataReceiveFunction()//接收数据
    {
        byte[] buffer = new byte[1024];
        while (true)
        {
            if (Port != null && Port.IsOpen)
            {
                Port.Write(_SendData, 0, _SendData.Length);
                Thread.Sleep(100);              
                    _length += Port.Read(buffer, _length, buffer.Length - _length);//接收字节                                 
                    if (_length == buffer[0] && _length != 5)
                    {
                        check_rfid(buffer);
                    }
                    else
                    {
                        RfidRead = "";
                    }
                    _length = 0;                         
            }
            Thread.Sleep(100);
        }
    }
    void check_rfid(byte[] buffer)//检查数据
    {
        int _length = buffer[0];
        int cards_number = (_length - 5) / 8;
        byte[][] vector = new byte[cards_number][];
        for (int i = 0; i < cards_number; i++)
        {           
            vector[i] = new byte[8];
            Array.Copy(buffer, 4 + i * 8, vector[i], 0, 8);
        }
        string UUID = "";
        for (int i = 0; i < 8; i++)
        {
            UUID += vector[0][i].ToString("X");//rfid
        }
        RfidRead = UUID;
    }
    void load()//读取
    {
        if (File.Exists(_JsonPath))
        {
            StreamReader sr = new StreamReader(_JsonPath);
            string jsonstr = sr.ReadToEnd();
            sr.Close();
            RFID = JsonMapper.ToObject<RFID>(jsonstr);
        }
        else
        {
            Debug.Log("json文件不存在");
        }
    }
    IEnumerator LoadAudio(string backPath)//加载音频
    {
        WWW www = new WWW(Application.dataPath + "/StreamingAssets/music/" + backPath);
        yield return www;
        AudioClip clip = www.GetAudioClip();
        Audio.clip = clip;
        Audio.Play();
    }
}

最后

以上就是敏感白昼为你收集整理的unity串口相关的全部内容,希望文章能够帮你解决unity串口相关所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(58)

评论列表共有 0 条评论

立即
投稿
返回
顶部