Unity3D版本2020.3.30f1中读取串口的最佳实践?

发布于 2025-01-11 16:51:19 字数 3019 浏览 1 评论 0原文

我有一个正在向串行端口发送数据的 MCU。我想让Unity3D读取数据。到目前为止,我还没有发现任何有效的方法,我正在寻求一些帮助来让 Unity 读取串行端口。

预期的行为是将串行数据保存到字符串中,以便对所包含的数据执行操作。

目前,当我运行 Unity 编辑器时,一切都很好,直到我真正尝试读取串行端口。一旦尝试读取串行端口,Unity 编辑器就会完全冻结。编辑器内没有错误日志,没有控制台错误(因为它被冻结),并且编辑器不响应我的输入(单击、滚动等)。

此时的解决办法是打开任务管理器并结束Unity编辑器的任务。

我当前的实现是利用线程,但是,我正在寻找读取串行端口的最佳实践。也许是协程?无论如何,这就是我到目前为止所拥有的。

    using System.Collections;
    using System.Collections.Generic;
    using System.Threading;
    using System.IO.Ports;
    using UnityEngine;

    public class Arduino_Read_Serial : MonoBehaviour
    {
        [Header("Settings")]
        [SerializeField] private bool enableReadThread = false;

        private SerialPort data_stream = new SerialPort("COM6", 115200);
        [SerializeField] private string receivedString;

        private Thread readThread;

        void Start()
        {
            data_stream.WriteTimeout = 300;
            data_stream.ReadTimeout = 5000;
            data_stream.DtrEnable = true;
            data_stream.RtsEnable = true;
            data_stream.Open();
            if (data_stream.IsOpen)
            {
                readThread = new Thread(ReadThread);
            }
        }

        void Update()
        {
            if (enableReadThread && data_stream.IsOpen)
            {
                readThread.Start();
                enableReadThread = false;
            }
            else
            {
                Debug.Log("Pulse...");
            }

            if (receivedString != "")
            {
                //readThread.
            }
            //string[] datas = receivedString.Split(',');
            //Debug.Log(datas);
        }

        void ReadThread()
        {
            while (data_stream.IsOpen)
            {
                receivedString = data_stream.ReadLine();
            }
        }

        public void OnApplicationQuit()
        {
            Debug.Log("Thread stopped...");
            readThread.Abort();
        }
    }

任何有关在 Unity 中读取串行端口的最佳实践的任何见解都将受到高度赞赏。

这些是我尝试使用的答案,但它们都不起作用:

[1] Unity Serial.ReadLine()问题

[2] https://answers.unity.com/questions/1696938/arduino-serial-delegate-help.html

[3] https://answers.unity.com/questions/1735855/unity-crashes-when-connecting-to-a-serial-port.html

[4] https://forum.unity.com/threads/cant-use-system-io-ports.141316/

[5] https://www.youtube.com/watch?v=5ElKFY3N1zs

I have a MCU that is sending data to serial port. I want Unity3D to read the data. So far, nothing I have found works and I'm looking for some help on getting Unity to read the serial port.

Expected behavior is for the serial data to be saved to a string in order to perform operations with the data contained.

At the moment, when I run the Unity editor everything is fine until I actually try to read the serial port. As soon as an attempt is made to read the serial port the Unity editor totally freezes. There are no error logs, no console errors within the editor (since it is frozen), and the editor does not respond to my inputs (clicks, scrolls, etc.).

The solution at this point is to open the task manager and end the task for the Unity editor.

My current implementation is utilizing threads, however, I'm looking for the best practice to read serial port. Perhaps coroutines? Anyway, here's what I have so far.

    using System.Collections;
    using System.Collections.Generic;
    using System.Threading;
    using System.IO.Ports;
    using UnityEngine;

    public class Arduino_Read_Serial : MonoBehaviour
    {
        [Header("Settings")]
        [SerializeField] private bool enableReadThread = false;

        private SerialPort data_stream = new SerialPort("COM6", 115200);
        [SerializeField] private string receivedString;

        private Thread readThread;

        void Start()
        {
            data_stream.WriteTimeout = 300;
            data_stream.ReadTimeout = 5000;
            data_stream.DtrEnable = true;
            data_stream.RtsEnable = true;
            data_stream.Open();
            if (data_stream.IsOpen)
            {
                readThread = new Thread(ReadThread);
            }
        }

        void Update()
        {
            if (enableReadThread && data_stream.IsOpen)
            {
                readThread.Start();
                enableReadThread = false;
            }
            else
            {
                Debug.Log("Pulse...");
            }

            if (receivedString != "")
            {
                //readThread.
            }
            //string[] datas = receivedString.Split(',');
            //Debug.Log(datas);
        }

        void ReadThread()
        {
            while (data_stream.IsOpen)
            {
                receivedString = data_stream.ReadLine();
            }
        }

        public void OnApplicationQuit()
        {
            Debug.Log("Thread stopped...");
            readThread.Abort();
        }
    }

Any insight on some best practices for reading the serial port in Unity would be supremely appreciated.

These are the answers I attempted to use but none of them are working:

[1] Unity Serial.ReadLine() issue

[2] https://answers.unity.com/questions/1696938/arduino-serial-delegate-help.html

[3] https://answers.unity.com/questions/1735855/unity-crashes-when-connecting-to-a-serial-port.html

[4] https://forum.unity.com/threads/cant-use-system-io-ports.141316/

[5] https://www.youtube.com/watch?v=5ElKFY3N1zs

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文