Serial port and checkboxes updating
Posted
by
hradecek
on Stack Overflow
See other posts from Stack Overflow
or by hradecek
Published on 2012-03-28T17:26:57Z
Indexed on
2012/03/28
17:30 UTC
Read the original article
Hit count: 242
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;
}
}
}
© Stack Overflow or respective owner