assign python array elements in arduino array (Read 3 times)

Thread Starter

Simurg

Joined Jan 14, 2021
1
Hi everyone,

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:
# 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:
C:
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[i] = y;
Serial.print(ang[i]);
}
// if I want to check each arduino elements it should be like this
ang[0] = y;
Serial.print(ang[0]);
}
thanks again...

Moderator edit: added code tags
 

bug13

Joined Feb 13, 2012
1,954
It looks like you are sending raw bytes and expecting string. Try this instead in your arduino code:
C:
//https://www.arduino.cc/reference/en/language/functions/communication/serial/readbytes/
Serial.readBytes()
 
Top