简体   繁体   中英

Timeout Exception when trying to connect Arduino to Unity

I instantly get Timeout Exception when i press play on Unity This is my Unity code 'comment to skip error on stackoverflow comment to skip error on 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);     

    }


}

Arduino code

const int buttonPin01 = 6;
const int buttonPin02 = 7;
const int buttonPin03 = 8;

void setup() {
  Serial.begin(9600);

  pinMode(buttonPin01, INPUT);
  pinMode(buttonPin02, INPUT);
  pinMode(buttonPin03, INPUT);

  digitalWrite(buttonPin01, HIGH);
  digitalWrite(buttonPin02, HIGH);
  digitalWrite(buttonPin03, HIGH);


}

void loop() {
  if (digitalRead(buttonPin01) == LOW){
    Serial.write(1);
    Serial.flush();
    delay(10);
  }

  if (digitalRead(buttonPin02) == LOW){
    Serial.write(2);
    Serial.flush();
    delay(10);
  }

  if (digitalRead(buttonPin03) == LOW){
    Serial.write(3);
    Serial.flush();
    delay(10);
  }

}

My error

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)

I have tried changing the delay to a higher and lower value, changing the baud speed. Would appreciate it if someone could identify this problem ty

Change sp.ReadTimeout = 100; to sp.ReadTimeout = 5000;
100 millisecond is to fast for read
I fase the same problem and its work for me.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM