Serial port and checkboxes updating
- by hradecek
in my app' i'm recieving data from serial port and save them into two bool arrays.
And depends on these array i'm setting checkboxes. But checkboxes are not updating only when i change the tabs....
Here's how i'm doin' it(maybe there's better way how to do it)
private void comboBoxCommunication_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
if (serialPort.IsOpen)
{
recieveThread.Abort();
serialPort.Close();
}
ComboBoxItem cbi = (ComboBoxItem)comboBoxCommunication.SelectedItem;
portCommunication = cbi.Content.ToString();
serialPort.PortName = portCommunication;
try
{
serialPort.Open();
recieveThread = new Thread(dataRecieving);
prijmiThread.Start();
checkBoxI1.IsChecked = vstupy[0] ? true : false;
checkBoxI2.IsChecked = inputs[1] ? true : false;
checkBoxI3.IsChecked = inputs[2] ? true : false;
checkBoxQ2.IsChecked = outputs[3] ? true : false;
}
catch (IOException ex)
{
MessageBox.Show(ex.ToString(), "Error!", MessageBoxButton.OK, MessageBoxImage.Error, MessageBoxResult.OK);
}
}
private void dataRecieving()
{
if (serialPort.IsOpen)
{
int i = serialPort.ReadChar();
if (i == 'A')
{
inputs[0] = true;
}
else if (i == 'a')
{
inputs[0] = false;
}
if (i == 'B')
{
inputs[1] = true;
}
else if (i == 'b')
{
inputs[1] = false;
}
if (i == 'C')
{
inputs[2] = true;
}
else if (i == 'c')
{
inputs[2] = false;
}
if (i == 'D')
{
outputs[0] = true;
}
else if (i == 'd')
{
outputs[0] = false;
}
}
}