Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We are updating our Terms of Service for all Unity subscription plans, effective October 13, 2022, to create a more streamlined, user-friendly set of terms. Please review them here: unity.com/legal/terms-of-service.
    Dismiss Notice
  3. Have a look at our Games Focus blog post series which will show what Unity is doing for all game developers – now, next year, and in the future.
    Dismiss Notice

can't use System.IO.Ports

Discussion in 'Multiplayer' started by mohammadali1375, Jun 26, 2012.

  1. mohammadali1375

    mohammadali1375

    Joined:
    Jun 26, 2012
    Posts:
    2
    hi every body
    i want conect with serial port but when I use "using System.IO.Ports;" I take this error:

    "Assets/NewBehaviourScript.cs(5,17): error CS0234: The type or namespace name `Ports' does not exist in the namespace `System.IO'. Are you missing an assembly reference?"

    but here is some method for serial port like "BaudRate" and unity Forecast "Ports" from "System.IO"
    please help me

    and my code is here :

    Code (csharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using System.IO.Ports;
    4. public class NewBehaviourScript : MonoBehaviour {
    5.      SerialPort serial1;
    6.     // Use this for initialization
    7.     void Start () {
    8.     serial1=new SerialPort();
    9.     }
    10.    
    11.     // Update is called once per frame
    12.     void Update () {
    13.    
    14.     }
    15.     void OnGUI()
    16.     {
    17.         if(GUI.Button(new Rect(10,10,100,50),"send"))
    18.         {
    19.             serial1.PortName="COM1";
    20.             serial1.Parity=Parity.None;
    21.             serial1.BaudRate=9600;
    22.             serial1.DataBits=8;
    23.             serial1.StopBits=StopBits.One;
    24.             serial1.Open();
    25.             serial1.Write("hi unity");
    26.             serial1.Close();
    27.            
    28.            
    29.         }
    30.     }
    31. }
     
    Last edited: Jun 26, 2012
  2. jmonsuarez

    jmonsuarez

    Joined:
    Jun 30, 2011
    Posts:
    6
    Hi, your problem is the .net configuration, use the .NET 2.0 (not subset) and will connect. I was able to transmit data, but not to receive. Config is into Edit>ProjectSettings>Player>ApiCompatibilityLevel.
     
    EdgarSoda, dman8723, Alco6 and 13 others like this.
  3. mohammadali1375

    mohammadali1375

    Joined:
    Jun 26, 2012
    Posts:
    2
    THANKS A LOT .
    I very wanted it.
    and for reading data you can use it :
    but it read hex :

    Code (csharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using System.IO.Ports;
    4.  
    5. public class NewBehaviourScript : MonoBehaviour {
    6.      SerialPort serial1;
    7.      byte[] buf = new byte[4]; // creates a byte array the size of the data you want to receive.
    8.      int bufCount = 0;
    9.      int a,b;
    10.     // Use this for initialization
    11.     void Start () {
    12.     serial1=new SerialPort();
    13.     }
    14.    
    15.     // Update is called once per frame
    16.     void Update () {
    17.    
    18.     }
    19.     void OnGUI()
    20.     {
    21.         if(GUI.Button(new Rect(10,10,100,50),"read"))
    22.         {
    23.             serial1.PortName="COM1";
    24.             serial1.Parity=Parity.None;
    25.             serial1.BaudRate=9600;
    26.             serial1.DataBits=8;
    27.             serial1.StopBits=StopBits.One;
    28.             serial1.Open();
    29.             bufCount = 0;
    30.             bufCount += serial1.Read(buf, bufCount, buf.Length - bufCount);
    31.  
    32.             a = 0;
    33.             b = 0;
    34.             while (a < bufCount)
    35.             {
    36.                 b += buf[a];
    37.                 a++;
    38.             }
    39.             print(b);
    40.             serial1.Close();
    41.            
    42.            
    43.         }
    44.     }
    45. }

    and I thank you again
     
    Last edited: Jun 27, 2012
    RickLeung likes this.
  4. edward454

    edward454

    Joined:
    Jun 19, 2015
    Posts:
    2
    wow thanks a lot
     
  5. andy78flavia28

    andy78flavia28

    Joined:
    Jun 25, 2015
    Posts:
    3
    Hi, thanks for your code, but I have a problem... I need to read data after sending a command (the command is "D\r").
    Do you know a solution? (the received data is a velocity...)
     
  6. Timiteck

    Timiteck

    Joined:
    Nov 29, 2018
    Posts:
    1
    Thank you
     
  7. vzheng

    vzheng

    Joined:
    Dec 4, 2012
    Posts:
    44
    Unity 2017.4.15,

    upload_2018-11-30_10-33-50.png

    show errors:
    IOException: 拒绝访问。
     
    aroraayushram likes this.
  8. vzheng

    vzheng

    Joined:
    Dec 4, 2012
    Posts:
    44
    and cannot change to .Net 2.0 for the System.IO.Ports based on .net 4.0
     
  9. tjwild0127

    tjwild0127

    Joined:
    Jan 13, 2019
    Posts:
    1
    Thank you soooo much! Worked perfectly!
     
  10. kamran-bigdely

    kamran-bigdely

    Joined:
    Jun 19, 2014
    Posts:
    25
    I got the same error on Unity 2018.3.2f1. None of the above posts helped. However, I found a work around that I explained here.
     
  11. zenmanenergy

    zenmanenergy

    Joined:
    Jul 23, 2019
    Posts:
    2
    None of these solutions are working for me. I don't see a .net 2.0 subset... I only have ".NET standard 2.0" and ".NET 4.x"

    Anyone have any new updates on this issue?
     
    hotchick, Fenikkel and TekMage71 like this.
  12. TekMage71

    TekMage71

    Joined:
    Aug 30, 2012
    Posts:
    1
    snelson_unity:
    So here's what I had to do and it has worked every since. You need to reference the old ..io.ports from a previous .Net. I found mine in C:\Windows\Microsoft.NET\Framework\v2.0.50727\System.dll. I also found some System.io.ports namespaces when looking in my reference assembly Frame work directories. Some are in the System.dll or the System.IO. dll and if your lucky you will actually find a System.IO.Ports.dll file. This latter is pretty old and I can't and won't vouch for its efficiency but hey use what you got.
     
  13. samra2494

    samra2494

    Joined:
    Nov 23, 2015
    Posts:
    20
    Go to Edit->projectsettings->Player->APIcompetibilityLevel = .Net4.x
    This worked for me.
     
    Fenikkel likes this.
  14. BarKrief

    BarKrief

    Joined:
    Apr 22, 2021
    Posts:
    1
    Thanks, helped me :)
     
  15. TheIronHobo

    TheIronHobo

    Joined:
    May 7, 2013
    Posts:
    2
    Howdy, The above solutions did not work for me unfortunately. Found a fix that did however. My target platform is Windows and I changed the architecture in build settings from x86_64 to x86. After that System.IO.Ports was referenceable.
     

    Attached Files:

    Guillermo_ and Fenikkel like this.
  16. Scyrode

    Scyrode

    Joined:
    Feb 1, 2019
    Posts:
    1
    This worked for me! Thanks :) I switched back to x86_64 after doing the fix and its still working :cool:
     
    Fenikkel likes this.
  17. Fenikkel

    Fenikkel

    Joined:
    Sep 24, 2019
    Posts:
    15
    To make it work in Unity 2020 with System.IO.Ports, as said above, change the APIcompetibilityLevel:
    • Edit->projectsettings->Player->APIcompetibilityLevel = .Net4.x
    If you still have errors in Visual Studio, you need to change the architecture as said above:
    • File -> BuildSettings -> Architecture = x86
    And then you can switch back:
    • File -> BuildSettings -> Architecture = x86_64
     
  18. qxia

    qxia

    Joined:
    Sep 1, 2021
    Posts:
    1
    Thank you, it is very helpful
     
    Fenikkel likes this.
  19. hotchick

    hotchick

    Joined:
    Apr 6, 2021
    Posts:
    5
    yes it worked! thanks
     
  20. Guillermo_

    Guillermo_

    Joined:
    Mar 3, 2022
    Posts:
    1
    GENIO!!!!
     
    Fenikkel likes this.
  21. nexdevus

    nexdevus

    Joined:
    Aug 25, 2021
    Posts:
    1
    Correct, this way works perfectly with 2020.3.26f1
     
    copa2894 and Fenikkel like this.
  22. iQMAL_

    iQMAL_

    Joined:
    Feb 5, 2020
    Posts:
    2
    upload_2022-9-22_15-45-54.png

    Note: On version 2021.3.x, the.NET 4.x is the .NET Framework (without version number)
     
unityunity