Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Access list item

Discussion in 'Scripting' started by kyuzumaki, Apr 15, 2015.

  1. kyuzumaki

    kyuzumaki

    Joined:
    Mar 6, 2015
    Posts:
    11
    I have a list of serial ports
    Code (CSharp):
    1. publicList<string> serialPorts = newList<string> ();
    I want to select the first port in that list and connect to it using

    Code (CSharp):
    1. serial = new SerialPort(serialPorts.**INSERT REAL COMMAND**, 115200);
    How do i access a list item like this? The only method I see converts the whole list to an array
     
  2. Sbizz

    Sbizz

    Joined:
    Oct 2, 2014
    Posts:
    250
    "Hello"

    serialPorts[0]

    "You're welcome"
     
  3. A.Killingbeck

    A.Killingbeck

    Joined:
    Feb 21, 2014
    Posts:
    483
    Arrays use 0 based indices, so the expression serialPorts[0] actually fetches the first element, serialPorts[1] fetches the 2nd element etc. Make sure you never try to use an index of -1 or an index who's value is equal to the length of the array.

    i.e. An array of size 8, fit's 8 elements. Index 8 will actually be a 9th element which doesn't exist( index out of range exception)
     
    Last edited: Apr 15, 2015
  4. KelsoMRK

    KelsoMRK

    Joined:
    Jul 18, 2010
    Posts:
    5,539
    Not to be pedantic but that would actually be an index out of range exception :)
     
    A.Killingbeck likes this.
  5. A.Killingbeck

    A.Killingbeck

    Joined:
    Feb 21, 2014
    Posts:
    483
    No idea what you're talking about!
     
  6. kyuzumaki

    kyuzumaki

    Joined:
    Mar 6, 2015
    Posts:
    11
    Thanks for the replies. Thats what I was trying initially just accessing like an array but it didn't seem to work.. I have now discovered the SerialPorts.Add($string$) was not adding the item to the list in the first place :confused:

    To make it work I have to call SerialPorts.Add twice in a row with the same value. Then it appears and subsequent Add operations seem to work.

    Is that a bug or an initalisation error? (I declare the list like this)

    Code (CSharp):
    1. public List<string> serialPorts = new List<string> ();
     
    Last edited: Apr 17, 2015
  7. A.Killingbeck

    A.Killingbeck

    Joined:
    Feb 21, 2014
    Posts:
    483

    Can you post the code where you are adding the strings to your list?
     
  8. Sbizz

    Sbizz

    Joined:
    Oct 2, 2014
    Posts:
    250
    We need to see the whole script. Maybe you initialize twice your list.
     
  9. kyuzumaki

    kyuzumaki

    Joined:
    Mar 6, 2015
    Posts:
    11
    I do not initialise the list twice just one call within the objects class. The list is declared as public.

    This is the code I currently use to get the list. At present I only have one serial port device so the double call is not an issue yet.

    Code (CSharp):
    1.     void getPortNames ()
    2.     {
    3.         int p = (int)System.Environment.OSVersion.Platform;
    4.      
    5.         if (p == 4 || p == 128 || p == 6) {
    6.             string[] ttys = Directory.GetFiles ("/dev/", "tty.usb*");
    7.             foreach (string dev in ttys) {
    8.                 if (dev.StartsWith ("/dev/tty.usb*"))
    9.                     serialPorts.Add(dev);
    10.                     serialPorts.Add(dev);
    11.                 Debug.Log (System.String.Format (dev));
    12.             }
    13.         }
    14.     }
     
  10. Sbizz

    Sbizz

    Joined:
    Oct 2, 2014
    Posts:
    250
    You need to add twice your value because your condition does not return TRUE.

    As it didn't return TRUE, you thought your "Add" didn't work, so you added another line just after : you're out of the scope, your second "Add" does not care about IF, it will ALWAYS add the string DEV.

    Code (CSharp):
    1. void getPortNames ()
    2.     {
    3.         int p = (int)System.Environment.OSVersion.Platform;
    4.  
    5.         if (p == 4 || p == 128 || p == 6) {
    6.             string[] ttys = Directory.GetFiles ("/dev/", "tty.usb*");
    7.             foreach (string dev in ttys) {
    8.                 if (dev.StartsWith ("/dev/tty.usb*")) {
    9.                     serialPorts.Add(dev);
    10.                     serialPorts.Add(dev);
    11. }
    12.                 Debug.Log (System.String.Format (dev));
    13.             }
    14.         }
    15.     }
    I'm pretty sure this code will have the same effect : it will NOT add something in your list.

    Are you sure "dev" starts with "/dev/tty.usb" ? You should use Debug.Log. Your list is not the problem here.

    Tip: you should ALWAYS put parentheses, you'll avoid mistakes like this. If you do not add parentheses when you use IF / ELSE / whatever, the compiler will add the line below in the scope, but not the next ones.

    Code (CSharp):
    1. if (mybool) {
    2. function();
    3. }
    4.  
    5. functionB();
    6.  
    7. // ==
    8.  
    9. if (mybool)
    10. function();
    11.  
    12. functionB();
    13.  
    14. // BUT
    15.  
    16. if (mybool) {
    17. function();
    18. functionB();
    19. }
    20.  
    21. // !=
    22.  
    23. if (mybool)
    24. function();
    25. functionB();
    26.  
    27.  
     
    Last edited: Apr 20, 2015