hi everyone I'm looking for a good alogrithm to send a float variable to a PC by the serial port. First I have to separate the float variable in bytes to be read to send by the serial port. do i have to send the variables in binary or ascii?? what do you recommend me?? Another problem is in the pc.. if I want to use visual basic to print the value how can I join the bytes to have the real float variable?? thanks
do i have to send the variables in binary or ascii? Which ever format you want. If you choose binary, you can encapsulate the float(s) in a binary framing protocol. Here is a document describing a very simple binary framing protocol: http://dhenry.home.sprynet.com/hdlcbcc.pdf
If transmission speed is not of primary importance I suggest you send the data as ASCII using printf(). If you want to use binary: float f=1.23; unsigned char *ptr=&f; ptr[0] through ptr[3] are then the four bytes of your float.
"Another problem is in the pc.. if I want to use visual basic to print the value how can I join the bytes to have the real float variable??" No idea! :-( This is a Keil forum, not a Microsoft and/or VB forum - so you'll have to read your VB documentation, visit MSDN, and/or find a VB forum for that one.
"If transmission speed is not of primary importance I suggest you send the data as ASCII using printf()." This might also be the easiest to get back to a floating-point number at the VB end?
"This might also be the easiest to get back to a floating-point number at the VB end?" No doubt, due to the the byte sex differences on the VB and C51 ends, not to mention that (to my knowledge) 32-bit floats are not well-supported in VB.
Can't help you with the VB, but here is a method that works great for uC to Labview over a serial port. Consider the following code snippet union { float floater; unsigned char string[4]; unsigned long int longinter; unsigned int interger; } float_char; char sendbuf[4]; float_char.longinter=MS_COUNTER_REF; sendbuf[0]=float_char.string[0]; sendbuf[1]=float_char.string[1]; sendbuf[2]=float_char.string[2]; sendbuf[3]=float_char.string[3]; UART0_Xmit(sendbuf, 4);//send 4 bytes float_char.floater=neon_pressure; sendbuf[0]=float_char.string[0]; sendbuf[1]=float_char.string[1]; sendbuf[2]=float_char.string[2]; sendbuf[3]=float_char.string[3]; UART0_Xmit(sendbuf, 4); end of code This results in sending out 4 bytes that make up a 32 bit floating point number. In labview, they have a unflatten from string function with will decode from 4 element string to 32 bit floating point. If VB has something equivilent, this may work for you. Hope this helps AC
Hi AC I'm very interested in the code for labview can you tell me your email. because I need to graph some value from my microcontroller. please help me thanks
You might want to take a look at http://www.windmill.co.uk They have loads of stuff (including free stuff) for getting serial data into Excel, and other PC things
Jean Paul, You can reach me at acurda@amsuper.com I can give you an example of converting such data to a floating point in labview if you need. I can try to help with other stuff if yo have specific question, but sometims this darn work thing gets in the way. Andy
Jean Paul, I sent you some examples but got another email from you. What country are you in anyway? You should of recieved 2 emails from me by now. Let me know here if you havn't recieved any email from me. Andy
Hi, My name is Claudio from Argentina. I had the same problem, but the microcontroller I'm using is a PIC 16F876. What I did was to use this article in the Microsoft Knoledge Base, were I could get the individual bytes of a VB long type variable: http://support.microsoft.com/default.aspx?scid=kb;en-us;Q171652 This same works for single, or double floating point real types, so you can change the individual bytes with the ones received from the UART. I hope this would help.