C# 尝试将Arduino连接到Unity时出现超时异常

C# 尝试将Arduino连接到Unity时出现超时异常,c#,unity3d,arduino,serial-port,timeoutexception,C#,Unity3d,Arduino,Serial Port,Timeoutexception,当我按下Unity上的play键时,立即出现超时异常 这是我的统一代码“stackoverflow上的注释跳过错误stackoverflow上的注释跳过错误” using UnityEngine; using System.Collections; using System.IO.Ports; using System.Threading; [RequireComponent(typeof(Controller2D))] public class Player : MonoBe

当我按下Unity上的play键时,立即出现超时异常 这是我的统一代码“stackoverflow上的注释跳过错误stackoverflow上的注释跳过错误”

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

[RequireComponent(typeof(Controller2D))]   

public class Player : MonoBehaviour {

    public float moveSpeed = 6;
    public float gravity = -20;
    public float jumpDistance = 8;
    Vector3 moveDistance;

    SerialPort sp = new SerialPort("COM7", 9600);

    Controller2D controller;    
    void Start() {
        controller = GetComponent<Controller2D>();  
        sp.Open();
        sp.ReadTimeout = 100;
    }

    void Update() {
        if (sp.IsOpen) {
            try {
                print(sp.ReadByte());

            }
            catch (System.Exception) {

                throw;
            }
        }

        if (controller.collisions.above || controller.collisions.below) {   
            moveDistance.y = 0;
        }
        if (Input.GetKeyDown(KeyCode.Space) || sp.ReadByte() == 1 && controller.collisions.below) {   
            moveDistance.y = jumpDistance;
        }
        Vector2 input = new Vector2(Input.GetAxisRaw("Horizontal"), Input.GetAxisRaw("Vertical"));  


        moveDistance.x = input.x * moveSpeed;   
        moveDistance.y += gravity * Time.deltaTime;     
        controller.Move(moveDistance * Time.deltaTime);     

    }


}
我的错误

TimeoutException: The operation has timed out.
System.IO.Ports.WinSerialStream.Read (System.Byte[] buffer, System.Int32 offset, System.Int32 count) (at <3845a180c26b4889bc2d47593a665814>:0)
System.IO.Ports.SerialPort.read_byte () (at <3845a180c26b4889bc2d47593a665814>:0)
System.IO.Ports.SerialPort.ReadByte () (at <3845a180c26b4889bc2d47593a665814>:0)
(wrapper remoting-invoke-with-check) System.IO.Ports.SerialPort.ReadByte()
Player.Update () (at Assets/Script/Player.cs:35)
TimeoutException:操作已超时。
System.IO.Ports.WinSerialStream.Read(System.Byte[]缓冲区、System.Int32偏移量、System.Int32计数)(位于:0)
System.IO.Ports.SerialPort.read_byte()(位于:0)
System.IO.Ports.SerialPort.ReadByte()(位于:0)
(带检查的包装器远程调用)System.IO.Ports.SerialPort.ReadByte()
Update()(位于Assets/Script/Player.cs:35)
我尝试过将延迟更改为更高和更低的值,更改波特率。
如果有人能发现这个问题,我们将不胜感激。ty

Change
sp.ReadTimeout=100
sp.ReadTimeout=5000
100毫秒对于读取来说太快了

我遇到了同样的问题,也遇到了同样的问题。

Change
sp.ReadTimeout=100
sp.ReadTimeout=5000
100毫秒对于读取来说太快了

我遇到了同样的问题,它对我也有作用。

您是否尝试过切换
sp.Open()
sp.ReadTimeout=100
(将后者放在第一位)的顺序,以便在尝试不同的值时,可以确保它在Open()之前正确设置启动?对我不起作用您是否尝试过切换
sp.Open()
sp.ReadTimeout=100
(将后者放在第一位)的顺序,以便在尝试不同的值时,确保在Open()启动之前已正确设置?对我不起作用
TimeoutException: The operation has timed out.
System.IO.Ports.WinSerialStream.Read (System.Byte[] buffer, System.Int32 offset, System.Int32 count) (at <3845a180c26b4889bc2d47593a665814>:0)
System.IO.Ports.SerialPort.read_byte () (at <3845a180c26b4889bc2d47593a665814>:0)
System.IO.Ports.SerialPort.ReadByte () (at <3845a180c26b4889bc2d47593a665814>:0)
(wrapper remoting-invoke-with-check) System.IO.Ports.SerialPort.ReadByte()
Player.Update () (at Assets/Script/Player.cs:35)