Hi,
I am currently trying to read the speed of a DC motor with pySerial. It works well if I just pring the value on the serial monitor, but when I try to read it with Python is returns weirds hexadecimal values. Here is the part of the code on the Arduino :
// Return the motor speed
if (input == 'g')
{
// Init measurement values
motor_speed_meas = 0;
motor_speed_mean = 0;
// Take 10 measurements and average
for (int i=0;i<10;i++)
{
motor_speed_meas = motor_speed_meas + 500000/(float)pulseIn(7, HIGH);
}
motor_speed_mean = (int)(motor_speed_meas/10);
// Write on the serial
Serial.write(motor_speed_mean);
}
And then the code I am using on Python :
import serial
import time
ser = serial.Serial('/dev/tty.usbmodemfd121', 9600)
time.sleep(1)
def read_speed():
ser.write("g")
time.sleep(0.01)
speed = ser.read()
return speed
I just made a test that gradually increases the speed of the motor, and measure the speed with the Python interface. Here is the result :
['\x90',
'\xc5',
'\xf5',
'\x1b',
'=',
'\\',
'v',
'\x8d',
'\xa2',
'\xb4',
'\xc3',
'\xcf',
'\xdd',
'\xea',
'\xf3',
'\xfd',
'\x05',
'\x0c',
'\x15',
'\x1a']
This return values that doesn't make sense, whereas it is correct if I just use Serial.print() on the Arduino. What am I doing wrong ?
Thanks !