実現したいこと
現在、Windowsアプリでシリアル通信を行うために、 package usb_serial を使用しての機能の実現を目指しております。
前提
https://github.com/altera2015/usbserial
そこで、上記URLにあるソースの"main.dart","pubspec.yaml"をコピペし、実行を行ったところ、以下のようなエラーが出ました
発生している問題・エラーメッセージ
Resolving dependencies... collection 1.17.2 (1.18.0 available) material_color_utilities 0.5.0 (0.8.0 available) Got dependencies! Launching lib\main.dart on Windows in debug mode... Building Windows application... √ Built build\windows\runner\Debug\serial_comm.exe. Debug service listening on ws://127.0.0.1:51368/fmj8FEKR9Cw=/ws Syncing files to device Windows... [ERROR:flutter/runtime/dart_vm_initializer.cc(41)] Unhandled Exception: MissingPluginException(No implementation found for method listDevices on channel usb_serial) #0 MethodChannel._invokeMethod (package:flutter/src/services/platform_channel.dart:313:7) <asynchronous suspension> #1 UsbSerial.listDevices (package:usb_serial/usb_serial.dart:444:29) <asynchronous suspension> #2 _MyAppState._getPorts (package:usb_serial_example/main.dart:85:31) <asynchronous suspension> ======== Exception caught by services library ====================================================== The following MissingPluginException was thrown while activating platform stream on channel usb_serial/usb_events: MissingPluginException(No implementation found for method listen on channel usb_serial/usb_events) When the exception was thrown, this was the stack: #0 MethodChannel._invokeMethod (package:flutter/src/services/platform_channel.dart:313:7) <asynchronous suspension> #1 EventChannel.receiveBroadcastStream.<anonymous closure> (package:flutter/src/services/platform_channel.dart:657:9) <asynchronous suspension> ==================================================================================================== Lost connection to device.
該当のソースコード
main.dart
1import 'dart:async'; 2import 'dart:typed_data'; 3 4import 'package:flutter/material.dart'; 5import 'package:usb_serial/transaction.dart'; 6import 'package:usb_serial/usb_serial.dart'; 7 8void main() => runApp(MyApp()); 9 10class MyApp extends StatefulWidget { 11 @override 12 _MyAppState createState() => _MyAppState(); 13} 14 15class _MyAppState extends State<MyApp> { 16 UsbPort? _port; 17 String _status = "Idle"; 18 List<Widget> _ports = []; 19 List<Widget> _serialData = []; 20 21 StreamSubscription<String>? _subscription; 22 Transaction<String>? _transaction; 23 UsbDevice? _device; 24 25 TextEditingController _textController = TextEditingController(); 26 27 Future<bool> _connectTo(device) async { 28 _serialData.clear(); 29 30 if (_subscription != null) { 31 _subscription!.cancel(); 32 _subscription = null; 33 } 34 35 if (_transaction != null) { 36 _transaction!.dispose(); 37 _transaction = null; 38 } 39 40 if (_port != null) { 41 _port!.close(); 42 _port = null; 43 } 44 45 if (device == null) { 46 _device = null; 47 setState(() { 48 _status = "Disconnected"; 49 }); 50 return true; 51 } 52 53 _port = await device.create(); 54 if (await (_port!.open()) != true) { 55 setState(() { 56 _status = "Failed to open port"; 57 }); 58 return false; 59 } 60 _device = device; 61 62 await _port!.setDTR(true); 63 await _port!.setRTS(true); 64 await _port!.setPortParameters(115200, UsbPort.DATABITS_8, UsbPort.STOPBITS_1, UsbPort.PARITY_NONE); 65 66 _transaction = Transaction.stringTerminated(_port!.inputStream as Stream<Uint8List>, Uint8List.fromList([13, 10])); 67 68 _subscription = _transaction!.stream.listen((String line) { 69 setState(() { 70 _serialData.add(Text(line)); 71 if (_serialData.length > 20) { 72 _serialData.removeAt(0); 73 } 74 }); 75 }); 76 77 setState(() { 78 _status = "Connected"; 79 }); 80 return true; 81 } 82 83 void _getPorts() async { 84 _ports = []; 85 List<UsbDevice> devices = await UsbSerial.listDevices(); 86 if (!devices.contains(_device)) { 87 _connectTo(null); 88 } 89 print(devices); 90 91 devices.forEach((device) { 92 _ports.add(ListTile( 93 leading: Icon(Icons.usb), 94 title: Text(device.productName!), 95 subtitle: Text(device.manufacturerName!), 96 trailing: ElevatedButton( 97 child: Text(_device == device ? "Disconnect" : "Connect"), 98 onPressed: () { 99 _connectTo(_device == device ? null : device).then((res) { 100 _getPorts(); 101 }); 102 }, 103 ))); 104 }); 105 106 setState(() { 107 print(_ports); 108 }); 109 } 110 111 @override 112 void initState() { 113 super.initState(); 114 115 UsbSerial.usbEventStream!.listen((UsbEvent event) { 116 _getPorts(); 117 }); 118 119 _getPorts(); 120 } 121 122 @override 123 void dispose() { 124 super.dispose(); 125 _connectTo(null); 126 } 127 128 @override 129 Widget build(BuildContext context) { 130 return MaterialApp( 131 home: Scaffold( 132 appBar: AppBar( 133 title: const Text('USB Serial Plugin example app'), 134 ), 135 body: Center( 136 child: Column(children: <Widget>[ 137 Text(_ports.length > 0 ? "Available Serial Ports" : "No serial devices available", style: Theme.of(context).textTheme.headline6), 138 ..._ports, 139 Text('Status: $_status\n'), 140 Text('info: ${_port.toString()}\n'), 141 ListTile( 142 title: TextField( 143 controller: _textController, 144 decoration: InputDecoration( 145 border: OutlineInputBorder(), 146 labelText: 'Text To Send', 147 ), 148 ), 149 trailing: ElevatedButton( 150 child: Text("Send"), 151 onPressed: _port == null 152 ? null 153 : () async { 154 if (_port == null) { 155 return; 156 } 157 String data = _textController.text + "\r\n"; 158 await _port!.write(Uint8List.fromList(data.codeUnits)); 159 _textController.text = ""; 160 }, 161 ), 162 ), 163 Text("Result Data", style: Theme.of(context).textTheme.headline6), 164 ..._serialData, 165 ])), 166 )); 167 } 168}
pubspec.yaml
1name: usb_serial_example 2description: Demonstrates how to use the usb_serial plugin. 3 4# The following line prevents the package from being accidentally published to 5# pub.dev using `pub publish`. This is preferred for private packages. 6publish_to: 'none' # Remove this line if you wish to publish to pub.dev 7 8# The following defines the version and build number for your application. 9# A version number is three numbers separated by dots, like 1.2.43 10# followed by an optional build number separated by a +. 11# Both the version and the builder number may be overridden in flutter 12# build by specifying --build-name and --build-number, respectively. 13# In Android, build-name is used as versionName while build-number used as versionCode. 14# Read more about Android versioning at https://developer.android.com/studio/publish/versioning 15# In iOS, build-name is used as CFBundleShortVersionString while build-number used as CFBundleVersion. 16# Read more about iOS versioning at 17# https://developer.apple.com/library/archive/documentation/General/Reference/InfoPlistKeyReference/Articles/CoreFoundationKeys.html 18version: 1.0.0+1 19 20environment: 21 sdk: '>=2.12.0 <3.0.0' 22 23dependencies: 24 flutter: 25 sdk: flutter 26 async: ^2.5.0 27 28 29 30 # The following adds the Cupertino Icons font to your application. 31 # Use with the CupertinoIcons class for iOS style icons. 32 cupertino_icons: ^1.0.3 33 usb_serial: ^0.4.0 34 35dev_dependencies: 36 flutter_test: 37 sdk: flutter 38 39 usb_serial: ^0.4.0 40# For information on the generic Dart part of this file, see the 41# following page: https://dart.dev/tools/pub/pubspec 42 43# The following section is specific to Flutter. 44flutter: 45 46 # The following line ensures that the Material Icons font is 47 # included with your application, so that you can use the icons in 48 # the material Icons class. 49 uses-material-design: true 50 51 # To add assets to your application, add an assets section, like this: 52 # assets: 53 # - images/a_dot_burr.jpeg 54 # - images/a_dot_ham.jpeg 55 56 # An image asset can refer to one or more resolution-specific "variants", see 57 # https://flutter.dev/assets-and-images/#resolution-aware. 58 59 # For details regarding adding assets from package dependencies, see 60 # https://flutter.dev/assets-and-images/#from-packages 61 62 # To add custom fonts to your application, add a fonts section here, 63 # in this "flutter" section. Each entry in this list should have a 64 # "family" key with the font family name, and a "fonts" key with a 65 # list giving the asset and other descriptors for the font. For 66 # example: 67 # fonts: 68 # - family: Schyler 69 # fonts: 70 # - asset: fonts/Schyler-Regular.ttf 71 # - asset: fonts/Schyler-Italic.ttf 72 # style: italic 73 # - family: Trajan Pro 74 # fonts: 75 # - asset: fonts/TrajanPro.ttf 76 # - asset: fonts/TrajanPro_Bold.ttf 77 # weight: 700 78 # 79 # For details regarding fonts from package dependencies, 80 # see https://flutter.dev/custom-fonts/#from-packages
試したこと
対策として行ったことは、"flutter clean" → "flutter pub get" → "flutter run" 後に実行を行いましたが、状況は変わりません。
Android開発用のSDKなどの影響(影響あるのかわからない)も考えて、開発環境の初期化も行いました。
補足情報(FW/ツールのバージョンなど)
そもそもpackageを使用することができるのかを確認したところ、"syncfusion_flutter_charts"というグラフを描写するためのpackageなどは使用可能でした。
Bluetooth開発を行おうとpackageを使用したときもawaitを使用した行でエラーが発生しました。
以上よろしくお願い致します。
回答1件
あなたの回答
tips
プレビュー