private void DownloadStart()
        {
            ThreadDay        = 0;
            ThreadEventInDay = 0;
            ThreadSetting    = THREAD_SET_EVENTS;

            byte[] USBPacket = new byte[USB_PACKET_SIZE];
            USBPacket[0]                         = USB_REPORT_ID;
            USBPacket[USB_CMD_POS]               = USB_CMD_EEPROM_WRITE;
            USBPacket[USB_EEPROM_SUBCMD_POS]     = EEPROM_SUBCMD_EVENTS_COUNT;
            USBPacket[USB_EEPROM_SUBCMD_POS + 1] = (byte)Days[0].Count;
            USBPacket[USB_EEPROM_SUBCMD_POS + 2] = (byte)Days[1].Count;
            USBPacket[USB_EEPROM_SUBCMD_POS + 3] = (byte)Days[2].Count;
            USBPacket[USB_EEPROM_SUBCMD_POS + 4] = (byte)Days[3].Count;
            USBPacket[USB_EEPROM_SUBCMD_POS + 5] = (byte)Days[4].Count;
            USBPacket[USB_EEPROM_SUBCMD_POS + 6] = (byte)Days[5].Count;
            USBPacket[USB_EEPROM_SUBCMD_POS + 7] = (byte)Days[6].Count;
            USBPacket[USB_START_STOP_FLAG_POS]   = USB_CMD_START_FLAG;
            Boolean USBSuccess = USB_device.Write(USBPacket);

            ShowConnectionState(USBSuccess);
            if (USBSuccess == false)
            {
                return;
            }

            ThreadWrite = true;
            ThreadTimer.Start();
        }
        private void ReadSettings()
        {
            ThreadEvent = 0;
            DeleteAllEvents();

            byte[] USBPacket = new byte[USB_PACKET_SIZE];
            USBPacket[0]                     = USB_REPORT_ID;
            USBPacket[USB_CMD_POS]           = USB_CMD_EEPROM_READ;
            USBPacket[USB_EEPROM_SUBCMD_POS] = EEPROM_SUBCMD_EVENTS_COUNT;
            Boolean USBSuccess = USB_device.Write(USBPacket);

            ShowConnectionState(USBSuccess);
            if (USBSuccess == false)
            {
                return;
            }

            USBSuccess = USB_device.Read(USBPacket);

            TotalEventsCount = (USBPacket[USB_ACTIVE_EVENT_POS + 1] << 8) | USBPacket[USB_ACTIVE_EVENT_POS];

            if (TotalEventsCount > 0)
            {
                ProgressBar.Owner = this;
                ProgressBar.Show();
                ThreadWrite = false;
                ThreadTimer.Start();
            }
        }
        public MainWindow()
        {
            InitializeComponent();

            byte[] MonthDaysCount =
            {
                31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31,
            };


            for (byte month = 0; month < MonthsCount; month++)
            {
                Months.Add(new Month(MonthNames[month], MonthDaysCount[month]));
            }

            EventsList.ItemsSource = Months[0].Days[0].Events;
            MonthDays.ItemsSource  = Months[0].Days;
            MonthDays.SelectedItem = Months[0].Days[0];
            MonthName.Text         = Months[0].Name;
            MonthsList.ItemsSource = Months;
            EventsCountUpdate();

            Boolean USBDevDetected = USB_device.Open();

            if (USBDevDetected)
            {
                ThreadTimer.Interval = TimeSpan.FromMilliseconds(10); //период опроса устройства
                ThreadTimer.Tick    += ThreadTimer_Tick;
            }

            ShowConnectionState(USBDevDetected);
        }
        private void TimeSetButton_Click(object sender, RoutedEventArgs e)
        {
            TimeSettings TimeSet = new TimeSettings();

            byte[] USBPacket = new byte[USB_PACKET_SIZE];
            USBPacket[0]                   = USB_REPORT_ID;
            USBPacket[USB_CMD_POS]         = USB_CMD_TIME;
            USBPacket[USB_TIME_SUBCMD_POS] = TIME_SUBCMD_GET;

            Boolean USBSuccess = USB_device.Write(USBPacket);

            ShowConnectionState(USBSuccess);
            if (USBSuccess == false)
            {
                return;
            }

            USBSuccess = USB_device.Read(USBPacket);

            TimeSet.Seconds    = USBPacket[USB_TIME_SECONDS_POS];
            TimeSet.Minutes    = USBPacket[USB_TIME_MINUTES_POS];
            TimeSet.Hours      = USBPacket[USB_TIME_HOURS_POS];
            TimeSet.WeekDay    = USBPacket[USB_TIME_WEEKDAY_POS];
            TimeSet.Date       = USBPacket[USB_TIME_DATE_POS];
            TimeSet.Month      = USBPacket[USB_TIME_MONTH_POS];
            TimeSet.Year       = USBPacket[USB_TIME_YEAR_POS];
            TimeSet.TimeZoneId = USBPacket[USB_TIME_ZONE_POS];

            TimeSetWindow TimeSettings = new TimeSetWindow(TimeSet);

            TimeSettings.Owner = this;
            bool?SetResult = TimeSettings.ShowDialog();

            if (SetResult == true)
            {
                USBPacket                       = new byte[USB_PACKET_SIZE];
                USBPacket[0]                    = USB_REPORT_ID;
                USBPacket[USB_CMD_POS]          = USB_CMD_TIME;
                USBPacket[USB_TIME_SUBCMD_POS]  = TIME_SUBCMD_SET;
                USBPacket[USB_TIME_SECONDS_POS] = TimeSet.Seconds;
                USBPacket[USB_TIME_MINUTES_POS] = TimeSet.Minutes;
                USBPacket[USB_TIME_HOURS_POS]   = TimeSet.Hours;
                USBPacket[USB_TIME_WEEKDAY_POS] = TimeSet.WeekDay;
                USBPacket[USB_TIME_DATE_POS]    = TimeSet.Date;
                USBPacket[USB_TIME_MONTH_POS]   = TimeSet.Month;
                USBPacket[USB_TIME_YEAR_POS]    = TimeSet.Year;
                USBPacket[USB_TIME_ZONE_POS]    = TimeSet.TimeZoneId;

                USBSuccess = USB_device.Write(USBPacket);

                ShowConnectionState(USBSuccess);
                if (USBSuccess == false)
                {
                    return;
                }
            }
        }
        private void ReadSettings()
        {
            bool EventsExist = false;

            byte[] USBPacket = new byte[USB_PACKET_SIZE];
            USBPacket[0]                     = USB_REPORT_ID;
            USBPacket[USB_CMD_POS]           = USB_CMD_EEPROM_READ;
            USBPacket[USB_EEPROM_SUBCMD_POS] = EEPROM_SUBCMD_EVENTS_COUNT;
            Boolean USBSuccess = USB_device.Write(USBPacket);

            ShowConnectionState(USBSuccess);
            if (USBSuccess == false)
            {
                return;
            }

            USBSuccess = USB_device.Read(USBPacket);

            for (byte i = 0; i < DAYS_NUM; i++)
            {
                Days[i].Clear();
                //создается один элемент, в поле Hours кол-во событий в дне
                //этот элемент потом нужно удалить
                byte EventsInThatDay = USBPacket[USB_EEPROM_SUBCMD_POS + 1 + i];
                if (EventsInThatDay > EVENTS_MAX)
                {
                    EventsInThatDay = 0;
                }
                Days[i].Add(new Event()
                {
                    Hours = EventsInThatDay
                });
            }

            for (byte i = 0; i < DAYS_NUM; i++)
            {
                if (Days[i][0].Hours != 0)
                {
                    ThreadDay        = i;
                    ThreadEventInDay = 0;
                    EventsExist      = true;
                    break;
                }
            }

            if (EventsExist)
            {
                ThreadWrite   = false;
                ThreadSetting = THREAD_SET_EVENTS;
                ThreadTimer.Start();
            }

            else
            {
                DeleteAllEvents();
            }
        }
        private void DownloadStart()
        {
            ThreadMonth      = 0;
            ThreadDay        = 0;
            ThreadEventInDay = 0;
            ThreadEvent      = 0;

            int EventsSum = TotalEventsCount;

            if (EventsSum > EVENTS_COUNT_MAX)
            {
                EventsSum = EVENTS_COUNT_MAX;
            }

            byte[] USBPacket = new byte[USB_PACKET_SIZE];
            USBPacket[0]                        = USB_REPORT_ID;
            USBPacket[USB_CMD_POS]              = USB_CMD_EEPROM_WRITE;
            USBPacket[USB_EEPROM_SUBCMD_POS]    = EEPROM_SUBCMD_EVENTS_COUNT;
            USBPacket[USB_ACTIVE_EVENT_POS]     = (byte)EventsSum;
            USBPacket[USB_ACTIVE_EVENT_POS + 1] = (byte)(EventsSum >> 8);
            USBPacket[USB_START_STOP_FLAG_POS]  = USB_CMD_START_FLAG;
            Boolean USBSuccess = USB_device.Write(USBPacket);

            ShowConnectionState(USBSuccess);
            if (USBSuccess == false)
            {
                return;
            }

            if (TotalEventsCount != 0)
            {
                FindNextEvent();
                ProgressBar.Owner = this;
                ProgressBar.Show();
                ThreadWrite = true;
                ThreadTimer.Start();
            }

            else
            {
                USBPacket[USB_EEPROM_SUBCMD_POS]   = 0xFF; //нет команды
                USBPacket[USB_START_STOP_FLAG_POS] = USB_CMD_STOP_FLAG;
                USBSuccess = USB_device.Write(USBPacket);
            }
        }
        public MainWindow()
        {
            InitializeComponent();

            for (byte day = 0; day < DAYS_NUM; day++)
            {
                Days[day] = new ObservableCollection <Event>();
            }

            Events.ItemsSource = Days[0];

            Boolean USBDevDetected = USB_device.Open();

            if (USBDevDetected)
            {
                ThreadTimer.Interval = TimeSpan.FromMilliseconds(10); //период опроса устройства
                ThreadTimer.Tick    += ThreadTimer_Tick;
            }

            ShowConnectionState(USBDevDetected);
        }
 private void WindowIsClosing(object sender, CancelEventArgs e)
 {
     ProgressBar.Close();
     USB_device.Close();
 }
        private void ThreadTimer_Tick(object sender, EventArgs e)
        {
            if (ThreadWrite)//запись
            {
                byte[] USBPacket = new byte[USB_PACKET_SIZE];
                USBPacket[0]           = USB_REPORT_ID;
                USBPacket[USB_CMD_POS] = USB_CMD_EEPROM_WRITE;

                if (ThreadSetting == THREAD_SET_EVENTS)
                {
                    ProgressBar.Owner = this;
                    ProgressBar.Show();

                    for (byte EventInPacket = 0; EventInPacket < USB_EVENT_IN_PACKET_MAX; EventInPacket++)
                    {
                        if (ThreadEventInDay != Days[ThreadDay].Count)
                        {
                            int EventStartPos = USB_EEPROM_SUBCMD_POS + EventInPacket * USB_EVENT_SIZE;
                            USBPacket[EventStartPos + USB_WEEK_DAY_CODE_OFFSET]  = EEPROM_SUBCMD_WEEK_DAY;
                            USBPacket[EventStartPos + USB_WEEK_DAY_OFFSET]       = ThreadDay;
                            USBPacket[EventStartPos + USB_EVENT_NUM_OFFSET]      = ThreadEventInDay;
                            USBPacket[EventStartPos + USB_EVENT_DATA_OFFSET]     = Days[ThreadDay][ThreadEventInDay].Hours;
                            USBPacket[EventStartPos + USB_EVENT_DATA_OFFSET + 1] = Days[ThreadDay][ThreadEventInDay].Minutes;
                            USBPacket[EventStartPos + USB_EVENT_DATA_OFFSET + 2] = (byte)Array.IndexOf(EventWindow.RelayStatesStrings, Days[ThreadDay][ThreadEventInDay].RelayState);

                            ThreadEventInDay++;
                        }

                        else
                        {
                            byte NextDay = DAYS_NUM;
                            for (byte i = (byte)(ThreadDay + 1); i < DAYS_NUM; i++)
                            {
                                if (Days[i].Count != 0)
                                {
                                    NextDay          = i;
                                    ThreadDay        = NextDay;
                                    ThreadEventInDay = 0;
                                    break;
                                }
                            }

                            if (NextDay == DAYS_NUM)
                            {
                                ThreadDay = 0;
                                ThreadTimer.Stop();
                                ProgressBar.Hide();

                                USBPacket[USB_START_STOP_FLAG_POS] = USB_CMD_STOP_FLAG;
                            }

                            break;
                        }
                    }
                }

                Boolean USBSuccess = USB_device.Write(USBPacket);
            }

            else //чтение
            {
                byte[] USBPacket = new byte[USB_PACKET_SIZE];
                USBPacket[0]           = USB_REPORT_ID;
                USBPacket[USB_CMD_POS] = USB_CMD_EEPROM_READ;

                if (ThreadSetting == THREAD_SET_EVENTS)
                {
                    ProgressBar.Owner = this;
                    ProgressBar.Show();
                    byte NextDay = ThreadDay;
                    USBPacket[USB_EEPROM_SUBCMD_POS] = EEPROM_SUBCMD_WEEK_DAY;
                    USBPacket[USB_EEPROM_SUBCMD_POS + USB_WEEK_DAY_OFFSET] = ThreadDay;
                    for (byte EventInPacket = 0; EventInPacket < USB_EVENT_IN_PACKET_MAX; EventInPacket++)
                    {
                        if (ThreadEventInDay != Days[ThreadDay][0].Hours)
                        {
                            USBPacket[USB_EEPROM_SUBCMD_POS + EventInPacket * USB_EVENT_SIZE + USB_EVENT_NUM_OFFSET] = ThreadEventInDay;
                            ThreadEventInDay++;
                        }

                        else
                        {
                            USBPacket[USB_EEPROM_SUBCMD_POS + EventInPacket * USB_EVENT_SIZE + USB_EVENT_NUM_OFFSET] = EVENTS_MAX;

                            NextDay = DAYS_NUM;
                            for (byte i = (byte)(ThreadDay + 1); i < DAYS_NUM; i++)
                            {
                                if (Days[i][0].Hours != 0)
                                {
                                    NextDay = i;
                                    break;
                                }
                            }

                            break;
                        }
                    }

                    Boolean USBSuccess = USB_device.Write(USBPacket);
                    USBSuccess = USB_device.Read(USBPacket);
                    for (byte EventInPacket = 0; EventInPacket < USB_EVENT_IN_PACKET_MAX; EventInPacket++)
                    {
                        int EventStartPos = USB_EEPROM_SUBCMD_POS + EventInPacket * USB_EVENT_SIZE;

                        if (USBPacket[EventStartPos + USB_EVENT_NUM_OFFSET] != EVENTS_MAX)
                        {
                            Days[ThreadDay].Add(new Event()
                            {
                                Hours      = USBPacket[EventStartPos + USB_EVENT_DATA_OFFSET],
                                Minutes    = USBPacket[EventStartPos + USB_EVENT_DATA_OFFSET + 1],
                                RelayState = EventWindow.RelayStatesStrings[USBPacket[EventStartPos + USB_EVENT_DATA_OFFSET + 2]],
                            });
                        }

                        else
                        {
                            break;
                        }
                    }

                    if ((NextDay != DAYS_NUM) && (ThreadDay != NextDay))
                    {
                        ThreadDay        = NextDay;
                        ThreadEventInDay = 0;
                    }
                    else if (NextDay == DAYS_NUM)
                    {
                        foreach (ObservableCollection <Event> day in Days)
                        {
                            day.RemoveAt(0);
                        }
                        //ThreadDay = 0;
                        ThreadTimer.Stop();
                        ProgressBar.Hide();
                    }
                }
            }
        }
        private void ThreadTimer_Tick(object sender, EventArgs e)
        {
            if (ThreadWrite)//запись
            {
                byte[] USBPacket = new byte[USB_PACKET_SIZE];
                USBPacket[0]                        = USB_REPORT_ID;
                USBPacket[USB_CMD_POS]              = USB_CMD_EEPROM_WRITE;
                USBPacket[USB_EEPROM_SUBCMD_POS]    = EEPROM_SUBCMD_EVENTS_DATA;
                USBPacket[USB_ACTIVE_EVENT_POS]     = (byte)ThreadEvent;
                USBPacket[USB_ACTIVE_EVENT_POS + 1] = (byte)(ThreadEvent >> 8);
                USBPacket[USB_EVENTS_IN_PACKET_POS] = 0; //событий в посылке

                for (byte EventInPacket = 0; EventInPacket < USB_EVENT_IN_PACKET_MAX; EventInPacket++)
                {
                    USBPacket[USB_EVENTS_IN_PACKET_POS]++;

                    int EventStartPos = USB_EVENTS_DATA_POS + EventInPacket * USB_EVENT_SIZE;
                    USBPacket[EventStartPos + USB_EVENT_HOUR_OFFSET]   = Months[ThreadMonth].Days[ThreadDay].Events[ThreadEventInDay].Hours;
                    USBPacket[EventStartPos + USB_EVENT_MINUTE_OFFSET] = Months[ThreadMonth].Days[ThreadDay].Events[ThreadEventInDay].Minutes;
                    USBPacket[EventStartPos + USB_EVENT_STATE_OFFSET]  = (byte)Array.IndexOf(EventWindow.RelayStatesStrings, Months[ThreadMonth].Days[ThreadDay].Events[ThreadEventInDay].RelayState);
                    USBPacket[EventStartPos + USB_EVENT_MONTH_OFFSET]  = (byte)(ThreadMonth + 1);
                    USBPacket[EventStartPos + USB_EVENT_DAY_OFFSET]    = Months[ThreadMonth].Days[ThreadDay].Number;

                    ThreadEvent++;
                    if ((ThreadEvent < TotalEventsCount) && (ThreadEvent < EVENTS_COUNT_MAX))
                    {
                        ThreadEventInDay++;
                        FindNextEvent();
                    }

                    else
                    {
                        ThreadTimer.Stop();
                        ProgressBar.Hide();

                        USBPacket[USB_START_STOP_FLAG_POS] = USB_CMD_STOP_FLAG;

                        break;
                    }
                }

                Boolean USBSuccess = USB_device.Write(USBPacket);
            }

            else //чтение
            {
                byte[] USBPacket = new byte[USB_PACKET_SIZE];
                USBPacket[0]                        = USB_REPORT_ID;
                USBPacket[USB_CMD_POS]              = USB_CMD_EEPROM_READ;
                USBPacket[USB_EEPROM_SUBCMD_POS]    = EEPROM_SUBCMD_EVENTS_DATA;
                USBPacket[USB_ACTIVE_EVENT_POS]     = (byte)ThreadEvent;
                USBPacket[USB_ACTIVE_EVENT_POS + 1] = (byte)(ThreadEvent >> 8);

                int EventsCount = TotalEventsCount - ThreadEvent;
                if (EventsCount > USB_EVENT_IN_PACKET_MAX)
                {
                    EventsCount = USB_EVENT_IN_PACKET_MAX;
                }

                USBPacket[USB_EVENTS_IN_PACKET_POS] = (byte)EventsCount;

                Boolean USBSuccess = USB_device.Write(USBPacket);
                USBSuccess = USB_device.Read(USBPacket);

                for (byte EventInPacket = 0; EventInPacket < EventsCount; EventInPacket++)
                {
                    int  EventStartPos = USB_EVENTS_DATA_POS + EventInPacket * USB_EVENT_SIZE;
                    byte ActiveMonth   = (byte)(USBPacket[EventStartPos + USB_EVENT_MONTH_OFFSET] - 1);
                    byte ActiveDay     = (byte)(USBPacket[EventStartPos + USB_EVENT_DAY_OFFSET] - 1);

                    Months[ActiveMonth].Days[ActiveDay].Events.Add(new Event()
                    {
                        Hours      = USBPacket[EventStartPos + USB_EVENT_HOUR_OFFSET],
                        Minutes    = USBPacket[EventStartPos + USB_EVENT_MINUTE_OFFSET],
                        RelayState = EventWindow.RelayStatesStrings[USBPacket[EventStartPos + USB_EVENT_STATE_OFFSET]],
                    });
                }

                ThreadEvent += USBPacket[USB_EVENTS_IN_PACKET_POS];
                if (ThreadEvent >= TotalEventsCount)
                {
                    ThreadTimer.Stop();
                    ProgressBar.Hide();
                    EventsCountUpdate();
                    MonthEventsCountUpdate();
                }
            }
        }