Transmitting Float and Integer Data Types Over an RS-232 Port

Using pointers to break the data types into byte size data
1/9/2010 9:49:00 PM

This article focuses on breaking apart various data types into byes so that they can be transmitted over an RS-232 port. These techniques can also be used when sending data through an SPI or I2C port.

A pointer to a char is declared (dataPointer) and an array of char data types is created (uartData) to hold the bytes of the floatData variable. The dataPointer is set equal to the address of the first byte of the floatData variable. A for loop is used to loop through each individual byte that holds the float data and store them into the uartData array. The floatData is now split into byte size data and can be transmitted over the RS-232 port.
code sniplet 1 A floating point variable can be reconstructed from the uartData array by setting the dataPointer equal to the address of the first byte of the floatData variable. Use a for loop to loop through the uartData array and store each byte into the individual bytes of the floatData variable.

code sniplet 2 This technique can also be used to split/reconstruct any data type. The example below shows how this is done for an integer data type. Simply change the float data type variable floatData to an integer data type intData.

code sniplet 3 Using pointers makes it easy to split and reconstruct multiple byte data types into byte size data.

 

Development Tools

  • Visual Studio 2005
  •   .NET Framework 2.0
  •   Visual C# 2005
  •   Visual Basic 2005
  •   SQL Server 2005
  • MPLAB IDE
  •   C32 C Compiler
  •   MPASM Assembler