Search Unity

  1. Unity 2019.1 is now released.
    Dismiss Notice

can't use System.IO.Ports

Discussion in 'Connected Games' 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.
     
    Warped, tjwild0127, cmyd and 6 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
    rock_ksayk 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:
    33
    Unity 2017.4.15,

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

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

    vzheng

    Joined:
    Dec 4, 2012
    Posts:
    33
    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:
    21
    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.