Search Unity

U3.b3 = System.IO.Ports?

Discussion in 'Editor & General Support' started by jjjack, Jul 12, 2010.

  1. xCyborg

    xCyborg

    Joined:
    Oct 4, 2010
    Posts:
    633
    Me too please, if that patch really can provide System.IO.Port how to use it; I'm a fan of Arduino development too(it's a surprising coincidence) but how would you use unity instead of Arduino IDE?
     
  2. xCyborg

    xCyborg

    Joined:
    Oct 4, 2010
    Posts:
    633
    And even if you comunicate with the board with a running windows application thru a usb cable, how are you gonna program the board with it, I just think it's not feasible.
     
  3. Tibbar

    Tibbar

    Joined:
    Jan 7, 2007
    Posts:
    46
    Here's some code based on the Unity Arduino sample. I've stripped it down almost to the bare minimum.
    1. Since we're not connecting using Bluetooth RFCOMM methods (there aren't any included with Mono), you need to first pair with the Bluetooth device in the Control Panel / Bluetooth utility so that you can access it with serial port methods. Check your device properties to see whether you're using COM3, COM4, or some other port.
    2. Create a new project and change the Build settings so that you are using .NET 2.0 instead of .NET 2.0 Subset. In the editor, choose File: Build Settings: Player Settings (under PC and Mac): Api Compatibility Level (Inspector panel)
    3. Restart Unity.
    4. Here's the script below. Attach it to a dummy object and press Play.
    In my experience, Mono and Java RXTX have very poor reliability when it comes to initiating connections with serial ports. The serial port library often thinks the port is busy or there is no response. You have to keep restarting your program until it connects. Haven't had much luck with connection retries within the same thread--it seems that a restart is required. (Anyone else know why? Maybe Java/Mono isn't "low" enough level access and the operating system periodically takes control of the resources?)
    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4. using System.IO.Ports;
    5.  
    6. public class SerialPortTest : MonoBehaviour {
    7.     public static SerialPort sp = new SerialPort("COM4", 115200, Parity.None, 8, StopBits.One);
    8.     public int count = 0;
    9.    
    10.     // Use this for initialization
    11.     void Start () {
    12.         OpenConnection();
    13.     }
    14.    
    15.     // Update is called once per frame
    16.     void Update () {
    17.         // print just the first 10 bytes
    18.         if (sp.IsOpen  count < 10) {
    19.             print(sp.ReadByte());
    20.             count++;
    21.         }
    22.     }
    23.    
    24.     public static void OpenConnection() {
    25.         if (sp != null) {
    26.             if (sp.IsOpen) {
    27.                 sp.Close();
    28.                 print("Closing port, because it was already open!");
    29.             }
    30.             else {
    31.                 sp.Open();  // opens the connection
    32.                 sp.ReadTimeout = 100;  // sets the timeout value before reporting error
    33.             }
    34.         }
    35.         else {
    36.             if (sp.IsOpen) {
    37.                 print("Port is already open");
    38.             }
    39.             else {
    40.                 print("Port == null");
    41.             }
    42.         }
    43.     }
    44. }
    45.  
    P.S. I'm not an Arduino user.
     
    Last edited: Feb 24, 2011
  4. Tibbar

    Tibbar

    Joined:
    Jan 7, 2007
    Posts:
    46
    I don't know how you go about programming the Arduino device, but if you do it by sending custom command strings, and if the Write() methods of System.IO.Ports work, then perhaps that's how you do it. I hear that Mono's serial port implementation overall is kind of spotty.

    Sorry to the above posters, I misread your request for the example. I only require reading data from my input device, not writing commands to it. Do the Write() commands not work for you?
     
    Last edited: Feb 24, 2011
  5. Yann

    Yann

    Joined:
    Oct 20, 2007
    Posts:
    432
    Any news on this important subject ? I just made a test on Unity 3.30f3 : on the Mac, trying to open a serial stream still returns a DLLNotFoundException.
     
  6. bigkahuna

    bigkahuna

    Joined:
    Apr 30, 2006
    Posts:
    5,434
    Worked for me under Windows 7 using 3.2.
     
  7. Dreamora

    Dreamora

    Joined:
    Apr 5, 2008
    Posts:
    26,601
    sure you have all the required dynamic libraries correctly in place?
     
  8. Yann

    Yann

    Joined:
    Oct 20, 2007
    Posts:
    432
    @dreamora : frankly, I don't know. I have tried the trick posted by Tibbar (replacing libMonoPosixHelper.dylib with the patched version) but keep getting the same error on the Mac. And of course, I have selected .NET 2.0 in the player settings.

    This line :

    Debug.Log(System.IO.Ports.SerialPort.GetPortNames ());

    Will return "System.String[]" and no error, but as soon as I try to open a serial port I get the same "DLLNotFoundException:MonoPosixHelper" message.

    Are we supposed to install extra libraries so that System.IO.Ports can talk to serial ports ?
     
  9. xCyborg

    xCyborg

    Joined:
    Oct 4, 2010
    Posts:
    633
    I used the GetPortName() function, and returns only COM4 port with a question mark ("COM4?"): Does that mean I can use any USB port for communication (i.e. is COM4 the USB port?) sorry this still an dark spot for me.;)
     
  10. MegatonPunch

    MegatonPunch

    Joined:
    Jul 26, 2009
    Posts:
    13
    Anyone seen an update on this issue? Or found a use-able work around?
     
  11. bigkahuna

    bigkahuna

    Joined:
    Apr 30, 2006
    Posts:
    5,434
    It works for me, no issues, at least with 3.2+ it's been working (under Windows 7 64 bit). If I recall correctly, you have to set the .NET implementation to "2.0 full" in the settings. What issue(s) are you having?
     
  12. MegatonPunch

    MegatonPunch

    Joined:
    Jul 26, 2009
    Posts:
    13
    Yes, it works perfectly under windows. However, the current version of Mono used on OSX is broken. Unfortunately, I don't have time to wait for Unity to upgrade the Mono version. If someone has a working workaround or some example C++ plugin code to share, I'd be enormously grateful.

    Thank you.
     
  13. bigkahuna

    bigkahuna

    Joined:
    Apr 30, 2006
    Posts:
    5,434
    @MegatonPunch - Oops, sorry. Didn't know you were on a Mac.
     
  14. JFo

    JFo

    Joined:
    Dec 9, 2007
    Posts:
    217
    Best place to start is to look at Mac OS X Developer Library. For example, there are SerialPortSample project.

    BR,
    Juha
     
  15. MegatonPunch

    MegatonPunch

    Joined:
    Jul 26, 2009
    Posts:
    13
    Thank you, JFo
     
  16. smallab

    smallab

    Joined:
    Feb 18, 2011
    Posts:
    3
    Is there any update regarding if/when there will be a fix for Mac? Is there any work around for Mac developers?
     
  17. Tibbar

    Tibbar

    Joined:
    Jan 7, 2007
    Posts:
    46
    Serial ports still don't work on Unity 3.4 (Mac), even though they gave us a slightly newer version of Mono and Monodevelop. When is Unity Technologies going to fix this?! It's already been repaired over a year ago in the standard Mono distribution.
     
    Last edited: Aug 10, 2011
  18. charles.rich

    charles.rich

    Joined:
    Mar 27, 2011
    Posts:
    14
    I am also blocked and frustrated by this PosixHelper problem on Mac. If anyone hears of solution, please post!

    -CR
     
  19. tbryant

    tbryant

    Joined:
    Aug 12, 2009
    Posts:
    17
    ditto - I am still blocked as well. I had forgotten about this issue and had planned to use serial ports in a project from a mac mini. agh
     
  20. holyjewsus

    holyjewsus

    Joined:
    Mar 7, 2011
    Posts:
    624
    this is depressing, is there a plugin for this around somewhere?
     
  21. holyjewsus

    holyjewsus

    Joined:
    Mar 7, 2011
    Posts:
    624
    Did you guys post any bug reports... from inside unity?
     
  22. Fattie

    Fattie

    Joined:
    Jul 5, 2012
    Posts:
    476
    PS you have a typo at ..

    sp.IsOpen count<10)