[Daily Unity3D] Unity3d and serial communication program development

I. Introduction

I remember writing an article on the development of Unity3D and serial communication program before, mainly talking about how to use the Unity3D program to send data to the serial port, you can check this article https://blog.csdn.net/q764424567/article/details / 78710739However, a friend
recently asked me how to receive the serial port program. Today I will share the more complete serial communication program developed by Unity, including sending and receiving data, and binding the serial number.

Second, the article link

Unity3d and serial communication program development, software and hardware combination
[Unity3D daily] Unity3d and serial communication program

3. Text

1. Let's
Insert picture description here
Insert picture description here
make a UI interface first

2. Code

using UnityEngine;
using System.IO.Ports;
using System.Text;
using UnityEngine.UI;

public class SerialPortTest : MonoBehaviour
{
    private SerialPort sp = new SerialPort();
    public Text m_TextSendDataPar;
    public Text m_TextShowData;

    // Use this for initialization
    void Start()
    {
        //打开串口
        Init("COM1", 9600, Parity.None, 8, StopBits.None);
    }

    //发送数据按钮
    public void Btn_SendData()
    {
        Data_Send(m_TextSendDataPar.ToString());
    }

    //初始化串口类
    public void Init(string _portName,int _baudRate,Parity _parity,int dataBits,StopBits _stopbits)
    {
        sp = new SerialPort(_portName, _baudRate, _parity, dataBits, _stopbits);//绑定端口
        sp.DataReceived += new SerialDataReceivedEventHandler(Data_Received);//订阅委托
    }

    //接收数据
    private void Data_Received(object sender, SerialDataReceivedEventArgs e)
    {
        byte[] ReDatas = new byte[sp.BytesToRead];
        sp.Read(ReDatas, 0, ReDatas.Length);//读取数据
        this.Data_Show(ReDatas);//显示数据
    }

    /// <summary>
    /// 显示数据
    /// </summary>
    /// <param name="data">字节数组</param>
    public void Data_Show(byte[] data)
    {
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < data.Length; i++)
        {
            sb.AppendFormat("{0:x2}" + "", data[i]);
        }
        Debug.Log(sb.ToString());
        m_TextShowData.text = sb.ToString();
    }

    //发送数据
    public void Data_Send(string _parameter)
    {
        sp.Open();
        sp.WriteLine(_parameter);
        sp.Close();
    }
}

3. Binding parameters
Insert picture description here
Insert picture description here
4. The problem of not finding the namespace

I quoted System.IO.Ports in unity but found that I could n’t check it before I saw that under [Edit-> Project Settings-> Player], modify [API Compatibility Level] of [Optimization] under [Other Settings] to [.NET 2.0] (The default is [.NET 2.0 Subset]. Only then can you find
* PS: Thanks for the reminder of the single looper
Insert picture description here

OK now. . . Guys can try it
. Right, the data is just received, and then displayed, how to parse it, let the friends come by themselves to get the port number. .

Published 226 original articles · praised 509 · 530,000 views

Guess you like

Origin blog.csdn.net/q764424567/article/details/101053821