private void BtnConnect_Click(object sender, RoutedEventArgs e) { try { if ((string)BtnConnect.Content == "Connect") { _serialPort.PortName = ArduinoPortName.Text; _serialPort.BaudRate = 9600; _serialPort.Parity = Parity.None; _serialPort.StopBits = StopBits.One; _serialPort.DataBits = 8; _serialPort.Handshake = Handshake.None; _serialPort.RtsEnable = true; if (!(_serialPort.IsOpen)) { _serialPort.Open(); } BtnConnect.Content = "Disconnect"; ArduinoPortName.IsEnabled = false; BtnScanCard.IsEnabled = true; _serialPort.DataReceived += SerialPortOnDataReceived; return; } if ((string)BtnConnect.Content == "Disconnect") { if (_serialPort.IsOpen) { _serialPort.Close(); } BtnConnect.Content = "Connect"; TxtTagId.Clear(); BtnScanCard.IsEnabled = false; ArduinoPortName.IsEnabled = true; } } catch (Exception ex) { MessageBox.Show(ex.Message); } }
private void BtnAddStudent_Click(object sender, RoutedEventArgs e) { try { //if ( string.IsNullOrEmpty(TxtStudentName.Text) || string.IsNullOrEmpty(TxtMatricNo.Text) || string.IsNullOrEmpty(TxtTagId.Text) ) if (string.IsNullOrEmpty(TxtStudentName.Text) || string.IsNullOrEmpty(TxtMatricNo.Text)) { MessageBox.Show(@"All field required"); return; } if (IsMatricNo(TxtMatricNo.Text.Trim()) == false) { MessageBox.Show("Enter a valid MatricNo!"); return; } // TODO: save to tag //WRITE string cmd = "WRITE"; SendMessage(cmd); MessageBox.Show("Place card on RFID reader"); Thread.Sleep(1000); // wait for (1sec) before saving to database //Thread.Sleep(TimeSpan.FromSeconds(1)); //Task.Delay(100) --wait for a particular seconds before saving to db var res = _receivedData; var student = new Student { Name = TxtStudentName.Text.Trim(), MatricNo = TxtMatricNo.Text.Trim(), //TagID = TxtTagId.Text.Trim(), TagID = _receivedData, Department = StudentDept.Text }; var maxId = (_db.Students.Select(p => (int?)p.ID).Max() ?? 0) + 1; student.ID = maxId; try { _db.Students.AddObject(student); } catch { _repo.Students = new List <Student> { student }; } _db.SaveChanges(); //MessageBox.Show("Created successfully"); Refresh(); itemListView.ScrollIntoView(student); TxtStudentName.Clear(); TxtMatricNo.Clear(); TxtTagId.Clear(); /*_db.SaveChanges(); * Refresh(); * itemListView.ScrollIntoView(student); * * // TODO: successful account * //ACCOUNT_CREATED * string cmd = "ACCOUNT_CREATED"; * SendMessage(cmd); * TxtStudentName.Clear(); * TxtMatricNo.Clear(); * TxtTagId.Clear();*/ } catch (Exception ex) { MessageBox.Show(ex.Message); } }