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(); } }
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 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(); } } }