Serial string in Hex to floating point number
-
- Posts: 33
- Joined: Tue May 21, 2013 4:21 pm
- Been thanked: 1 time
Serial string in Hex to floating point number
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
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
- 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
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.
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.
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 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;
Regards Ben Rowland - MatrixTSL
Flowcode Product Page - Flowcode Help Wiki - Flowcode Examples - Flowcode Blog - Flowcode Course - My YouTube Channel
Flowcode Product Page - Flowcode Help Wiki - Flowcode Examples - Flowcode Blog - Flowcode Course - My YouTube Channel
-
- Posts: 33
- Joined: Tue May 21, 2013 4:21 pm
- Been thanked: 1 time
Re: Serial string in Hex to floating point number
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
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
Re: Serial string in Hex to floating point number
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
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
To sign up to the V5 forum follow this link http://www.matrixmultimedia.com/forum_upgrades.php
-
- Posts: 33
- Joined: Tue May 21, 2013 4:21 pm
- Been thanked: 1 time
Re: Serial string in Hex to floating point number
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
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 649 times
- 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
Hi Dave,
You had introduced a typo into the supplementary code. Unit instead of UINT.
This file should compile correctly. 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?
You had introduced a typo into the supplementary code. Unit instead of UINT.
This file should compile correctly. 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?
Regards Ben Rowland - MatrixTSL
Flowcode Product Page - Flowcode Help Wiki - Flowcode Examples - Flowcode Blog - Flowcode Course - My YouTube Channel
Flowcode Product Page - Flowcode Help Wiki - Flowcode Examples - Flowcode Blog - Flowcode Course - My YouTube Channel
-
- Posts: 33
- Joined: Tue May 21, 2013 4:21 pm
- Been thanked: 1 time
Re: Serial string in Hex to floating point number
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
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
- 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
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
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
Regards Ben Rowland - MatrixTSL
Flowcode Product Page - Flowcode Help Wiki - Flowcode Examples - Flowcode Blog - Flowcode Course - My YouTube Channel
Flowcode Product Page - Flowcode Help Wiki - Flowcode Examples - Flowcode Blog - Flowcode Course - My YouTube Channel
-
- Posts: 33
- Joined: Tue May 21, 2013 4:21 pm
- Been thanked: 1 time
Re: Serial string in Hex to floating point number
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
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 528 times
-
- Posts: 33
- Joined: Tue May 21, 2013 4:21 pm
- Been thanked: 1 time
Re: Serial string in Hex to floating point number
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
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 555 times
- 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
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.
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.
Regards Ben Rowland - MatrixTSL
Flowcode Product Page - Flowcode Help Wiki - Flowcode Examples - Flowcode Blog - Flowcode Course - My YouTube Channel
Flowcode Product Page - Flowcode Help Wiki - Flowcode Examples - Flowcode Blog - Flowcode Course - My YouTube Channel