I should point out that there is a downside to sending commands to the Arduino.
Sp.Close() //You can't program the Arduino while the serial port is open, so let's close it. Sp.WriteTimeout = 1 //Shortest possible write time out.ĭebug.LogError( "Serial port: " + sp.PortName + " is unavailable") Sp.ReadTimeout = 1 //Shortest possible read time out. A 10 uF cap across RST and GND will prevent this. Sp.DtrEnable = false //Prevent the Arduino from rebooting once we connect to it. Sp = new SerialPort( "COM4", 115200, Parity.None, 8, StopBits.One) //Replace "COM4" with whatever port your Arduino is on.
UNITY SERIAL LIBRARY ARDUINO CODE
All code examples assume you will be working in Unity/C# and the Arduino IDE.Ĭreate a new C# script (I called mine "Interface") in Unity and attach it to any game object. Let's begin by brute-force connecting to the Arduino and sending our first command over serial. Because of this, I was forced to manually connect to the Arduino, verify that the device I'm talking to is the Arduino controller, and poll for incoming serial data myself. Namely, the event that is fired whenever there is incoming serial data does not work. The problem is, Unity, which uses the Mono compiler, does not fully support all of the great. My professor at the time previously expressed a great interest in hobby electronics and Arduinos, so I thought, "This is a perfect chance to create something that I love to work with too!" I forked my previous attempt at a C# serial communication system and began work on the "new and improved" version for my final project immediately.Ĭ# actually has pretty great support for serial ports right out of the box. In my final semester in the game program at my college, I felt that I needed to end my four year journey with something special - something that hasn't been done before at this school. It was a neat experiment, but it was ultimately shelved. It honestly didn't take too long before I had a basic proof-of-concept program that could set pins high and low on an Arduino board, but my original system was not very robust, reliable, or even intuitive to use.
UNITY SERIAL LIBRARY ARDUINO MAC
Then go to Edit | Project Settings | Player | PC, Mac & Linux Standalone settings | Other Settings | Optimization | API Compatibility Level and select ".Net 2.0". In some circonstances, the setting switches back to another platform. It should be "PC, Mac & Linux standalone". Troubleshooting Error CS0234 Ports does not exist in the namespaceĮrror CS0234: The type or namespace name `Ports' does not exist in the namespace `System.IO'.įirst make sure the correct platform is selected in File | Build Settings.
whether you wand debug informations in console.if not not already, the serial port will be opened automatically.ĭrop the SerialConfig component to an empty GameObject in your scene and configure:.from any script, call the static functions Serial.write() or Serial.writeLn().from any script, use the static props Serial.ReceivedBytesCount, Serial.ReceivedBytesĪnd don't forget to call ClearReceivedBytes() to avoid overflowing the buffer.} Usage 2: Receive data (when you don't expect line breaks)