Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by HashEdits · May 28, 2020 at 10:21 PM · vrarduinoserialport

Unity crashes when connecting to a serial port

Hi, I wanted to create a VR game with a custom made controller made using Arduino.

I want to ready my values from my Arduino board by printing them on the serial port, the only problem is that when unity connects correctly to the port it just won't respond, I checked my task manager and it utilises 0% of my CPU.

I've tried to reduce my code to be as barebones as possible to avoid any errors but it seems like the crash shows up only when a correct connection is established between the COM port and Unity since my game will run just fine and display the appropriate error when the port is not available or I don't open the port.

It also appears that a connection has indeed started since when I try to open the Serial Monitor from the Arduino IDE it says that the port is busy.

Here's my Unity code:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using System.IO.Ports;
 
 public class ArduinoCommunication : MonoBehaviour
 {
 
     SerialPort stream = new SerialPort("COM1", 9600);
     public string RecevedString;
     public string[] Donnee;
     public string[] DonneRecu;
     // Start is called before the first frame update
     void Start()
     {
         stream.Open();
     }
 
     // Update is called once per frame
     void Update()
     {
         
         RecevedString = stream.ReadLine();
         //stream.BaseStream.Flush();
 
         /*string[] Donnee = RecevedString.Split(',');
         if (Donnee[0] != "" && Donnee[1] != "" && Donnee[2] != "" && Donnee[3] != "" && Donnee[4] != "" && Donnee[5] != "" && Donnee[6] != "" && Donnee[7] != "")
         {
             DonneRecu[0] = Donnee[0];
             DonneRecu[1] = Donnee[1];
             DonneRecu[2] = Donnee[2];
             DonneRecu[3] = Donnee[3];
             DonneRecu[4] = Donnee[4];
             DonneRecu[5] = Donnee[5];
             DonneRecu[6] = Donnee[6];
             DonneRecu[7] = Donnee[7];
             stream.BaseStream.Flush();
         }
     */
     }
 }


If you think you know how I can solve my problem make sure to respond, please.

Have a nice day!

Hash

Comment
Add comment · Show 3
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image viesc123 · May 29, 2020 at 08:00 PM 0
Share

I don't really know the final solution, but it might have something to do with Unity's single threaded nature.. You might want to check if the serial connection blocks the main thread, if you can release it or if you can run the connection on another thread.

avatar image HashEdits viesc123 · May 29, 2020 at 11:56 PM 0
Share

It was that!

Idk why it did block the main thread tho, but I followed this man's way of doing it and it works fine now:

https://answers.unity.com/questions/1696938/arduino-serial-delegate-help.html

I can even exchange data in both directions without any heckups now!

avatar image viesc123 HashEdits · May 30, 2020 at 10:05 AM 0
Share

You could now post your code and accept your own answer, to help other ppl find it quicker ;)

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

164 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Sending Data From Unity To Arduino 0 Answers

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

Two Arduino Boards for Same project 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges