我是新来的,所以我有一个关于 Arduino 和 Visual C# 软件的问题。 我正在制作一个软件,通过 COM5 端口向我的 arduino 设备发送“数据”,我有 3 组字:ON、OFF 和 INTER。 ON = LED 高亮,OFF = LED 低亮,INTER 使其闪烁。 ON 有效,OFF 也有效,但是当我按下软件时,将 INTER 数据发送到我的 arduino 的按钮会收到 ON 信号,因此在发送 ON 数据时它会保持通电状态。
这是我的arduino代码。 (我不认为是来 self 的 C# 软件,因为其他一切都工作正常,但在将 INTER 字发送到我的 char 变量时闪烁不起作用)。
int led = 11;
int led13 = 13;
void setup()
{
Serial.begin(9600);
pinMode(led,OUTPUT);
}
void loop()
{
digitalWrite(led13,LOW);
char data = Serial.read();
switch (data)
{
case 'ON': digitalWrite(led,HIGH); break;
case 'OFF': digitalWrite(led,LOW); break;
}
data = Serial.read();
if (data == 'INTER') { inter(); }
}
void inter()
{
digitalWrite(led,LOW);
delay(1600);
digitalWrite(led,HIGH);
delay(1600);
}
最佳答案
出于多种原因,我认为代码结构是错误的。
1-Serial.read(); 返回一个字符,因此每次尝试读取时只有一个字符
2-您正在使用大小写“ON”:我不确定arduino如何解释它。
以下是有关 switch case 的一些一般规则:
- values for 'case' must be integer or character constants
- List item the order of the 'case' statements is unimportant
- List item the default clause may occur first (convention places it last) you cannot use expressions or ranges
3-一旦可用,您需要读取整个串行缓冲区 while(Serial.avilable()) { } 那么你可以检查接收到的字符串是否包含或等于 ON/OFF/INTER
我真的不知道arduino会如何处理“INTER”或“OFF”,因为单引号标识单个字符
关于c# - Arduino 用 Visual C# 制作的软件无法正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29300566/