Serial.GetPortName C# (CSharp) Method

GetPortName() static private method

static private GetPortName ( ) : string
return string
    static string GetPortName()
    {
        string[] portNames;

        switch (Application.platform) {

        case RuntimePlatform.OSXPlayer:
        case RuntimePlatform.OSXEditor:
        case RuntimePlatform.OSXDashboardPlayer:
        case RuntimePlatform.LinuxPlayer:

            portNames = System.IO.Ports.SerialPort.GetPortNames ();

            if (portNames.Length == 0) {
                portNames = System.IO.Directory.GetFiles ("/dev/");
            }

            foreach (string portName in portNames) {
                if (portName.StartsWith ("/dev/tty.usb") || portName.StartsWith ("/dev/ttyUSB"))
                    return portName;
            }
            return "";

        default: // Windows

            portNames = System.IO.Ports.SerialPort.GetPortNames ();

            // Defaults to last port in list (most chance to be an Arduino port)
            if (portNames.Length > 0)
                return portNames [portNames.Length - 1];
            else
                return "";
        }
    }

Usage Example

    /**
     * @brief Update the app's gui and take actions based on user interaction with gui.
     * Additional buttons can be added to easily send EagleAPI commands<br>
     * if (GUI.Button(new Rect(200, 200, 75, 20), "Reset Position"))    EagleAPI.actuators[target_actuator].ResetPosition();
     */
    private void OnGUI()
    {
        // ///Search for a serial port that has an eagle controller, stop and white light when correct port found
        switch (EagleAPI.correctPort)
        {
        case false:
            style.normal.textColor = Color.black;
            if ((Time.time - waiting_for_response) > 0.05)
            {
                EagleAPI.Handshake();
                waiting_for_response = Time.time;
            }
            break;

        case true:
            style.normal.textColor = Color.white;
            break;
        }

        ///Comm port that is being used by the Serial class
        GUI.Label(new Rect(Screen.width - 250, Screen.height - 100, 250, 40), "Port:     " + Serial.GetPortName(), style);

        ///Send a system ready command
        if (GUI.Button(new Rect(50, 250, 150, 20), "System Ready"))
        {
            EagleAPI.SystemReady();
        }

        ///Actuator information, target acutator, position, actuator force, temperature etc.
        GUI.Label(new Rect(50, 50, 250, 40), "Target Actuator:               " + target_actuator.ToString());
        target_actuator = Mathf.RoundToInt(GUI.HorizontalSlider(new Rect(50, 30, 150, 20), target_actuator, 0, 5));
        if (target_actuator != last_target_actuator)
        {
            pcenableflag = true;
        }
        GUI.Label(new Rect(50, 75, 250, 20), "Actuator Position (mm):    " + (EagleAPI.actuators[target_actuator].position / 1000f).ToString("0.00")); //position is received in micrometers
        GUI.Label(new Rect(50, 100, 250, 20), "Actuator Force:                " + EagleAPI.actuators[target_actuator].force.ToString("0"));
        GUI.Label(new Rect(50, 125, 250, 20), "Errors:                            " + EagleAPI.actuators[target_actuator].errors.ToString());
        GUI.Label(new Rect(50, 150, 250, 20), "Temperature (C):              " + EagleAPI.actuators[target_actuator].temperature.ToString());
        GUI.Label(new Rect(50, 175, 250, 20), "Voltage (V):                     " + EagleAPI.actuators[target_actuator].voltage.ToString("0.00"));
        GUI.Label(new Rect(50, 200, 250, 20), "Power (W):                      " + EagleAPI.actuators[target_actuator].power.ToString("0.00"));

        ///Force Control section, spring constant and center, sine magnitude and frequency.
        if (forceControl = GUI.Toggle(new Rect(500, 50, 250, 20), forceControl, "ForceControl", "Button"))
        {
            if (positionControl)
            {
                pcenableflag = true;
            }
            positionControl = false;
        }
        GUI.Label(new Rect(300, 75, 250, 20), "Spring Constant(force/mm): " + springK.ToString("0.00"));
        springK = (GUI.HorizontalSlider(new Rect(500, 80, 250, 20), springK, 0, 5));
        GUI.Label(new Rect(300, 100, 250, 20), "Spring Center(mm):             " + springCenter.ToString("0.00"));
        springCenter = (GUI.HorizontalSlider(new Rect(500, 105, 250, 20), springCenter, 0, 200));
        GUI.Label(new Rect(300, 125, 250, 20), "Sine Magnitude:                  " + sine_mag.ToString("0.00"));
        sine_mag = (GUI.HorizontalSlider(new Rect(500, 130, 250, 20), sine_mag, 0, 50));
        GUI.Label(new Rect(300, 150, 250, 20), "Sine Frequency (Hz):           " + sine_freq.ToString("0"));
        sine_freq = (GUI.HorizontalSlider(new Rect(500, 155, 250, 20), sine_freq, 0, 50));
        GUI.Label(new Rect(300, 175, 250, 20), "Constant Force:                  " + const_force.ToString("0"));
        const_force = (GUI.HorizontalSlider(new Rect(500, 180, 250, 20), const_force, -30, 30));

        ///Position control sections
        if (positionControl = GUI.Toggle(new Rect(500, 250, 250, 20), positionControl, "Position Control", "Button"))
        {
            if (forceControl)
            {
                pcenableflag = true;
            }
            forceControl = false;
        }
        GUI.Label(new Rect(300, 275, 250, 20), "Target Position(mm):          " + target_position.ToString("0"));
        target_position = (int)(GUI.HorizontalSlider(new Rect(500, 280, 250, 20), target_position, 0, 150));

        GUI.Label(new Rect(50, Screen.height - 175, 150, 20), "Received: " + EagleAPI.lastResponse);
        GUI.Label(new Rect(50, Screen.height - 150, 150, 20), "Sent: " + EagleAPI.lastSent);
        ///General serial command sending box
        GUI.Label(new Rect(50, Screen.height - 100, 150, 20), "Send Serial Command");
        stringCommand = GUI.TextField(new Rect(50, Screen.height - 75, 150, 20), stringCommand);
        if (GUI.Button(new Rect(200, Screen.height - 75, 75, 20), "Send"))
        {
            Serial.Write(stringCommand + "\r");
        }
    }