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:30 am

PeterO wrote:
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).

thanks

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

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

Tue Dec 17, 2019 10:00 am

Though not a habitual C or Arduino user I do have the Arduino IDE installed on my Pi and PC, so I guess I should have run my code through one of those. This compiles okay, but I cannot say if the semantics are right ...

Code: Select all

void setup() {
  Serial.begin(115200);
}

void loop(){
  do { } while( not Serial.available() );
  char s = Serial.read();
  int x1 = 1;
  int x2 = 2;
  int x3 = 3;
  Serial.print(x1);
  Serial.print(',');
  Serial.print(x2);
  Serial.print(',');
  Serial.println(x3);
}

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 10:41 am

hippy wrote:
Tue Dec 17, 2019 10:00 am
Though not a habitual C or Arduino user I do have the Arduino IDE installed on my Pi and PC, so I guess I should have run my code through one of those. This compiles okay, but I cannot say if the semantics are right ...
You ought to order an Arduino Nano clone to play with, its dirty cheap and is really fun!

a) void loop(){ is already a loop, you don't need a do..while statement, which syntax would have been different anyway:

Code: Select all

while (condition) {
  // statement(s)
}
or if you really want a do wrapper:

Code: Select all

do {
  // statement block
} while (condition);
b) not is (usually) expressed as !, but not is admitted as well.

Code: Select all

  while (!Serial) {
    ; // wait for serial port to connect. Needed for native USB
  }
I depends on what the OP wants to do:
If the Raspberry gives the tempo Python sould just send a character and wait for a reply. The Arduino replies to everything received with all variables.
If the Arduino gives the tempo I would wait to receive the start character, then send periodically the values and let Python wait to get the string.

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

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

Tue Dec 17, 2019 4:23 pm

rin67630 wrote:
Tue Dec 17, 2019 10:41 am
You ought to order an Arduino Nano clone to play with, its dirty cheap and is really fun!
I have a few and some other Arduino boards but never got into using them because I'm not a great fan of C. I also work for a company which produces a competing product so am more inclined towards those.
rin67630 wrote:
Tue Dec 17, 2019 10:41 am
a) void loop(){ is already a loop, you don't need a do..while statement, which syntax would have been different anyway:
The loop is just idling for a trigger to arrive. I think it comes down to preference which one prefers; -

while ( not trigger() ) { };
do { } while ( not trigger() );

I prefer the second form. And that is usually 'better' if using a non-optimising compiler but I would expect an optimising compiler to generate the same code for both.

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 9:04 pm

hippy wrote:
Tue Dec 17, 2019 4:23 pm
I think it comes down to preference which one prefers; -
while ( not trigger() ) { };
do { } while ( not trigger() );
if (Serial.available() > 0) {
// code
} should be the simplest variant, being anyway placed within the loop() branch.

By the way, one should built on the given example SerialCallResponseASCII is almost what the OP needs, that is the very purpose of the built-in examples!

Return to “General discussion”