logo

串口uart如何发送小数

2020-10-12 14:06
I
pm1f

uart.write()怎么才

能发送小数,例如uart.write(13.468946)可以吗

2020-10-12 15:10
I
网红小智智 星瞳科技

uart.write里面应该是字节传。

串口发送的是字节。

你的思路应该是,怎么把小数转为字节。

1,直接转为字符串,uart.write(str(13.468946)[0:4])。缺点:截取到长度4,而且字符串可能不好转换。

2,使用struct.pack('<f',13.468946)。小端,四字节float。https://cn.bing.com/search?q=python3+struct.pack&qs=n&form=QBRE&sp=-1&pq=python3+struct.pack&sc=1-19&sk=&cvid=CDDA981B461A4729922E0C09E82933B8

import struct
struct.pack('<f',13.468946)
b'\xce\x80WA'