Serial string in Hex to floating point number

For MIAC users to discuss projects, applications, and any other issues related to the MIAC unit.

Moderators: Benj, Mods

Post Reply
Dave @Harper Adams
Posts: 33
Joined: Tue May 21, 2013 4:21 pm
Been thanked: 1 time
Contact:

Serial string in Hex to floating point number

Post by Dave @Harper Adams »

Hi

I am reading in a string of serial information from a sensor via the RS232 link (24 bytes). The information is in hex with packets of data in bytes of 4 (eg 42 47 71 05), and are in the IEEE754 format. I want to convert this to a floating point number. There is a StringToFloat function in Flowcode5, but all it appears to do is convert a string of ASCII data (which is as a number with a decimal point), to an actual number.
Are there any functions that can convert directly from hex to a floating point number or is it a case of producing my own matematical finctions to do the conversion in steps?
I've never writen in c code yet, but are there any c code functions for the PIC which can do this conversion, and which could be inserted in flowcose?

Regards Dave

User avatar
Benj
Matrix Staff
Posts: 15312
Joined: Mon Oct 16, 2006 10:48 am
Location: Matrix TS Ltd
Has thanked: 4803 times
Been thanked: 4314 times
Contact:

Re: Serial string in Hex to floating point number

Post by Benj »

Hi Dave,

Yes you can probably do this using C code and a structure.

First add this code to the headers section of the supplementary code within the project options.

Code: Select all

typedef union
{
  MX_FLOAT AsFloat;
  MX_UINT8  AsByte[4];
} MX_UnionFloat;

MX_UnionFloat Temp;
Then if you receive the four bytes into Flowcode variables named byte0 to byte3.

Then using a C icon you can pass the data into the Float using the union. Here I have used a Flowcode float variable named FVar.

Code: Select all

Temp.AsByte[0] = FCV_BYTE0;
Temp.AsByte[1] = FCV_BYTE1;
Temp.AsByte[2] = FCV_BYTE2;
Temp.AsByte[3] = FCV_BYTE3;
FCV_FVAR = Temp.AsFloat;

Dave @Harper Adams
Posts: 33
Joined: Tue May 21, 2013 4:21 pm
Been thanked: 1 time
Contact:

Re: Serial string in Hex to floating point number

Post by Dave @Harper Adams »

Hi

Please bear with me as I never done this before.
I put the first section of code near the start of the main C code.
(Build, then View C) At the start of the code there are a lot of #define MX lines. I put the first lot of code somewhere here?

For the second lot of code I pull in a 'C' icon box, open it and put the code in here?

Regards Dave

dazz
Posts: 1314
Joined: Sun Jan 15, 2012 3:15 pm
Has thanked: 474 times
Been thanked: 458 times
Contact:

Re: Serial string in Hex to floating point number

Post by dazz »

HI
The supplementary code goes in its own area, see attached pic i've numbered some arrows to show the sequence for opening the supplementary code box. hope this gets you started,sorry i cant be more help bit ive got the headache from hell at the moment, oh remember c-code wont simulate
Regards
Dazz
Untitled.jpg
(329.6 KiB) Downloaded 4726 times
To sign up to the V5 forum follow this link http://www.matrixmultimedia.com/forum_upgrades.php

Dave @Harper Adams
Posts: 33
Joined: Tue May 21, 2013 4:21 pm
Been thanked: 1 time
Contact:

Re: Serial string in Hex to floating point number

Post by Dave @Harper Adams »

Hi

Thanks for your assistance Dazz

I'm still having problems.
I put the code in as Dazz has indicated and created a short flowcode program just to prove out the code, but it won't compile. The error message is:-

Hex to Float conversion.c(300): error: unexpected identifier
Hex to Float conversion.c(300): error: missing right brace
Hex to Float conversion.c(300): error: unexpected identifier
Hex to Float conversion.c(300): error: missing right brace

failure
Return code = 1
Flowcode was unable to compile the flowchart's C code due to the following errors:


If your flowchart contains C code, please review this carefully. If your flowchart contains no C-code or you have thoroughly reviewed the code, contact Technical Support.

I've check the code and can't see any errors.

I've attached the flowcode which simply takes 4 byte values (as initial variable values 42, 47, 71, 05) and should print the converted floating point variable on the screen of the MIAC. (the answer in this case should be 49.9)

Regards Dave
Attachments
Hex to Float conversion.fcf
Hex to floating point number conversion
(8 KiB) Downloaded 566 times

User avatar
Benj
Matrix Staff
Posts: 15312
Joined: Mon Oct 16, 2006 10:48 am
Location: Matrix TS Ltd
Has thanked: 4803 times
Been thanked: 4314 times
Contact:

Re: Serial string in Hex to floating point number

Post by Benj »

Hi Dave,

You had introduced a typo into the supplementary code. Unit instead of UINT.

This file should compile correctly.
Hex to Float conversion.fcf
(8 KiB) Downloaded 539 times
Tried it on the MIAC and it is printing out 0. Hmm.

Looking at this page it seems I am doing things correctly.
http://stackoverflow.com/questions/4742 ... t-in-netmf

Maybe the bit endian is incorrect?

Dave @Harper Adams
Posts: 33
Joined: Tue May 21, 2013 4:21 pm
Been thanked: 1 time
Contact:

Re: Serial string in Hex to floating point number

Post by Dave @Harper Adams »

Hi Benj

Thanks for pointing out the typo.
I too can now get it to compile and get the same answer of '0'
As I don't understand the C code I can't comment on what's in the link.
The data sheet for the sensor I'm using just states:-

Floats are coded based on IEEE754
Bytes order should be reverted to "Little endian"
1 Float (single precision).

I've tried changing the order of the bytes and still get the same answer '0'. It should be 1.13

I don't know if the following links are at all useful.

http://en.wikipedia.org/wiki/Single-pre ... int_format
http://www.binaryconvert.com/result_flo ... l=42477105

Regards Dave

User avatar
Benj
Matrix Staff
Posts: 15312
Joined: Mon Oct 16, 2006 10:48 am
Location: Matrix TS Ltd
Has thanked: 4803 times
Been thanked: 4314 times
Contact:

Re: Serial string in Hex to floating point number

Post by Benj »

Hi Dave,

I've had another look at the code and really we should convert the float to a string and then print out the string. However in doing this I am getting 0.000000 on the LCD.

Could be something to do with the weird way BoostC handles floats as it generally needs 3rd party functions to allow floats to work. Might be worth having a go with HiTech and seeing if this has the same issue.

Details of how to do this are available from here:
http://www.matrixmultimedia.com/support ... f=68&t=788

Dave @Harper Adams
Posts: 33
Joined: Tue May 21, 2013 4:21 pm
Been thanked: 1 time
Contact:

Re: Serial string in Hex to floating point number

Post by Dave @Harper Adams »

Hi Benj

I modified my flowcode so that it convets the float value to a string and then prints the hex values and the float string on the MIAC screen.
This works okay except the float value is 0.000000 as you are getting.
I've downloaded and installed HITECH C Complier for PIC18MCUs (pro)v9.80 windows.
Followed the instructions in the link to change the links and perameters within 'Build'.
The file compiles but I just get a whole load of spurious characters on the MIAC screen now.

I've attached my modified flowcode so that you can see what I'm doing.

Regards Dave
Attachments
HexToFloatConversion2.fcf
(9.5 KiB) Downloaded 468 times

Dave @Harper Adams
Posts: 33
Joined: Tue May 21, 2013 4:21 pm
Been thanked: 1 time
Contact:

Re: Serial string in Hex to floating point number

Post by Dave @Harper Adams »

Hi Benj

I have now got it working with the standard compiler.
I was cycling home last night thinking about it and then realised that the byte values that were being used may be in the wrong format (decimal and not hex).
I have now put 0x in front of the values and it works. To check it I've tried different input byte values and get the right floating point output values.
Just need to work out how to pull out the right bytes from the data string and convert to hex so that they can be used in the coversion, but that should be relatively easy.

Thanks for your help
Dave
Attachments
String Hex to Float conversion.fcf
(11.66 KiB) Downloaded 525 times

User avatar
Benj
Matrix Staff
Posts: 15312
Joined: Mon Oct 16, 2006 10:48 am
Location: Matrix TS Ltd
Has thanked: 4803 times
Been thanked: 4314 times
Contact:

Re: Serial string in Hex to floating point number

Post by Benj »

Hi Dave,

Aha that's brilliant news.

The values you pull from the RS232 connection will already be in the right format unless they are being sent as ASCII in which case you would have to manipulate to convert into the correct value.

Post Reply