I need to assign python array elements in arduino array, I sent the array elements to arduino but I could not solve the assignments, can anybody help me, here my codes;
Python parts;
Importing Libraries
import serial
import time
arduino = serial.Serial(port='COM3', baudrate=9600, timeout=.1)
def write_read(x):
for a in x:
arduino.write(bytes(a, 'utf-8'))
time.sleep(0.05)
data = arduino.readline()
ser_bytes = data
decoded_bytes = ser_bytes[0:len(ser_bytes)].decode("utf-8")
print(decoded_bytes)
num = input("Enter a number: ")
numSplit = num.split(" ")
numLen = len(numSplit)
print(numSplit[1])
print(numLen)
write_read(numSplit)
arduino.close()
Arduino parts
String x;
int ang[5];
int y;
void setup() {
Serial.begin(9600);
Serial.setTimeout(1);
}
void loop() {
while (!Serial.available());
x = Serial.readString();
y = x.toInt();
for(int i=0; i<5; i++){
ang = y;
_ Serial.print(ang*);_
_ }_
_// if I want to check each arduino elements it should be like this*_ * ang[0] = y;* * Serial.print(ang[0]);* } thanks again...
If you are sending the data in binary format then you need to use the Python struct.pack() function and you should save the received bytes into a suitable array or struct on the Arduino.
# Importing Libraries
import serial
import time
arduino = serial.Serial(port='COM3', baudrate=9600, timeout=.1)
def write_read(x):
for a in x:
arduino.write(bytes(a, 'utf-8'))
time.sleep(0.05)
data = arduino.readline()
ser_bytes = data
decoded_bytes = ser_bytes[0:len(ser_bytes)].decode("utf-8")
print(decoded_bytes)
num = input("Enter a number: ")
numSplit = num.split(" ")
numLen = len(numSplit)
print(numSplit[1])
print(numLen)
write_read(numSplit)
arduino.close()
String x;
int ang[5];
int y;
void setup() {
Serial.begin(9600);
Serial.setTimeout(1);
}
void loop() {
while (!Serial.available());
x = Serial.readString();
y = x.toInt();
for(int i=0; i<5; i++){
ang = y;
Serial.print(ang);
}
// if I want to check each arduino elements it should be like this
ang[0] = y;
Serial.print(ang[0]);
}
when I use python code I send to arduino such as "120 23 43 356", these numbers divided with blanks and first I split this text and then send arduino,
arduino send me back these separate numbers too but I want to assign these separated numbers in array elements in arduino, I have not found this solution yet,
Have a look at the parse example in Serial Input Basics. It uses a comma as the delimiter but that can easily be changed to a space. And as it's a demo it expects a string, an int and a float and that can be changed so it expects 4 ints. Then you can assign each int in turn to an element of an array with something like the following ...
in place of
You can send arrays, numbers, floats, and more between Python and Arduino using the compatible libraries SerialTransfer.h and pySerialTransfer. SerialTransfer.h is installable via the Arduino IDE's Libraries Manager, pySerialTransfer is pip-installable and both come with examples (to include arrays/lists)