Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

This is my first post so please excuse if it's not perfect.

I'm currently having some problems with hooking up my Arduino Uno board with my Raspberry. I want to create a small personal smart home and for that I want to use some Arduino modules.

I'm already strugling with the first step getting the two systems to exchange data. I have tried all kinds of web guides but nothing has worked for me.

The Arduino output when entering the Serial Screen on port 9600 works fine.

enter image description here

Here is my Python code:

import serial
import time, os
os.system('clear')
     
s = serial.Serial('/dev/ttyACM0', 9600) # Namen ggf. anpassen
time.sleep(5) # der Arduino resettet nach einer Seriellen Verbindung 
s.close()

def loop():
    try:
        s.open()
        print('opened')
    except:
        print("open")
  
    s.write(b"test")
    time.sleep(1)
    print(">pre-response")
    response = s.readline()
    print(response)
    print(">past-response")

try:
    for i in range(5):
        loop()
        print(i)
        time.sleep(2)
except KeyboardInterrupt:
    print("shutting down...")
    s.close()`

And here is my Arduino code (C++):

void setup() {
  Serial.begin(9600);
}
     
void loop() {
  if (Serial.available()) {
    byte nr = Serial.read();
    //Serial.print("Folgender char wurde empfangen: ");
    Serial.println(nr, DEC);
  }
}

enter image description here


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
748 views
Welcome To Ask or Share your Answers For Others

1 Answer

Please use the below link for me it's working.

https://diyprojects.io/python-code-read-serial-port-raspberry-pi/


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...