Exemplo n.º 1
0
        private void AskHeat_Click(object sender, EventArgs e)
        {
            SerialPort serialPort = null;

            // Create a new SerialPort object with default settings.
            serialPort = new SerialPort();

            // Allow the user to set the appropriate properties.
            serialPort.PortName = COM.Text;
            serialPort.DataBits = 8;
            serialPort.Parity   = Parity.None;
            serialPort.StopBits = StopBits.One;
            serialPort.BaudRate = 9600;

            // Set the read/write timeouts
            serialPort.ReadTimeout  = 500;
            serialPort.WriteTimeout = 500;

            try
            {
                serialPort.Open();
                serialPort.DiscardOutBuffer();
                serialPort.DiscardInBuffer();

                String ReceivedData;
                //RecievedData = serialPort.ReadLine();
                //serialPort.DataReceived += new SerialDataReceivedEventHandler(responseHandler);
                serialPort.Write("i" + "\r\n");

                Boolean conti = true;
                do
                {
                    ReceivedData = serialPort.ReadLine();
                    if (ReceivedData.Contains('$'))
                    {
                        conti = false;
                    }
                } while (conti);


                ReceivedData = ReceivedData.Replace("$", "");
                ReceivedData = ReceivedData.Replace("\r", "");
                ReceivedData = ReceivedData.Replace("\n", "");

                AppendHeatLabel("Temperature U:" + ReceivedData.Split(',')[4] + "," + "Temperature M:" + ReceivedData.Split(',')[5]);

                serialPort.Close();
            }
            catch (Exception exc)
            {
                MessageBox.Show("Serial could not be opened, please check that the device is correct one");
                serialPort.Close();
            }
        }
Exemplo n.º 2
0
        private void connect()
        {
            string[] ArrayComPortsNames = null;
            //int index = -1;
            //string ComPortName = null;

            ArrayComPortsNames = SerialPort.GetPortNames();

            SerialPort serialPort = null;

            // Create a new SerialPort object with default settings.
            serialPort = new SerialPort();

            // Allow the user to set the appropriate properties.
            serialPort.PortName = selectedCom;
            serialPort.DataBits = 8;
            serialPort.Parity   = Parity.None;
            serialPort.StopBits = StopBits.One;
            serialPort.BaudRate = 9600;

            // Set the read/write timeouts
            serialPort.ReadTimeout  = 10000;
            serialPort.WriteTimeout = 500;

            try
            {
                serialPort.Open();
                serialPort.DiscardOutBuffer();
                serialPort.DiscardInBuffer();

                String ReceivedData;
                //RecievedData = serialPort.ReadLine();
                //serialPort.DataReceived += new SerialDataReceivedEventHandler(responseHandler);
                serialPort.Write("AT+CWQAP" + "\r\n");

                Boolean cont = true;
                do
                {
                    ReceivedData = serialPort.ReadLine();
                    writeConnect(ReceivedData);
                    if (ReceivedData.Contains('$'))
                    {
                        cont = false;
                    }
                } while (cont);

                serialPort.Write("AT+CWJAP=" + "\"" + "QB3@953" + "\"" + "," + "\"" + "QB34innovation!" + "\"" + "\r\n");

                cont = true;
                do
                {
                    ReceivedData = serialPort.ReadLine();
                    writeConnect(ReceivedData);
                    if (ReceivedData.Contains('$'))
                    {
                        cont = false;
                    }
                } while (cont);
                cont = true;

                serialPort.Write("AT+CIFSR" + "\r\n");

                cont = true;
                do
                {
                    ReceivedData = serialPort.ReadLine();
                    writeConnect(ReceivedData);
                    Console.WriteLine(ReceivedData);
                    if (ReceivedData.Contains('$'))
                    {
                        cont = false;
                        Console.WriteLine(ReceivedData.Split('\"')[5]);
                        TCPIP.ip = ReceivedData.Split('\"')[5];
                    }
                } while (cont);
                cont = true;


                serialPort.Close();
            }
            catch (Exception exc)
            {
                MessageBox.Show("Serial could not be opened, please check that the device is correct one");
                serialPort.Close();
            }
        }
Exemplo n.º 3
0
 private string[] Desinflate()
 {
     return(ReceivedData.Split(','));
 }
Exemplo n.º 4
0
            /// <summary>
            /// Wymiana danych (klient - serwer). Podstawowa funkcja
            /// w ktorej obslugujemy danego klienta
            /// </summary>
            private void UpdateData()
            {
                while (true)
                {
                    try
                    {
                        BinaryReader odbr           = new BinaryReader(_clientSocket.GetStream());
                        int          howMuchreceive = odbr.ReadInt32();
                        String       ReceivedData;
                        Byte[]       data = new Byte[howMuchreceive];
                        Int32        sData;
                        sData        = _clientSocket.GetStream().Read(data, 0, data.Length);
                        ReceivedData = Encoding.ASCII.GetString(data, 0, sData);

                        if (ReceivedData[0] == '?') // Obaj przegrali
                        {
                            Console.WriteLine("Client {0} disconnected", lastId.ToString());
                            if ((lastId == _clientSockets[0].Id && _clientSockets[0].Id < _clientSockets[1].Id) ||
                                (lastId == _clientSockets[1].Id && _clientSockets[0].Id > _clientSockets[1].Id))
                            {
                                ZeroArray(tabA, tabPos);
                            }
                            if ((lastId == _clientSockets[1].Id && _clientSockets[0].Id < _clientSockets[1].Id) ||
                                (lastId == _clientSockets[0].Id && _clientSockets[0].Id > _clientSockets[1].Id))
                            {
                                ZeroArray(tabB, tabPos);
                            }
                            tabPos.Clear();
                            _clientSocket.GetStream().Close();
                            _clientSocket.Close();
                            _clientSockets.RemoveAt(GetIndex(_clientSockets, lastId));
                            _clientSocket.Dispose();
                            break;
                        }

                        if (ReceivedData[0] == '!') // Ktorys przegral badz sie rozlaczyl
                        {
                            if (_clientSockets.Count > 1)
                            {
                                for (int i = 0; i < _clientSockets.Count; i++)
                                {
                                    if (_clientSockets[i].Socket != _clientSocket)
                                    {
                                        SendDataToClient("K" + "&", _clientSockets[i].Socket);
                                    }
                                }
                            }
                            if (_clientSockets.Count > 1)
                            {
                                if ((lastId == _clientSockets[0].Id && _clientSockets[0].Id < _clientSockets[1].Id) ||
                                    (lastId == _clientSockets[1].Id && _clientSockets[0].Id > _clientSockets[1].Id))
                                {
                                    ZeroArray(tabA, tabPos);
                                }
                                if ((lastId == _clientSockets[1].Id && _clientSockets[0].Id < _clientSockets[1].Id) ||
                                    (lastId == _clientSockets[0].Id && _clientSockets[0].Id > _clientSockets[1].Id))
                                {
                                    ZeroArray(tabB, tabPos);
                                }
                            }
                            else
                            {
                                if (lastId == 1)
                                {
                                    ZeroArray(tabA, tabPos);
                                }
                                else
                                {
                                    ZeroArray(tabB, tabPos);
                                }
                            }
                            tabPos.Clear();
                            Console.WriteLine("Client {0} disconnected", lastId.ToString());
                            _clientSocket.GetStream().Close();
                            _clientSocket.Close();
                            _clientSockets.RemoveAt(GetIndex(_clientSockets, lastId));
                            //_clientSocket.Dispose();
                            break;
                        }
                        if (ReceivedData[0] == 'F') // Pozycje jedzenia
                        {
                            RandInfoAboutFood();
                            for (int i = 0; i < _clientSockets.Count; i++)
                            {
                                string dataAboutFood = "F#" + _typeOfFood.ToString() + "@"
                                                       + _foodX.ToString() + "," + _foodY.ToString() + "&";

                                SendDataToClient(dataAboutFood, _clientSockets[i].Socket);
                            }
                        }
                        if (ReceivedData[0] == 'S') // Wspolrzedne weza
                        {
                            if (_clientSockets.Count > 1)
                            {
                                for (int i = 0; i < _clientSockets.Count; i++)
                                {
                                    if (_clientSockets[i].Socket != _clientSocket)
                                    {
                                        string toSend = "S#" + lastId.ToString() + "*" + ReceivedData.Remove(0, 2);
                                        SendDataToClient(toSend, _clientSockets[i].Socket);
                                    }
                                }
                                if ((lastId == _clientSockets[0].Id && _clientSockets[0].Id < _clientSockets[1].Id) ||
                                    (lastId == _clientSockets[1].Id && _clientSockets[0].Id > _clientSockets[1].Id))
                                {
                                    ZeroArray(tabA, tabPos);
                                }
                                if ((lastId == _clientSockets[1].Id && _clientSockets[0].Id < _clientSockets[1].Id) ||
                                    (lastId == _clientSockets[0].Id && _clientSockets[0].Id > _clientSockets[1].Id))
                                {
                                    ZeroArray(tabB, tabPos);
                                }
                                tabPos.Clear();
                                string[] split1    = ReceivedData.Split('&');
                                string[] split2    = split1[0].Split('$');
                                string[] positions = split2[1].Split(';');

                                foreach (var el in positions)
                                {
                                    string[] tmp = el.Split(',');
                                    if (tmp.Length > 1)
                                    {
                                        tabPos.Add(Int32.Parse(tmp[0]) / 10);
                                        tabPos.Add(Int32.Parse(tmp[1]) / 10);
                                    }
                                }
                                if ((lastId == _clientSockets[0].Id && _clientSockets[0].Id < _clientSockets[1].Id) ||
                                    (lastId == _clientSockets[1].Id && _clientSockets[0].Id > _clientSockets[1].Id))
                                {
                                    UpdateArray(tabA, tabPos);
                                }
                                if ((lastId == _clientSockets[1].Id && _clientSockets[0].Id < _clientSockets[1].Id) ||
                                    (lastId == _clientSockets[0].Id && _clientSockets[0].Id > _clientSockets[1].Id))
                                {
                                    UpdateArray(tabB, tabPos);
                                }
                                //if (lastId == _clientSockets[0].Id)
                                //{
                                //    UpdateArray(tabA, tabPos);

                                //}
                                //else UpdateArray(tabB, tabPos);
                                if (CheckCollsion(tabA, tabB) == true)
                                {
                                    for (int k = 0; k < _clientSockets.Count; k++)
                                    {
                                        if (_clientSockets[k].Socket != _clientSocket)
                                        {
                                            SendDataToClient("C&", _clientSockets[k].Socket);
                                        }
                                    }
                                }
                            }
                        }
                        if (ReceivedData[0] == 'P') //Wyslanie punktow
                        {
                            if (_clientSockets.Count > 1)
                            {
                                for (int i = 0; i < _clientSockets.Count; i++)
                                {
                                    if (_clientSockets[i].Socket != _clientSocket)
                                    {
                                        string toSend2 = ReceivedData + "&";
                                        SendDataToClient(toSend2, _clientSockets[i].Socket);
                                    }
                                }
                            }
                        }
                        if (_clientSockets.Count == 1)
                        {
                            string toSendIfOne = lastId.ToString() + "*" + "0";
                            SendDataToClient(toSendIfOne, _clientSocket);
                        }
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine(" >> " + ex.ToString());
                    }
                }
            }