我的问题是:如何使用 wpf 应用程序从串口接收数据?我已经尝试了很多次,但仍然无法得到它;这是我的 Arduino 代码:
int switchPin = 7;
int ledPin = 13;
boolean lastButton = LOW;
boolean currentButton = LOW;
boolean flashLight = LOW;
void setup()
{
pinMode(switchPin, INPUT);
pinMode(ledPin, OUTPUT);
Serial.begin(9600);
}
boolean debounce(boolean last)
{
boolean current = digitalRead(switchPin);
if (last != current)
{
delay(5);
current = digitalRead(switchPin);
}
return current;
}
void loop()
{
currentButton = debounce(lastButton);
if (lastButton == LOW && currentButton == HIGH)
{
Serial.println("UP");
digitalWrite(ledPin, HIGH);
}
if (lastButton == HIGH && currentButton == LOW)
{
Serial.println("DOWN");
digitalWrite(ledPin, LOW);
}
lastButton = currentButton;
}
每次按下按钮时,它都会发送消息“DOWN”和“UP”。但是如何从 C# 应用程序接收它呢?请写一个这样的 wpf 应用程序的例子。