Raymondo02
Posts: 8
Joined: Thu Apr 18, 2019 11:48 pm

Sending a number to raspberry pi from arduino card via USB

Wed Jul 17, 2019 11:10 pm

I have a problem with a part of my project that is a slider to take time lapse. I use a Raspberry pi 3B+ and a Arduino Uno. I'm able to send "message" to my Arduino with my raspberry pi, but not the other way.
I search on Internet and normally we use on Arduino Serial.println( ) or Serial.write( ) and on the Raspberry pi we use ser.readline( ). This doesn't work in the part of my program.

Code: Select all

t=True
while t:
    try:
        print(ser.readline)
        if ser.readline==4:
            t=False
        else:
            print("Entrain de prendre des photos")
            GPIO.output(12,1)
            time.sleep(1)
            GPIO.output(12,0)
            time.sleep(interval)
            

    except KeyboardInterrupt:
        ser.write(str(3).encode())# Envoie un message pour arrêter le step motor.
        GPIO.cleanup()
        t=False
        
The bloc after try: doesn't work if it's ser.readline( ). With print(ser.readline) I get : <built-in method readline of Serial object at 0x7594bd50> and my bloc if ser.readline==4 doesn't work.

The part of program on my Arduino is

Code: Select all

if (conteur == 117){
	digitalWrite(IN1, LOW);
	digitalWrite(IN2,LOW);
	digitalWrite(IN3,LOW);
	digitalWrite(IN4,LOW);
	Serial.write(4);
	conteur=0;
	asm volatile("jmp 0");
	exit(0);
}
All work, but not Serial.write(4); . I don't know where is my error, on my raspberry pi or on my Arduino and I don't know how to fix that.
I hope you can help me.

ghp
Posts: 1389
Joined: Wed Jun 12, 2013 12:41 pm
Location: Stuttgart Germany
Contact: Website

Re: Sending a number to raspberry pi from arduino card via USB

Thu Jul 18, 2019 5:15 am

Hello, look into https://pyserial.readthedocs.io/en/late ... intro.html, where some samples are given.
Using ser.readline will not work,
- ser.readline() should be used
- you call this twice

Code: Select all

        print(ser.readline)  #missing ()
        if ser.readline==4:  #missing ()
better is

Code: Select all

        res = ser.readline()
        print(res)
        if res ==4:  # in python3, use res[0]
BUT THERE IS A PROBLEM. readline() relies on a '\n' terminating the message. As you send single bytes, you should use ser.read() instead.
Do you use pyton2 or python3 (encouraged). With python 3 the methods return byte array, so retrieve single bytes like

Code: Select all

res = ser.read()
print("got byte", res[0])
Good luck !

Raymondo02
Posts: 8
Joined: Thu Apr 18, 2019 11:48 pm

Re: Sending a number to raspberry pi from arduino card via USB

Thu Jul 18, 2019 11:43 am

I use Python3. When I test it. The byte incrases by one all the time. So my program doesn't work. Do you know what can I do to fix this?

gordon77
Posts: 4062
Joined: Sun Aug 05, 2012 3:12 pm

Re: Sending a number to raspberry pi from arduino card via USB

Thu Jul 18, 2019 1:39 pm

Heres' an example which sends 4 from the arduino when the Pi sends R to it...

PI

Code: Select all

#! /usr/bin/env python3
import serial
import time
ser = serial.Serial(port='/dev/ttyACM0',baudrate = 9600,parity=serial.PARITY_NONE,
        stopbits=serial.STOPBITS_ONE,bytesize=serial.EIGHTBITS,timeout=1)
# Wait to read from Arduino
while 1:
   try:
       time.sleep(1)
       # send an R to the arduino
       ser.write(b"R")
       #read data from arduino
       myData = ser.readline()
       myData = myData.decode("utf-8","ignore")
       print (myData)
       if float(myData) == 4:
           print (myData)
   except KeyboardInterrupt:
       exit() 
Uno

Code: Select all

int number = 4;
void setup() {
    Serial.begin( 9600 );}
void loop() {
    if (Serial.available()>0) {
         if (Serial.read() == 'R') {
              Serial.print(number);
              Serial.write("\n");
            }
        }
    } 

Raymondo02
Posts: 8
Joined: Thu Apr 18, 2019 11:48 pm

Re: Sending a number to raspberry pi from arduino card via USB

Thu Jul 18, 2019 5:05 pm

Thank you, nut with the if float(myData) == 4: I get this : ValueError: could not convert string to float: Why? How can I fix that?

gordon77
Posts: 4062
Joined: Sun Aug 05, 2012 3:12 pm

Re: Sending a number to raspberry pi from arduino card via USB

Thu Jul 18, 2019 5:39 pm

try

if len(myData) > 0 and float(myData) == 4:

I think it's getting an empty string sometimes

ghp
Posts: 1389
Joined: Wed Jun 12, 2013 12:41 pm
Location: Stuttgart Germany
Contact: Website

Re: Sending a number to raspberry pi from arduino card via USB

Thu Jul 18, 2019 9:18 pm

Code: Select all

       print (myData)
       if float(myData) == 4:
The number sent by the arduino is a 'int number', so try using "if int(myData) == 4". If you convert a value to a float, you loose precision and comparing gets a nightmare.
What does the printout say in these cases when there are errors? If you add something like "print ("myData[", myData,"]"), then you better see whats going on.

From the python serial docs:
line = ser.readline() # read a '\n' terminated line

So a good idea to add a strip() too.

Code: Select all

       print ("mydata[", myData, "]")
       if int(myData.strip()) == 4:

Raymondo02
Posts: 8
Joined: Thu Apr 18, 2019 11:48 pm

Re: Sending a number to raspberry pi from arduino card via USB

Fri Jul 19, 2019 1:07 am

Thanks all of you. It's finally working like I want!

Return to “Python”