Python Forum
Serial loopback with Arduino doesn't work
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Serial loopback with Arduino doesn't work
#1
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:
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'
Reply
#2
What is this supposed to do?
while(delay<1000):
    delay+=1
    ArduinoData.write(b'a')
Reply
#3
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.
Reply
#4
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.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  pyserial/serial "has no attribute 'Serial' " gowb0w 9 3,805 Aug-24-2023, 07:56 AM
Last Post: gowb0w
  Why doesn't calling a parent constructor work with arbitrary keyword arguments? PurposefulCoder 4 926 Jun-24-2023, 02:14 PM
Last Post: deanhystad
Star Pyserial not reading serial.readline fast enough while using AccelStepper on Arduino MartyTinker 4 4,021 Mar-13-2023, 04:02 PM
Last Post: deanhystad
  Why doesn't this code work? What is wrong with path? Melcu54 7 1,770 Jan-29-2023, 06:24 PM
Last Post: Melcu54
  color code doesn't work harryvl 1 881 Dec-29-2022, 08:59 PM
Last Post: deanhystad
  client.get_all_tickers() Doesn't work gerald 2 1,704 Jun-16-2022, 07:59 AM
Last Post: gerald
  pip doesn't work after Python upgrade Pavel_47 10 4,177 May-30-2022, 03:31 PM
Last Post: bowlofred
  For Loop Works Fine But Append For Pandas Doesn't Work knight2000 2 1,998 Dec-18-2021, 02:38 AM
Last Post: knight2000
  Class Method to Calculate Age Doesn't Work gdbengo 1 1,695 Oct-30-2021, 11:20 PM
Last Post: Yoriz
  serial connection to Arduino Jack9 4 2,452 Oct-22-2021, 10:18 AM
Last Post: Jack9

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020