我想将 Arduino 中出现的数据传输到我的 C# 应用程序,但不知道我的代码有什么问题。Arduino代码来了:
int switchPin = 7;
int ledPin = 13;
boolean lastButton = LOW;
boolean currentButton = LOW;
boolean flashLight = LOW;
void setup()
{
pinMode(switchPin, INPUT);
pinMode(ledPin, OUTPUT);
}
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.print("UP");
digitalWrite(ledPin, HIGH);
}
if (lastButton == HIGH && currentButton == LOW)
{
Serial.print("DOWN");
digitalWrite(ledPin, LOW);
}
lastButton = currentButton;
}
如您所见,这个简单的草图在按下按钮时向端口发送消息。我创建了一个控制台 C# 应用程序来接收数据:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Text;
using System.IO.Ports;
namespace ArduinoTestApplication
{
class Program
{
static void Main(string[] args)
{
SerialPort port = new SerialPort("COM3", 9600);
port.Open();
string lane;
while (true)
{
lane = port.ReadLine();
Console.WriteLine(lane);
}
}
}
}
但是当我按下按钮控制台仍然是空的。请告诉我怎么了!