Beispiel #1
0
        //Runs in the background and updates temperature, if Arduino is available
        private void updateTemperature()
        {
            Task.Run(() =>
            {
                while (true)
                {
                    Thread.Sleep(10000);

                    //Gets all available ports on the computer and tries to connect to it
                    foreach (string port in SerialPort.GetPortNames())
                    {
                        try
                        {
                            Port1.PortName = port;
                            Port1.Open();
                            Port1.ReadTimeout = 100;
                            Port1.ReadLine();
                            break;
                        }
                        catch { Port1.Close(); }
                    }

                    if (!Port1.IsOpen)
                    {
                        continue;
                    }

                    try
                    {
                        while (true)
                        {
                            string temperature;
                            temperature = Port1.ReadLine();
                            temperatureBox.Invoke(new Action(() => temperatureBox.Text = $"The tempeature is: {temperature.Replace("\r", "")} ℃"));
                        }
                    }
                    catch { continue; }
                }
            });
        }
        private void Process_when_receive_value(object sender, SerialDataReceivedEventArgs e)
        {
            string s = Port1.ReadLine();

            this.BeginInvoke(new LineReceivedEvent(LineReceived), s);
        }