英文:
Different values being returned on Flutter Blue Plus
问题
我正在使用flutter_blue_plus的示例代码。我可以连接到设备并将数据流读取为数组:
[90, 0, 0, 6, 13, 0, 0, 0, 0, 0, 0, 0, 37, 5, 0, 0, 0, 0, 0, 0, 0, 0, 45, 5, 49, 0, 0, 0, 0, 0, 0, 0, 3, 2, 0, 0, 0, 91, 118, 90]
void onNewReceivedData(List<int> data) {
_numberOfMessagesReceived += 1;
_receivedData.add( "$_numberOfMessagesReceived: $data" as int);
if (_receivedData.length > 5) {
_receivedData.removeAt(0);
}
print(_receivedData.join("\n"));
refreshScreen();
}
但这些数字不正确。我已经联系了我尝试使用的蓝牙模块制造商,他告诉我这是正确的,并发送了这个:
低字节 45 = 0x2D
高字节 05 = 0x05
0x052D = 1325 = 13.2伏
制造商是中国人,我们陷入了巨大的语言障碍;我一直在使用谷歌翻译来翻译他的电子邮件,但大部分对我来说毫无意义。你知道他在说什么吗?
谢谢!
丹
英文:
I'm using the example code from the flutter_blue_plus. I can connect to the device and read the data stream as an array:
[90, 0, 0, 6, 13, 0, 0, 0, 0, 0, 0, 0, 37, 5, 0, 0, 0, 0, 0, 0, 0, 0, 45, 5, 49, 0, 0, 0, 0, 0, 0, 0, 3, 2, 0, 0, 0, 91, 118, 90]
void onNewReceivedData(List<int> data) {
_numberOfMessagesReceived += 1;
_receivedData.add( "$_numberOfMessagesReceived: $data" as int);
if (_receivedData.length > 5) {
_receivedData.removeAt(0);
}
print(_receivedData.join("\n"));
refreshScreen();
}
But they are not the correct numbers. I've contacted the manufacturer of the Bluetooth module I'm trying to use, and he told me It's correct and sent me this:
Low byte 45 = 0x2D
High byte 05 = 0x05
0x052D = 1325 = 13.2V
The manufacturer is Chinese, and we are stuck in a big language barrier here; I've been using google translate to translate his emails, but most of it makes no sense to me. Do you happen to know what he is talking about?
Thanks!
Dan
答案1
得分: 1
我没有理解让你困惑的部分。您正在尝试从16位小端值构建一个值。我猜测结果以分厘伏(1/100伏特)为单位。(如果您能提供这是使用哪个服务,回答会更容易。)
如果您已经提取了值45和5,那么这是如何将其转换为完整值的方法:
const low = 45;
const high = 5;
const volts = ((high << 8) + low) / 100; // 13.25
这将high
左移8位(使其成为最高有效字节),然后加上low
(作为最低有效字节),最后将整个值除以100以获得伏特。
这些值是以十六进制还是十进制表示没有实际意义。这只是不同的数字书写方式,不影响数字本身的含义。
另请参阅https://stackoverflow.com/questions/65445283/dart-convert-two-uint8-to-uint16-that-are-little-endian,了解如何直接从Uint8List进行此操作。
英文:
I didn't understand the part that was confusing you. You're trying to build a value from a 16-bit little-endian value. I'm guessing the result is then in centivolts (1/100 volt). (It would be much easier to answer if you would include what service this is using.)
If you have already pulled out the values, 45 and 5, then this is how you turn that into a complete value:
const low = 45;
const high = 5;
const volts = ((high << 8) + low) / 100; // 13.25
This shifts high
left 8 bits (to make it the most-significant byte), adds low
(as the least-significant byte), and divides the entire value by 100 to get volts.
There is no significance to whether these values are represented in hexadecimal or decimal. Those are just different ways of writing down numbers. It has no impact on what the numbers actually are.
See also https://stackoverflow.com/questions/65445283/dart-convert-two-uint8-to-uint16-that-are-little-endian for how to do this directly out of a Uint8List.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论