简体   繁体   English

Arduino Serial.Read() 与 Raspberry Pi Serial.write() 不稳定

[英]Arduino Serial.Read() unstable with Raspberry Pi Serial.write()

I am building something where a Raspberry Pi connects to a database to check if a value is right.我正在构建一些 Raspberry Pi 连接到数据库以检查值是否正确的东西。 If the value is right it sends a signal to an Arduino through Serial.write().如果值正确,它会通过 Serial.write() 向 Arduino 发送信号。 Then the Arduino reads this data and if the Arduino reads 1 it should send a HIGH signal trough pin 13.然后 Arduino 读取这些数据,如果 Arduino 读取到 1,它应该通过引脚 13 发送一个高电平信号。

This worked a few months ago, but when I tried it last week it only worked a few times.这在几个月前有效,但是当我上周尝试时,它只工作了几次。 It seems as if the Arduino isn't always able to read the signal the Pi sends.似乎 Arduino 并不总是能够读取 Pi 发送的信号。 Most of the times it only works the first time after I make a change and then it seems like it doesn't read the signals anymore.大多数情况下,它仅在我进行更改后第一次工作,然后似乎不再读取信号。

Arduino code: Arduino代码:

int n;

void setup(){
  pinMode(13,OUTPUT);
  pinMode(10,INPUT);
  Serial.begin(9600);
  n=7;
}

void loop(){
  if (Serial.available()){
    n = Serial.read() - '0';
  }
  if(n==1){
    digitalWrite(13,HIGH);
  }
  else{
    digitalWrite(13,LOW);
  }
  int rst = digitalRead(10);
  if(rst==HIGH){
     n=7;
  }
}

Raspberry Pi Python code which sends the signal via Serial.write ((arduinoSerialData.write()).通过 Serial.write ((arduinoSerialData.write()) 发送信号的 Raspberry Pi Python 代码。

if strscan == barcode:
    print ('code valid')
    arduinoSerialData.write('1')
    break
else:
    print ('code invalid')
    arduinoSerialData.write('2')

Have you checked manually if the RPi is actually sending something?您是否手动检查过 RPi 是否确实在发送某些内容?

If you have, and it is, I'm afraid the answer to your question is not so software related.如果您有,而且确实如此,恐怕您的问题的答案与软件无关。 Bottom line is that RPi works with 3v3 signals where most Arduino boards work with 5V.底线是 RPi 使用 3v3 信号,而大多数 Arduino 板使用 5V。 To make it even more complex, the pi can send out signals of 5V but can only take up to 3v3 on its inputs.更复杂的是,pi 可以发出 5V 的信号,但其输入最多只能接受 3v3。 When you put 5V on such a pin, the pi probably won't go up in smoke immediately but prolonged exposure to 5V may hurt the pi in the end and cause inexplicable behavior, like for example what you're describing.当您在这样的引脚上施加 5V 电压时,pi可能不会立即冒烟,但长时间暴露在 5V 下最终可能会伤害 pi 并导致莫名其妙的行为,例如您所描述的。

You're saying the pi is connected through serial.你是说pi是通过串口连接的。 If it is connected bidirectionally and the Arduino is sending stuff over to the pi this will cause problems.如果它是双向连接的并且 Arduino 正在向 pi 发送东西,这将导致问题。

Also, the digital input pins of your Arduino are configured such that they trigger above a certain threshold voltage.此外,您的 Arduino 的数字输入引脚被配置为在高于某个阈值电压时触发。 I don't know this value for your the Arduino board you are using.我不知道您正在使用的 Arduino 板的这个值。 But if it is around 3.3 volts or higher, the signal of the pi may be just to low voltage to trigger the Arduino.但如果它在 3.3 伏左右或更高,则 pi 的信号可能只是低电压以触发 Arduino。

The solution to the problems described above are logic level converters .上述问题的解决方案是逻辑电平转换器 You hook one Vcc up to the 3v3 and the other to 5V.您将一个 Vcc 连接到 3v3,另一个连接到 5V。 Then you connect the pi and the Arduino to each other by routing the signal wires through this logic level converter.然后你通过这个逻辑电平转换器将信号线连接起来,将 pi 和 Arduino 相互连接起来。 The llc will take care of voltage conversion. llc 将负责电压转换。 Make sure you get a llc that is bidirectional, this will save a lot of hassle.确保你得到一个双向的 llc,这会省去很多麻烦。 For example, this one is a suitable one.例如, 是一个合适的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM