/// <summary> /// Scans each of the communication ports listed in the registry to determine if it is connected to target hardware and generates a list /// of the target configuration information and the communication settings for each target that is located. /// </summary> /// <param name="targetConfigurationList">The list containing the target configuration information for any targets connected to the PTU.</param> /// <param name="communicationSettingList">The communication settings associated with each target that was found.</param> /// <param name="listBoxTargetsFound">The <c>ListBox</c> control on which the target names are to be displayed.</param> /// <param name="statusInformation">The control on which the status information is to be displayed.</param> /// <returns>A flag to indicate whether one or more targets were found; true, if targets were found, otherwise, false.</returns> public bool GetTargets(ListBox listBoxTargetsFound, Control statusInformation, out List <TargetConfiguration_t> targetConfigurationList, out List <CommunicationSetting_t> communicationSettingList) { // Instantiate to output parameters. communicationSettingList = new List <CommunicationSetting_t>(); targetConfigurationList = new List <TargetConfiguration_t>(); CommunicationApplication communicationInterface; // Flag to indicate whether target hardware was found; true, if one or more targets were found, otherwise, false. bool targetFound = false; if (Parameter.CommunicationType == Parameter.CommunicationTypeEnum.Both || Parameter.CommunicationType == Parameter.CommunicationTypeEnum.RS232) { // ----------------------------------------------------------------- // Scan each serial communication (COM) port listed in the Registry. // ----------------------------------------------------------------- // Get the list of available serial COM ports from the registry. RegistryKey root = Registry.LocalMachine; CommunicationSetting_t communicationSetting = new CommunicationSetting_t(); // Set the protocol to serial communication. communicationSetting.Protocol = Protocol.RS232; // Initialize the serial communication parameters. communicationSetting.SerialCommunicationParameters.SetToDefault(); TargetConfiguration_t targetConfiguration; using (RegistryKey serialCommunication = root.OpenSubKey(RegistryKeySerialCommunication)) { // Scan each port in the Registry. foreach (string valueName in serialCommunication.GetValueNames()) { // Filter out those '\HKEY_LOCAL_MACHINE\HARDWARE\DEVICEMAP\SERIALCOMM' keys that are not to be included in the search. switch (valueName) { // Skip those registry keys defined here. case SerialCommRegistryKeyWinachsf0: continue; default: // Process the key. break; } string value = serialCommunication.GetValue(valueName).ToString(); communicationSetting.Port.Name = value; communicationSetting.Port.FullSpecification = value.PadRight(ComDeviceTotalCharacters) + " - " + valueName; communicationSetting.Port.Type = (communicationSetting.Port.FullSpecification.Contains(VirtualComPort)) ? PortType.VCP : PortType.COM; // Determine the port identifier, this is a 16 bit Unicode string representation of the serial port number e.g. for physical and virtual COM ports // this takes the form: 1, 2, 3 ... etc and is used by the InitCommunication() method in PTUDLL32 to specify the serial communication port. switch (communicationSetting.Port.Type) { case PortType.COM: case PortType.VCP: communicationSetting.PortIdentifier = communicationSetting.Port.Name.Remove(0, communicationSetting.Port.Type.ToString().Length); break; default: throw new ArgumentException("FormSelectTargetLogic.LocateTargetHardware()", "communicationSetting.Port.Type"); } statusInformation.Text = Resources.TextSearchingForTargetOn + CommonConstants.Space + communicationSetting.Port.FullSpecification; statusInformation.Update(); // Instantiate the appropriate type of communication interface. communicationInterface = new CommunicationApplication(); try { if (communicationInterface.ScanPort(communicationSetting, out targetConfiguration) == true) { targetConfigurationList.Add(targetConfiguration); listBoxTargetsFound.Items.Add(targetConfiguration.SubSystemName); listBoxTargetsFound.Update(); statusInformation.Text = Resources.TextTargetFoundOn + CommonConstants.Space + communicationSetting.Port.FullSpecification; communicationSettingList.Add(communicationSetting); targetFound = true; } } catch (Exception) { statusInformation.Text = Resources.TextNoTargetFoundOn + CommonConstants.Space + communicationSetting.Port.FullSpecification; statusInformation.Update(); continue; } } } } if (Parameter.CommunicationType == Parameter.CommunicationTypeEnum.Both || Parameter.CommunicationType == Parameter.CommunicationTypeEnum.TCPIP) { // ----------------------------------------------------------------- // Scan each URI listed in the Data Dictionary. // ----------------------------------------------------------------- CommunicationSetting_t communicationSetting = new CommunicationSetting_t(); // Set the protocol to IP communication. communicationSetting.Protocol = Protocol.TCPIP; TargetConfiguration_t targetConfiguration; // Scan each port in the Registry. foreach (string URI in Parameter.URIList) { communicationSetting.PortIdentifier = URI; // Instantiate the appropriate type of communication interface. communicationInterface = new CommunicationApplication(); try { if (communicationInterface.ScanPort(communicationSetting, out targetConfiguration) == true) { targetConfigurationList.Add(targetConfiguration); listBoxTargetsFound.Items.Add(targetConfiguration.SubSystemName + CommonConstants.Space + "(" + URI + ")"); listBoxTargetsFound.Update(); statusInformation.Text = Resources.TextTargetFoundOn + CommonConstants.Space + URI; communicationSettingList.Add(communicationSetting); targetFound = true; } } catch (Exception) { statusInformation.Text = Resources.TextNoTargetFoundOn + CommonConstants.Space + communicationSetting.Port.Name; statusInformation.Update(); continue; } } } if (targetFound == true) { statusInformation.Text = targetConfigurationList.Count.ToString() + CommonConstants.Space + Resources.TextTargetsFound; statusInformation.Update(); // Highlight the first logic controller that was found. listBoxTargetsFound.SetSelected(0, true); } else { statusInformation.Text = Resources.TextNoTargetsFound; statusInformation.Update(); } return(targetFound); }
/// <summary> /// Scans each of the communication ports listed in the registry to determine if it is connected to target hardware and generates a list /// of the target configuration information and the communication settings for each target that is located. /// </summary> /// <param name="targetConfigurationList">The list containing the target configuration information for any targets connected to the PTU.</param> /// <param name="communicationSettingList">The communication settings associated with each target that was found.</param> /// <param name="listBoxTargetsFound">The <c>ListBox</c> control on which the target names are to be displayed.</param> /// <param name="statusInformation">The control on which the status information is to be displayed.</param> /// <returns>A flag to indicate whether one or more targets were found; true, if targets were found, otherwise, false.</returns> public bool GetTargets(ListBox listBoxTargetsFound, Control statusInformation, out List <TargetConfiguration_t> targetConfigurationList, out List <CommunicationSetting_t> communicationSettingList) { // Instantiate to output parameters. communicationSettingList = new List <CommunicationSetting_t>(); targetConfigurationList = new List <TargetConfiguration_t>(); CommunicationApplication communicationInterface; // Flag to indicate whether target hardware was found; true, if one or more targets were found, otherwise, false. bool targetFound = false; if (Parameter.CommunicationType == Parameter.CommunicationTypeEnum.Both || Parameter.CommunicationType == Parameter.CommunicationTypeEnum.RS232) { // ----------------------------------------------------------------- // Scan each serial communication (COM) port listed in the Registry. // ----------------------------------------------------------------- // Get the list of available serial COM ports from the registry. RegistryKey root = Registry.LocalMachine; CommunicationSetting_t communicationSetting = new CommunicationSetting_t(); // Set the protocol to serial communication. communicationSetting.Protocol = Protocol.RS232; // Initialize the serial communication parameters. communicationSetting.SerialCommunicationParameters.SetToDefault(); TargetConfiguration_t targetConfiguration; using (RegistryKey serialCommunication = root.OpenSubKey(RegistryKeySerialCommunication)) { // If no serial ports have ever been connected to the PC since reset, "serialCommunication == null" // will be true. if (serialCommunication == null) { if (Parameter.CommunicationType == Parameter.CommunicationTypeEnum.RS232) { // Since only RS-232 has been selected as the only method to communicate with // the VCU, throw an exception which is handled in the calling function throw new Exception(); } } // If a USB/Serial port has been connected and removed, serialCommunication // will be a non-null value, but no serial ports will be discovered (i.e. // serialCommunication.GetValueNames() will return an empty array else { // Scan each port in the Registry. foreach (string valueName in serialCommunication.GetValueNames()) { // Filter out those '\HKEY_LOCAL_MACHINE\HARDWARE\DEVICEMAP\SERIALCOMM' keys that are not to be included in the search. switch (valueName) { // Skip those registry keys defined here. case SerialCommRegistryKeyWinachsf0: continue; default: // Process the key. break; } string value = serialCommunication.GetValue(valueName).ToString(); communicationSetting.Port.Name = value; communicationSetting.Port.FullSpecification = value.PadRight(ComDeviceTotalCharacters) + " - " + valueName; communicationSetting.Port.Type = (communicationSetting.Port.FullSpecification.Contains(VirtualComPort)) ? PortType.VCP : PortType.COM; // Determine the port identifier, this is a 16 bit Unicode string representation of the serial port number e.g. for physical and virtual // COM ports this takes the form: 1, 2, 3 ... etc. switch (communicationSetting.Port.Type) { case PortType.COM: case PortType.VCP: communicationSetting.PortIdentifier = communicationSetting.Port.Name.Remove(0, communicationSetting.Port.Type.ToString().Length); break; default: throw new ArgumentException("FormSelectTargetLogic.LocateTargetHardware()", "communicationSetting.Port.Type"); } statusInformation.Text = Resources.TextSearchingForTargetOn + CommonConstants.Space + communicationSetting.Port.FullSpecification; statusInformation.Update(); // Instantiate the appropriate type of communication interface. communicationInterface = new CommunicationApplication(); try { if (communicationInterface.ScanPort(communicationSetting, out targetConfiguration) == true) { targetConfigurationList.Add(targetConfiguration); listBoxTargetsFound.Items.Add(targetConfiguration.SubSystemName + " - (COM" + communicationSetting.PortIdentifier + ")"); listBoxTargetsFound.Update(); statusInformation.Text = Resources.TextTargetFoundOn + CommonConstants.Space + communicationSetting.Port.FullSpecification; statusInformation.Update(); communicationSettingList.Add(communicationSetting); targetFound = true; } } catch (Exception) { statusInformation.Text = Resources.TextNoTargetFoundOn + CommonConstants.Space + communicationSetting.Port.FullSpecification; statusInformation.Update(); continue; } } } } } statusInformation.Text = string.Empty; statusInformation.Update(); if (Parameter.CommunicationType == Parameter.CommunicationTypeEnum.Both || Parameter.CommunicationType == Parameter.CommunicationTypeEnum.TCPIP) { // ----------------------------------------------------------------- // Scan each URI listed in the Data Dictionary. // ----------------------------------------------------------------- CommunicationSetting_t communicationSetting = new CommunicationSetting_t(); // Set the protocol to IP communication. communicationSetting.Protocol = Protocol.TCPIP; TargetConfiguration_t targetConfiguration; // Initialize the ProgressBar control. m_ProgressBarScan.Enabled = true; m_ProgressBarScan.Visible = true; m_LegendScanProgress.Visible = true; m_ProgressBarScan.Maximum = Parameter.URIList.Count; m_ProgressBarScan.Value = 0; m_BtnOK.Enabled = false; m_CancelSelected = false; // Scan each port in the Registry. foreach (string uRI in Parameter.URIList) { // Ensure that the form remains responsive during the asynchronous operation. Application.DoEvents(); // Check whether the Cancel button has been selected. if (m_CancelSelected == true) { // Yes - Terminate the scan. break; } // Update the progress bar. m_ProgressBarScan.Value++; communicationSetting.PortIdentifier = uRI; statusInformation.Text = Resources.TextSearchingForTargetOn + CommonConstants.Space + Resources.TextURI + CommonConstants.Colon + communicationSetting.PortIdentifier; statusInformation.Update(); // Instantiate the appropriate type of communication interface. communicationInterface = new CommunicationApplication(); try { if (communicationInterface.ScanPort(communicationSetting, out targetConfiguration) == true) { targetConfigurationList.Add(targetConfiguration); listBoxTargetsFound.Items.Add(targetConfiguration.SubSystemName + CommonConstants.BindingMessage + Resources.TextURI + CommonConstants.Colon + communicationSetting.PortIdentifier); listBoxTargetsFound.Update(); statusInformation.Text = Resources.TextTargetFoundOn + CommonConstants.Space + Resources.TextURI + CommonConstants.Colon + communicationSetting.PortIdentifier; statusInformation.Update(); communicationSettingList.Add(communicationSetting); targetFound = true; } } catch (Exception) { continue; } } m_BtnOK.Enabled = true; m_ProgressBarScan.Enabled = false; m_ProgressBarScan.Visible = false; m_LegendScanProgress.Visible = false; } if (targetFound == true) { statusInformation.Text = targetConfigurationList.Count.ToString() + CommonConstants.Space + Resources.TextTargetsFound; statusInformation.Update(); // Highlight the first logic controller that was found. listBoxTargetsFound.SetSelected(0, true); } else { statusInformation.Text = Resources.TextNoTargetsFound; statusInformation.Update(); } return(targetFound); }