所以我已经解决了我的Arduino发送到我的GUI问题(主动收到)。但很可惜还有另外一个问题弹出。我无法使用我的GUI功能更新我的GUI,如Disabler() - >>这将禁用按钮。
所以这是我的示例代码。
我的数据收到活动
private void Senport_DataReceived(Object sender, SerialDataReceivedEventArgs e)
{
SerialPort Senport = (SerialPort)sender;
rdata = Senport.ReadLine();
ShowWindow();
}
我的显示窗口功能
public void ShowWindow()
{
if(rdata.Contains("FEED")==true)
{
MessageBox.Show("Feeds already being dispense!","Feeding Process",MessageBoxButton.OK,MessageBoxImage.Information);
}
else if(rdata.Contains("DRINK")==true)
{
MessageBox.Show("Drinkable water already being dispense!", "Drinking Water Process",MessageBoxButton.OK,MessageBoxImage.Information);
}
else if(rdata.Contains("CLEAN")==true)
{
MessageBox.Show("Cleaning the cage is done!", "Cleaning Process",MessageBoxButton.OK,MessageBoxImage.Information);
}
else if(rdata.Contains("Fsms")==true)
{
MessageBox.Show("Feeds Dispensing is starting","Drinking Water Process",MessageBoxButton.OK,MessageBoxImage.Information);
}
else if(rdata.Contains("Dsms")==true)
{
MessageBox.Show("Water Dispensing is starting", "Drinking Water Process",MessageBoxButton.OK,MessageBoxImage.Information);
}
else if(rdata.Contains("Csms")==true)
{
MessageBox.Show("Cleaning Process is starting", "Cleaning Process",MessageBoxButton.OK,MessageBoxImage.Information);
}
}
但我想在下面使用此功能,但会弹出异常错误。
public void Disabler()
{
switch(flagger)
{
case 1: FEED.IsEnabled=false; FEED2.IsEnabled=false; Clean.IsEnabled=false; break;
case 2: Clean.IsEnabled=false; FEED.IsEnabled=false; FEED2.IsEnabled=false; break;
case 3: Clean.IsEnabled=false; FEED.IsEnabled=false; FEED2.IsEnabled=false; break;
}
}
那我怎么能用这个功能呢?顺便说一句,错误说这个函数不在同一个线程上。希望你能帮助我:D谢谢!