我正在使用顫振藍牙串行包從微控制器接收資料。當我從微控制器發送整數值時,我得到以下形式的值: [64], [144, 22]] 串列中的最后一個數字(22)是我發送的實際數字。但是,每次我發送任何值時,我都會得到這兩個值 [64]、[144、...]。另一件事是當我從微控制器發送一個浮點值時,我將它作為 int 接收,當我嘗試將資料型別轉換為雙精度時,我收到如下錯誤:
引數型別“void Function(double)”不能分配給引數型別“void Function(Uint8List)?”。
引數型別“double”不能分配給引數型別“List”。
下面是出現第一個錯誤的代碼片段
List<List<double>> chunks = <List<double>>[];
_getBTConnection(){
BluetoothConnection.toAddress(widget.server.address).then((_connection){
connection = _connection;
isConnecting = false;
isDisconnecting = false;
setState(() {});
connection.input?.listen(_onDataReceived).onDone(() {
if(isDisconnecting){
print("Disconnecting locally");
}else{
print("Disconnecting remotely");
}
if(mounted){
setState(() {});
}
Navigator.of(context).pop();
});
}).catchError((error){
Navigator.of(context).pop();
});
}
以下是出現的第二個錯誤的片段:
void _onDataReceived(double data){
if(data != null && data > 0){
chunks.add(data);
}
if (kDebugMode) {
print(" chunks: , $chunks " );
}
}
uj5u.com熱心網友回復:
嘗試此代碼轉換Uint8List為List<double>
List<double> convertUint8ListToDoubleList(Uint8List uint8list) {
var bdata = ByteData.view(uint8list.buffer);
return List.generate(
(uint8list.length / 8).round(), (index) => bdata.getFloat64(index * 8));
}
您的流中的資料型別是 Uint8List 嗎?如果是Uint8List,你試過這段代碼嗎?
void _onDataReceived(Uint8List data){
if(data != null){
chunks.add(convertUint8ListToDoubleList(data));
}
if (kDebugMode) {
print(" chunks: , $chunks " );
}
}
如果您List<double>chunks=[]使用List<double> convertUint8ListToDoubleList(Uint8List uint8list).
uj5u.com熱心網友回復:
如果您在 a 中有一個 8(8 位)位元組序列Uint8List,您可以獲得 aByteBuffer或ByteData它的視圖以將這些位元組決議為 64 位double。
例如:
import 'dart:typed_data';
void main() {
// Big-endian byte sequence for pi.
// See <https://en.wikipedia.org/wiki/Double-precision_floating-point_format>
var bytes =
Uint8List.fromList([0x40, 0x09, 0x21, 0xFB, 0x54, 0x44, 0x2D, 0x18]);
var doubleValue = bytes.buffer.asByteData().getFloat64(0);
print(doubleValue); // Prints: 3.141592653589793
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/481622.html
