Sep-18-2020, 06:40 AM
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:
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:
Output:=== RESTART: C:\Users\thoma\Documents\studium\learn python\mySerial\test1.py ===
port is open!
b'133\r\n'
>>>
=== RESTART: C:\Users\thoma\Documents\studium\learn python\mySerial\test1.py ===
port is open!
b'133\r\n'
>>>
=== RESTART: C:\Users\thoma\Documents\studium\learn python\mySerial\test1.py ===
port is open!
b'88\r\n'
>>>
=== RESTART: C:\Users\thoma\Documents\studium\learn python\mySerial\test1.py ===
port is open!
b'133\r\n'
>>>
=== RESTART: C:\Users\thoma\Documents\studium\learn python\mySerial\test1.py ===
port is open!
b'133\r\n'
>>>
=== RESTART: C:\Users\thoma\Documents\studium\learn python\mySerial\test1.py ===
port is open!
b'133\r\n'
>>>
=== RESTART: C:\Users\thoma\Documents\studium\learn python\mySerial\test1.py ===
port is open!
b'97\r\n'
>>>
=== RESTART: C:\Users\thoma\Documents\studium\learn python\mySerial\test1.py ===
port is open!
b'97\r\n'
>>>
=== RESTART: C:\Users\thoma\Documents\studium\learn python\mySerial\test1.py ===
port is open!
b'133\r\n'
>>>
=== RESTART: C:\Users\thoma\Documents\studium\learn python\mySerial\test1.py ===
port is open!
b'88\r\n'