user4911: I am trying to send a floating point number from a python script to an Arduino.  I am not sure how to do this, especially in a pythonic way.
A little bit of research brought me to this very similar question:
How to send numbers to Arduino UNO via Python 3 and the module serial
I vaguely understand why this would work for an int but don't know how to modify it.
1) In this question it makes sense to convert to a char, send, and then convert back to a int by casting on the Arduino, I can't think how to do that for a float.
2) Why do they convert to a Char anyway?
3) What code do I run on the arduino to get this back into a float?
4) Can I use bitwise operators on floating point numbers?
I'm new to python and also to low level programming. I am also aware that python and low level don't mix to well. I am using Linux but ideally I need a cross platform solution. Although I would prefer to get it working then worry about cross platform later.

user2926: Floating point numbers are (should) be stored in IEEE 754 format.
If you simply write the binary bytes of a floating point number down the serial line, it should be possible to unpack them at the other end.
To do this (Arduino C/C++), make a pointer from your variable, by casting the address of it to char*. Void* would probably work too.
 float my_value = 22.812;
 my_port.write((const char *)&my_value, sizeof(float)/*4*/);

And the unpacking - there's a few ways to do this, but I like:
 float incoming_value;
 unsigned char buffer[4];

 // If we read enough bytes, unpacked it
 if (my_serial.readBytes(buffer, sizeof(float)) == sizeof(float))
     memcpy(&incoming_value, buffer, sizeof(float));
 else
     // I/O error - no data, not enough bytes, etc.
     incoming_value = 0

On the other end (python), bytes are best handled with the struct module.  This will handle the packing and unpacking for you.
import struct
...

try:
    ieee754_data = my_serial.read(4)
    my_float = struct.unpack('f', ieee754_data)
catch:
    # I/O Error, or junk data
    my_float = 0.0

And packing:
 ieee754_data = struct.pack('f', my_float)
 try:
     my_serial.write(ieee754_data)
 catch:
     pass #TODO - I/O Error

One more note - You might consider using doubles instead of floats for extra precision.  Python floats have the precision of a C/C++ double already.

4911: <p>I am trying to send a floating point number from a python script to an Arduino.  I am not sure how to do this, especially in a pythonic way.</p>

<p>A little bit of research brought me to this very similar question:
<a href="https://arduino.stackexchange.com/questions/3753/how-to-send-numbers-to-arduino-uno-via-python-3-and-the-module-serial">How to send numbers to Arduino UNO via Python 3 and the module serial</a>
I vaguely understand why this would work for an int but don't know how to modify it.</p>

<p>1) In this question it makes sense to convert to a char, send, and then convert back to a int by casting on the Arduino, I can't think how to do that for a float.</p>

<p>2) Why do they convert to a Char anyway?</p>

<p>3) What code do I run on the arduino to get this back into a float?</p>

<p>4) Can I use bitwise operators on floating point numbers?</p>

<p>I'm new to python and also to low level programming. I am also aware that python and low level don't mix to well. I am using Linux but ideally I need a cross platform solution. Although I would prefer to get it working then worry about cross platform later.</p>
 2926: <p>Floating point numbers are (should) be stored in <a href="https://en.wikipedia.org/wiki/IEEE_floating_point" rel="nofollow">IEEE 754 format</a>.</p>

<p>If you simply write the binary bytes of a floating point number down the serial line, it should be possible to unpack them at the other end.</p>

<p>To do this (Arduino C/C++), make a pointer from your variable, by casting the address of it to char*. Void* would probably work too.</p>

<pre><code> float my_value = 22.812;
 my_port.write((const char *)&amp;my_value, sizeof(float)/*4*/);
</code></pre>

<p>And the unpacking - there's a few ways to do this, but I like:</p>

<pre><code> float incoming_value;
 unsigned char buffer[4];

 // If we read enough bytes, unpacked it
 if (my_serial.readBytes(buffer, sizeof(float)) == sizeof(float))
     memcpy(&amp;incoming_value, buffer, sizeof(float));
 else
     // I/O error - no data, not enough bytes, etc.
     incoming_value = 0
</code></pre>

<p>On the other end (python), bytes are best handled with the <strong>struct</strong> module.  This will handle the packing and unpacking for you.</p>

<pre><code>import struct
...

try:
    ieee754_data = my_serial.read(4)
    my_float = struct.unpack('f', ieee754_data)
catch:
    # I/O Error, or junk data
    my_float = 0.0
</code></pre>

<p>And packing:</p>

<pre><code> ieee754_data = struct.pack('f', my_float)
 try:
     my_serial.write(ieee754_data)
 catch:
     pass #TODO - I/O Error
</code></pre>

<p>One more note - You might consider using doubles instead of floats for extra precision.  Python floats have the precision of a C/C++ double already.</p>
