Electronic – Gui float data from micro controller

datapicusb

I am building a framework for a data acquisition system where the controller sends a (integer/float) data in to the GUI. I have done some ground work on data types and compilers and found out that each integer/float takes 2/4 bytes each.

My doubt is that how to club this integer/float byte in to one and show them as the real integer/float value in GUI.

This is a part of GUI programming I trust, but all data communication were sending data in byte and later clubbing those and displaying as float/int.

eg case:

  • step1: controller sends float data to the GUI.
  • step2: Controller and compiler splits the data in to 4 bytes.
  • step3: Gui recieves 4 Bytes.
  • step4: Gui converts Receieved 4 bytes in to float and displays it on the GUI.

I need more clarity on step 4.

So if we need a float/integer in the GUI, how should we club the data coming from the communication ports (USB/uart)?

This would be helpful to build my understanding and clear my doubts, if you suggest your opinions.

Best Answer

How the value types are represented in memory depends on the architecture (little/big endian for example) and on the standards it complies to.

Example: The IEEE Standard for Floating-Point Arithmetic (IEEE 754) is a technical standard for floating-point computation established in 1985.

From a micro controller perspective, the easiest way to send this kind of data is just to send it in this native (in memory) representation using pointers or unions.

On the PC/GUI side, you may have additional resources/libraries that help you with byte-sequence to type conversions. However you perform the conversion, you just have to make sure it uses the same rules/standard.

Lets take an 16-bit unsigned integer ´i´ with the assigned value of 1000 for example:

i = 1000

The little endian representation would be

0xE8 0x03

in memory. So when you want send this value as a serial byte stream just cast ´i´ to ´byte´ and send two bytes starting from that address.

Your GUI software could perform the cast in the other direction, given that it uses the same standard for the type representation. If it uses big endian, you may have to interchange byte order first.

btw: What kind of GUI/language are you referring to? For example C#/.NET provides extensive mechanisms for type conversions using the BitConverter class.

EDIT Since the author mentioned that he uses C#, here some additional info:
Note that C# itself doesn't define the endianness. Endianness is decided by hardware. However, most platforms that use .NET are LITTLE endian. If you want to be sure, you can check the endianness of the system with the ´BitConverter.IsLittleEndian´ field to tell you how it will behave.

Assuming that your micro controller uses little endian (like all(?) atmel controllers for example), you could convert the bytes given from the 16-bit unsigned int example above using:

    UInt16 value = BitConverter.ToUInt16(new byte[] { 0xE8, 0x03 }, 0);

Otherwise, you may have to revert the byte order first.