title: Sending float type data from Arduino to Python
body: <p>I need to send float data to Arduino from Python and get the same value back. I thought to send some float data from the Arduino first.  The data is sent as 4 successive bytes.  I'm trying to figure out how to collect these successive bytes and convert it to proper format at the Python end (system end)</p>
  
  <p><strong>Arduino code:</strong></p>
  
  <pre><code>void USART_transmitdouble(double* d)
  {
      union Sharedblock
      {
          char part[4];
          double data;
  
      } my_block;
      my_block.data = *d;
      for(int i=0;i&lt;4;++i)
      {
          USART_send(my_block.part[i]);
      }
  
  
  }
  
  int main()
  {
      USART_init();
      double dble=5.5;
      while(1)
      {
         USART_transmitdouble(&amp;dble);
      }
      return 0;
  }
  </code></pre>
  
  <p>Python code (system end):</p>
  
  <pre><code>my_ser = serial.Serial('/dev/tty.usbmodemfa131',19200)
  
  while 1:
      #a = raw_input('enter a value:')
      #my_ser.write(a)
      data = my_ser.read(4)
      f_data, = struct.unpack('&lt;f',data)
      print f_data
      #time.sleep(0.5)
  </code></pre>
  
  <p>Using the <code>struct</code> module as shown in the above code is able to print float values.</p>
  
  <p>50% of the time,the data is printed correctly.  However, if I mess with <code>time.sleep()</code> or stop the transmission and restart it, incorrect values are printed out.  I think the wrong set of 4 bytes are being unpacked in this case.  Any idea on what we can do here?</p>
  
  <p>Any other ideas other than using struct module to send and receive float data to and from Arduino?</p>