I have searched several questions and through google but could not find a solution. I am simply trying to send a float number from python3 with struct to arduino or if there is any other ways other than processing a char array at the arduino side because it is very slow in my opinion..
My arduino code:
void setup() {
// initialize the serial communication:
Serial.begin(9600);
}
void loop() {
unsigned long t = millis();
float x = analogRead(A3);
float y = analogRead(A2);
float c = Serial.read();
Serial.println(c);
//Serial.print(t/1000.0);
//Serial.print(" , ");
//Serial.print(x);
//Serial.print(" , ");
//Serial.println(y);
// wait a bit for the analog-to-digital converter to stabilize after the last
// reading:
//delayMicroseconds(200);
//delay(2);
delay(250);
}
My python3 code :
import serial
import struct
arduino = serial.Serial('/dev/ttyACM0', 9600)
data = arduino.write(struct.pack('f', 111.32))
while True:
# arduino.write(b"123")
data = arduino.write(struct.pack('f', 111.32))
Getting the serial output as :
163.00
222.00
66.00
215.00
163.00
222.00
66.00
215.00
163.00
222.00
66.00
215.00
163.00
How can i solve my problem ?
EDIT : I changed the arduino code according to the answer the new arduino code is
float f;
void setup(){
Serial.begin(9600);
}
void loop(){
while(Serial.available() >= 3){
f = Serial.read() << 24;
f = ((unsigned long) f) | (Serial.read() << 16);
f = ((unsigned long) f) | (Serial.read() << 8);
f = ((unsigned long) f) | Serial.read();
Serial.println(f);
}
Serial.println(f);
}
However, the output is now as such:
ovf
ovf
ovf
ovf
ovf
ovf
ovf
4294941184.00
4294941184.00
4294941184.00
4294941184.00
4294941184.00
4294941184.00
4294941184.00