示例#1
0
        private void timer1_Tick(object sender, EventArgs e)
        {
            int    target;
            object report;

            while (Reporter.GetReport(out target, out report))
            {
                switch (target)
                {
                case 1:     //connected divices case 2: disconnect device case 3: log
                {
                    lbDevices.Items.Add(report.ToString());
                    break;
                }

                case 2:     //disconnect device
                {
                    string devicename = report.ToString();

                    if (devicename.Length > 0)
                    {
                        foreach (string item in lbDevices.Items)
                        {
                            if (item.Contains(devicename))
                            {
                                lbDevices.Items.Remove(item);
                                if (selecteddevicename == devicename)
                                {
                                    selecteddevicename    = null;
                                    commandtxtbox.Enabled = true;
                                }
                                break;
                            }
                        }

                        for (int i = 0; i < devicenames.Count; i++)
                        {
                            if (devicenames.ElementAt(i) == devicename)
                            {
                                devicenames.RemoveAt(i);
                                break;
                            }
                        }
                    }
                    break;
                }

                case 3:    //remember devicename
                {
                    string devicename = report as string;


                    for (int i = 0; i < devicenames.Count; i++)
                    {
                        if (devicenames.ElementAt(i) == devicename)
                        {
                            devicenames.RemoveAt(i);
                            break;
                        }
                    }

                    devicenames.Add(devicename);

                    break;
                }

                case 4:     //log
                {
                    lbLog.Items.Add(report.ToString());
                    break;
                }
                }
            }
        }
示例#2
0
        /// <summary>
        /// Returns the state of the connection and tries to connect if possible
        /// </summary>
        /// <returns>True when the connection is valid;False when connection doesn't seem valid</returns>
        public bool checkConnection()
        {
            if (TestConnection())
            {
                if (Program.MasterServer.Connected && !Connected)
                {
                    int    deviceID   = 0;
                    int    serverid   = 0;
                    string servername = "";

                    if (HandShake(this.DeviceName, devicetype, ref deviceID, ref serverid, ref servername, Program.MasterServer.Server))
                    {
                        if (Send("{0,0,2," + serverid + "," + comID + "}"))
                        {
                            comID = deviceID;

                            handshaked = true;

                            Reporter.AddReport(4, "Handshake for '" + this.DeviceName + "' succeeded!");

                            isdisconnected = false;
                            return(true);
                        }
                    }
                    else
                    {
                        if (!isdisconnected)
                        {
                            isdisconnected = true;

                            Reporter.AddReport(4, "Handshake for '" + this.DeviceName + "' failed");
                        }
                    }
                }
                return(true);
            }
            else
            {
                if (Board != null)
                {
                    LostConnection("Disconnected '" + this.DeviceName + "' from `" + this.ComPort + "`");
                    Board             = null;
                    readTimer.Enabled = false;
                }
            }

            //if no comport (length = 0) is given, select any comport that is available and not in use
            if (TargetComPort == null || TargetComPort.Length == 0)
            {
                //loop each available comport and try to connect
                foreach (string s in SerialPort.GetPortNames())
                {
                    bool success = makeconnection(s, TargetBaudRate);
                    if (success == true)
                    {
                        break;
                    }
                }
                //NOTE: success will stay false when no comport is available or no good connnection can be made
            }
            else //A COMPORT is specified
            {
                //define SerialPort object
                makeconnection(TargetComPort, TargetBaudRate);
            }

            if (Board == null)
            {
                Debug.WriteLine("ERROR: Could not establish connection.");
                return(false);
            }
            else
            {
                return(true);
            }
        }