Exemple #1
0
        protected void LostConnection(string message)
        {
            connection = false;
            Debug.WriteLine("Device `" + DeviceName + "` communication line was lost");

            if (Connected && Program.MasterServer.Connected)
            {
                try
                {
                    Program.MasterServer.Server.Exit(comID);
                }
                catch (System.ObjectDisposedException)
                {
                    Debug.WriteLine("Tried to unregister but server channel was lost");
                }

                comID      = 0;
                handshaked = false;

                showhandshakefailmessage = true;
            }

            Reporter.AddReport(2, this.DeviceName);
            Reporter.AddReport(4, message);
        }
Exemple #2
0
        protected void MadeConnection()
        {
            connection = true;
            Debug.WriteLine("Device `" + DeviceName + "` communication line was opened");

            Reporter.AddReport(3, DeviceName);
        }
Exemple #3
0
        private bool makeconnection(string COMID, int baudrate)
        {
            if (Board != null)
            {
                if (Board.IsOpen)
                {
                    return(true);
                }
            }

            //no board comport connection is made
            Board = new SerialPort(COMID, baudrate);

            try
            {
                Board.Open();
            }
            catch
            {
                return(false);
            }

            //check condition of communication line
            if (TestConnection())
            {
                Reporter.AddReport(1, "\'" + this.DeviceName + "\'," + this.ComPort + "," + this.BaudRate.ToString());

                Reporter.AddReport(4, "Arduino `" + this.DeviceName + "` connected on port " + this.ComPort + ", baudrate" + this.BaudRate.ToString());

                readTimer.Enabled = true;

                MadeConnection();

                return(true);
            }
            Board = null;
            return(false);
        }
        private bool DoHandShake()
        {
            int    deviceID   = 0;
            int    serverid   = 0;
            string servername = "";

            if (HandShake(DeviceName, devicetype, ref deviceID, ref serverid, ref servername, Server))
            {
                serverID   = serverid;
                comID      = deviceID;
                ServerName = servername;

                handshaked = true;

                tryconnecting = false;

                Reporter.AddReport(1, "\'" + this.DeviceName + "\', " + this.ServerName);
                Reporter.AddReport(4, devicetype + " connected '" + this.DeviceName + "' to `" + this.ServerName + "`");
                Reporter.AddReport(4, "Handshake for '" + this.DeviceName + "' succeeded!");

                isdisconnected = false;

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

                    LostConnection("Disconnected '" + this.DeviceName + "' from `" + this.ServerName + "`");
                }

                Server = null;
                return(false);
            }
        }
Exemple #5
0
        public bool HandShake(string devicename, string devicetype, ref int deviceID, ref int serverID, ref string ServerName, GarageServer.TrafficMessageClient Server)
        {
            string result;

            try
            {
                result = Server.Handshake(1, DeviceName + "," + devicetype);
            }
            catch
            {
                Debug.WriteLine("Handshake failed (0)");
                return(false);
            }

            if (Int32.TryParse(result, out deviceID))
            {
                if (deviceID > 0)
                {
                    comID = deviceID;

                    Debug.WriteLine("Completed handshake 1");

                    result = Server.Handshake(2, comID.ToString());

                    if (result.Length > 0)
                    {
                        ServerName = result;
                        Debug.WriteLine("Completed handshake 2");

                        result = Server.Handshake(3, comID.ToString());

                        int expectedserverID;

                        if (Int32.TryParse(result, out expectedserverID))
                        {
                            if (expectedserverID > 0)
                            {
                                serverID = expectedserverID;

                                Debug.WriteLine("Completed handshake 3");

                                result = Server.Handshake(4, Program.MasterServer.comID.ToString() + "," + comID);

                                if (result == "OK")
                                {
                                    Debug.WriteLine("Completed handshake 4 - linking");
                                    MadeConnection();
                                    return(true);
                                }
                                else
                                {
                                    Debug.WriteLine("Handshake failed (4)");
                                }
                            }
                            else
                            {
                                Debug.WriteLine("Handshake failed (3)");
                            }
                        }
                    }
                    else
                    {
                        Debug.WriteLine("Handshake failed (2)");
                    }
                }
                else
                {
                    Debug.WriteLine("Handshake failed (1)");
                }
            }

            if (showhandshakefailmessage)
            {
                showhandshakefailmessage = false;
                Reporter.AddReport(4, "Handshake for '" + this.DeviceName + "' failed!");
            }
            return(false);
        }
Exemple #6
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);
            }
        }
Exemple #7
0
        static void Main()
        {
            LocalDevices = new List <Communication>();
            instructions = new List <List <object> >();


            //create the functions to handle incoming commands by
            Action <ComData, Communication> PCCommandHandle =
                delegate(ComData incomingcommand, Communication handler)
            {
                int commandID = 0;
                incomingcommand.Get(2, ref commandID);

                switch (commandID)
                {
                case 3:
                {
                    string message = "";
                    incomingcommand.Get(3, ref message);

                    string message2 = "";
                    incomingcommand.Get(4, ref message2);

                    string message3 = "";
                    incomingcommand.Get(4, ref message3);

                    if (message.Length > 0)
                    {
                        Reporter.AddReport(4, handler.DeviceName + ": " + message);
                    }
                    if (message2.Length > 0)
                    {
                        Reporter.AddReport(4, handler.DeviceName + ": " + message2);
                    }
                    if (message3.Length > 0)
                    {
                        Reporter.AddReport(4, handler.DeviceName + ": " + message3);
                    }
                    break;
                }

                case 4:
                {
                    string CODE        = "";
                    string name        = "";
                    string balance     = "";
                    string parkingspot = "";
                    string amountdue   = "";
                    string timestamp   = "";


                    incomingcommand.Get(3, ref CODE);
                    incomingcommand.Get(4, ref name);
                    incomingcommand.Get(5, ref balance);
                    incomingcommand.Get(6, ref parkingspot);
                    incomingcommand.Get(7, ref amountdue);
                    incomingcommand.Get(8, ref timestamp);

                    List <object> instructionparams = new List <object>();

                    instructionparams.Add("CHIPFOUND");
                    instructionparams.Add(CODE);
                    instructionparams.Add(name);
                    instructionparams.Add(balance);
                    instructionparams.Add(parkingspot);
                    instructionparams.Add(amountdue);
                    instructionparams.Add(timestamp);

                    instructions.Add(instructionparams);

                    Reporter.AddReport(4, "NFC Chip found: " + CODE);

                    break;
                }
                }
            };

            Action <ComData, Communication> ArduinoCommandHandle =
                delegate(ComData incomingcommand, Communication handler)
            {
                int commandID = 0;
                incomingcommand.Get(2, ref commandID);

                switch (commandID)
                {
                case 3:
                {
                    string message = "";
                    incomingcommand.Get(3, ref message);

                    string message2 = "";
                    incomingcommand.Get(4, ref message2);

                    string message3 = "";
                    incomingcommand.Get(4, ref message3);

                    if (message.Length > 0)
                    {
                        Reporter.AddReport(4, handler.DeviceName + ": " + message);
                    }
                    if (message2.Length > 0)
                    {
                        Reporter.AddReport(4, handler.DeviceName + ": " + message2);
                    }
                    if (message3.Length > 0)
                    {
                        Reporter.AddReport(4, handler.DeviceName + ": " + message3);
                    }
                    break;
                }
                }
            };

            mega = new Arduino("Garage I/O", "", 19200, ArduinoCommandHandle);
            //mega2 = new Arduino("ChipSCan", "", 19200, ArduinoCommandHandle);

            MasterServer = new PCconnectionHandler("TEST", PCCommandHandle);

            LocalDevices.Add(mega);
            //LocalDevices.Add(mega2);
            LocalDevices.Add(MasterServer);

            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);

            masterform = new ControlPanel();

            Application.Run(masterform);
        }