2020-01-20 19:14发布
xf331785508 发表于 2015-1-14 17:31 好吧。正式回复一下;
arm7tdmi 发表于 2015-1-15 16:37 这种办法貌似不行啊,能得到整数部分,小数部分总是被抹去,变为0,
最多设置5个标签!
这种办法貌似不行啊,能得到整数部分,小数部分总是被抹去,变为0,
- union Data
- {
- double srcData;
- u8 memData[sizeof(srcData)];
- };
- double am = 1010.0f;
- union Data d;
- void fun(void)
- {
- unsigned char table[sizeof(am)] = {0}, i = 0, *p = NULL;
- p = (unsigned char*)&am;
- for( i = 0; i < COUNTOF(table); i++ ){
- table[i] = *(p + i);
- printf("table %u value is %u
", i, table[i]);
- }
- d.srcData = am;
- printf("%u
", d.memData[0]);
- MemToFlash(d.memData, sizeof(d));
- sendData((u8*)d.memData, sizeof(d)); // <==> sendData((unsigned char*)&am, sizeof(am));
- }
- //你的代码改一下也是等效的
- double a = 1010.001;
- unsinged char txdata[sizeof(a)] = {0};
- void func(void)
- {
- unsigned long data = 0;
- a=1010.0f; //10进制
- data = *((unsigned long*)&a);
- txdata[0] = (data >> 56) & 0xff;
- txdata[1] = (data >> 48) & 0xff;
- txdata[2] = (data >> 40) & 0xff;
- txdata[3] = (data >> 32) & 0xff;
- txdata[4] = (data >> 24) & 0xff;
- txdata[5] = (data >> 16) & 0xff;
- txdata[6] = (data >> 8) & 0xff;
- txdata[7] = a & 0xff; //发送代码
- }
复制代码另外用下面这个也行:
unsigned long float_to_bits(float x)
{
unsigned long y;
memcpy(&y, &x, 4);
return y;
}
//Modbus 报文 AE F8 41 55
unsigned char data[4] = {0xF8, 0xAE, 0x55, 0x41, };
float* f;
f = (float*)data;
printf("报文数值为: %g ", *f);
一周热门 更多>