hussein91
Posts: 18
Joined: Tue Dec 10, 2019 9:12 am

Slow serial communication between Raspberry Pi 3+ and Arduino Mega

Mon Dec 16, 2019 9:41 am

Hello
I used the serial communication between Raspberry Pi 3+ and Arduino Mega, but the connection was very slow. is there any way to make it faster.
Those are codes of arduino and python :
arduino code:

Code: Select all

float a=3.55;
float x1,x2,x3;
char s;
boolean rc=0,sx1=0,sx2=0,sx3=0;
void setup(){
  Serial.begin(115200);
}
void loop(){
  if(rc==0 & Serial.available()){
    s=Serial.read();
    rc=1;
  }
  if(rc==1){
    rc=0;
    if(s=='z'){//reset the system states on the initial conditions (x1=x1_0;x2=x2_0;x3=x3_0)
      x1=2,x2=5,x3=-1;
      s=0;sx1=0;sx2=0;sx3=0;
    }
    if(s=='a'){
      Serial.println(x1*3);
      sx1=1;
      s=0;
    }
    if(s=='b'){
      Serial.println(x2*5);
      sx2=1;
      s=0;
    }
    if(s=='c'){
      Serial.println(x3*10);
      sx3=1;
      s=0;
    }
    if(sx1*sx2*sx3==1){
      x1=sin(x1);
      x2=sin(x1+x2);
      x3=sin(x3-x1*x2);
      sx1=0;
      sx2=0;
      sx3=0;
    }
  }
}

and this is the code of python using thonny 3.2.0 on raspberry

Code: Select all

import serial
s1=serial.Serial("/dev/ttyACM0",115200)
X1,X2,X3,t=[],[],[],[];
dt=0.01
i=0;
rx1=0
rx2=0
rx3=0
s1.write(b'z')
while i<20/dt:
    s1.write(b'a')
    while rx1==0:
        if s1.inWaiting():
            x1=s1.readline()
            rx1=1        
    s1.write(b'b')
    while rx1==1 and rx2==0:
        if s1.inWaiting():
            x2=s1.readline()
            rx2=1        
    s1.write(b'c')
    while rx2==1 and rx3==0:
        if s1.inWaiting():
            x3=s1.readline()
            rx3=1
    i=i+1
    rx1=0
    rx2=0
    rx3=0
    X1.append(float(x1))
    X2.append(float(x2))
    X3.append(float(x3))
    t.append(dt*i)
    

scotty101
Posts: 3855
Joined: Fri Jun 08, 2012 6:03 pm

Re: Slow serial communication between Raspberry Pi 3+ and Arduino Mega

Mon Dec 16, 2019 9:44 am

How did you characterise the speed of the connection as 'slow'? What speed are you looking for?

You are currently sending data at 115200 bps. Have you tried increasing the baud rate?
Electronic and Computer Engineer
Pi Interests: Home Automation, IOT, Python and Tkinter

User avatar
rin67630
Posts: 982
Joined: Fri Mar 04, 2016 10:15 am

Re: Slow serial communication between Raspberry Pi 3+ and Arduino Mega

Mon Dec 16, 2019 10:20 am

hussein91 wrote:
Mon Dec 16, 2019 9:41 am
Hello
I used the serial communication between Raspberry Pi 3+ and Arduino Mega, but the connection was very slow. is there any way to make it faster.
Those are codes of arduino and python :
[program examples not repeated here]
May I suggest, that you explain from the beginning what you're trying to do and which results you expect to get?
it commonly does not make much sense to let the much weaker Arduino make floating-point trigonometric computations, that could have been done much efficiently on the Raspberry side.
Maybe the speed limitation comes from here?

scotty101
Posts: 3855
Joined: Fri Jun 08, 2012 6:03 pm

Re: Slow serial communication between Raspberry Pi 3+ and Arduino Mega

Mon Dec 16, 2019 11:03 am

rin67630 wrote:
Mon Dec 16, 2019 10:20 am
May I suggest, that you explain from the beginning what you're trying to do and which results you expect to get?
it commonly does not make much sense to let the much weaker Arduino make floating-point trigonometric computations, that could have been done much efficiently on the Raspberry side.
Maybe the speed limitation comes from here?
Good suggestion here.
If you want to time how long the mathematical functions take, you could turn on one of the Arduino's pins before the calculations and then off once they are complete. You can then measure how long they take with an oscilloscope (or use the raspberry pi to measure the pulse length)
Electronic and Computer Engineer
Pi Interests: Home Automation, IOT, Python and Tkinter

hussein91
Posts: 18
Joined: Tue Dec 10, 2019 9:12 am

Re: Slow serial communication between Raspberry Pi 3+ and Arduino Mega

Mon Dec 16, 2019 12:20 pm

thanks for you all
i mean when i excute the python code, It takes a long time, although the program is simple

Paul Hutch
Posts: 471
Joined: Fri Aug 25, 2017 2:58 pm
Location: Blackstone River Valley, MA, USA
Contact: Website

Re: Slow serial communication between Raspberry Pi 3+ and Arduino Mega

Mon Dec 16, 2019 12:44 pm

I'd bet most of time is spent waiting for the 8-bit processor in the arduino to complete the heavy math involving floating point manipulation. The heavy math is more suited to a 32 bit or higher processor like the Raspberry Pi.

hippy
Posts: 6849
Joined: Fri Sep 09, 2011 10:34 pm
Location: UK

Re: Slow serial communication between Raspberry Pi 3+ and Arduino Mega

Mon Dec 16, 2019 2:12 pm

My response on your other thread, before that gets deleted as a duplicate -
hippy wrote:
Mon Dec 16, 2019 1:52 pm
You will probably have to explain what "this problem" is, in what way the Arduino to Raspberry Pi solution is slower than Arduino to Arduino. How are you determining or observing there is this 'slowness' ?

I would guess it comes down to the code running on the Pi, what that's doing and how it's doing it. You may want to post your Pi code so people can look to see what the cause of any slowness may be.

hussein91
Posts: 18
Joined: Tue Dec 10, 2019 9:12 am

Re: Slow serial communication between Raspberry Pi 3+ and Arduino Mega

Mon Dec 16, 2019 2:17 pm

hippy wrote:
Mon Dec 16, 2019 2:12 pm
My response on your other thread, before that gets deleted as a duplicate -
hippy wrote:
Mon Dec 16, 2019 1:52 pm
You will probably have to explain what "this problem" is, in what way the Arduino to Raspberry Pi solution is slower than Arduino to Arduino. How are you determining or observing there is this 'slowness' ?

I would guess it comes down to the code running on the Pi, what that's doing and how it's doing it. You may want to post your Pi code so people can look to see what the cause of any slowness may be.
I already posted the code

hippy
Posts: 6849
Joined: Fri Sep 09, 2011 10:34 pm
Location: UK

Re: Slow serial communication between Raspberry Pi 3+ and Arduino Mega

Mon Dec 16, 2019 2:27 pm

hussein91 wrote:
Mon Dec 16, 2019 2:17 pm
I already posted the code
Not in the duplicate thread, and I wasn't going to let the time spent answering there go to waste.

User avatar
rin67630
Posts: 982
Joined: Fri Mar 04, 2016 10:15 am

Re: Slow serial communication between Raspberry Pi 3+ and Arduino Mega

Mon Dec 16, 2019 3:20 pm

hussein91 wrote:
Mon Dec 16, 2019 2:17 pm
I already posted the code
Unless you explain what you expect and why you have conceived that apparently ridiculous handshake between the Arduino and the Pi to forward information in the most inefficient way one could imagine, you won't get any more advice from me. You surely have got reasons, but we don't have a working glass ball at disposal.

hussein91
Posts: 18
Joined: Tue Dec 10, 2019 9:12 am

Re: Slow serial communication between Raspberry Pi 3+ and Arduino Mega

Mon Dec 16, 2019 3:42 pm

rin67630 wrote:
Mon Dec 16, 2019 3:20 pm
hussein91 wrote:
Mon Dec 16, 2019 2:17 pm
I already posted the code
Unless you explain what you expect and why you have conceived that apparently ridiculous handshake between the Arduino and the Pi to forward information in the most inefficient way one could imagine, you won't get any more advice from me. You surely have got reasons, but we don't have a working glass ball at disposal.
I would like Raspberry to receive 3 variables from Arduino, and then it will perform operations on these variables. I used the following method
1. Raspberry sends the variable reference to the Arduino
2. Arduino reads this reference and then sends the appropriate variable to Raspberry
3. Raspberry reads this variable and saves it
These steps are repeated 3 times in order to obtain all the 3 variables
Then, Raspberry performs operations on these variables. After completing these operations, this procedure is repeated a number of times

User avatar
rin67630
Posts: 982
Joined: Fri Mar 04, 2016 10:15 am

Re: Slow serial communication between Raspberry Pi 3+ and Arduino Mega

Mon Dec 16, 2019 4:02 pm

hussein91 wrote:
Mon Dec 16, 2019 3:42 pm
I would like Raspberry to receive 3 variables from Arduino, and then it will perform operations on these variables. I used the following method
1. Raspberry sends the variable reference to the Arduino
2. Arduino reads this reference and then sends the appropriate variable to Raspberry
3. Raspberry reads this variable and saves it
These steps are repeated 3 times in order to obtain all the 3 variables
Then, Raspberry performs operations on these variables. After completing these operations, this procedure is repeated a number of times
You will be far better off to forward all 3 variables in the same message. You probably lose most of the time in the repeated handshake upon reading the serial buffers in both directions, not in the communication itself...
What is the very reason to let the Arduino compute a sinus, just a preliminary step to get more interesting stuff from it, once the communication is running?

hussein91
Posts: 18
Joined: Tue Dec 10, 2019 9:12 am

Re: Slow serial communication between Raspberry Pi 3+ and Arduino Mega

Mon Dec 16, 2019 4:14 pm

rin67630 wrote:
Mon Dec 16, 2019 4:02 pm
hussein91 wrote:
Mon Dec 16, 2019 3:42 pm
I would like Raspberry to receive 3 variables from Arduino, and then it will perform operations on these variables. I used the following method
1. Raspberry sends the variable reference to the Arduino
2. Arduino reads this reference and then sends the appropriate variable to Raspberry
3. Raspberry reads this variable and saves it
These steps are repeated 3 times in order to obtain all the 3 variables
Then, Raspberry performs operations on these variables. After completing these operations, this procedure is repeated a number of times
You will be far better off to forward all 3 variables in the same message. You probably lose most of the time in the repeated handshake upon reading the serial buffers in both directions, not in the communication itself...
What is the very reason to let the Arduino compute a sinus, just a preliminary step to get more interesting stuff from it, once the communication is running?
thanks for help
how can i writhe those variables on the same message
i'm not intersted on sinus i only made simple code to verify the good comunication between raspberry and rduino because my aim is to make a synchronisation between those boards

User avatar
rin67630
Posts: 982
Joined: Fri Mar 04, 2016 10:15 am

Re: Slow serial communication between Raspberry Pi 3+ and Arduino Mega

Mon Dec 16, 2019 4:22 pm

hussein91 wrote:
Mon Dec 16, 2019 4:14 pm
how can i write those variables on the same message?
Replace writeln(...) with write(...) add a write(",") between all but the last variable, which is the only one to get a writeln.*
On the python side, read the message into a tuple.

*by the way, doing it that way, you can see the communication in the serial monitor as a table and in the serial plotter graphically, which is a great way to debug.

MarkDH102
Posts: 382
Joined: Fri Feb 13, 2015 3:18 pm

Re: Slow serial communication between Raspberry Pi 3+ and Arduino Mega

Mon Dec 16, 2019 4:28 pm

Code: Select all

if(rc==0 & Serial.available())
Did you actually mean (note the double &&)

Code: Select all

if(rc==0 && Serial.available())

hussein91
Posts: 18
Joined: Tue Dec 10, 2019 9:12 am

Re: Slow serial communication between Raspberry Pi 3+ and Arduino Mega

Mon Dec 16, 2019 4:30 pm

MarkDH102 wrote:
Mon Dec 16, 2019 4:28 pm

Code: Select all

if(rc==0 & Serial.available())
Did you actually mean (note the double &&)

Code: Select all

if(rc==0 && Serial.available())
Yes thanks

hippy
Posts: 6849
Joined: Fri Sep 09, 2011 10:34 pm
Location: UK

Re: Slow serial communication between Raspberry Pi 3+ and Arduino Mega

Mon Dec 16, 2019 4:35 pm

You can probably simplify your Arduino code to something like -

Code: Select all

void loop(){
  do { } while( not Serial.available() );
  s  = Serial.read();
  x1 = ...
  x2 = ...
  x3 = ...
  Serial.println(x1, ',',  x2, ',', x3);
}
And the equivalent Python code which interacts with that could be something like -

Code: Select all

def Main():
  ser = serial.Serial('/dev/ttyACM0', 115200)
  while True:
    ser.write(b'x')
    x1, x2, x3 = ser.readline().strip().split(',')
    print(x1 + ', ' + x2 + ', ' + x3)

hussein91
Posts: 18
Joined: Tue Dec 10, 2019 9:12 am

Re: Slow serial communication between Raspberry Pi 3+ and Arduino Mega

Mon Dec 16, 2019 4:40 pm

hippy wrote:
Mon Dec 16, 2019 4:35 pm
You can probably simplify your Arduino code to something like -

Code: Select all

void loop(){
  do { } while( not Serial.available() );
  s  = Serial.read();
  x1 = ...
  x2 = ...
  x3 = ...
  Serial.println(x1, x2, x3);
}
And the equivalent Python code which interacts with that could be something like -

Code: Select all

def Main():
  ser = serial.Serial('/dev/ttyACM0', 115200)
  while True:
    ser.write(b'x')
    x1, x2, x3 = ser.readline().strip().split(',')
    print(x1 + ', ' + x2 + ', ' + x3)

Thanks a lot

User avatar
PeterO
Posts: 5623
Joined: Sun Jul 22, 2012 4:14 pm

Re: Slow serial communication between Raspberry Pi 3+ and Arduino Mega

Mon Dec 16, 2019 4:43 pm

If this really does turn out to be a problem caused by slow serial transfers there is also the possibility of using an Ethernet shield on the Arduino and transferring the data via a wired network.

PeterO
Discoverer of the PI2 XENON DEATH FLASH!
Interests: C,Python,PIC,Electronics,Ham Radio (G0DZB),1960s British Computers.
"The primary requirement (as we've always seen in your examples) is that the code is readable. " Dougie Lawson

MarkDH102
Posts: 382
Joined: Fri Feb 13, 2015 3:18 pm

Re: Slow serial communication between Raspberry Pi 3+ and Arduino Mega

Mon Dec 16, 2019 4:48 pm

I can't believe the Mega is slow.
I use one to read masses of serial data (at 115200 baud) from 2 different devices, manipulate it and then store it away. It also has time to update an I2C 480*320 colour LCD.
I did increase the size of the serial buffers somewhat but that shouldn't be necessary here...

User avatar
rin67630
Posts: 982
Joined: Fri Mar 04, 2016 10:15 am

Re: Slow serial communication between Raspberry Pi 3+ and Arduino Mega

Mon Dec 16, 2019 5:13 pm

hussein91 wrote:
Mon Dec 16, 2019 4:40 pm
hippy wrote:
Mon Dec 16, 2019 4:35 pm
You can probably simplify your Arduino code to something like -

Code: Select all

  ...
  Serial.println(x1, x2, x3);
  ...
Thanks a lot
Please note: hippy has meanwhile corrected the line into

Code: Select all

  ...
  Serial.println(x1, ",",x2, ",", x3);
  ...
Just to avoid a trap...

hippy
Posts: 6849
Joined: Fri Sep 09, 2011 10:34 pm
Location: UK

Re: Slow serial communication between Raspberry Pi 3+ and Arduino Mega

Mon Dec 16, 2019 5:21 pm

rin67630 wrote:
Mon Dec 16, 2019 5:13 pm
Please note: hippy has meanwhile corrected the line into

Code: Select all

  ...
  Serial.println(x1, ",",x2, ",", x3);
  ...
Just to avoid a trap...
Yes; apologies for that. I'm not familiar with C or Arduino so the code might need tweaking. I think the "not" should be some single character symbol. The println might well have some other format.

hussein91
Posts: 18
Joined: Tue Dec 10, 2019 9:12 am

Re: Slow serial communication between Raspberry Pi 3+ and Arduino Mega

Tue Dec 17, 2019 8:05 am

rin67630 wrote:
Mon Dec 16, 2019 5:13 pm
hussein91 wrote:
Mon Dec 16, 2019 4:40 pm
hippy wrote:
Mon Dec 16, 2019 4:35 pm
You can probably simplify your Arduino code to something like -

Code: Select all

  ...
  Serial.println(x1, x2, x3);
  ...
Thanks a lot
Please note: hippy has meanwhile corrected the line into

Code: Select all

  ...
  Serial.println(x1, ",",x2, ",", x3);
  ...
Just to avoid a trap...
hello
I tried the code you adviced me but i got this error message on arduino

sketch_dec17a.ino: In function 'void loop()':
sketch_dec17a.ino:18:41: error: no matching function for call to 'HardwareSerial::println(float&, const char [2], float&, const char [2], float&)'
In file included from /usr/share/arduino/hardware/arduino/cores/arduino/Stream.h:26:0,
from /usr/share/arduino/hardware/arduino/cores/arduino/HardwareSerial.h:28,
from /usr/share/arduino/hardware/arduino/cores/arduino/Arduino.h:193,
from sketch_dec17a.ino:1:
/usr/share/arduino/hardware/arduino/cores/arduino/Print.h:67:12: note: candidate: size_t Print::println(const __FlashStringHelper*)
size_t println(const __FlashStringHelper *);
^
/usr/share/arduino/hardware/arduino/cores/arduino/Print.h:67:12: note: candidate expects 1 argument, 5 provided
/usr/share/arduino/hardware/arduino/cores/arduino/Print.h:68:12: note: candidate: size_t Print::println(const String&)
size_t println(const String &s);
^
/usr/share/arduino/hardware/arduino/cores/arduino/Print.h:68:12: note: candidate expects 1 argument, 5 provided
/usr/share/arduino/hardware/arduino/cores/arduino/Print.h:69:12: note: candidate: size_t Print::println(const char*)
size_t println(const char[]);
^
/usr/share/arduino/hardware/arduino/cores/arduino/Print.h:69:12: note: candidate expects 1 argument, 5 provided
/usr/share/arduino/hardware/arduino/cores/arduino/Print.h:70:12: note: candidate: size_t Print::println(char)
size_t println(char);
^
/usr/share/arduino/hardware/arduino/cores/arduino/Print.h:70:12: note: candidate expects 1 argument, 5 provided
/usr/share/arduino/hardware/arduino/cores/arduino/Print.h:71:12: note: candidate: size_t Print::println(unsigned char, int)
size_t println(unsigned char, int = DEC);
^
/usr/share/arduino/hardware/arduino/cores/arduino/Print.h:71:12: note: candidate expects 2 arguments, 5 provided
/usr/share/arduino/hardware/arduino/cores/arduino/Print.h:72:12: note: candidate: size_t Print::println(int, int)
size_t println(int, int = DEC);
^
/usr/share/arduino/hardware/arduino/cores/arduino/Print.h:72:12: note: candidate expects 2 arguments, 5 provided
/usr/share/arduino/hardware/arduino/cores/arduino/Print.h:73:12: note: candidate: size_t Print::println(unsigned int, int)
size_t println(unsigned int, int = DEC);
^
/usr/share/arduino/hardware/arduino/cores/arduino/Print.h:73:12: note: candidate expects 2 arguments, 5 provided
/usr/share/arduino/hardware/arduino/cores/arduino/Print.h:74:12: note: candidate: size_t Print::println(long int, int)
size_t println(long, int = DEC);
^
/usr/share/arduino/hardware/arduino/cores/arduino/Print.h:74:12: note: candidate expects 2 arguments, 5 provided
/usr/share/arduino/hardware/arduino/cores/arduino/Print.h:75:12: note: candidate: size_t Print::println(long unsigned int, int)
size_t println(unsigned long, int = DEC);
^
/usr/share/arduino/hardware/arduino/cores/arduino/Print.h:75:12: note: candidate expects 2 arguments, 5 provided
/usr/share/arduino/hardware/arduino/cores/arduino/Print.h:76:12: note: candidate: size_t Print::println(double, int)
size_t println(double, int = 2);
^
/usr/share/arduino/hardware/arduino/cores/arduino/Print.h:76:12: note: candidate expects 2 arguments, 5 provided
/usr/share/arduino/hardware/arduino/cores/arduino/Print.h:77:12: note: candidate: size_t Print::println(const Printable&)
size_t println(const Printable&);
^
/usr/share/arduino/hardware/arduino/cores/arduino/Print.h:77:12: note: candidate expects 1 argument, 5 provided
/usr/share/arduino/hardware/arduino/cores/arduino/Print.h:78:12: note: candidate: size_t Print::println()
size_t println(void);
^
/usr/share/arduino/hardware/arduino/cores/arduino/Print.h:78:12: note: candidate expects 0 arguments, 5 provided

User avatar
PeterO
Posts: 5623
Joined: Sun Jul 22, 2012 4:14 pm

Re: Slow serial communication between Raspberry Pi 3+ and Arduino Mega

Tue Dec 17, 2019 8:18 am

hippy wrote:
Mon Dec 16, 2019 5:21 pm
rin67630 wrote:
Mon Dec 16, 2019 5:13 pm
Please note: hippy has meanwhile corrected the line into

Code: Select all

  ...
  Serial.println(x1, ",",x2, ",", x3);
  ...
Just to avoid a trap...
Yes; apologies for that. I'm not familiar with C or Arduino so the code might need tweaking. I think the "not" should be some single character symbol. The println might well have some other format.
https://www.arduino.cc/reference/en/lan ... l/println/
Note that this is NOT the standard C printf so it doesn't support multiple parameters (or a format string).

PeterO

Code: Select all

Serial.println()
Description

Prints data to the serial port as human-readable ASCII text followed by a carriage return character (ASCII 13, or '\r') and a newline character (ASCII 10, or '\n'). This command takes the same forms as Serial.print().
Syntax

Serial.println(val)
Serial.println(val, format)
Parameters

Serial: serial port object. See the list of available serial ports for each board on the Serial main page.
val: the value to print. Allowed data types: any data type.
format: specifies the number base (for integral data types) or number of decimal places (for floating point types).
Discoverer of the PI2 XENON DEATH FLASH!
Interests: C,Python,PIC,Electronics,Ham Radio (G0DZB),1960s British Computers.
"The primary requirement (as we've always seen in your examples) is that the code is readable. " Dougie Lawson

User avatar
rin67630
Posts: 982
Joined: Fri Mar 04, 2016 10:15 am

Re: Slow serial communication between Raspberry Pi 3+ and Arduino Mega

Tue Dec 17, 2019 8:29 am

PeterO wrote:
Tue Dec 17, 2019 8:18 am
...
rin67630 wrote:
Mon Dec 16, 2019 5:13 pm
Please note: hippy has meanwhile corrected the line into

Code: Select all

  ...
  Serial.println(x1, ",",x2, ",", x3);
  ...
Just to avoid a trap...
...
https://www.arduino.cc/reference/en/lan ... l/println/
Note that this is NOT the standard C printf so it doesn't support multiple parameters (or a format string).
Ups ! Shame on me ! I did not pay attention! Sorry.
At least the error message was informative enough.

Return to “General discussion”