Using a consortium to send and receive floating-point numbers via serial

Source: Internet
Author: User

Here are two methods, one is to use pointers, one is to use the Union, time-valuable spectator can jump directly to the second methodThis is just a discussion of the processing of data before and after receiving, not discussing the specific sending process method One: pointer

UART Serial port has a disadvantage, that is, send and receive is a byte of a byte of the receive, if the number of floating points sent how to do ah?

Someone would say, that's a byte to send a byte. Well, I'll first define a double data32, a double type of 8 bytes, which means that data needs to split 8 blocks to send through the UART how? When you learn C, you must have read this sentence: The pointer is the essence of C language then use the pointer! How to use it? I don't know, there's only one variable, so just declare a pointer to the variable.
Double *= &data;
P will take 8 bytes as a whole so that p+1 cannot point to the next byte address so if I turn P strong into unsigned char *? Or we declare a unsigned char * pointing to this data
unsigned  char  *= (unsigned  char *) p;
Then the serial port is sent with a for loop is enough
 for (i=0;i<8; i++) {    Send (* (q+i));}
The receiver first uses an array to start receiving data from the low and then forcibly points to the array header using a double pointer. The following code is the process of simulating the data splitting combination before and after the serial port is sent VS2010
Method Two: ConsortiumMethod one is too cumbersome, is there a simple way? How simple is the use of a consortium (community)? Directly on the code
#include"stdafx.h"#defineMax_lenth 8Union u1{CharS[max_lenth]; DoubleD;}; Union u2{CharS[max_lenth]; DoubleD;}; int_tmain (intARGC, _tchar*argv[])    {U1 u1;    U2 U2; intI=0; U1.D=2.111; U2.D=3.00; printf ("u1.d =%lf\n", U1.D); printf ("u2.d =%lf\n", U2.D); printf ("Send Data ...");  for(i=0; i<max_lenth;i++) {U2.s[i]=U1.s[i]; } printf ("u2.d =%lf\n", U2.D);  while(GetChar () = ='Q'); return 0;}

Method One is simply wasting brain power!

VS2010 Results

Why would a consortium be so magical?because all its members have an offset of 0 relative to the base addressThis means that the double-precision floating-point address &u1.d is the same as the first address of the array u1.s "Hindsight": in fact, method one method and the idea of the consortium is the same as the use of the array to save the received data (the essence of the data is the memory of the binary), a pointer to a double point to the first address of the array, then we have the value of the symbol * will be the data intact read out Ah! Only the address of double in the Union is the first address of the array, do not need to be as strong as method one!

Using a consortium to send and receive floating-point numbers via serial

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.