Search Unity

System.IO.ports Problem... It's not there.

Discussion in 'Editor & General Support' started by Caliber-Mengsk, Feb 5, 2011.

  1. Caliber-Mengsk

    Caliber-Mengsk

    Joined:
    Mar 24, 2010
    Posts:
    689
    Ok, so a long while back (unity 2.6) I made a spiffy little script that received serial data from an arduino. It was cool because it had a tilt sensor for doing some basic things. One person used my script to make a labyrinth game that used the physical object of tilting the arduino.

    I just decided I wanted to work on some stuff with unity and the arduino again, and come to find out after upgrading the project to Unity 3, I get the following error:

    Code (csharp):
    1. Assets/serialRotation.cs(3,17): error CS0234: The type or namespace name `Ports' does not exist in the namespace `System.IO'. Are you missing an assembly reference?
    Now, to state, I do only have Unity free, and I've also looked and found that you guys are simply trying to streamline the libraries for web deployment and such which can't use these things to begin with, but it did remove the functionality for talking with anything over serial.

    Is there any way to fix this? I know that the libraries are in Mono, so why isn't the functionality there?
     
  2. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    Use .NET 2.0 compatibility instead of .NET 2.0 subset.

    --Eric
     
  3. Caliber-Mengsk

    Caliber-Mengsk

    Joined:
    Mar 24, 2010
    Posts:
    689
    Thanks. Just a note for anyone that may come across this problem themselves later, you have to change the setting then save project and restart unity before it works.
     
  4. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    Actually, just re-saving the script after changing the setting will work.

    --Eric
     
  5. EducaSoft

    EducaSoft

    Joined:
    Sep 9, 2007
    Posts:
    650
    Caliber, I have an aduino connected to com3 at 115200baud and sending IMU information over the rs232 to the pc.
    Now if I try

    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4. using System.IO.Ports;
    5.  
    6. public class serialRotation : MonoBehaviour {
    7.  
    8.    SerialPort stream = new SerialPort("COM3", 115200);
    9.    string receivedstring;
    10.                  
    11.    void Start (){
    12.     stream.Open(); //Open the Serial Stream.
    13.    }
    14.  
    15.    void Update (){
    16.     receivedstring = stream.ReadLine(); //Read the information
    17.     stream.BaseStream.Flush(); //Clear the serial information so we assure we get new information.
    18.    }
    19.  
    20.    void OnGUI(){
    21.     GUI.Label(new Rect(10,10,300,100), receivedstring); //Display new values
    22.    }
    23.  
    24. }
    25.  
    The this makes unity hang (endless loop and no response anymore)
    I don't see anything onscreen and can only terminate unity in taskmanager.


    Do you have any clue what I do wrong here? the .NET is set like described above to find the System.IO.Ports again but still it hangs.



    I also allready tested to connect to the arduino over a socket connection (which would be way better for me), but that also seems to fail.


    Any help/hints would be so highly appreciated,

    Thanks,

    Bart
     
    Last edited: May 9, 2011
  6. subhash

    subhash

    Joined:
    Jun 10, 2011
    Posts:
    3
    Hi Bart,
    I have the same problem. i tried using multithreading. But unity doesnot read any data.Have you found a soulution yet
    Thanks
    Subhash
     
    Last edited: Jun 13, 2011
  7. yson

    yson

    Joined:
    May 23, 2011
    Posts:
    7
    I tried multithreading and event handler but none of them worked. I also tried plug-in (dll) with Unity Pro license but it didn't work.
     
  8. yson

    yson

    Joined:
    May 23, 2011
    Posts:
    7

    I finally solved the issue by running an external process in unity. Please refer to this thread. http://forum.unity3d.com/threads/17488-Start-a-external-process

    So I have an independent process running with thread and prints out data from serial port. And Unity intercepts the standard output of the process. No hanging and no latency.
     
  9. Caliber-Mengsk

    Caliber-Mengsk

    Joined:
    Mar 24, 2010
    Posts:
    689
    Hi, sorry I didn't respond to this before. (I don't check the forums as much as I use to since I've gotten a full time job.)

    The only thing I see wrong in your script is the update loop doesn't have a check to see if there is any data in the buffer.

    void Update ()
    {
    if(stream.BytesToRead >0)
    {
    receivedstring = stream.ReadLine(); //Read the information
    stream.BaseStream.Flush(); //Clear the serial information so we assure we get new information.
    }
    }

    That said, I haven't tested it, but I believe it's running into an issue because it's trying to call data that isn't there, and it's waiting for it.
    If you want more information of the serial port object, check out the MSDN. Mono was made to be pretty much the exact same as .net languages, so for the most part (not 100% always), you can use MSDN as a resource for imported commands you don't know much about.

    http://msdn.microsoft.com/en-us/library/system.io.ports.serialport.aspx
    That's the link to the serial port command.

    You may also want to set the timeout to something like 20ms, that way if it doesn't work still, the game engine will actually move on, so you aren't stuck there without data and in the infinite loop. Serial port connections haven't really been messed with in unity that I've seen except the posts by me initially, and unfortunately, I really don't know a ton about the commands. I kind of make it up as I go.

    For instance, now I do a handshaking system. I send a specific character out to the serial port (for instance, I use 1), and the device waits for that command, then gets data and returns it through serial. Then the device simply waits for another 1. This clears up the need for the flush command, and removes the possibility of only getting half of the string. I'll also note, I return a specific character length (for instance, x,y,z from an accelerometer value would be sent to the pc as 020,152,253. Always leading with a 0 if it's less then 100, and always leading with 2 zeros if it's less then 10. Then in your script you just do an if statement. If bytestoread is greater then or equal to 12, run the update code, otherwise, go on.

    It's a simple handshake, and probably not the "proper" way to do it, but it speeds things up and makes it more accurate, because you aren't filling/flushing the buffer all the time.

    I made a different post with all the handshaking, I think... O-o If not, I can probably write an example up fairly quickly with the arduino.

    EDIT:
    And just saying, but external processes with a visible command prompt bug me. One on the biggest things that bugs me about blender in fact.
     
    EchtMe likes this.
  10. holyjewsus

    holyjewsus

    Joined:
    Mar 7, 2011
    Posts:
    624
    so no luck with mac + arduino then?