![]() |
Serial loopback with Arduino doesn't work - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: Serial loopback with Arduino doesn't work (/thread-29745.html) |
Serial loopback with Arduino doesn't work - ThomasS - Sep-18-2020 Hi, I'm trying to establish a serial communication with my Arduino Uno using pyserial. To test it, I programmed a simple loop back example, where the data I send with my python program over USB is send back unchanged. However, I get weird results. I already checked that the data the Arduino receives is OK (it is). When I just send data from the Arduino to the Computer (e.g. Serial.print('a')) the data is received correctly by my python program. I already tried to use ArduinoData.read(1) instead of ArduinoData.readline() in python and Serial.print(), Serial.println() and Serial.write() in Arduino. I get similar results each time. I already looked in several forums and the pyserial documentation but couldn't find anything that helps. I hope someone here can help me. Thanks! Here is my python code: import serial delay = 0 ArduinoData = serial.Serial('COM8', 9600) print("port is open!") while(delay<1000): delay+=1 ArduinoData.write(b'a') data = ArduinoData.readline() #data1 = ArduinoData.read(1) print(data) #print(data1) ArduinoData.close()Arduino Code: byte data; void setup(){ pinMode(13, OUTPUT); Serial.begin(9600); } void loop() { //wait for data while(!Serial.available()); data = Serial.read(); Serial.println(data); delay(500); }console output:
RE: Serial loopback with Arduino doesn't work - deanhystad - Sep-18-2020 What is this supposed to do? while(delay<1000): delay+=1 ArduinoData.write(b'a') RE: Serial loopback with Arduino doesn't work - ThomasS - Sep-19-2020 For some reason the Arduino wouldn't receive any data when send right after the port is opened. I found this solution to wait after it's open in some forum. RE: Serial loopback with Arduino doesn't work - deanhystad - Sep-19-2020 The code does not wait, it writes b'a' 1000 times. Even if you did implement a wait (looping 1000 times is hardly a wait) it doesn't make any sense to do so because the start of the Arduino code and the start of the Python program are not simultaneous. |