Using the Unity 5 game engine, something I have a lot of experience with, I thought I'd be able to create a quick way to read data from the serial port sent from an Uno board and a gyro.
I have started off with something quite basic, using 2 buttons that write a 1 or 2 to the serial port on COM4 with a bitrate of 9600 bps.
In Unity, I wrote a short script to detect these inputs and act depending on the value (1 or 2).
Both scripts are below, but errors in Unity keep throwing saying the operation on the SerialPort have timed out, it also seems incapable of finding the data/file name.
Does anyone know what I am doing wrong on either end?
Unity Error
System.TimeoutException: The operation has timed-out.
at System.IO.Ports.WinSerialStream.Read (System.Byte[] buffer, Int32 offset, Int32 count) [0x00000] in <filename unknown>:0
at System.IO.Ports.SerialPort.read_byte () [0x00000] in <filename unknown>:0
at System.IO.Ports.SerialPort.ReadByte () [0x00000] in <filename unknown>:0
at (wrapper remoting-invoke-with-check) System.IO.Ports.SerialPort:ReadByte ()
at ArduinoInput.Update () [0x00022] in D:\Labyrith\Tabletop\Assets\ArduinoInput.cs:26
UnityEngine.MonoBehaviour:print(Object)
ArduinoInput:Update() (at Assets/ArduinoInput.cs:34)
I keep getting "not responding" issues with Unity like it's crashing every time it tries to open/read the serial port.
I have tried this on 2 computers running different OS's and I still get the same issue, either Unity is somehow incapable of handling the input or the Arduino itself is sending something it doesn't like.
Has anyone else ever tried to receive Arduino data in Unity before (and succeeded)?
When a port is opened then the Arduino is reset. Then the boot loader sends a request to see if code needs to be uploaded. It is likely that this code is upsetting your PC program.
You might like to try and disable the auto reset function with a large capacitor.
You don't have Serial Monitor open while you try to receive data in Unity, right? Cause that would cause an issue.
I'm not really sure what's wrong with your code, I was able to receive Serial data in Unity using C# in the past, which is what it looks like you are doing. However, instead of using ReadByte() I did ReadLine().
I highly suggest putting your serial processing in Unity on a separate thread. If it is continuously looking for data, that will stop other things in Unity from running well unless they are on separate threads, at least in my experience when I did something similar.
Also... if you do if(sp.ReadByte() != 0) and then print(sp.ReadByte()) wouldn't it proceed one extra byte afterwards and not actually read the intended byte? It's probably better to store your data in a temporary variable and then do checks on it.
Edit: actually, that seems to be where your error lies... It is a timeout exception, which means it was trying to read data but timed out when there wasn't any (I think?). In this case, I think you are trying to read more data than there actually is, by running sp.ReadByte() one extra time by accident after the if statement. Try only using sp.ReadByte() once and see if that fixes it