Beispiel #1
0
        private void Form1_Load(object sender, EventArgs e)
        {
            //var usbDevices = USBDeviceInfo.GetUSBDevices();

            //foreach (var usbDevice in usbDevices)
            //{
            //    Console.WriteLine("Device ID: {0}, PNP Device ID: {1}, Description: {2}",
            //        usbDevice.DeviceID, usbDevice.PnpDeviceID, usbDevice.Description);
            //}


            string[] ports = SerialPort.GetPortNames();

            comports = COMPortInfo.GetCOMPortsInfo();

            //foreach (COMPortInfo port in comports)
            //{
            //    cbSerialPort.Items.Add(port.Name);
            //};


            foreach (string port in ports)
            {
                cbSerialPort.Items.Add(port);
            }
            ;


            label5.Text = GetMacAddress();
            Console.WriteLine(label5.Text);

            tbServerIP.Text = Properties.Settings.Default["cfgIP"].ToString();
            tbPort.Text     = Properties.Settings.Default["cfgPort"].ToString();
            tbResource.Text = Properties.Settings.Default["cfgRes"].ToString();
        }
Beispiel #2
0
        public void Test1()
        {
            var ports = COMPortInfo.GetCOMPortsInfo();

            Assert.NotNull(ports);
            Assert.NotEmpty(ports);
        }
Beispiel #3
0
 /// <summary>
 /// Use WMI to search for a device by its Frindly Name and returns a COM port string if found or NULL
 /// </summary>
 /// <param name="strFriendlyName">Friendly Name of device to find a COM port for</param>
 /// <returns>COMn where n is the COM port number</returns>
 private static string UseWMIForCOMPortByFriendlyName(string strFriendlyName)
 {
     foreach (COMPortInfo comPort in COMPortInfo.GetCOMPortsInfo())
     {
         if (comPort.Description.StartsWith(strFriendlyName))
         {
             return(comPort.Name);
         }
     }
     return(null);
 }
Beispiel #4
0
 public void Connect(int i)
 {
     _sp.DtrEnable       = true;
     _sp.RtsEnable       = true;
     _sp.ReadBufferSize  = 1024;
     _sp.WriteBufferSize = 1024;
     _sp.PortName        = COMPortInfo.GetCOMPortsInfo()[i].Name;
     _sp.BaudRate        = 115200;
     try
     {
         _sp.Open();
     }
     catch
     { }
 }
Beispiel #5
0
        public List <string> GetSerialPorts()
        {
            List <string>      available_ports = new List <string>();
            List <COMPortInfo> SerialInfo      = new List <COMPortInfo>(COMPortInfo.GetCOMPortsInfo());

            SerialInfo.Sort();

            foreach (COMPortInfo comPort in SerialInfo)
            {
                if (int.TryParse(comPort.Name.Substring(3), out int test))
                {
                    available_ports.Add(string.Format("{0} : \"{1}\"", comPort.Name, comPort.Description));
                }
            }
            return(available_ports);
        }
Beispiel #6
0
        void OnCOMRefresh(object sender, EventArgs e)
        {
            try
            {
                if (__uC_TabFirmware.IsBootloadMode)
                {
                    return;
                }

                this.BeginInvoke(new Action(() =>
                {
                    if (COMPortInfo.GetCOMPortsInfo().Count <= 0)
                    {
                        __com_toolStripComboBox.Items.Clear();
                        __com_toolStripComboBox.Items.Add("NO Device");
                        __com_toolStripComboBox.SelectedIndex = 0;
                        __connect_toolStripButton.Enabled     = false;
                        __refresh_toolStripButton.Enabled     = true;
                    }
                    else
                    {
                        __com_toolStripComboBox.Enabled   = true;
                        __refresh_toolStripButton.Enabled = true;
                        // com 리스트 업데이트
                        __com_toolStripComboBox.Items.Clear();

                        foreach (COMPortInfo com in COMPortInfo.GetCOMPortsInfo())
                        {
                            __com_toolStripComboBox.Items.Add(com.Description);
                        }

                        for (int i = 0; i < __com_toolStripComboBox.Items.Count; i++)
                        {
                            if (__com_toolStripComboBox.Items[i].ToString() == Properties.Settings.Default.LASTCOMDEVICE)
                            {
                                __com_toolStripComboBox.SelectedIndex = i;
                                break;
                            }
                        }
                    }
                }));
            }catch
            {
            }
        }
Beispiel #7
0
 public static string[] getListCOM()
 {
     try
     {
         List <string> result = new List <string>();
         foreach (COMPortInfo comPort in COMPortInfo.GetCOMPortsInfo())
         {
             if (comPort.Description.IndexOf("Prolific USB-to-Serial Comm Port") >= 0)
             {
                 result.Add(comPort.Name);
             }
         }
         return(result.ToArray());
     }
     catch (Exception)
     {
         return(new string[0]);
     }
 }
Beispiel #8
0
 private void __uC_TabFirmware_On_BootloaderUploadMode(object sender, EventArgs e)
 {
     firm_dowing();
     _com.SerialStop();
     if (__com_toolStripComboBox.SelectedIndex < 0)
     {
         MessageBox.Show(CLanguages.GetTranslate("MESSAGE_COM_SELECT"), CLanguages.GetTranslate("MESSAGE_ERROR"), MessageBoxButtons.OK, MessageBoxIcon.Error);
     }
     else
     {
         try
         {
             __uC_TabFirmware.COMPORT = COMPortInfo.GetCOMPortsInfo()[__com_toolStripComboBox.SelectedIndex].Name;
             __uC_TabFirmware.UploadFirmware();
         }catch
         {
             MessageBox.Show("Please try again in a few minutes.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
         }
     }
 }
Beispiel #9
0
 private void __uC_TabFirmware_On_BootloaderMode(object sender, EventArgs e)
 {
     firm_dowing();
     _com.SerialStop();
     if (__com_toolStripComboBox.SelectedIndex < 0)
     {
         MessageBox.Show(CLanguages.GetTranslate("MESSAGE_COM_SELECT"), CLanguages.GetTranslate("MESSAGE_ERROR"), MessageBoxButtons.OK, MessageBoxIcon.Error);
     }
     else
     {
         try {
             __uC_TabFirmware.COMPORT = COMPortInfo.GetCOMPortsInfo()[__com_toolStripComboBox.SelectedIndex].Name;
             __uC_TabFirmware.DownloadFirmware();
         }
         catch
         {
             Console.WriteLine("firmware down 올바른 com 포트가 없습니다.");
         }
     }
 }
Beispiel #10
0
        private void connectToolStripMenuItem_Click(object sender, EventArgs e)
        {
            String selectedPort = "";

            foreach (COMPortInfo comPort in COMPortInfo.GetCOMPortsInfo())
            {
                if (comPort.Description == serialPortToolStripMenuItem.SelectedItem.ToString())
                {
                    selectedPort = comPort.Name;
                }
            }

            serSock.init(selectedPort, Properties.Settings.Default.baudRate);

            try {
                serSock.connect();
                this.tsLblStatus.Text = "Status: Connected";
            } catch (serialException exc) {
                log(String.Format("ERROR: {0}\r\nStack{1}", exc.Message, exc.StackTrace), logTags.serialServer);
            }
        }
Beispiel #11
0
        private void frmMain_Load(object sender, EventArgs e)
        {
            //Find the serial port names
            foreach (COMPortInfo comPort in COMPortInfo.GetCOMPortsInfo())
            {
                serialPortToolStripMenuItem.Items.Add(comPort.Description);
            }

            // HACK!
            if (serialPortToolStripMenuItem.Items.Count == 1)
            {
                serialPortToolStripMenuItem.SelectedIndex = 0;
                connectToolStripMenuItem_Click(sender, e);
                // Nog een!
            }
            else if (serialPortToolStripMenuItem.Items.Count == 2)
            {
                serialPortToolStripMenuItem.SelectedIndex = 1;
                connectToolStripMenuItem_Click(sender, e);
            }
        }
        internal List <COMPortInfo> GetCOMPortList()
        {
            COMPortInfo info = new COMPortInfo();

            return(info.GetCOMPortsInfo());
        }
Beispiel #13
0
        public async Task Open(SerialEnum serialType)
        {
            if (Status == SerialPortStatus.Initialled)
            {
                Stopwatch sw = new Stopwatch();
                sw.Start();
                Status = SerialPortStatus.Opening;
                var dis = SerialPort.GetPortNames();
                if (!dis.Any())
                {
                    return;
                }

                foreach (COMPortInfo comPort in COMPortInfo.GetCOMPortsInfo())
                {
                    Console.WriteLine($"{comPort.Name} – {comPort.Description}");
                    if (comPort.Description.IndexOf("USB", StringComparison.Ordinal) == -1 &&
                        comPort.Description.IndexOf("Virtual Serial Port", StringComparison.Ordinal) == -1)
                    {
                        continue;
                    }

                    try
                    {
                        var portName = comPort.Name.Split("->".ToCharArray())[0];
                        LogFactory.Create().Info("portname:" + portName);

                        SerialPort = new SerialPort(portName);
                        if (SerialPort == null)
                        {
                            continue;
                        }

                        SerialPort.WriteTimeout    = 50;
                        SerialPort.ReadTimeout     = 50;
                        SerialPort.BaudRate        = 9600;
                        SerialPort.Parity          = Parity.None;
                        SerialPort.StopBits        = StopBits.One;
                        SerialPort.DataBits        = 8;
                        SerialPort.Handshake       = Handshake.None;
                        SerialPort.WriteBufferSize = 1024;
                        SerialPort.ReadBufferSize  = 1024;

                        SerialPort.DataReceived  += SerialPort_DataReceived;
                        SerialPort.ErrorReceived += SerialPort_ErrorReceived;

                        _readCancellationTokenSource = new CancellationTokenSource();
                        SerialPort.Open();
                        Send(generatePingBytesByType(serialType), CancellationToken.None);

                        var cancellationToken = new CancellationTokenSource(1000).Token;
                        completionSource = new TaskCompletionSource <byte[]>();
                        using (cancellationToken.Register(() => completionSource.TrySetResult(new byte[] { })))
                        {
                            var x   = await completionSource.Task;
                            var ret = Shunxi.Common.Utility.Common.BytesToString(x);
                            if (!string.IsNullOrEmpty(ret) && ret.Length > 10)
                            {
                                Status = SerialPortStatus.Opened;
                                LogFactory.Create().Info($"Serial Port {portName} Opened");
                                break;
                            }

                            Status = SerialPortStatus.Initialled;
                            SerialPort.DataReceived  -= SerialPort_DataReceived;
                            SerialPort.ErrorReceived -= SerialPort_ErrorReceived;
                            SerialPort.Close();
                            SerialPort.Dispose();
                            SerialPort = null;
                        }
                    }
                    catch (Exception ex)
                    {
                        if (SerialPort != null)
                        {
                            SerialPort.DataReceived  -= SerialPort_DataReceived;
                            SerialPort.ErrorReceived -= SerialPort_ErrorReceived;
                        }

                        Status = SerialPortStatus.Initialled;
                        SerialPort?.Close();
                        SerialPort?.Dispose();
                        LogFactory.Create().Info("port open error" + ex.Message);
                    }
                }

                sw.Stop();
                LogFactory.Create().Info("open port consume time " + sw.ElapsedMilliseconds);
            }
        }