0
在我的應用程序'我從串行端口接收數據並將它們保存到兩個布爾陣列。
並取決於這些數組我設置複選框。但是,當我改變標籤....
這裏就是我怎麼做就行了(也許還有更好的辦法如何做到這一點)串行端口和複選框更新
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;
}
}
}
添加註釋:複選框不刷新,因爲您正在創建新線程,並且當您嘗試更改複選框時,從串口讀取的對象仍然具有相同的值。 – gbianchi 2012-03-28 17:45:59