Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 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
1
Question by Mikeanzi · Aug 04, 2021 at 03:33 PM · problem during runtimecommunicationarduinoport

Help with Unity communicating with Arduino (serial port communication)

Hi everyone. Please bear with me as I am fairly new to both Unity and Arduino so I will most likely butcher the typical terminology of most things. I am working on a project where I am using the encoder counts on a motor to control the position of a drillbit model within Unity. The encoder position is written to the Arduino as a float with a baud rate of 115200. I have it writing to COM Port 4 which is then being read by Unity. I am encountering a problem where, quite frequently, the strings being read by Unity do not match what is being written by Arduino. For instance, say Arduino is writing 0.0152, the console in Unity will say 00152, or 0.152. Either it will forget a decimal or a digit entirely and will cause the system to glitch as the incorrect value is being read in Unity. I have noticed that when I run the program on my desktop computer which has a strong processor and graphics card, I have this problem far less compared to when I run the program on my laptop, which is much less powerful. This makes me think it is just a processing issue but I am not entirely sure. If anyone has any suggestions to stop Unity from incorrectly reading the Arduino values, that would be amazing. If there is any additional information that you need to help you better understand the problem, please don't hesitate to ask.

Here is the Unity code:

using System.Collections; using System.Collections.Generic; using UnityEngine; using System.IO.Ports;

public class ArduinoCommunication : MonoBehaviour { // Data Variables public float piecePosition; public float pieceRotationSpeed; public float clearedInput; public float betweenInput; public float cloggedInput;

 // Data Communication
 private SerialPort stream;
 private string data;
 private bool dataFound = false;
 private List<float> parsedData;

 void Start()
 {
     stream = new SerialPort(PlayerPrefs.GetString("COM"), 115200);
     stream.Open();
     stream.ReadTimeout = 101;

     Debug.Log(PlayerPrefs.GetString("COM"));
 }

 void Update()
 {
     try
     {
         data = stream.ReadExisting();
         parsedData = ParseFile(data);
         SetDataVariables();
         dataFound = true;
         Debug.Log("Data found.");
     }
     catch (System.Exception)
     {
         dataFound = false;
         Debug.Log("No data found.");
     }
 }

 private List<float> ParseFile(string text)
 {
     char[] separators = {','};
     string[] strValues = text.Split(separators);

     List<float> intValues = new List<float>();
     foreach (string str in strValues)
     {
         float val = 0;
         if (float.TryParse(str, out val))
             intValues.Add(val);
     }
     return intValues;
 }

 private void SetDataVariables()
 {
     piecePosition = parsedData[0];
     pieceRotationSpeed = parsedData[1];
     clearedInput = parsedData[2];
     betweenInput = parsedData[3];
     cloggedInput = parsedData[4];
 }

}

Also here is a photo from Unity's console showing the incorrect value being produced. alt text

img-0021-1.png (450.3 kB)
Comment
Add comment
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

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

124 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

Related Questions

HELP: Arduino Genuino 101 to Unity3D. Serial Communication 0 Answers

Send data through usb using android phone 0 Answers

Access to serial port on Android device 6 Answers

how to communicate with arduino for a local two player game? 0 Answers

Unity to Arduino (write serial) Framerate fall 2 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