Sending data from Python to Arduino

Hello,

I have problem with communication with Arduino through Serial. I have following code in Arduino:

When I send for example "255" through Serial monitor in Arduino IDE everything is alright. But when I send "255" from Python it doesnt work. My code in Python:

When I send only 1 ASCII sign it works but when I send String it does not. What am I doing wrong? Basically what I need is to send 9 numbers to Arduino that are <0;255>. Is there easier way to do it that to parse it from String?

Thank you.

Read this before you post, please.

Do not use the "String" class, it will cause problems at some point.

Check this sticky post, it contains guides that will help you solve your problem in the "Communications" section.

I would suggest to study Serial Input Basics to handle this

Have you done a loopback test with the RPi? Are you using level shifters between the RPi and Uno?

I attached, as a text file, some Python code I use to use when I connected a RPi to a Uno, Mega, and Due.

LidarPythonCode.txt (49.6 KB)

Danois90:
Read this before you post, please.

Do not use the "String" class, it will cause problems at some point.

Check this sticky post, it contains guides that will help you solve your problem in the "Communications" section.

Thanks but this is not the case I have been already trying to send array one by one byte and pot end sign at the end. There is just something wrong with what Python sends and what Arduino receives thats why am asking specifically about Python.

write a python sketch sending "Hello World" (and reading what's coming back in Python as well and print it) and on the arduino side run this:

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

void loop()
{
  int r;
  if ((r = Serial.read()) != -1) {
    // we have received a byte, it's in the LSB of r
    Serial.print(F("0x")); Serial.print((uint8_t) r, HEX);
    Serial.print(F("\t--> '")); Serial.print((char) r);
    Serial.println(F("'"));
  }
}

=> you'll see what you receive, make sure to open the Serial port at 115200 bauds on the Python side

RichardKubik:
There is just something wrong with what Python sends and what Arduino receives thats why am asking specifically about Python.

Besides the code I posted in the text attachment on how I used to do Arduino to RPi serial, I'm not going to be much help with your Python code as this site does not support Python.

It is very important to get a result on the RPi using Python and a loopback test. If you cannot get a loop back test on the RPi to work, you cannot send info to the RPi.

In regards to sending 255 from Python to the Uno and it working, consider that is sent when nothing is working. Will all zeros or all ones be received? What would be 11111111. Perhaps a 255?

What kind of level shifters are you using?

Consider that the RPi is 3.3V on the I/O and an Uno is 5V on the I/O. Frankly, if you are not using level shifters your RPi will be compromised.

Alright turns out after you define Serial in Python you have to wait like 2 seconds. Python was sending data while connection was not estabilished. Stupid mistake :drooling_face:

However I am now facing weird problem...I get from input one array (receivedChars) and then want to parse it into smaller arrays but this happens:

Why does it assign to green/blue array values that shouldn´t be there?

RichardKubik:

Why does it assign to green/blue array values that shouldn´t be there?

What would happen if the array is not sized correctly?

Let's say you made an array of 1 byte called 1byte but tried to read 1byte[5]?

Idahowalker:
What would happen if the array is not sized correctly?

Let's say you made an array of 1 byte called 1byte but tried to read 1byte[5]?

I just tried it it probably reads whatever is in memory out of bounds of that array. In this case my receivedChars is size 10 and r/g/b arrays are size 3 which should be fine.

Would you mind to post the text instead of screen shots? Impossible to copy text from an image..

Danois90:
Would you mind to post the text instead of screen shots? Impossible to copy text from an image..

Sure just posted the image so you can see array is filled and I am not out of bounds

#include <FastLED.h>
#define NUM_LEDS 30
#define PIN 5

char serialData;
CRGB led[NUM_LEDS];

void setup() {

}



const byte numChars = 10;
char receivedChars[numChars];
boolean newData = false;
int dataNumber = 0;


void loop() {
  recvWithEndMarker();

  if (newData == true)
  {
    setLEDColor();
    newData = false;
  }
}

void setLEDColor()
{
  char red[3];
  char green[3];
  char blue[3];

  Serial.println(receivedChars);
  for (int i = 0; i < 3; i++)
  {
    red[i] = receivedChars[i];
    green[i] = receivedChars[i + 3];
    blue[i] = receivedChars[i + 6];
  }

  Serial.println(red);
  Serial.println(green);
  Serial.println(blue);

}

void recvWithEndMarker() {
  static byte ndx = 0;
  char endMarker = 'e';
  char rc;

  while (Serial.available() > 0 && newData == false) {
    rc = Serial.read();
    if (rc != endMarker) {
      receivedChars[ndx] = rc;
      ndx++;
      if (ndx >= numChars) {
        ndx = numChars - 1;
      }
    }
    else {
      receivedChars[ndx] = '\0'; 
      ndx = 0;
      newData = true;
    }
  }
}
 red[i] = receivedChars[i];
    green[i] = receivedChars[i + 3];
    blue[i] = receivedChars[i + 6];

Then something wrong with the above code to retrieve the values.

Try:

Serial.println( receivedChars[i] );
Serial.println( receivedChars[i + 3] );
Serial.println( receivedChars[i + 6];

To see what those values are.

I put it like this:

for (int i = 0; i < 3; i++)
  {
    Serial.println( receivedChars[i] );
    Serial.println( receivedChars[i + 3] );
    Serial.println( receivedChars[i + 6]);
    red[i] = receivedChars[i];
    green[i] = receivedChars[i + 3];
    blue[i] = receivedChars[i + 6];
  }

And output was
1
4
7
2
5
8
3
6
9

Which is exactly what is expected. I am kinda lost here whats wrong.

This Simple Python - Arduino demo may help.

...R

Can you post the Python code as well? You should probably use some sort of separator for the numeric values, because the values may be less than 3 characters wide.

Danois90:
Can you post the Python code as well? You should probably use some sort of separator for the numeric values, because the values may be less than 3 characters wide.

In this case I am using only serial input directly from Arduino IDE. I even tested same code in C and it works. Is there any difference in array manipulation between C and Arduiino IDE?

Which flavor of Arduino are you using?

RichardKubik:
In this case I am using only serial input directly from Arduino IDE. I even tested same code in C and it works. Is there any difference in array manipulation between C and Arduiino IDE?

Arduino IDE is a C(++) editor. If you use the serial monitor to send data to the Arduino, how exactely are you doing it?

From the printout you provided it looks like the correct info is being received. Now to figure out if is the message being placed into the array correctly or is the array info being recalled incorrectly?

The IDE gives a message in white lettering that looks like this:

Sketch uses 213720 bytes (16%) of program storage space. Maximum is 1310720 bytes.
Global variables use 13176 bytes (4%) of dynamic memory, leaving 314504 bytes for local variables. Maximum is 327680 bytes.

What does your message about memory usage read?