python - 如何读取Python Arduino Uno的多个条件

标签 python arduino pyserial

我有一个问题。 我目前正在研究 python-Arduino Uno 项目。

所以我们开始吧。 我想将值从 python 1 发送到 Arduino 并从 python 2 中获取另一个值。 所以实际上我想让Arduino Uno可以读取2个条件 然后它会做一些事情...

Python代码1

i = results[1]
    i *= 100
    if i >= 75:
        print('Recognised as Me!')
        arduino.write(str.encode('1'))
    else:
        print('not matches')
        arduino.write(str.encode('0'))

代码Python 2

c = results[0]
    c *= 100
    d = results[2]
    d *= 100
    e = results[1]
    e *= 100
    for a in top_k:
        if a == 0 and c >= 50:
            arduino.write(str.encode('a'))
        if a == 1 and e > 50:
            arduino.write(str.encode('h'))
        if a == 2 and d >= 50:
            arduino.write(str.encode('s'))

Arduino代码

int pin = 13;
char getValue;
void setup()
{

  pinMode(pin, OUTPUT);
  Serial.begin(9600);
}

void loop()
{



 if(Serial.available() > 0)
  getValue = Serial.read();
  Serial.print(getValue);
  if (getValue == '1' && getValue == 'h' ) { //its not works for me. 
    digitalWrite(pin, HIGH);
      delay(1000);
  }

  else if (getValue == '0'){
    digitalWrite(pin, LOW);
    delay(1000); 
  }
 }

最佳答案

您只读取该值一次,然后比较同一值两次。如果您想比较第二个值,则需要使用 Serial.read() 再次读取该值。一个简单的例子:

  value1 = Serial.read();
  Serial.print(value1);

  value2 = Serial.read();
  Serial.print(value2);

  if (value1 == '1' && value2 == 'h' ) { 
    digitalWrite(pin, HIGH);
      delay(1000);
  }

关于python - 如何读取Python Arduino Uno的多个条件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59121063/

相关文章:

python - 使用 Pandas(python)计算 csv 文件中的唯一 ID

python - autofmt_xdate 删除所有子图的 x 轴标签

python - 如何切换打开 jupyter notebook 的环境

python - 尝试在 WinXP 上使用 pyserial 打开串口 -> "Access denied"

用于与 Zaber TLSR300B 通信的 Python、pyserial 程序

python - RS232串口数据编解码

python - 匹配nltk语法中的字母数字字符串

c - 使用指针获取数组的长度

c - Arduino 和 ESP8266 HTTP GET 响应处理

c# - 使用 Arduino 期间的 WPF 数据绑定(bind)