1

I've connected both Arduino and Raspberry Pi vis USB serial communication. On Raspeberry Side, Python code has to read three ultrasonic sensors using below attached code. Thus, depending to sensors information Python will send command via a string e.g. M, 2000, 1500 to drive the two wheels of a robot. The problem is each time I run python code, it loses some digits or comma, for example if Arduino sends 200, 122, 60 (left, centre, right) distance, on python side I receive some times same data but most the time there is a missing number or even the comma and thus the split function shows error because instead of having three sensors if I lose the comma in reading then it will be just like two sensors.

import serial
import time
import string
DEVICE = '/dev/ttyACM0'
BAUD = 9600
ser= serial.Serial(DEVICE, BAUD)
while (1==1):
    Sensors_Data=ser.readline()
    print Sensors_Data
    (left_distance, centre_distance, right_distance)=[int(s) for s in  Sensors_Data.split(',')]
    print left_distance
    print centre_distance
    print right_distance
    if (centre_distance<50):
        ser.write('M%4d' %1500)
        ser.write('M%4d' %1500)
    else:
        ser.write('M%4d' %1600)
        ser.write('M%4d' %1500)
4
  • Could you print the output Sensors_Data here? Commented Sep 16, 2015 at 10:07
  • Also, what do you mean you get an error with the split function? The results are not correct? Commented Sep 16, 2015 at 10:16
  • 1- The output of Sensors_Data is 0, 93,0 ; 0,96,0 and so on. Commented Sep 16, 2015 at 11:09
  • 1- The output of Sensors_Data is 0, 93,0 ; 0,96,0 and so on. If the Sensors_Data is alway like that there is no problem. but just accidently the Sensor_Data prints 093,0 or 0,9,0 thus split function won't work beacuse it should always receive x,y,x. Also, if you look to the end of the code if centre_distance less than 50 I send a string i.e M,1500,1500 to arduino to stop the motors. However, some times also I see the output of Sensors_Data is mixed with the values of string such as 0, 93,0M 0r 0,96,01500 which obviously is wrong. Commented Sep 16, 2015 at 11:18

1 Answer 1

1

First let's make sure you don't have extraneous characters like newlines and carriage returns.

import serial
import time
import string
DEVICE = '/dev/ttyACM0'
BAUD = 9600
ser= serial.Serial(DEVICE, BAUD)
while (1==1):
    Sensors_Data=ser.readline().encode('string-escape')
    print Sensors_Data # This should show you what all is there.
    Sensors_Data = Sensors_Data.split('\r\n')[0] # Assuming you have a carriage return and a newline, which is typical.
    (left_distance, centre_distance, right_distance)=[int(s) for s in  Sensors_Data.split(',')]
    print left_distance
    print centre_distance
    print right_distance
    if (centre_distance<50):
        ser.write('M%4d' %1500)
        ser.write('M%4d' %1500)
    else:
        ser.write('M%4d' %1600)
        ser.write('M%4d' %1500)
Sign up to request clarification or add additional context in comments.

5 Comments

I've modified my code according to yours. On Arduino serial monitor, it was 0, 63, 0 but python prints Sensors_Data as 063,0\r\n if you notice the first comma wa missing the errror message is "Traceback (most recent call last): File "/home/pi/Raspberry_Arduino/Example_3/Raspberry_Arduino_Ex_3.py", line 23, in <module> (left_distance, centre_distance, right_distance)=[int(s) for s in Sensors_Data.split(',')] ValueError: invalid literal for int() with base 10: '0\\r\\n'"
I hope you are not using the serial monitor and running this python script at the same time, that may corrupt your data.
Actually, I tried both cases. Also, when I run python and serial monitor was not running I have the same problem. I think the problem is only in readline() because it doesn't read exactly the same string as on Arduino. I don't what I have to do else?
I just saw the error you wrote again. Could you try changing the split statement to Sensors_Data.split('\\r\\n')[0]
Thanks, I've tried this unfortunately it doesn't work too.This statement "encode('string-escape')" adds \r\n to my string (Sensors data) so definitely I will got an error because my split functions need only three sensors data separated by comma without any addition. I have solved the problem by using my same code but on Arduino side I didn't send anything else except the sensors data. Python mixed between sensors data string xxx,yyy,zzz and the motor sting M,1500,1500 that send also from Arduino.

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.