- Home /
Need help with Arduino to Unity "IO:Exception Access is Denied",Need help with sending data from Arduino to Unity "IO:Exception Access is Denied"
Hello,
I seem to be having an issue with using an Arduino to control a cube objects within a scene. When I apply my code to in the scene I get the error in the Unity console"IO: Exception Access is Denied". Can any of you experts help me out? Thanks a million.
My C# code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.IO.Ports;
public class MoveHand : MonoBehaviour {
public float increment;
SerialPort stream = new SerialPort("COM5", 9600);
// Use this for initialization`
void Start () {
stream.Open();
stream.ReadTimeout = 25;
}
// Update is called once per frame`
void Update () {
Vector3 temp = transform.position;
if (stream.IsOpen) {
try {
float data = stream.ReadByte();
data = Mathf.Clamp(data, 5, 25);
data -= 5;
data /= 20;
data *= 10;
data -= 5;
temp.x = data;
} catch (System.Exception) {
Debug.Log("timeout");
}
transform.position = temp;
}
}
}
My Arduino code:
const int TRIG = 9;
const int ECHO = 10;
void setup() {
Serial.begin(9600);
pinMode(TRIG, OUTPUT);
pinMode(ECHO, INPUT);
}
void loop() {
int data = GetUltra(TRIG, ECHO);
Serial.write(data);
delay(20);
}
double GetUltra (int trig, int echo){
digitalWrite(trig , LOW);
delayMicroseconds(2);
digitalWrite(trig, HIGH);
delayMicroseconds(8);
digitalWrite(trig, LOW);
double distance = ( pulseIn(echo, HIGH) )* 343.2 / 20000;
return distance;
}
Are you running the console/debugger for Arduino right now? I worked with Arduino/Unity once and would get this error if I attempted to play the Unity scene while the Arduino console was connected to the device.
Your answer
Follow this Question
Related Questions
Need Help Connecting Arduino to Unity 0 Answers
Connect Android and Arduino with OTG Cable 1 Answer
SerialPort doesn't work on Xcode simulator 0 Answers
Unity crashes when connecting to a serial port 0 Answers
Sending Data From Unity To Arduino 0 Answers