Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Apr 26th, 2012  |  syntax: None  |  size: 1.46 KB  |  hits: 15  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. Serial Port, Checkboxes, Threading
  2. private void receiveData()
  3. {            
  4.     for(int i = 0; i < 3; ++i)          
  5.         inputs[i] = serialPort.ReadByte();
  6.     for (int i = 0; i < 3; ++i)          
  7.         outputs[i] = serialPort.ReadByte();
  8.  
  9.     checkBoxI1.IsChecked = inputs[0] == 32 ? true : false;
  10.     checkBoxI2.IsChecked = inputs[1] == 32 ? true : false;
  11.     checkBoxI3.IsChecked = inputs[2] == 32 ? true : false;
  12.     checkBoxQ1.IsChecked = outputs[0] == 32 ? true : false;
  13.     checkBoxQ2.IsChecked = outputs[1] == 32 ? true : false;
  14.     checkBoxQ3.IsChecked = outputs[2] == 32 ? true : false;
  15. }
  16. // Tab change
  17. private void tabControl1_SelectionChanged(object sender, SelectionChangedEventArgs e)
  18. {
  19.     if (tabControl1.SelectedItem == tabItem1)
  20.     {                                    
  21.         serialPort.Close();
  22.         try
  23.         {
  24.             receiveThread.Abort();
  25.         }
  26.         catch (NullReferenceException)
  27.         {
  28.         }
  29.     }
  30.     else if (tabControl1.SelectedItem == tabItem2)
  31.     {                
  32.         serialPort.Open();                
  33.         receiveThread = new Thread(receiveData);
  34.         receiveThread.Start();                
  35.     }
  36. }
  37.        
  38. checkBoxI1.Invoke(new Action(() =>
  39. {
  40.     checkBoxI1.IsChecked = inputs[0] == 32;
  41.     checkBoxI2.IsChecked = inputs[1] == 32;
  42.     checkBoxI3.IsChecked = inputs[2] == 32;
  43.     checkBoxQ1.IsChecked = outputs[0] == 32;
  44.     checkBoxQ2.IsChecked = outputs[1] == 32;
  45.     checkBoxQ3.IsChecked = outputs[2] == 32;
  46. }));