Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 NivekJump · Nov 06, 2014 at 05:57 PM · monobehaviourdatavisualstudiocompatibility

Get the Data from a Serial Port, Work in VB but in Unity Dont.

  • Re Edit *

Hi Guys Im still stuck with this problem, but now im more conscient about the state of the proyect. I do an miniapp in Visual Studio, and it worket, its a console app, but she recognized me the data and i can capture it.

I Create a Class in Unity... But guys that doesnt work, the data is null and IDK why. Please help me! Im Stuck in this since the last friday.

This is my script in Unity:

 using UnityEngine;
 using System;
 using System.Collections;
 using System.IO.Ports;
 using System.Text;
 
 
 public class ScriptPort : MonoBehaviour{
     public GUIText guiDisplay;
     string data2;
     void Start(){
         InvokeRepeating ("UpdateData",5,5);
     }
     void Upate(){
 
     }
     void UpdateData(){
         ScriptCOM3 sp = new ScriptCOM3 ();
         sp.OpenPort ();
         data2 = sp.data;
         Debug.Log (data2);
         sp.ClosePort();
     }
 }
 
 public class ScriptCOM3 {
     public string data;
     public SerialPort COM3 = new SerialPort();
 
     public ScriptCOM3() {
         COM3.BaudRate = 9600;
         COM3.DataBits = 8;
         COM3.StopBits = StopBits.One;
         COM3.Parity = Parity.None;
         COM3.PortName = "COM3";
         COM3.DataReceived += new SerialDataReceivedEventHandler(comPort_DataReceived);
     }
 
     public void OpenPort() {
         COM3.Open();
     }
 
     public void ClosePort() {
         COM3.Close();
     }
 
     public string ByteToHex(byte[] comByte){
         StringBuilder builder = new StringBuilder(comByte.Length * 3);
         foreach (byte data in comByte)
         {
             builder.Append(Convert.ToString(data, 16).PadLeft(2, '0').PadRight(3, ' '));
         }
         return builder.ToString().ToUpper();
     }
 
     public void DisplayData(string msg){
         data = msg;
     }
     
     public void comPort_DataReceived(object sender, SerialDataReceivedEventArgs e){
         int bytes = COM3.BytesToRead;
         byte[] comBuffer = new byte[bytes];
         COM3.Read(comBuffer, 0, bytes);
         DisplayData(ByteToHex(comBuffer) + "\n");
     }
 }


How you could see im using another class and then instantiate that class in the Mono behaviour script, but the data is null... Why??

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

2 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by Bunny83 · Nov 10, 2014 at 06:02 PM

I'm pretty sure you want to do this:

 public class ScriptPort : MonoBehaviour
 {
     public GUIText guiDisplay;
     string data2;
     ScriptCOM3 sp = new ScriptCOM3 ();
     void Start()
     {
         sp.OpenPort ();
         InvokeRepeating ("UpdateData",5,5);
     }
     
     void OnDestroy()
     {
         sp.ClosePort();
     }
     
     void UpdateData()
     {
         data2 = sp.data;
         Debug.Log (data2);
     }
 }

Comment
Add comment · Show 2 · Share
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 Bunny83 · Nov 10, 2014 at 06:07 PM 0
Share

Your "ScriptCO$$anonymous$$3" class looks a bit strange. The DataReceived callback could be called multiple times but you always scrap the old data and replace it with the data that arrived last. Also returning data as hex string is a bit strange too... What kind of hardware do you work with?

avatar image NivekJump · Nov 10, 2014 at 07:21 PM 0
Share

Its a D$$anonymous$$X Console... And Sure thats want i want to do, i just need the last data from the console. The reason why i used the returning data as a Hex string, its because is easy for me to manipulate, and because i have too manipulate colors with that, so... Its better for RGB a hexstring 0-255 data :)

avatar image
0

Answer by RevoltingMuffin · Nov 04, 2016 at 07:09 PM

Please visit the following web site to view updates on this issue and comment letting the Unity staff know how important this functionality is to the COM Port and TTY reading community! VOICE YOU OPINION!

https://issuetracker.unity3d.com/issues/serialport-bytestoread-returns-null-reference

Comment
Add comment · Share
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

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

A node in a childnode? 1 Answer

Split string data 0 Answers

Saving data. 1 Answer

How does one go about creating a list of GameObjects? 1 Answer

Is Unity 3 compatable with earlier version like unity 2.6 or 1.7 3 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