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();
    }
}
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

陌上桑AGO

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值