Example #1
0
        private void ctxMenuPortsTable_Opening(object sender, CancelEventArgs e)
        {
            //Don't show the "KILL" menu if the serial port is not opened.
            if (listPortTable.SelectedIndices.Count < 0)
            {
                menuKill.Visible = false;
                return;
            }

            string      strPort = listPortTable.SelectedItems[0].SubItems[0].Text;
            ComPortItem item    = ComPortControlTable.GetItemByPortName(strPort);

            if (item == null || item.OwnProcessId <= 0)
            {
                menuKill.Visible = false;
                return;
            }

            menuKill.Visible = true;

            menuEnableAutoRefresh.Checked     = AppConfig.EnableAutoRefresh;
            menuOwnerFormatDomainUser.Checked = (AppConfig.OwnerFormat == OwnerShowFormat.DomainUser);
            menuOwnerFormatFullName.Checked   = (AppConfig.OwnerFormat == OwnerShowFormat.FullName);
            menuOwnerFormatShortName.Checked  = (AppConfig.OwnerFormat == OwnerShowFormat.ShortName ||
                                                 AppConfig.OwnerFormat == OwnerShowFormat.Default);
            menuOwnerFormatPhone.Checked = (AppConfig.OwnerFormat == OwnerShowFormat.Phone);
        }
        public static void InitTable()
        {
            RegistryKey key    = Registry.LocalMachine;
            RegistryKey comKey = null;

            try
            {
                comKey = key.OpenSubKey("HARDWARE\\DEVICEMAP\\SERIALCOMM");
                if (comKey == null)
                {
                    throw new Exception("Cannot find the serial port device map from registry \"HARDWARE\\DEVICEMAP\\SERIALCOMM\"!");
                }
            }
            catch
            {
                throw new Exception("Cannot find the serial port device map from registry \"HARDWARE\\DEVICEMAP\\SERIALCOMM\"!");
            }

            string[] valNames = comKey.GetValueNames();

            //As we need the table is sorted by port integer number, so here we need create such sorted dictionary.
            SortedDictionary <uint, string> portTable = new SortedDictionary <uint, string>();

            foreach (string name in valNames)
            {
                string val = (string)comKey.GetValue(name);
                try
                {
                    uint port = uint.Parse(val.Substring("COM".Length)); //extract the port integer number, remove "COM" prefix

                    string handleName = name.ToLower();
                    if (!handleName.StartsWith("\\device\\"))
                    {
                        handleName = "\\device\\" + handleName;
                    }
                    portTable.Add(port, handleName);
                }
                catch
                {
                    continue;
                }
            }

            _tableByDeviceName.Clear();
            _tableByPortName.Clear();

            foreach (uint port in portTable.Keys)
            {
                string      thePortName   = "COM" + port.ToString();
                string      theDeviceName = portTable[port];
                ComPortItem item          = new ComPortItem(thePortName, theDeviceName);
                _tableByDeviceName.Add(theDeviceName, item);
                _tableByPortName.Add(thePortName, item);
            }

            _deviceNamePatterns = ExtractDeviceNamePatterns();
        }
Example #3
0
        private void listPortTable_DoubleClick(object sender, EventArgs e)
        {
            if (listPortTable.SelectedIndices.Count <= 0)
            {
                return;
            }
            string      portName = listPortTable.SelectedItems[0].Text;
            ComPortItem item     = ComPortControlTable.GetItemByPortName(portName);

            if (item == null || item.OwnProcessId < 0 || item.OwnUser == null)
            {
                return;
            }

            new ComPortShowDialog(item).ShowDialog();
        }
Example #4
0
 /// <summary>
 /// Create row for each COM port and initialize it to empty except port name.
 /// </summary>
 private void CreateRows()
 {
     listPortTable.BeginUpdate();
     listPortTable.Items.Clear();
     string[] ports = ComPortControlTable.AllPortNames;
     comboBoxGotoPort.Items.Clear();
     comboBoxGotoPort.Items.AddRange(ports);
     foreach (string port in ports)
     {
         ListViewItem lvi = new ListViewItem(new string[] { port, string.Empty, string.Empty, string.Empty });
         listPortTable.Items.Add(lvi);
         ComPortItem item = ComPortControlTable.GetItemByPortName(port);
         if (item != null)
         {
             item.GuiItem = lvi;
         }
     }
     listPortTable.EndUpdate();
 }
        public static List <ComPortItem> GetOtherPortsInSameProcess(ComPortItem item)
        {
            if (item.OwnProcessId < 0)
            {
                return(null);
            }

            List <ComPortItem> listPorts = new List <ComPortItem>();

            foreach (ComPortItem ci in _tableByPortName.Values)
            {
                if (ci.PortName != item.PortName && item.OwnProcessId == ci.OwnProcessId)
                {
                    listPorts.Add(ci);
                }
            }

            return(listPorts);
        }
        public ComPortShowDialog(ComPortItem selPort)
        {
            InitializeComponent();

            Theme theme = ThemeManager.CurrentTheme;

            this.BackColor                = theme.BackColor;
            panelUp.BackColor             = theme.ColorA;
            listViewAllOwnProcs.BackColor = theme.ColorE;
            foreach (Control c in this.Controls)
            {
                c.ForeColor = theme.FontColor;
            }
            listViewAllOwnProcs.ForeColor = theme.FontColor;

            _selPort = selPort;
            AutoSizeUIComponent();

            RefreshUIData();
        }
        static public void RefreshAll()
        {
            ResetDetectedFlag();

            //Get all processes that are opening serial port by utility "handle"
            List <ProcessOwnInfo> listOwnProcesses =
                HandleWrapper.GetAllOwnProcesses(ComPortControlTable.DeviceNamePatterns);

            if (listOwnProcesses == null)
            {
                return;
            }

            //Usually, one process often opens more than one serial port, so here creats the dictionary
            //to store the process that have known its owner, so as not to search process owner twice.
            Dictionary <int, string> procOwnerTable = new Dictionary <int, string>(); //key: process ID; value: owner

            foreach (ProcessOwnInfo pinfo in listOwnProcesses)
            {
                string owner    = null;
                string procName = null;

                Application.DoEvents();

                //here map the device name to serial port name
                ComPortItem citem = ComPortControlTable.GetItemByDevieName(pinfo.DeviceName);
                if (citem == null)
                {
                    continue;
                }

                //We have searched the process owner before, we just copy its result,
                //so as not to search twice.
                if (procOwnerTable.ContainsKey(pinfo.ProcessId))
                {
                    owner = procOwnerTable[pinfo.ProcessId];
                }
                else
                {
                    //We havn't searched the process owner before
                    string sid = null;
                    owner = Utility.GetProcessOwner(pinfo.ProcessId, true);
                    //owner = ProcessOwnerFinderWmi.GetProcessOwnerByPID(pinfo.ProcessId, out sid);
                    if (owner == null) //search owner failed
                    {
                        continue;
                    }
                    procOwnerTable.Add(pinfo.ProcessId, owner);
                }

                procName = pinfo.ProcessName;
                //if the process name have suffix ".exe", we just remove it to have nice format data show to user.
                int exeindex = pinfo.ProcessName.LastIndexOf(".exe");
                if (exeindex > 0)
                {
                    procName = pinfo.ProcessName.Substring(0, exeindex); //remove the suffix ".exe"
                }

                //update the serial port info as we get all result.
                if (citem.Update(owner, procName, pinfo.ProcessName + " (" + pinfo.ProcessId + ")", pinfo.ProcessId))
                {
                    citem.GuiItem.SubItems[1].Text = citem.FormatedOwner;
                    citem.GuiItem.SubItems[2].Text = citem.OwnAppName;
                    citem.GuiItem.SubItems[3].Text = citem.OwnDetailProcessInfo;
                }
                citem.DetectedFlag = true; //mark the serial port is detected be opened.
            }

            //handle other serial ports that is not opening, so reset the info to empty.
            ComPortItem[] allItems = ComPortControlTable.AllItems;
            foreach (ComPortItem item in allItems)
            {
                if (item.GuiItem != null && !item.DetectedFlag)
                {
                    item.ClearContent();
                }
            }
        }