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?
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
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:
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.
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?
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.