is anyone experiencing issues with their Uno R4 when trying to connect and send data to Unity? I wrote a simple script that prints 'Hello world' in the Arduino IDE serial monitor and also in the Unity console. It works fine with the Uno R3, but not with the R4. The message still appears in the serial monitor, but strangely, it's not showing up in the Unity console.
Does anyone have any ideas about what might be causing this problem?
Hi @khaleelasyraaf. It is possible we might be able to spot the cause in your "Hello world" sketch code.
I'll provide instructions you can follow to post your sketch here on the forum:
Select Tools > Auto Format from the Arduino IDE menus. ⓘ This is done to make the code easier for us to read.
Select Edit > Copy for Forum (Markdown) from the Arduino IDE menus.
In a forum reply here, click on the post composer field.
Press the Ctrl+V keyboard shortcut.
This will paste the sketch to the post composer.
Move the cursor outside of the code block markup before you add any additional text to your reply.
Repeat the above process if your sketch has multiple tabs.
Click the "Reply" button to post your reply.
When your code requires a library that's not included with the Arduino IDE please post a link to where you downloaded that library from, or if you installed it using Library Manager (Sketch > Include Library > Manage Libraries... in Arduino IDE) then say so and state the full name of the library.
void setup() {
// Start the serial communication at a baud rate of 9600
Serial.begin(9600);
}
void loop() {
Serial.println("Hello, world!");
delay(1000);
}
Unity:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.IO.Ports;
using System;
public class HelloWorldExample : MonoBehaviour
{
private SerialPort arduinoSerial;
// Start is called before the first frame update
void Start()
{
arduinoSerial = new SerialPort("COM36", 9600);
arduinoSerial.Open();
arduinoSerial.ReadTimeout = 100;
}
// Update is called once per frame
void Update()
{
if (arduinoSerial.BytesToRead > 0)
{
try
{
string data = arduinoSerial.ReadLine();
Debug.Log("Received from Arduino: " + data);
}
catch (System.TimeoutException)
{
Debug.LogWarning("Read timeout");
}
}
}
void OnDestroy()
{
if (arduinoSerial != null && arduinoSerial.IsOpen)
{
arduinoSerial.Close();
}
}
}
I wonder if there might be a need to adjust a few things given that it's the newest version of the Uno. Otherwise, it should also work since it works with our Uno R3.
Great job of creating an effective minimal sketch! That sketch should work just as well on UNO R4 WiFi as on UNO R3.
Did you verify that the serial port produced by the UNO R4 WiFi is COM36? The UNO R4 WiFi will be assigned a different serial port number by Windows than the UNO R3, so you must adjust this line of your Unity code when you switch boards.
thank you for the suggestion. I did verify the serial port to COM36 in the first place. The serial monitor displays the message, but for some reason, the Unity console doesn't show it...
Would the Unity program throw an exception (or give some other form of feedback) if there was a failure during the arduinoSerial.Open() call? I don't have any experience with Unity or C# in general.
One thing that might cause the opening of the port to fail would be having Serial Monitor open at the same time you are trying to get the data in the Unity console. Generally you can only have a serial port open in one application at a time. So if the port is already open in the Arduino IDE Serial Monitor, then your Unity program won't be able to open it.
Yes, the Serial Monitor was closed when the Unity program was running. I realized this issue when using the Uno R3 and fixed it. Oddly enough, it's working flawlessly with the Uno R3 but as soon as I plugged in the Uno R4, the message could not be printed.
Perhaps there's a line of code which we need to use to enable the message to be sent?
Arduino UNO R4 have a native USB serial port, so ... you MUST add one line after Serial.begin() ...
while ( !Serial ) delay ( 50 );
... that blocks your program until the serial port is opened and connected.
If, on the other hand, you want the program to be able to continue running after waiting a certain period, without the serial port being opened, you can add a timeout in the while ...