How to send float through serial port in c#? [duplicate]


This question already has an answer here:

<ul><li> <a href="/questions/25338897/how-to-convert-float-into-binary-in-c" dir="ltr" rel="nofollow">how to convert float into binary in c#?</a> <span class="question-originals-answer-count"> 1 answer </span> </li> </ul>

I have two float now,say f1 and f2. I want to send them to my micro controller through my computer serial port, how should I do it? My understanding is we need to convert f1 and f2 to binary first,then send to micro, after that cnvert back to float again. So I am confused that HOW DO I CONVERT FLOAT TO BINARY BEFORE I SEND THEM OUT? My code now is

b1 = System.BitConverter.GetBytes(f1); b2 = System.BitConverter.GetBytes(f2);


It depends on the endianness of your microcontroller's architecture. But roughly speaking you need to do this:

<ol><li>Convert your float to a 4-byte array. You're on the right track, although my C# is very rusty.</li> <li>Send them one by one to the microcontroller</li> <li>

If your microcontroller supports floating point representation you could cast the four-byte array into a float:

byte array[4]; // Pre-allocate your array getBytesFromSerial(array); // Fill the array with values from serial port float converted = 0; memcpy(&converted, array, 4); /** Now "converted" holds the float value */ </li> </ol>

Now that will work only if the endianness of your uC is consistent with the order in which you are sending the data. If that doesn't work try sending the byte array in inverted order. And please mind the error checking!


  • How to cast type bytea to double precision
  • Accessing Sql FILESTREAM from within a CLR stored procedure
  • Compare Dictionary
  • Omit template arguments when create an instance of template class from another instance of template
  • Random(staticSeed).Next() alternative that will never change implementation and is guaranteed consis
  • Does Apple Clang lack CRC32 for ARMv8/Aarch64 under Xcdoe 10?
  • Monotouch floating point pointer throws NullReferenceException when not 4-byte aligned
  • Need advice in designing tables in SQL-Server
  • python setuptools and easy_install numpy question
  • Tips for creating scalable WPF user control
  • What is wrong in my MVC implementation?
  • google-maps-sdk-ios - arm 7/iphone 5 issues for Google Maps 1.4.3 for iOS
  • oracle row contention causing deadlock errors in high throughtput JMS application
  • I don't get what's the difference between format() and … (python)
  • How to set `secure` and `httpOnly` for Plones `__ac` cookie?
  • Consuming a web service with the Netbeans Platform
  • How to define an array of floats in Shader properties?
  • Backward compatibility of Python 3.5 for external modules
  • Scala: Function returning an unknown type
  • Hibernate to update table schema
  • Convert SQLite database to XML
  • WPF Visiblity Binding to Boolean Expression with multiple Variables
  • Conversion from string “a” to type 'Boolean' is not valid
  • zope_i18n_compile_mo_files doesn't work on a Zeo configuration
  • Abort upload large uploads after reading headers
  • C# - Is there a limit to the size of an httpWebRequest stream?
  • Opengl-es onTouchEvents problem or a draw problem? [closed]
  • Updating server-side rendering client-side
  • Timeout for blocking function call, i.e., how to stop waiting for user input after X seconds?
  • How do you troubleshoot character encoding problems?
  • How to pass list parameters for each object using Spring MVC?
  • Arrays break string types in Julia
  • Windows forms listbox.selecteditem displaying “System.Data.DataRowView” instead of actual value
  • Proper folder structure for lots of source files
  • Acquiring multiple attributes from .xml file in c#
  • How to CLICK on IE download dialog box i.e.(Open, Save, Save As…)
  • How does Linux kernel interrupt the application?
  • How can I remove ASP.NET Designer.cs files?
  • sending mail using smtp is too slow
  • java string with new operator and a literal