0

I am communicating from raspberry pi using python to my Arduino via usb and serial communication. I got it to work perfectly when I ask for an input but when I try and save a variable and push that through, it doesn't work. I think the Arduino is reading the serial until a newline is found. When I type input from python and press enter, the arduino successfully recognizes a newline and executes the code. However, when I save a string with \n at the end, the Arduino does not correctly receive the string and the rest of the code does not continue.

Is there an easy way to fix this, or even a longer way to trick python by still asking for an input, but the computer automatically types in what I need my variable to be and then presses enter?

Here is my python code that works:

import serial
import time

if __name__ == '__main__':
    ser = serial.Serial('/dev/ttyACM0', 9600, timeout=1)
    ser.flush()
    
    while True:
                direction_distance = input("Enter String :")
                
                ser.write(direction_distance.encode('utf-8'))
                
                time.sleep(0.5)
                
                receive_string=ser.readline().decode('utf-8').rstrip()
                
                print(receive_string)

Here is Arduino Code that works:

void setup(){
Serial.begin(9600);
}
void loop(){
 
  if(Serial.available() > 0) {
    String data = Serial.readStringUntil('\n');

    //Rest of code continues below with newly imported 'data'
  }
}

Here is my python code that sends data to the Arduino, but the Arduino fails to recognize the newline '\n'

import serial
import time

if __name__ == '__main__':
    ser = serial.Serial('/dev/ttyACM0', 9600, timeout=1)
    ser.flush()
    
    while True:
                direction_distance = "2000\n"
                
                ser.write(direction_distance.encode('utf-8'))
                
                time.sleep(0.5)
                
                receive_string=ser.readline().decode('utf-8').rstrip()
                
                print(receive_string)
3
  • Did you mean: direction_distance = "2000". Why do you want to send a "\n"? Commented May 22, 2022 at 14:20
  • I tried both, my thinking was that the arduino is reading until '\n' so if I add a new line to the string it will stop reading then. Commented May 22, 2022 at 16:12
  • Looking at the code that works, you are not sending a new line, so I'm not sure why it works. Commented May 22, 2022 at 17:56

1 Answer 1

2

There wasn't a long enough delay for the strings to go fully go through. I was able to fix this issue changing the sleep to 2 seconds. (And by removing the \n)

import serial
import time

if __name__ == '__main__':
    ser = serial.Serial('/dev/ttyACM0', 9600, timeout=1)
    ser.flush()
    
    while True:
                direction_distance = "2000"
                
                time.sleep(2)

                ser.write(direction_distance.encode('utf-8
                
                receive_string=ser.readline().decode('utf-8').rstrip()
                
                print(receive_string)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.