/// <summary> /// Refresh correct Ports available for device in ComboBox COM Ports /// If there is an available COM port already used by m_objRFE.PortNameExternal it will be ignored /// </summary> public void UpdateComboBox() { string sPortExternal; if (m_objRFE != null && !m_objRFE.PortConnected) { if (m_objRFE.ValidCP2101Ports != null) { string sSelectedPort = ""; if (m_comboCOMPort.SelectedItem != null) { sSelectedPort = m_comboCOMPort.SelectedItem.ToString(); } sPortExternal = m_objRFE.PortNameExternal; string[] arrPortsAvailable = null; if (!String.IsNullOrEmpty(sPortExternal)) { //If exists external port associated to the connection, remove from available port list arrPortsAvailable = m_objRFE.ValidCP2101Ports.Where(str => str != sPortExternal).ToArray(); } else { arrPortsAvailable = m_objRFE.ValidCP2101Ports; } #if UNIX_LIKE if (RFECommunicator.IsUnixLike()) { if (RFECommunicator.IsMacOSPlatform()) { //Mac replace string for USB port name sSelectedPort = sSelectedPort.Replace(MAC_PORT_PREFIX, USB_PREFIX); if (sSelectedPort.Equals(USB_PREFIX)) { sSelectedPort += "0"; } for (int nInd = 0; nInd < arrPortsAvailable.Length; nInd++) { arrPortsAvailable[nInd] = arrPortsAvailable[nInd].Replace(MAC_PORT_PREFIX, USB_PREFIX); if (arrPortsAvailable[nInd].Equals(USB_PREFIX)) { arrPortsAvailable[nInd] += "0"; } } } else { //Replace COM port names for Linux sSelectedPort = sSelectedPort.Replace(LINUX_PORT_PREFIX, ""); for (int nInd = 0; nInd < arrPortsAvailable.Length; nInd++) { arrPortsAvailable[nInd] = arrPortsAvailable[nInd].Replace(LINUX_PORT_PREFIX, ""); } } } #endif m_comboCOMPort.DataSource = arrPortsAvailable; m_comboCOMPort.SelectedItem = sSelectedPort; } else { m_comboCOMPort.DataSource = null; } } }
/// <summary> /// Connect to specified serial port /// It could be a IOTBoard, connect automatically if it is an device connected by USB or select a specified port from command line /// </summary> /// <param name="args"></param> /// <returns></returns> static bool ConnectPort(string[] args) { //Connect to the right UART port (may be USB in Windows/Unix/Mac or a Raspberry Mainboard) if (g_bIoTBoard) { try { //Define pins to control baudrate (GPIO2 on Pin21) and force a HW reset of the MWSUB3G (Pin12) OutputPinConfiguration pinGPIO2 = ConnectorPin.P1Pin21.Output(); m_pinConnection = new GpioConnection(pinGPIO2); OutputPinConfiguration pinRESET = ConnectorPin.P1Pin12.Output(); m_pinConnection.Add(pinRESET); //Reset sequence m_pinConnection[pinRESET] = false; Thread.Sleep(100); m_pinConnection[pinGPIO2] = g_bHighSpeed; //true for 500Kbps, change to false for 2400bps low speed m_pinConnection[pinRESET] = true; Thread.Sleep(2500); //wait for initialization firmware code to finish startup //Open COM port from Raspberry mainboard string sCOMPort = "/dev/ttyAMA0"; g_objRFE.ConnectPort(sCOMPort, g_nBaudrate, true); Trace.WriteLine("Connected to port " + sCOMPort); Thread.Sleep(500); } catch { Trace.WriteLine("ERROR: Unable to connect IoT device"); } } else if (args.Contains("/p:AUTO", StringComparer.OrdinalIgnoreCase)) //Accept /p:auto or /p:AUTO { //This is any non-IoT platform with a single device connected to USB if (g_objRFE.GetConnectedPorts()) { if (g_objRFE.ValidCP2101Ports.Length == 1) { bool bForceBaudrate = (RFECommunicator.IsUnixLike() && !RFECommunicator.IsMacOSPlatform()); g_objRFE.ConnectPort(g_objRFE.ValidCP2101Ports[0], g_nBaudrate, RFECommunicator.IsUnixLike(), bForceBaudrate); } } if (g_objRFE.PortConnected) { Trace.WriteLine("Connected to port " + g_objRFE.ValidCP2101Ports[0]); } else { Trace.WriteLine("ERROR: No port available or several connections detected." + Environment.NewLine + "Please, review your RF Explorer connection"); return(false); } } else { //Use specified port from command line int nPos = Array.FindIndex(args, x => x.StartsWith("/p:")); if (nPos >= 0) { string sCOMPort = args[nPos].Replace("/p:", ""); Trace.WriteLine("Trying manual port: " + sCOMPort); g_objRFE.ConnectPort(sCOMPort, g_nBaudrate, RFECommunicator.IsUnixLike(), RFECommunicator.IsUnixLike() && !RFECommunicator.IsMacOSPlatform()); Trace.WriteLine("Connected to port " + sCOMPort); } else { Trace.WriteLine("ERROR: Please, insert /IOT or /p:PORT parameter"); } } return(g_objRFE.PortConnected); }
/// <summary> /// Connect the selected port /// </summary> public void ConnectPort() { Cursor.Current = Cursors.WaitCursor; try { string sCOMPort = ""; if (m_objRFE.ValidCP2101Ports != null && m_objRFE.ValidCP2101Ports.Length > 0) { //there are valid ports available if (m_objRFE.ValidCP2101Ports.Length == 1) { if (m_objRFE.PortNameExternal != m_objRFE.ValidCP2101Ports[0]) { //if only one, ignore the selection from any combo and use what is available sCOMPort = m_objRFE.ValidCP2101Ports[0]; m_sDefaultCOMPort = sCOMPort; m_comboCOMPort.SelectedItem = m_sDefaultCOMPort; } } else { //if more than one, try to use the one from the combo and otherwise fail if ((m_comboCOMPort != null) && (m_comboCOMPort.Items.Count > 0) && (m_comboCOMPort.SelectedValue != null)) { foreach (string sTestCOMPort in m_objRFE.ValidCP2101Ports) { string sTestCOMPortName = ""; #if UNIX_LIKE if (RFECommunicator.IsMacOSPlatform()) { sTestCOMPortName = sTestCOMPort.Replace(MAC_PORT_PREFIX, USB_PREFIX); if (sTestCOMPortName.Equals(USB_PREFIX)) //In MacOS USB 0 match with SLAB_USBtoUART { sTestCOMPortName += "0"; } } else #endif sTestCOMPortName = sTestCOMPort.Replace(LINUX_PORT_PREFIX, ""); //Linux prefix is removed if exist to compare correctly with selectem item if (sTestCOMPortName == m_comboCOMPort.SelectedValue.ToString()) { sCOMPort = m_comboCOMPort.SelectedValue.ToString(); break; } } } } } #if UNIX_LIKE if (!String.IsNullOrEmpty(sCOMPort)) { if (RFECommunicator.IsUnixLike()) { if (RFECommunicator.IsMacOSPlatform()) { if (!sCOMPort.StartsWith("/")) { if (sCOMPort.EndsWith(USB_PREFIX + "0")) //USB0 match with /dev/tty.SLAB_USBtoUART { sCOMPort = MAC_PORT_PREFIX; } else { sCOMPort = sCOMPort.Replace(USB_PREFIX, MAC_PORT_PREFIX); //In MacOS USB 0 match with SLAB_USBtoUART } } } else { if (!sCOMPort.StartsWith("/")) { sCOMPort = LINUX_PORT_PREFIX + sCOMPort; } } } } #endif m_objRFE.ConnectPort(sCOMPort, Convert.ToInt32(m_ComboBaudRate.SelectedItem.ToString()), RFECommunicator.IsUnixLike(), RFECommunicator.IsUnixLike() && !RFECommunicator.IsMacOSPlatform()); m_objRFE.HoldMode = false; UpdateButtonStatus(); OnPortConnected(new EventArgs()); } catch (Exception obEx) { Trace.WriteLine(obEx.ToString()); } Cursor.Current = Cursors.Default; }