private void StartEncoding() { if (IsEncoding) { IsEncoding = false; StatusBarText = "Encoding stopped"; StatusBarBackground = Brushes.Green; return; } if (string.IsNullOrEmpty(TagOperationsViewModel.SelectedRegion) || TagOperationsViewModel.SelectedRegion == "Select") { MessageBox.Show("Please, select the region first.", "Information", MessageBoxButton.OK, MessageBoxImage.Information); //return; } Task.Factory.StartNew(() => { do { //1. Wait for Tag Application.Current.Dispatcher.Invoke(() => { IsEncoding = true; StatusBarText = "Waiting for tag..."; StatusBarBackground = Brushes.Yellow; }); //2. Read tag var tag = TagOperationsViewModel.ReadTagSync(); //3. if epc tag is in the number set to be encoded as defined by the project (i.e. 12301020) this indicates it is already encoded, //3a. show 'already encoded as 1020' //3b. return to 1. if (!IsEncoding) { if (tag.HasValue && !OverrideTags && CheckRepeatedTag(tag)) { StatusBarText = "Tag " + tag + " is already encoded"; StatusBarBackground = Brushes.OrangeRed; Speak("Already encoded"); Thread.Sleep(300); continue; } } var apLocked = false; //4. determine if access password is locked. In URA I see 'Gen2 memory locked' in Reserved Memory Bank (0) access password. if (TagOperationsViewModel.CheckAccessPasswordIsLocked()) { //4a. if access password locked, try the current access password from 'new project dialogue' //which has yet to be created. if fails, open dialogue to ask for old access password. Remember this password for //future uses of this dialogue. if (!TagOperationsViewModel.ApplyLockAction (new Gen2.LockAction(Gen2.LockAction.ACCESS_UNLOCK), _totalRaceInfo.AccessPassword)) { //open dialog var dialog = new NewPasswordWnd(_totalRaceInfo.AccessPassword); if (dialog.ShowDialog().GetValueOrDefault(false)) { if (!TagOperationsViewModel.ApplyLockAction (new Gen2.LockAction(Gen2.LockAction.ACCESS_UNLOCK), dialog.Password)) { // failed again // check if epc is locked // if locked -> invalid chip -> continue if (TagOperationsViewModel.CheckEpcIsLocked(dialog.Password)) { StatusBarText = "Invalid chip, continue..."; StatusBarBackground = Brushes.Red; Speak("Invalid chip"); Thread.Sleep(300); continue; } //if not locked ask for continue without locking else { if (MessageBox.Show("EPC is not locked. Continue without locking?", "Question", MessageBoxButton.YesNo, MessageBoxImage.Question) == MessageBoxResult.Yes) { //if yes apLocked = true; } //if no -> continue else { Thread.Sleep(300); continue; } } } } else { } } } //4b. if access password is not locked, encode access password = //#8 digits from access password dialogue needed in 'new project' screen# . if (!apLocked) { TagOperationsViewModel.WriteAccessPassword(_totalRaceInfo.AccessPassword); } //5. encode tag to proper number var encoded = TagOperationsViewModel.WriteTag(NextTagNumber); if (encoded && !apLocked) { //6. lock epc memory (tag) with write lock. Gen2.LockAction.EPC_LOCK TagOperationsViewModel.ApplyLockAction( new Gen2.LockAction(Gen2.LockAction.EPC_LOCK), _totalRaceInfo.AccessPassword); //7. lock access password with read/write lock. Gen2.LockAction.ACCESS_LOCK TagOperationsViewModel.ApplyLockAction( new Gen2.LockAction(Gen2.LockAction.ACCESS_LOCK), _totalRaceInfo.AccessPassword); //8. set kill password =#8 digits from kill password dialogue needed in 'new project' screen# TagOperationsViewModel.WriteKillPassword(_totalRaceInfo.KillPassword); //9. lock kill password with read/write lock. Gen2.LockAction.LILL_LOCK TagOperationsViewModel.ApplyLockAction( new Gen2.LockAction(Gen2.LockAction.KILL_LOCK), _totalRaceInfo.AccessPassword); } Application.Current.Dispatcher.Invoke(() => { SelectedRace.TagList[SelectedTagIndex] = NextTagNumber; if (encoded) { StatusBarText = "Successfully encoded!"; StatusBarBackground = Brushes.LawnGreen; } else { StatusBarText = "Encoding error, trying again..."; StatusBarBackground = Brushes.Red; } }); if (encoded) { //10. re-read tag to validate that it is properly coded. StatusBarText = "Verifying..."; StatusBarBackground = Brushes.Orange; encoded = TagOperationsViewModel.VerifyTag(NextTagNumber); } //11. have computer speak the last two digits of the number. if (encoded || Debugger.IsAttached) { SayNumber(NextTagNumber); WriteToFile(); //12. increment to next tag TotalRaceInfo.FireNextTag(NextTagNumber); } Thread.Sleep(300); } while (IsEncoding); } ); }