I need to send an array of floating-point numbers from python to Arduino Mega. For that, I read many sources [this this this this this] this] and many more links for the same I referred. But unable to solve the problem.
I tried to send the angles to Arduino through python and return these values to python to print.
Below is the Python code in which I try to send the array named angle and tries to print the values received from Arduino.
import cv2
import numpy as np
import serial
import struct
import time
ser = serial.Serial('COM3', 9600, timeout = 1)
time.sleep(2)
if(ser.isOpen() == False):
ser.open()
print('com3 is open', ser.isOpen())
angle = [120.2,154.2,14.25]
ser.write(struct.pack('>fff', 10.5,11.9,48.2)) # B for interger and f for float
#ser.write(struct.pack('>f', angle)) # B for interger and f for float
time.sleep(2)
ser.flush()
data = ser.readline()
data = str(data.decode("utf"))
print(data)
ser.close()
Below is the Arduino code:
float f1; float f2; float f3;
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
Serial.println("Arduino is in parseFloat!");
}
void loop() {
if (Serial.available() > 0)
{
d1 = Serial.parseFloat(); d2 = Serial.parseFloat(); d3 = Serial.parseFloat();
ad = d1 +2.5;
Serial.println(ad)
}
delay(2000);
}
The error I got is:
I got no error or warnings but there is no output also for the print commands as shown in below Figure.
I need help to send the values to Arduino using python.