How to send float over serial

前端 未结 9 1921
Happy的楠姐
Happy的楠姐 2021-01-02 09:15

What\'s the best way to send float, double, and int16 over serial on Arduino?

The Serial.print() only sends val

9条回答
  •  抹茶落季
    2021-01-02 10:01

    Perhaps that is best Way to convert Float to Byte and Byte to Float,-Hamid Reza.

    int breakDown(int index, unsigned char outbox[], float member)
    {
      unsigned long d = *(unsigned long *)&member;
    
      outbox[index] = d & 0x00FF;
      index++;
    
      outbox[index] = (d & 0xFF00) >> 8;
      index++;
    
      outbox[index] = (d & 0xFF0000) >> 16;
      index++;
    
      outbox[index] = (d & 0xFF000000) >> 24;
      index++;
      return index;
    }
    
    
    float buildUp(int index, unsigned char outbox[])
    {
      unsigned long d;
    
      d =  (outbox[index+3] << 24) | (outbox[index+2] << 16)
        | (outbox[index+1] << 8) | (outbox[index]);
      float member = *(float *)&d;
      return member;
    }
    

    regards. `

提交回复
热议问题