using System;
using System.Linq;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Imaging;
using System.Drawing.Drawing2D;
using System.Text;
using System.Windows.Forms;
using System.IO.Ports;
using System.Globalization;
using System.IO;



namespace meteo
{
    public partial class mereni : Form
    {
        
        Bitmap image1;
        private Graphics graX;
                
        public mereni()
        {
            InitializeComponent();    
        }
        Bitmap snih = new Bitmap ("pozadi.png");

        int interval=20,dnu=7,cas_trend = 3;
        byte byte21X, byte22X, byte23X, byte24X, byte25X, byte26X, byte27X;
        byte byte1X, byte2X, byte3X, byte4X, byte5X, byte6X, byte7X, byte8X, byte9X, byte10X;
        double vlh, tepl, vetr, twc, ros, pocit, tlak;
        decimal sr;
        
        double tend_tep = 0;
        double tend_vlh = 0;
        double tend_tlak = 0;
        int tete = 0, tete2 =0, tete3 = 0;

        decimal t_max, t_min, tl_max, tl_min, vt_max, sraz_max_den, vlh_max_celk, vlh_min_celk;
        string datum_t_max, datum_t_min, datum_tl_max, datum_tl_min, datum_vt_max, datum_srazk_max, datum_vlh_max, datum_vlh_min, srazky_rok = " -.- mm", vetr_max_cas = " v hh:mm";

        string tep_in = "--.-- °", vlh_in = "--.-- %";
        string tlak_max = " ----.- h", tlak_min = " ----.- h", vlh_max = " --.-- %", vlh_min = " --.-- %", tep_max = " --.-- °C", tep_min = " --.-- °C", tep_max2 = " --.-- °C", tep_min2 = " --.-- °C", vetr2 = " -.- m/s", srazky =" -.- mm", vetr_max=" -.- m/s",vetr_min=" -.- m/s";
        string tep_max_cas = null, tep_min_cas = null, vlh_max_cas = null, vlh_min_cas = null, tlak_max_cas = null, tlak_min_cas = null;
        string smer = "N/A", smer_text = "n/a";
        int smerr = 0;
        string date = DateTime.Now.ToString("dd\\/MM\\/yyyy");
        string time = DateTime.Now.ToString("HH\\:mm"); 
        byte high, low;

        

       

        private void bt_menu_Click(object sender, EventArgs e)
        {
            this.Hide();
        }
        private void bt_pripojit_Click(object sender, EventArgs e)
        {
            connect();
        }

        private void connect() 
        {
            try
            {
                /* Nastavime vybrany port ke komunikaci */
                serialPort1.PortName = cb_port.Text;

                /* Nastavime jeho rychlost */
                serialPort1.BaudRate = 1200;

                /* Otevreme spojeni */
                serialPort1.Open();

                /* Pokud jsme pripojeni, dynamicky zmenime moznosti okna */
                if (serialPort1.IsOpen)
                {
                    /* Do status baru vypiseme soucasny stav */
                    //stat_label.Text = "Connected...";

                    /* Vypneme/zapneme prvky v okne */
                    cb_port.Enabled = false;
                    //cb_baud.Enabled = false;
                    bt_pripojit.Enabled = false;
                    //bt_odpojit.Enabled = true;
                    
                    timer1.Enabled = true;
                    timer2.Enabled = true;

                }
            }
            catch (Exception)
            {
                MessageBox.Show("RF modul nebyl nalazen na portu COM0");
            }

            try
            {
                /* Nastavime vybrany port ke komunikaci */
                serialPort2.PortName = "COM1";

                /* Nastavime jeho rychlost */
                serialPort2.BaudRate = 1200;
                serialPort2.DataBits = 8;
                serialPort2.Parity = Parity.None;
                serialPort2.StopBits = StopBits.One;

                /* Otevreme spojeni */
                serialPort2.Open();

                /* Pokud jsme pripojeni, dynamicky zmenime moznosti okna */
                if (serialPort2.IsOpen)
                {
                    label1.Text = "otevren";
                }
            }
            catch (Exception)
            {
                MessageBox.Show("COM1 neotevren");
            }
        }

        private void on_received(object sender, SerialDataReceivedEventArgs e)
        {
            byte1X = (byte)serialPort1.ReadByte(); //cte prvni byte X
            byte2X = (byte)serialPort1.ReadByte(); //cte druhy byte X

            if ((byte1X == 0xAA) && (byte2X == 0xA5))
            {
                byte3X = (byte)serialPort1.ReadByte();
                byte4X = (byte)serialPort1.ReadByte();
                byte5X = (byte)serialPort1.ReadByte();
                byte6X = (byte)serialPort1.ReadByte();
                byte7X = (byte)serialPort1.ReadByte();
                byte8X = (byte)serialPort1.ReadByte();
                byte9X = (byte)serialPort1.ReadByte();  //kontrolni soucet
                byte10X = (byte)serialPort1.ReadByte(); //stop slovo
                serialPort1.DiscardInBuffer();
            }
            else return;
            if (byte10X != 0x5A) { return; }
            this.Invoke(new EventHandler(ZpracDat));
            //ZpracDat();
        }

        private void ZpracDat(object sender, EventArgs e)
        {

            byte[] retez = new byte[12];

            retez[0] = byte3X;
            retez[1] = byte4X;
            retez[2] = byte5X;
            retez[3] = byte6X;
            retez[4] = byte7X;
            retez[5] = byte8X;
            read_byte(retez);

         
        }
        void read_byte(byte[] neco)
        {
            int i, x = 0, n = 1, j, k;
            int data_1;
            int data_2 = 0;
            double ki = 0;
            int crc = 0;
            bool znamenko = false;
            Int16 cislo;
            char[] vysledek = new char[9];


            for (k = 0; k < 3; k++)
            {
                for (j = 0; j < 2; j++)
                {
                    data_1 = neco[j + (2 * k)];
                    for (i = 0; i < 4; i++)
                    {
                        data_1 = data_1 & 0xc0;
                        if (data_1 == 0x80) data_2 = data_2 + 1;   //jestlize je jedna tak 10
                        else if (data_1 == 0x40) data_2 = data_2 + 0;
                        else { crc = 1; break; }
                        if ((i == 3) && (j == 1)) x = 1;
                        data_2 = data_2 << (1 - x);		//posune vytvareny retez o dva bity 
                        data_1 = neco[j + (2 * k)] << (n * 2);
                        n++;
                    }
                    if (crc == 1) break;

                    x = 0;
                    n = 1;

                }
                if (crc == 1) break;

                vysledek[k] = Convert.ToChar(data_2);
                data_2 = 0;
            }
            if (crc == 1) return;

            label1.Text = vysledek[0].ToString();
            high = (byte)vysledek[1];
            low = (byte)vysledek[2];

            // osetreni chyb
            int crcc = ((high / 2) + (low / 2))+1;
            if (crcc != byte9X) return;
            //  osetreni chyb

            cislo = (Int16)(high << 8);
            cislo = (Int16)(cislo + low);

            if (cislo < 0) { cislo = (Int16)(cislo * -1); znamenko = true;}


            vysledek[1] = (char)(cislo % 100000 / 10000 + 48);
            vysledek[2] = (char)(cislo % 10000 / 1000 + 48);
            vysledek[3] = (char)(cislo % 1000 / 100 + 48);
            vysledek[4] = (char)(cislo % 100 / 10 + 48);
            vysledek[5] = (char)(cislo % 10 + 48);
            vysledek[6] = '\0';

            switch (vysledek[0]) 
            {
                case 'T':   //teplota
                    {

                        if (znamenko == true) { vysledek[0] = '-'; znamenko = false; }
                        else { vysledek[0] = ' '; }
                        vysledek[1] = vysledek[2];
                        vysledek[2] = vysledek[3];
                        vysledek[3] = '.';
                        vysledek[6] = '\0';
                        string pokus2 = new string(vysledek);

                        try
                        {
                            tepl = double.Parse(pokus2, CultureInfo.InvariantCulture);
                            if ((decimal)tepl > t_max) { t_max = (decimal)tepl; datum_t_max = (date.ToString() + " v " + time.ToString()); uloz_log(); }
                            if ((decimal)tepl < t_min) { t_min = (decimal)tepl; datum_t_min = (date.ToString() + " v " + time.ToString()); uloz_log(); }
                        }
                        catch
                        { return; }


                    }
                    break;

                case 'R':  //vlhkost a rosny bod
                    {
                        vysledek[0] = ' ';
                        vysledek[1] = vysledek[2];
                        vysledek[2] = vysledek[3];
                        vysledek[3] = '.';
                        vysledek[6] = '\0';
                        string pokus = new string(vysledek);
                        try
                        {
                            vlh = double.Parse(pokus, CultureInfo.InvariantCulture);
                            if ((decimal)vlh > vlh_max_celk) { vlh_max_celk = (decimal)vlh; datum_vlh_max = (date.ToString() + " v " + time.ToString()); uloz_log(); }
                            if ((decimal)vlh < vlh_min_celk) { vlh_min_celk = (decimal)vlh; datum_vlh_min = (date.ToString() + " v " + time.ToString()); uloz_log(); }
                        }
                        catch
                        { return; }

                        //mer_inicializace();

                        //---------------rosny bod------------------------------//
                        char[] jjjj = new char[8];
                        ki = (Math.Log10(vlh) - 2) / 0.4343 + (17.62 * tepl) / (243.12 + tepl);
                        double dw = 243.12 * ki / (17.62 - ki);
                        dw = dw * 100;
                        int sen = (int)dw;
                        jjjj[0] = ' ';
                        if (sen < 0) { sen = Math.Abs(sen); jjjj[0] = '-'; }
                        jjjj[1] = (char)(sen % 10000 / 1000 + 48);
                        jjjj[2] = (char)(sen % 1000 / 100 + 48);
                        jjjj[3] = '.';
                        jjjj[4] = (char)(sen % 100 / 10 + 48);
                        jjjj[5] = (char)(sen % 10 + 48);
                        jjjj[6] = '°';
                        jjjj[7] = 'C';
                        if ((jjjj[0] == '-') && (jjjj[1] == '0')) { jjjj[0] = ' '; jjjj[1] = '-'; }
                        if ((jjjj[0] != '-') && (jjjj[1] == '0')) { jjjj[0] = ' '; jjjj[1] = ' '; }
                        string pokus7 = new string(jjjj);
                        jjjj[6] = '\0';
                        pokus = new string(jjjj);
                        try
                        {
                            ros = double.Parse(pokus, CultureInfo.InvariantCulture);
                        }
                        catch
                        { return; }

                    }
                    break;

                case 'P': //tlak
                    {
                        vysledek[0] = vysledek[1];
                        vysledek[1] = vysledek[2];
                        vysledek[2] = vysledek[3];
                        vysledek[3] = vysledek[4];
                        vysledek[4] = '.';
                        vysledek[6] = '\0';

                        string pokus3 = new string(vysledek);
                        try
                        {
                            tlak = double.Parse(pokus3, CultureInfo.InvariantCulture);
                            if ((decimal)tlak > tl_max) { tl_max = (decimal)tlak; datum_tl_max = (date.ToString() + " v " + time.ToString()); uloz_log(); }
                            if ((decimal)tlak < tl_min) { tl_min = (decimal)tlak; datum_tl_min = (date.ToString() + " v " + time.ToString()); uloz_log(); }
                        }
                        catch
                        { return; }
                    }
                    break;

                case 'V': //rychlost vetru a pocitovana teplota
                    {
                        vysledek[0] = vysledek[2];
                        vysledek[1] = vysledek[3];
                        vysledek[2] = vysledek[4];
                        vysledek[3] = '.';
                        vysledek[4] = vysledek[5];
                        vysledek[5] = ' ';
                        vysledek[6] = 'm';
                        vysledek[7] = '/';
                        vysledek[8] = 's';

                        if (vysledek[0] == '0' && vysledek[1] == '0') { vysledek[0] = ' '; vysledek[1] = ' '; }
                        else { vysledek[0] = ' '; }

                        vetr2 = new string(vysledek);

                        //-----------------wind chill------------------//
                        char[] chill = new char[8];
                        vysledek[5] = '\0';
                        string pokus6 = new string(vysledek);
                        try
                        {
                            vetr = double.Parse(pokus6, CultureInfo.InvariantCulture);
                            if ((decimal)vetr > vt_max) { vt_max = (decimal)vetr; datum_vt_max = (date.ToString() + " v " + time.ToString()); uloz_log(); }
                        }
                        catch
                        { return; }

                        vetr = vetr * 3.6;
                        if (vetr == 0) 
                        {
                            pocit = tepl;
                        }
                        else if (tepl <=10)
                        {
                            if (vetr == 0) { twc = 0; }
                            else twc = 13.13 + 0.6215 * tepl - 11.37 * (Math.Pow(vetr, 0.16)) + 0.3965 * tepl * (Math.Pow(vetr, 0.16));

                            twc = twc * 100;
                            int sen = (int)twc;
                            chill[0] = ' ';
                            if (sen < 0) { sen = Math.Abs(sen); chill[0] = '-'; }
                            chill[1] = (char)(sen % 10000 / 1000 + 48);
                            chill[2] = (char)(sen % 1000 / 100 + 48);
                            chill[3] = '.';
                            chill[4] = (char)(sen % 100 / 10 + 48);
                            chill[5] = (char)(sen % 10 + 48);
                            chill[6] = '\0';
                            if ((chill[0] == '-') && (chill[1] == '0')) { chill[0] = ' '; chill[1] = '-'; }
                            if ((chill[0] != '-') && (chill[1] == '0')) { chill[0] = ' '; chill[1] = ' '; }
                            string pokus8 = new string(chill);
                            try
                            {
                                pocit = double.Parse(pokus8, CultureInfo.InvariantCulture);
                            }
                            catch
                            { return; }
                        }
                        else
                        {
                            double pp = tepl * 100;
                            chill[0] = ' ';
                            chill[1] = (char)(pp % 10000 / 1000 + 48);
                            chill[2] = (char)(pp % 1000 / 100 + 48);
                            chill[3] = '.';
                            chill[4] = (char)(pp % 100 / 10 + 48);
                            chill[5] = (char)(pp % 10 + 48);
                            chill[6] = '\0';
                            string po = new string(chill);
                            try
                            {
                                pocit = double.Parse(po, CultureInfo.InvariantCulture);
                            }
                            catch
                            { return; }
                        }
                        mer_inicializace();
                    }
                    break;

                case 'S': //smer vetru
                    {
                        vysledek[0] = ' ';
                        smer = "-/-";
                        smerr = high;
                        switch (smerr)
                        {
                            case 0: { smer = "S";   smer_text = "  Severní 360°"; }     break;
                            case 1: { smer = "S/V"; smer_text = "Severovýchodní 45°"; } break;
                            case 2: { smer = "V";   smer_text = "  Východní 90°"; }     break;
                            case 3: { smer = "J/V"; smer_text = "Jihovýchodní 135°"; }  break;
                            case 4: { smer = "J";   smer_text = "   Jižní 180°"; }      break;
                            case 5: { smer = "J/Z"; smer_text = " Jihozápadní 225°"; }  break;
                            case 6: { smer = "Z";   smer_text = "  Západní 270°"; }     break;
                            case 7: { smer = "S/Z"; smer_text = "Severozápadní 315°"; } break;
                            default:{ smer = "N/A"; } break;
                        }

                        mer_inicializace();
                    }
                    break;

                case 'D': //srazky
                    {
                        vysledek[0] = vysledek[2];
                        vysledek[1] = vysledek[3];
                        vysledek[2] = vysledek[4];
                        vysledek[3] = '.';
                        vysledek[4] = vysledek[5];
                        if (vysledek[0] == '0' && vysledek[1] == '0') { vysledek[0] = ' '; vysledek[1] = ' '; }
                        else if (vysledek[0] == '0') { vysledek[0] = ' '; }
                        vysledek[5] = 'm';
                        vysledek[6] = 'm';
                        vysledek[7] = ' ';
                        vysledek[8] = ' ';

                        srazky = new string(vysledek);
                        vysledek[5] = '\0';
                        string srazk = new string(vysledek);
                        try
                        {
                            sr = decimal.Parse(srazk.ToString(), CultureInfo.InvariantCulture);
                            if (sr > sraz_max_den) { sraz_max_den = sr; datum_srazk_max = (date.ToString() + " v " + time.ToString()); uloz_log(); };
                        }
                        catch { }
                    }
                    break;

                case 'A': //teplota max
                    {
                        if (znamenko == true) { vysledek[0] = '-'; znamenko = false; }
                        else vysledek[0] = ' ';
                        if (vysledek[2] == '0') { vysledek[2] = ' '; }
                        vysledek[1] = vysledek[2];
                        vysledek[2] = vysledek[3];
                        vysledek[3] = '.';
                        vysledek[6] = ' ';
                        vysledek[7] = '°';
                        vysledek[8] = 'C';
                        tep_max = new string(vysledek);
                        vysledek[6] = '\0';
                        vysledek[7] = ' ';
                        vysledek[7] = ' ';
                        vysledek[8] = ' ';
                        tep_max2 = new string(vysledek);
                    }
                    break;

                case 'B':  //teplota min
                    {
                        if (znamenko == true) { vysledek[0] = '-'; znamenko = false; }
                        else vysledek[0] = ' ';
                        if (vysledek[2] == '0') { vysledek[2] = ' '; }
                        vysledek[1] = vysledek[2];
                        vysledek[2] = vysledek[3];
                        vysledek[3] = '.';
                        vysledek[6] = ' ';
                        vysledek[7] = '°';
                        vysledek[8] = 'C';
                        tep_min = new string(vysledek);
                        vysledek[6] = '\0';
                        vysledek[7] = ' ';
                        vysledek[7] = ' ';
                        vysledek[8] = ' ';
                        tep_min2 = new string(vysledek);
                    }
                    break;

                case 'G': //vlhkost max
                    {
                        vysledek[0] = ' ';
                        vysledek[1] = vysledek[2];
                        vysledek[2] = vysledek[3];
                        vysledek[3] = '.';
                        vysledek[6] = ' ';
                        vysledek[7] = '%';
                        vysledek[8] = ' ';
                        if (vysledek[1] == '0') { vysledek[1] = ' '; }
                        vlh_max = new string(vysledek);
                    }
                    break;

                case 'H': //vlhkost min
                    {
                        vysledek[0] = ' ';
                        vysledek[1] = vysledek[2];
                        vysledek[2] = vysledek[3];
                        vysledek[3] = '.';
                        vysledek[6] = ' ';
                        vysledek[7] = '%';
                        vysledek[8] = ' ';
                        if (vysledek[1] == '0') { vysledek[1] = ' '; }
                        vlh_min = new string(vysledek);
                    }
                    break;

                case 'K': //tlak max
                    {
                        vysledek[0] = ' ';
                        vysledek[6] = vysledek[5];
                        vysledek[5] = '.';
                        vysledek[7] = ' ';
                        vysledek[8] = 'h';
                        tlak_max = new string(vysledek);
                    }
                    break;

                case 'L': // tlak min
                    {
                        vysledek[0] = ' ';
                        vysledek[6] = vysledek[5];
                        vysledek[5] = '.';
                        vysledek[7] = ' ';
                        vysledek[8] = 'h';
                        tlak_min = new string(vysledek);
                    }
                    break;

                case 'M': //vetr max
                    {
                        vysledek[1] = vysledek[3];
                        vysledek[2] = vysledek[4];
                        vysledek[3] = '.';
                        vysledek[4] = vysledek[5];
                        vysledek[5] = ' ';
                        vysledek[6] = 'm';
                        vysledek[7] = '/';
                        vysledek[8] = 's';
                        vysledek[0] = ' ';
                        if (vysledek[1] == '0') { vysledek[1] = ' '; }
                        vetr_max = new string(vysledek);
                    }
                    break;

                case 'N': // teplota max cas
                    {
                        vysledek[0] = (char)(high % 100 / 10 + 48);
                        vysledek[1] = (char)(high % 10 + 48);
                        vysledek[2] = ':';
                        vysledek[3] = (char)(low % 100 / 10 + 48);
                        vysledek[4] = (char)(low % 10 + 48);
                        vysledek[5] = ' ';
                        vysledek[6] = ' ';
                        vysledek[7] = ' ';
                        vysledek[8] = ' ';
                        tep_max_cas = new string(vysledek);
                    }
                    break;

                case 'O': // teplota max cas
                    {
                        vysledek[0] = (char)(high % 100 / 10 + 48);
                        vysledek[1] = (char)(high % 10 + 48);
                        vysledek[2] = ':';
                        vysledek[3] = (char)(low % 100 / 10 + 48);
                        vysledek[4] = (char)(low % 10 + 48);
                        vysledek[5] = ' ';
                        vysledek[6] = ' ';
                        vysledek[7] = ' ';
                        vysledek[8] = ' ';
                        tep_min_cas = new string(vysledek);
                    }
                    break;

                case 'E': // vlhkost max cas
                    {
                        vysledek[0] = (char)(high % 100 / 10 + 48);
                        vysledek[1] = (char)(high % 10 + 48);
                        vysledek[2] = ':';
                        vysledek[3] = (char)(low % 100 / 10 + 48);
                        vysledek[4] = (char)(low % 10 + 48);
                        vysledek[5] = ' ';
                        vysledek[6] = ' ';
                        vysledek[7] = ' ';
                        vysledek[8] = ' ';
                        vlh_max_cas = new string(vysledek);
                        //label26.Text = "v " + (vlh_max_cas).ToString();
                    }
                    break;

                case 'F': // vlhkost max cas
                    {
                        vysledek[0] = (char)(high % 100 / 10 + 48);
                        vysledek[1] = (char)(high % 10 + 48);
                        vysledek[2] = ':';
                        vysledek[3] = (char)(low % 100 / 10 + 48);
                        vysledek[4] = (char)(low % 10 + 48);
                        vysledek[5] = ' ';
                        vysledek[6] = ' ';
                        vysledek[7] = ' ';
                        vysledek[8] = ' ';
                        vlh_min_cas = new string(vysledek);
                    }
                    break;

                case 'I': // tlak max cas
                    {
                        vysledek[0] = (char)(high % 100 / 10 + 48);
                        vysledek[1] = (char)(high % 10 + 48);
                        vysledek[2] = ':';
                        vysledek[3] = (char)(low % 100 / 10 + 48);
                        vysledek[4] = (char)(low % 10 + 48);
                        vysledek[5] = ' ';
                        vysledek[6] = ' ';
                        vysledek[7] = ' ';
                        vysledek[8] = ' ';
                        tlak_max_cas = new string(vysledek);
                    }
                    break;

                case 'J': //tlak min cas]
                    {
                        vysledek[0] = (char)(high % 100 / 10 + 48);
                        vysledek[1] = (char)(high % 10 + 48);
                        vysledek[2] = ':';
                        vysledek[3] = (char)(low % 100 / 10 + 48);
                        vysledek[4] = (char)(low % 10 + 48);
                        vysledek[5] = ' ';
                        vysledek[6] = ' ';
                        vysledek[7] = ' ';
                        vysledek[8] = ' ';
                        tlak_min_cas = new string(vysledek);
                    }
                    break;

                case 'Z': // srazky rok
                    {
                        vysledek[0] = vysledek[2];
                        vysledek[1] = vysledek[3];
                        vysledek[2] = vysledek[4];
                        vysledek[3] = '.';
                        vysledek[4] = vysledek[5];
                        vysledek[5] = 'm';
                        vysledek[6] = 'm';
                        vysledek[7] = ' ';
                        vysledek[8] = ' ';
                        if (vysledek[0] == '0' && vysledek[1] == '0') { vysledek[0] = ' '; vysledek[1] = ' '; }
                        else if (vysledek[0] == '0') { vysledek[0] = ' '; }
                        srazky_rok = new string(vysledek);
                    }
                    break;

                case 'C': // vetr max cas
                    {
                        vysledek[0] = (char)(high % 100 / 10 + 48);
                        vysledek[1] = (char)(high % 10 + 48);
                        vysledek[2] = ':';
                        vysledek[3] = (char)(low % 100 / 10 + 48);
                        vysledek[4] = (char)(low % 10 + 48);
                        vysledek[5] = ' ';
                        vysledek[6] = ' ';
                        vysledek[7] = ' ';
                        vysledek[8] = ' ';
                        vetr_max_cas = new string(vysledek);
                    }
                    break;
                default: break;
            } //end switch

        }

       private void on_load(object sender, EventArgs e)
        {
            image1 = new Bitmap(800, 480);
            graX = Graphics.FromImage(image1);
            pictureBox1.Image = image1;
            graX.Clear(SystemColors.Control);
            graX.DrawImage(snih, 0, 0);

           
            tete = 1;
            tete2 = 1;
            tete3 = 1;

            mer_inicializace();

            try
            {
                nacti();
            }
            catch { }

            
          

            string[] allPorts;
            try
            {
                /* Najdeme vsechny seriove porty na pocitaci */
                allPorts = SerialPort.GetPortNames();

                /* Vynulujeme ComboBox s nabidkou portu */
                cb_port.Items.Clear();

                foreach (string port in allPorts)
                {
                    /* Do roletoveho menu pridame vsechny nalezene porty */
                    cb_port.Items.Add(port);
                }
                cb_port.SelectedItem = "COM0";
                connect();
            }
            catch (Exception)
            {
                MessageBox.Show("RF modul nenalezen");
            }

            
        }

       private void zapis_soubor(object sender, EventArgs e)
       {
           try
           {
           StreamWriter writer = new StreamWriter(@"Windows/www/wwwpub/aaa.txt", true);
           writer.WriteLine("{0};{1};{2};{3};{4};{5};{6};{7}", DateTime.Now.ToString("d.M.yyyy"), DateTime.Now.ToString("HH:mm"), tepl, ros, vlh, tlak, sr, (vetr/3.6));
           writer.Flush();
           writer.Close();
           }
           catch { MessageBox.Show("problem pri zapisu do aaa.txt"); }

           /*-------------zapis data na net -----*/
           try
           {
           //File.Delete(@"Windows/www/wwwpub/text.txt");
           TextWriter tw = new StreamWriter(@"Windows/www/wwwpub/text.txt");
           tw.WriteLine("mydata1= " + tepl);
           tw.WriteLine("mydata2= " + "\"" + vlh + "\"");
           tw.WriteLine("mydata3= " + ros );
           tw.WriteLine("mydata4= " + tlak);
           tw.WriteLine("mydata5= " + "\"" + vetr2 + "\"");
           tw.WriteLine("mydata6= " + smerr);
           tw.WriteLine("mydata7= " + "\"" + DateTime.Now.ToString("d.M.yyyy HH:mm") + "\"");
           tw.WriteLine("tep_max= " + "\"" + tep_max2 + "\"");
           tw.WriteLine("tep_min= " + "\"" + tep_min2 + "\"");
           tw.WriteLine("vlh_max= " + "\"" + vlh_max + "\"");
           tw.WriteLine("vlh_min= " + "\"" + vlh_min + "\"");
           tw.WriteLine("tlak_max= " + "\"" + tlak_max + "\"");
           tw.WriteLine("tlak_min= " + "\"" + tlak_min + "\"");
           tw.WriteLine("vetr_max= " + "\"" + vetr_max + "\"");
           tw.WriteLine("srazky_rok= " + "\"" + srazky_rok + "\"");
           tw.WriteLine("tep_max_cas= " + "\"" + tep_max_cas + "\"");
           tw.WriteLine("tep_min_cas= " + "\"" + tep_min_cas + "\"");
           tw.WriteLine("vlh_max_cas= " + "\"" + vlh_max_cas + "\"");
           tw.WriteLine("vlh_min_cas= " + "\"" + vlh_min_cas + "\"");
           tw.WriteLine("tlak_max_cas= " + "\"" + tlak_max_cas + "\"");
           tw.WriteLine("tlak_min_cas= " + "\"" + tlak_min_cas + "\"");
           tw.WriteLine("srazky= " + "\""+ srazky +"\"");

           // close the stream
           tw.Close();
           }
           catch { MessageBox.Show("problem pri zapisu do text.txt"); }
           /*-------------zapis data na net -----*/

           /*zapis pro graf 1*/
           try
           {
               StreamWriter writer2 = new StreamWriter(@"Windows/www/wwwpub/amline/data.txt", true);
               writer2.WriteLine("{0};{1};{2};{3};", DateTime.Now.ToString("d.M.yyyy HH:mm"), tepl, vlh, ros);
               writer2.Flush();
               writer2.Close();
           }
           catch {MessageBox.Show("problem pri zapisu do data.txt"); }
           /*zapis pro graf 1*/

           /*zapis pro graf 2*/
           try
           {
               StreamWriter writer3 = new StreamWriter(@"Windows/www/wwwpub/amline/data2.txt", true);
               writer3.WriteLine("{0};{1};{2};", DateTime.Now.ToString("d.M.yyyy HH:mm"),tlak,(vetr/3.6));
               writer3.Flush();
               writer3.Close();
           }
           catch { MessageBox.Show("problem pri zapisu do data2.txt"); }
           /*zapis pro graf 2*/

           /*maze data pro 7-mi denni grafy*/
           try
           {
               promaz_data(dnu);
               promaz_data2(dnu);
           }
           catch { MessageBox.Show("Problem promazavani"); }
           /*maze grafy*/

            //nalazeni hodnotz -3h
           
           trend1();
           trend2();
          
           
       }

       private void Timer_cas(object sender, EventArgs e)
       {
           time = DateTime.Now.ToString("HH\\:mm"); // includes leading zeros
           date = DateTime.Now.ToString("dd\\/MM\\/yyyy");
       }

      

       void mer_inicializace(){

           if (serialPort2.IsOpen)
           {
               serialPort2.Write("A"); // posel prikay na mereni po seriove lince
           }

            time = DateTime.Now.ToString("HH\\:mm"); // includes leading zeros
            date = DateTime.Now.ToString("dd\\/MM\\/yyyy");

            
            graX.Clear(SystemColors.Control);
            graX.DrawImage(snih, 0, 0);

           

            graX.DrawString("Teplota IN:", new Font("Tahoma", 15, FontStyle.Regular), new SolidBrush(Color.Black), 547, 25);
            graX.DrawString("Vlhkost IN:", new Font("Tahoma", 15, FontStyle.Regular), new SolidBrush(Color.Black), 547, 65);
            graX.DrawString(tep_in + "C", new Font("Tahoma", 25, FontStyle.Bold), new SolidBrush(Color.Yellow), 645, 20);
            graX.DrawString(vlh_in, new Font("Tahoma", 25, FontStyle.Bold), new SolidBrush(Color.Yellow), 655, 57);
                  

            graX.DrawString("Teplota OUT:", new Font("Tahoma", 15, FontStyle.Regular), new SolidBrush(Color.Black), 62, 35);
            graX.DrawString("maximum:", new Font("Tahoma", 15, FontStyle.Regular), new SolidBrush(Color.Black), 67, 104);
            graX.DrawString("minimum:", new Font("Tahoma", 15, FontStyle.Regular), new SolidBrush(Color.Black), 67, 157);
            graX.DrawString(tepl.ToString()+"°C", new Font("Tahoma", 40, FontStyle.Bold), new SolidBrush(Color.Yellow), 57, 50);
            graX.DrawString(tep_max, new Font("Tahoma", 25, FontStyle.Bold), new SolidBrush(Color.Blue), 42, 122);
            graX.DrawString(tep_min, new Font("Tahoma", 25, FontStyle.Bold), new SolidBrush(Color.Blue), 42, 175);
            graX.DrawString("v " + tep_max_cas, new Font("Tahoma", 15, FontStyle.Regular), new SolidBrush(Color.Black), 175, 104);
            graX.DrawString("v " + tep_min_cas, new Font("Tahoma", 15, FontStyle.Regular), new SolidBrush(Color.Black), 175, 157);
            tendence(32, 85, tete);  

            graX.DrawString("Srážky (24h):", new Font("Tahoma", 15, FontStyle.Regular), new SolidBrush(Color.Black), 23, 245);
            graX.DrawString("Srážky rok:", new Font("Tahoma", 15, FontStyle.Regular), new SolidBrush(Color.Black), 150, 245);
            graX.DrawString(srazky, new Font("Tahoma", 20, FontStyle.Bold), new SolidBrush(Color.Yellow), 22, 270);
            graX.DrawString(srazky_rok, new Font("Tahoma", 20, FontStyle.Bold), new SolidBrush(Color.Yellow), 132, 270);
            graX.DrawString(ros.ToString() + " °C", new Font("Tahoma", 25, FontStyle.Bold), new SolidBrush(Color.Blue), 72, 324);
            graX.DrawString(pocit.ToString() + " °C", new Font("Tahoma", 25, FontStyle.Bold), new SolidBrush(Color.Blue), 72, 378);
            graX.DrawString("Rosný bod:", new Font("Tahoma", 15, FontStyle.Regular), new SolidBrush(Color.Black), 62, 303);
            graX.DrawString("Pociťovaná teplota:", new Font("Tahoma", 15, FontStyle.Regular), new SolidBrush(Color.Black), 62, 358);
         
            graX.DrawString("Relativní vlhkost OUT:", new Font("Tahoma", 15, FontStyle.Regular), new SolidBrush(Color.Black), 321, 35);
            graX.DrawString("maximum:", new Font("Tahoma", 15, FontStyle.Regular), new SolidBrush(Color.Black), 331, 104);
            graX.DrawString("minimum:", new Font("Tahoma", 15, FontStyle.Regular), new SolidBrush(Color.Black), 331, 157);
            graX.DrawString(vlh.ToString()+ " %", new Font("Tahoma", 40, FontStyle.Bold), new SolidBrush(Color.Yellow), 325, 50);
            graX.DrawString(vlh_max, new Font("Tahoma", 25, FontStyle.Bold), new SolidBrush(Color.Blue), 316, 122);
            graX.DrawString(vlh_min, new Font("Tahoma", 25, FontStyle.Bold), new SolidBrush(Color.Blue), 316, 175);
            graX.DrawString("v " + vlh_max_cas, new Font("Tahoma", 15, FontStyle.Regular), new SolidBrush(Color.Black), 445, 104);
            graX.DrawString("v " + vlh_min_cas, new Font("Tahoma", 15, FontStyle.Regular), new SolidBrush(Color.Black), 445, 157);
            tendence(304, 85, tete2);


            graX.DrawString("Tlak na hladinu moře:", new Font("Tahoma", 15, FontStyle.Regular), new SolidBrush(Color.Black), 321, 245);
            graX.DrawString("Maximum:", new Font("Tahoma", 15, FontStyle.Regular), new SolidBrush(Color.Black), 331, 303);
            graX.DrawString("Minimum:", new Font("Tahoma", 15, FontStyle.Regular), new SolidBrush(Color.Black), 331, 358);
            graX.DrawString(tlak.ToString() + " hPa", new Font("Tahoma", 30, FontStyle.Bold), new SolidBrush(Color.Yellow), 325, 262);
            graX.DrawString(tlak_max + "Pa", new Font("Tahoma", 25, FontStyle.Bold), new SolidBrush(Color.Blue), 316, 325);
            graX.DrawString(tlak_min + "Pa", new Font("Tahoma", 25, FontStyle.Bold), new SolidBrush(Color.Blue), 316, 378);
            graX.DrawString("v " + tlak_max_cas, new Font("Tahoma", 15, FontStyle.Regular), new SolidBrush(Color.Black), 445, 303);
            graX.DrawString("v " + tlak_min_cas, new Font("Tahoma", 15, FontStyle.Regular), new SolidBrush(Color.Black), 445, 358);
            tendence(304, 288, tete3);

            graX.DrawString("Rychlost větru:", new Font("Tahoma", 15, FontStyle.Regular), new SolidBrush(Color.Black), 568, 280);
            graX.DrawString("Maximum:", new Font("Tahoma", 15, FontStyle.Regular), new SolidBrush(Color.Black), 568, 345);
            graX.DrawString("v " + vetr_max_cas, new Font("Tahoma", 15, FontStyle.Regular), new SolidBrush(Color.Black), 675, 345);
            graX.DrawString(vetr2, new Font("Tahoma", 35, FontStyle.Bold), new SolidBrush(Color.Yellow), 556, 294);
            graX.DrawString(vetr_max, new Font("Tahoma", 25, FontStyle.Bold), new SolidBrush(Color.Blue), 585, 365);
            graX.DrawString("Směr:", new Font("Tahoma", 15, FontStyle.Regular), new SolidBrush(Color.Black), 568, 180);
            graX.DrawString(smer, new Font("Tahoma", 35, FontStyle.Bold), new SolidBrush(Color.Yellow), 600, 200);
            graX.DrawString(smer_text, new Font("Tahoma", 17, FontStyle.Bold), new SolidBrush(Color.Yellow), 568, 250);
           
            graX.DrawString("Dnes je: " + date.ToString() + "  " + time.ToString(), new Font("Tahoma", 20, FontStyle.Regular), new SolidBrush(Color.Black), 35, 430);
   
            pictureBox1.Image = image1;
       }

       private void button1_Click(object sender, EventArgs e)
       {         
           trend1();
           trend2();
           mer_inicializace();
          // promaz_data(dnu); 
         //  promaz_data2(dnu);  
       }


       void promaz_data(int pocet_dni) {

               string tempFile = "Windows/www/wwwpub/amline/temp.txt";
               object[] row;
               var thisWeekEnd = DateTime.Now.AddDays(-pocet_dni).AddSeconds(-1);

               using (var sr = new StreamReader("Windows/www/wwwpub/amline/data.txt"))
               {
                   using (var sw = new StreamWriter(tempFile))
                   {
                       string line;

                       while ((line = sr.ReadLine()) != null)
                       {
                           if (line != "")
                           {
                               row = line.Split(';'); // nacte datum
                               DateTime dt1 = Convert.ToDateTime(row[0].ToString());
                               if (dt1 >= thisWeekEnd) sw.WriteLine(line);  //pokud je mladsi nez 7dni, tak ho ponecha
                           }
                       }
                   }
               }

               try
               {
                   File.Delete("Windows/www/wwwpub/amline/data.txt");
                   File.Move(tempFile, "Windows/www/wwwpub/amline/data.txt");
               }
               catch 
               {
                   MessageBox.Show("problem promazavani 1");
               }
       }

       void promaz_data2(int pocet_dni)
       {

           string tempFile = "Windows/www/wwwpub/amline/temp.txt";
           object[] row;
           var thisWeekEnd = DateTime.Now.AddDays(-pocet_dni).AddSeconds(-1);

           using (var sr = new StreamReader("Windows/www/wwwpub/amline/data2.txt"))
           {
               using (var sw = new StreamWriter(tempFile))
               {
                   string line;

                   while ((line = sr.ReadLine()) != null)
                   {
                       if (line != "")
                       {
                           row = line.Split(';'); // nacte datum
                           DateTime dt1 = Convert.ToDateTime(row[0].ToString());
                           if (dt1 >= thisWeekEnd) sw.WriteLine(line);  //pokud je mladsi nez 7dni, tak ho ponecha
                       }
                   }
               }
           }

           try
           {
               File.Delete("Windows/www/wwwpub/amline/data2.txt");
               File.Move(tempFile, "Windows/www/wwwpub/amline/data2.txt");
           }
           catch
           {
               MessageBox.Show("problem promazavani 2");
           }

       }



       private void on_received2(object sender, SerialDataReceivedEventArgs e)
       {
           byte21X = (byte)serialPort2.ReadByte();
           if ((byte21X == 'Y') || (byte21X == 'Z'))
           {
               byte22X = (byte)serialPort2.ReadByte();
               byte23X = (byte)serialPort2.ReadByte();
               byte24X = (byte)serialPort2.ReadByte();
               byte25X = (byte)serialPort2.ReadByte();
               byte26X = (byte)serialPort2.ReadByte();
               byte27X = (byte)serialPort2.ReadByte();
               serialPort2.DiscardInBuffer();
           }
           else return;
           this.Invoke(new EventHandler(ZpracDat2));
           //ZpracDat();
       }

       private void ZpracDat2(object sender, EventArgs e)
       {

           char[] retez = new char[8];
           retez[0] = (char)byte21X;
           retez[1] = (char)byte22X;
           retez[2] = (char)byte23X;
           retez[3] = (char)byte24X;
           retez[4] = (char)byte25X;
           retez[5] = (char)byte26X;
           retez[6] = (char)byte27X;

          

           if (retez[0] == 'Y') 
           {
               retez[0] = retez[1]; // -
               retez[1] = retez[2]; // 2
               retez[2] = retez[3]; // 2
               retez[3] = retez[4]; // .
               retez[4] = retez[5]; // 2
               retez[5] = retez[6]; // 2
               retez[6] = ' ';
               retez[7] = '°';
               tep_in = new string(retez);
            
           }
           if (retez[0] == 'Z')
           {
               retez[0] = retez[1]; // 2
               retez[1] = retez[2]; // 1
               retez[2] = retez[3]; // .
               retez[3] = retez[4]; // 2
               retez[4] = retez[5]; // 2
               retez[5] = ' ';
               retez[6] = '%';
               retez[7] = ' ';
               vlh_in = new string(retez);
             
           }


       }


       void nacti()
       {
       //string tempFile = Path.GetTempFileName();
            object[] row;
            string path = @"log.txt";

            try {

                if(File.Exists(path))
                {
            
                StreamReader sr = new StreamReader(path, true);
                {
                    {
                        string line;

                        while ((line = sr.ReadLine()) != null)
                        {
                            if (line != "")
                            {
                                row = line.Split(';');
                                //label1.Text = row[1].ToString();
                                interval = int.Parse(row[0].ToString());
                                dnu = int.Parse(row[1].ToString());
                                t_max = decimal.Parse(row[2].ToString());
                                datum_t_max = row[3].ToString();
                                t_min = decimal.Parse(row[4].ToString());
                                datum_t_min = row[5].ToString();
                                tl_max = decimal.Parse(row[6].ToString());
                                datum_tl_max = row[7].ToString();
                                tl_min = decimal.Parse(row[8].ToString());
                                datum_tl_min = row[9].ToString();
                                vt_max = decimal.Parse(row[10].ToString());
                                datum_vt_max = row[11].ToString();
                                sraz_max_den = decimal.Parse(row[12].ToString());
                                datum_srazk_max = row[13].ToString();
                                vlh_max_celk = decimal.Parse(row[14].ToString());
                                datum_vlh_max = row[15].ToString();
                                vlh_min_celk = decimal.Parse(row[16].ToString());
                                datum_vlh_min = row[17].ToString();
                                cas_trend = int.Parse(row[18].ToString());

                                if (serialPort1.IsOpen) //jen pokud je pripojen RF modul
                                {

                                    if (timer1.Interval != interval * 1000 * 60)
                                    {
                                        timer1.Interval = (interval * 1000 * 60);
                                        timer1.Enabled = true;
                                    }
                                }
                            }
                        }
                    }
                }
                sr.Close();
                }
                else { MessageBox.Show("neexistuje log.txt"); }
            }
            catch{MessageBox.Show("error log.txt");}
                  
       }

       void uloz_log() 
       {
           try
           {
               StreamWriter sw = new StreamWriter(@"log.txt");
               sw.WriteLine("{0};{1};{2};{3};{4};{5};{6};{7};{8};{9};{10};{11};{12};{13};{14};{15};{16};{17};{18};", interval, dnu, t_max, datum_t_max, t_min, datum_t_min, tl_max, datum_tl_max, tl_min, datum_tl_min, vt_max, datum_vt_max, sraz_max_den, datum_srazk_max, vlh_max_celk, datum_vlh_max, vlh_min_celk, datum_vlh_min,cas_trend);
               sw.Flush();
               sw.Close();
           }
           catch { MessageBox.Show("nepodarilo se ulozit log.txt"); }
       }

       private void activated(object sender, EventArgs e)
       {
           try
           {
               nacti();
               label4.Text = interval.ToString();
               label2.Text = dnu.ToString();
           }
           catch { }

           mer_inicializace();
   
       }

       private void tendence(int x, int y, int sipka)
       { // drawing.2d
           Pen pen = new Pen(Color.Green, 3);

           switch (sipka)
           {
               case 1:
                   pen.Color = Color.Green;
                   graX.DrawLine(pen, x - 15, y, x + 15, y);
                   graX.DrawLine(pen, x, y - 5, x + 15, y);
                   graX.DrawLine(pen, x + 15, y, x, y + 5);  // rovna
                   break;

               case 2:
                   pen.Color = Color.Red;
                   graX.DrawLine(pen, x, y - 15, x, y + 15);
                   graX.DrawLine(pen, x, y - 15, x + 5, y);
                   graX.DrawLine(pen, x, y - 15, x - 5, y);  // up
                   break;

               case 3:
                   pen.Color = Color.Red;
                   graX.DrawLine(pen, x, y - 15, x, y + 15);
                   graX.DrawLine(pen, x, y + 15, x + 5, y);
                   graX.DrawLine(pen, x, y + 15, x - 5, y);  // dwn
                   break;

               case 4:
                   pen.Color = Color.Blue;
                   graX.DrawLine(pen, x - 12, y - 12, x + 12, y + 12);
                   graX.DrawLine(pen, x + 8, y, x + 12, y + 12);
                   graX.DrawLine(pen, x + 12, y + 12, x, y + 8);  // dwn 45°
                   break;

               case 5:
                   pen.Color = Color.Blue;
                   graX.DrawLine(pen, x + 12, y - 12, x - 12, y + 12);
                   graX.DrawLine(pen, x + 12, y - 12, x, y - 8);
                   graX.DrawLine(pen, x + 12, y - 12, x + 8, y);  // up 45°
                   break;



               default:
                   break;
           }  
       }

       private void trend1() 
       {
           object[] row;
           string teplota = null, vlhkost = null;
           double[] arrteplota, arrvlhkost;

           int cas = (cas_trend * 60)/interval;
           label5.Text = cas.ToString();

           string path = @"Windows/www/wwwpub/amline/data.txt";

           try
           {
               if (File.Exists(path))
               {

                   StreamReader sr = new StreamReader(path, true);
                   //using (var sr = new StreamReader(path, true))

                       string line;

                       while ((line = sr.ReadLine()) != null)
                       {
                           if (line != "")
                           {
                               row = line.Split(';'); // nacte datum
                               teplota += row[1].ToString() + ';';
                               vlhkost += row[2].ToString() + ';';
                           }
                       }

                       string[] arrString1 = teplota.Split(new char[] { ';' });
                       string[] arrString2 = vlhkost.Split(new char[] { ';' });

                       int pocet = arrString1.Length - 1;
                       // osetrit

                       arrteplota = new double[arrString1.Length];
                       arrvlhkost = new double[arrString2.Length];

                       for (int i = 0; i < arrString1.Length - 1; i++)   // -1 pridano
                       {
                           arrteplota[i] = double.Parse(arrString1[i], CultureInfo.InvariantCulture);
                           arrvlhkost[i] = double.Parse(arrString2[i], CultureInfo.InvariantCulture);
                       }
                       if (arrString1.Length < (cas+2))
                       {
                           tend_tep = tepl;
                           tend_vlh = vlh;
                       }
                       else
                       {
                           int delka = arrteplota.Length - 2;
                           tend_tep = arrteplota[delka - cas];
                           tend_vlh = arrvlhkost[delka - cas];
                           label2.Text = tend_tep.ToString();
                           label3.Text = tend_vlh.ToString();
                       }

                       if (tepl >= tend_tep) // zacatek alg
                       {
                           if ((tepl - 1) > tend_tep)
                           {
                               if (((tepl - 5) > tend_tep))
                               { tete = 2; }  //up
                               else
                               { tete = 5; } //+45
                           }
                           else { tete = 1; } //rovne

                       }

                       else if (tepl <= tend_tep)
                       {
                           if ((tepl + 1) < tend_tep)
                           {
                               if (((tepl + 5) < tend_tep))
                               { tete = 3; }  //dwn
                               else
                               { tete = 4; } //-45
                           }
                           else { tete = 1; } //rovne

                       } // konec algoritmu teplota

                       if (vlh >= tend_vlh) // zacatek alg
                       {
                           if ((vlh - 5) > tend_vlh)
                           {
                               if (((vlh - 15) > tend_vlh))
                               { tete2 = 2; }  //up
                               else
                               { tete2 = 5; } //+45
                           }
                           else { tete2 = 1; } //rovne

                       }

                       else if (vlh <= tend_vlh)
                       {
                           if ((vlh + 5) < tend_vlh)
                           {
                               if (((vlh + 15) < tend_vlh))
                               { tete2 = 3; }  //dwn
                               else
                               { tete2 = 4; } //-45
                           }
                           else { tete2 = 1; } //rovne

                       } // konec algoritmu vlhkost
                       sr.Close();
                   //sr
               } //if
               else { MessageBox.Show("soubor neexistuje data.txt"); }
           }
           catch{MessageBox.Show("chyba trend");}
       }

       private void trend2()
       {
           object[] row = null;
           string tlaky = null;
           double[] arrtlak = null;
           int cas = (cas_trend * 60) / interval;

           string path = @"Windows/www/wwwpub/amline/data2.txt";

           try
           {
               if (File.Exists(path))
               {

                   StreamReader sr = new StreamReader(path, true);
                   //using (var sr = new StreamReader(path, true))
                  // {

                       string line;

                       while ((line = sr.ReadLine()) != null)
                       {
                           if (line != "")
                           {
                               row = line.Split(';'); // nacte datum
                               tlaky += row[1].ToString() + ';';
                           }
                       }

                       string[] arrString = tlaky.Split(new char[] { ';' });
                       arrtlak = new double[arrString.Length];


                       for (int i = 0; i < arrString.Length - 1; i++)   // -1 pridano
                       {
                           arrtlak[i] = double.Parse(arrString[i], CultureInfo.InvariantCulture);

                       }

                       if (arrString.Length < (cas+2))
                       {
                           tend_tlak = tlak;
                       }
                       else
                       {
                           int delka = arrtlak.Length - 2;
                           tend_tlak = arrtlak[delka - cas];
                           label4.Text = tend_tlak.ToString();
                       }


                       if (tlak >= tend_tlak) // zacatek alg
                       {
                           if ((tlak - 1) > tend_tlak)
                           {
                               if (((tlak - 3) > tend_tlak))
                               { tete3 = 2; }  //up
                               else
                               { tete3 = 5; } //+45
                           }
                           else { tete3 = 1; } //rovne

                       }

                       else if (tlak <= tend_tlak)
                       {
                           if ((tlak + 1) < tend_tlak)
                           {
                               if (((tlak + 3) < tend_tlak))
                               { tete3 = 3; }  //dwn
                               else
                               { tete3 = 4; } //-45
                           }
                           else { tete3 = 1; } //rovne

                       } // konec algoritmu 
                       sr.Close();
                  // }sr
               }//if
               else { MessageBox.Show("soubor neexistuje data.txt"); }
           }//catch
           catch { MessageBox.Show("chyba trend2"); }
          }

       private void minuta_tick(object sender, EventArgs e)
       {
           mer_inicializace();
       }
      


      
    }
}