This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

Data transmission for float variables by uart0

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

Parents
  • 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

Reply
  • 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

Children