1
This commit is contained in:
189
example/lib/main.dart
Normal file
189
example/lib/main.dart
Normal file
@@ -0,0 +1,189 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'dart:async';
|
||||
|
||||
import 'package:flutter/services.dart';
|
||||
import 'package:ch34/ch34.dart';
|
||||
|
||||
void main() {
|
||||
runApp(const MyApp());
|
||||
}
|
||||
|
||||
class MyApp extends StatefulWidget {
|
||||
const MyApp({super.key});
|
||||
|
||||
@override
|
||||
State<MyApp> createState() => _MyAppState();
|
||||
}
|
||||
|
||||
class _MyAppState extends State<MyApp> {
|
||||
String _platformVersion = 'Unknown';
|
||||
List<UsbDeviceInfo> _devices = [];
|
||||
String _status = '未连接';
|
||||
String _receivedData = '';
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
initPlatformState();
|
||||
}
|
||||
|
||||
Future<void> initPlatformState() async {
|
||||
String platformVersion;
|
||||
try {
|
||||
platformVersion =
|
||||
await Ch34Manager.getPlatformVersion() ?? 'Unknown platform version';
|
||||
} on PlatformException {
|
||||
platformVersion = 'Failed to get platform version.';
|
||||
}
|
||||
|
||||
if (!mounted) return;
|
||||
|
||||
setState(() {
|
||||
_platformVersion = platformVersion;
|
||||
});
|
||||
}
|
||||
|
||||
Future<void> _scanDevices() async {
|
||||
try {
|
||||
final devices = await Ch34Manager.enumDevice();
|
||||
setState(() {
|
||||
_devices = devices;
|
||||
_status = '发现 ${devices.length} 个设备';
|
||||
});
|
||||
} catch (e) {
|
||||
setState(() {
|
||||
_status = '扫描失败: $e';
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
Future<void> _openDevice(String deviceName) async {
|
||||
try {
|
||||
final success = await Ch34Manager.openDevice(deviceName);
|
||||
if (success) {
|
||||
await Ch34Manager.setSerialParameter(
|
||||
deviceName,
|
||||
0,
|
||||
const SerialParameter(baud: 115200),
|
||||
);
|
||||
|
||||
await Ch34Manager.registerDataCallback(
|
||||
deviceName,
|
||||
0,
|
||||
(Uint8List data) {
|
||||
final hex = data.map((b) => b.toRadixString(16).padLeft(2, '0')).join(' ');
|
||||
setState(() {
|
||||
_receivedData = '收到: $hex';
|
||||
});
|
||||
},
|
||||
);
|
||||
|
||||
setState(() {
|
||||
_status = '已连接: $deviceName';
|
||||
});
|
||||
} else {
|
||||
setState(() {
|
||||
_status = '打开失败';
|
||||
});
|
||||
}
|
||||
} catch (e) {
|
||||
setState(() {
|
||||
_status = '连接失败: $e';
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
Future<void> _sendData(String deviceName) async {
|
||||
try {
|
||||
final data = Uint8List.fromList([0x01, 0x02, 0x03]);
|
||||
final sent = await Ch34Manager.writeData(deviceName, 0, data);
|
||||
setState(() {
|
||||
_status = '已发送 $sent 字节';
|
||||
});
|
||||
} catch (e) {
|
||||
setState(() {
|
||||
_status = '发送失败: $e';
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
Future<void> _disconnect(String deviceName) async {
|
||||
try {
|
||||
Ch34Manager.removeDataCallback(deviceName);
|
||||
await Ch34Manager.disconnect(deviceName);
|
||||
setState(() {
|
||||
_status = '已断开';
|
||||
});
|
||||
} catch (e) {
|
||||
setState(() {
|
||||
_status = '断开失败: $e';
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return MaterialApp(
|
||||
home: Scaffold(
|
||||
appBar: AppBar(
|
||||
title: const Text('CH34X 示例'),
|
||||
),
|
||||
body: SingleChildScrollView(
|
||||
padding: const EdgeInsets.all(16),
|
||||
child: Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
Text('平台版本: $_platformVersion\n'),
|
||||
Text('状态: $_status\n'),
|
||||
if (_receivedData.isNotEmpty)
|
||||
Text('接收数据: $_receivedData\n'),
|
||||
const SizedBox(height: 16),
|
||||
ElevatedButton(
|
||||
onPressed: _scanDevices,
|
||||
child: const Text('扫描设备'),
|
||||
),
|
||||
const SizedBox(height: 8),
|
||||
if (_devices.isNotEmpty)
|
||||
ListView.builder(
|
||||
shrinkWrap: true,
|
||||
physics: const NeverScrollableScrollPhysics(),
|
||||
itemCount: _devices.length,
|
||||
itemBuilder: (context, index) {
|
||||
final device = _devices[index];
|
||||
return ListTile(
|
||||
title: Text('${device.deviceName}'),
|
||||
subtitle: Text(
|
||||
'VID: 0x${device.vendorId.toRadixString(16).toUpperCase()} '
|
||||
'PID: 0x${device.productId.toRadixString(16).toUpperCase()} '
|
||||
'端口: ${device.serialCount}'),
|
||||
trailing: Row(
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
children: [
|
||||
IconButton(
|
||||
icon: const Icon(Icons.power),
|
||||
onPressed: () => _openDevice(device.deviceName),
|
||||
),
|
||||
IconButton(
|
||||
icon: const Icon(Icons.send),
|
||||
onPressed: _status.contains('已连接')
|
||||
? () => _sendData(device.deviceName)
|
||||
: null,
|
||||
),
|
||||
IconButton(
|
||||
icon: const Icon(Icons.power_off),
|
||||
onPressed: _status.contains('已连接')
|
||||
? () => _disconnect(device.deviceName)
|
||||
: null,
|
||||
),
|
||||
],
|
||||
),
|
||||
);
|
||||
},
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user