/// <summary>
        /// Initializes a new instance of the class.
        /// </summary>
        /// <param name="communicationInterface">Reference to the communication interface that is to be used to communicate with the VCU.</param>
        public FormShowSystemInformation(ICommunicationParent communicationInterface) : this()
        {
            if (communicationInterface is CommunicationParent)
            {
                CommunicationInterface = new CommunicationApplication(communicationInterface);
            }
            else
            {
                CommunicationInterface = new CommunicationApplicationOffline(communicationInterface);
            }

            Debug.Assert(CommunicationInterface != null);

            #region - [Display Timer] -
            m_TimerDisplayUpdate          = new System.Windows.Forms.Timer();
            m_TimerDisplayUpdate.Tick    += new EventHandler(DisplayUpdate);
            m_TimerDisplayUpdate.Interval = IntervalDisplayUpdate;
            m_TimerDisplayUpdate.Enabled  = true;
            m_TimerDisplayUpdate.Stop();
            #endregion - [Display Timer] -
        }
        /// <summary>
        /// Initializes a new instance of the class.
        /// </summary>
        /// <param name="communicationInterface">Reference to the communication interface that is to be used to communicate with the VCU.</param>
        public FormRealTimeClock(ICommunicationParent communicationInterface) : this()
        {
            if (communicationInterface is CommunicationParent)
            {
                CommunicationInterface = new CommunicationApplication(communicationInterface);
            }
            else
            {
                CommunicationInterface = new CommunicationApplicationOffline(communicationInterface);
            }

            Debug.Assert(CommunicationInterface != null);

            // Initialize the DateTime picker controls.
            m_DateTimePickerDate.Value = DateTime.Now;
            m_DateTimePickerTime.Value = DateTime.Now;

            // If the VCU uses a 4 digit year code, change the MaxDate and MinDate values of the DateTimePicker from the current default values. The default values
            // are MinDate = 1st Jan 1970 and MaxDate = 31st Dec 2069.
            if (Parameter.Use4DigitYearCode == true)
            {
                m_DateTimePickerDate.MaxDate = new DateTime(MaxYear4DigitYearCode, MaxMonth, MaxDay);
                m_DateTimePickerDate.MinDate = new DateTime(MinYear4DigitYearCode, MinMonth, MinDay);
            }
            else
            {
                m_DateTimePickerDate.MaxDate = new DateTime(MaxYear2DigitYearCode, MaxMonth, MaxDay);
                m_DateTimePickerDate.MinDate = new DateTime(MinYear2DigitYearCode, MinMonth, MinDay);
            }

            m_TableLayoutPanelDateTimePicker.Enabled = false;

            #region - [Display Timer] -
            m_TimerDisplayUpdate          = new System.Windows.Forms.Timer();
            m_TimerDisplayUpdate.Tick    += new EventHandler(DisplayUpdate);
            m_TimerDisplayUpdate.Interval = IntervalDisplayUpdate;
            m_TimerDisplayUpdate.Enabled  = true;
            m_TimerDisplayUpdate.Stop();
            #endregion - [Display Timer] -
        }
Example #3
0
        /// <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);
        }