|
|
@@ -0,0 +1,321 @@
|
|
|
+import 'dart:typed_data';
|
|
|
+import 'package:chicken_farm/components/vb_app_bar.dart';
|
|
|
+import 'package:chicken_farm/core/utils/toast.dart';
|
|
|
+import 'package:flutter/material.dart';
|
|
|
+
|
|
|
+class SerialSettingPage extends StatefulWidget {
|
|
|
+ const SerialSettingPage({super.key});
|
|
|
+
|
|
|
+ @override
|
|
|
+ State<SerialSettingPage> createState() => _SerialSettingPageState();
|
|
|
+}
|
|
|
+
|
|
|
+class _SerialSettingPageState extends State<SerialSettingPage> {
|
|
|
+ // final SerialPortService _serialService = SerialPortService();
|
|
|
+ List<String> _availablePorts = [];
|
|
|
+ String? _selectedPort;
|
|
|
+ final TextEditingController _baudRateController = TextEditingController(
|
|
|
+ text: '9600',
|
|
|
+ );
|
|
|
+ final TextEditingController _dataController = TextEditingController();
|
|
|
+ final List<String> _receivedData = [];
|
|
|
+ bool _isConnected = false;
|
|
|
+
|
|
|
+ @override
|
|
|
+ void initState() {
|
|
|
+ super.initState();
|
|
|
+ // _loadAvailablePorts();
|
|
|
+ // _listenToSerialEvents();
|
|
|
+ }
|
|
|
+
|
|
|
+ // /// 加载可用串口列表
|
|
|
+ // void _loadAvailablePorts() {
|
|
|
+ // setState(() {
|
|
|
+ // _availablePorts = SerialPortService.getAvailablePorts();
|
|
|
+ // if (_availablePorts.isNotEmpty) {
|
|
|
+ // _selectedPort = _availablePorts.first;
|
|
|
+ // }
|
|
|
+ // });
|
|
|
+ // }
|
|
|
+
|
|
|
+ // /// 监听串口事件
|
|
|
+ // void _listenToSerialEvents() {
|
|
|
+ // _serialService.events.listen((event) {
|
|
|
+ // switch (event.type) {
|
|
|
+ // case VbSerialPortEventType.statusChanged:
|
|
|
+ // final status = event.data as VbSerialPortStatus;
|
|
|
+ // setState(() {
|
|
|
+ // _isConnected = status == VbSerialPortStatus.opened;
|
|
|
+ // });
|
|
|
+ // break;
|
|
|
+ // case VbSerialPortEventType.dataReceived:
|
|
|
+ // final data = event.data as Uint8List;
|
|
|
+ // final text = String.fromCharCodes(data);
|
|
|
+ // setState(() {
|
|
|
+ // _receivedData.add('RX: $text');
|
|
|
+ // if (_receivedData.length > 100) {
|
|
|
+ // _receivedData.removeAt(0);
|
|
|
+ // }
|
|
|
+ // });
|
|
|
+ // break;
|
|
|
+ // case VbSerialPortEventType.errorOccurred:
|
|
|
+ // final errorMsg = event.errorMessage ?? '未知错误';
|
|
|
+ // setState(() {
|
|
|
+ // _receivedData.add('ERR: $errorMsg');
|
|
|
+ // if (_receivedData.length > 100) {
|
|
|
+ // _receivedData.removeAt(0);
|
|
|
+ // }
|
|
|
+ // });
|
|
|
+ // break;
|
|
|
+ // }
|
|
|
+ // });
|
|
|
+ // }
|
|
|
+
|
|
|
+ // /// 连接/断开串口
|
|
|
+ // void _toggleConnection() async {
|
|
|
+ // if (!_isConnected) {
|
|
|
+ // // 连接串口
|
|
|
+ // if (_selectedPort == null) {
|
|
|
+ // ToastUtil.warning("请选择串口");
|
|
|
+ // return;
|
|
|
+ // }
|
|
|
+
|
|
|
+ // final baudRate = int.tryParse(_baudRateController.text) ?? 9600;
|
|
|
+ // final config = VbSerialPortConfig(baudRate: baudRate);
|
|
|
+
|
|
|
+ // final result = await _serialService.openPort(_selectedPort!, config);
|
|
|
+ // if (!result) {
|
|
|
+ // ToastUtil.errorB("连接串口失败");
|
|
|
+ // }
|
|
|
+ // } else {
|
|
|
+ // // 断开串口
|
|
|
+ // await _serialService.closePort();
|
|
|
+ // }
|
|
|
+ // }
|
|
|
+
|
|
|
+ // /// 发送数据
|
|
|
+ // void _sendData() async {
|
|
|
+ // if (!_isConnected) {
|
|
|
+ // ToastUtil.errorB("请先连接串口");
|
|
|
+ // return;
|
|
|
+ // }
|
|
|
+
|
|
|
+ // final data = _dataController.text;
|
|
|
+ // if (data.isEmpty) {
|
|
|
+ // ToastUtil.warning("请输入要发送的数据");
|
|
|
+ // return;
|
|
|
+ // }
|
|
|
+
|
|
|
+ // final result = await _serialService.sendString(data);
|
|
|
+ // if (result) {
|
|
|
+ // setState(() {
|
|
|
+ // _receivedData.add('TX: $data');
|
|
|
+ // if (_receivedData.length > 100) {
|
|
|
+ // _receivedData.removeAt(0);
|
|
|
+ // }
|
|
|
+ // });
|
|
|
+ // _dataController.clear();
|
|
|
+ // } else {
|
|
|
+ // ToastUtil.errorB("发送数据失败");
|
|
|
+ // }
|
|
|
+ // }
|
|
|
+
|
|
|
+ // /// 清空接收区
|
|
|
+ // void _clearReceivedData() {
|
|
|
+ // setState(() {
|
|
|
+ // _receivedData.clear();
|
|
|
+ // });
|
|
|
+ // }
|
|
|
+
|
|
|
+ @override
|
|
|
+ Widget build(BuildContext context) {
|
|
|
+ return Scaffold(
|
|
|
+ appBar: VberAppBar(title: '串口调试工具'),
|
|
|
+ body: Padding(
|
|
|
+ padding: const EdgeInsets.all(16.0),
|
|
|
+ // child: SingleChildScrollView(
|
|
|
+ // physics: const BouncingScrollPhysics(),
|
|
|
+ // child: Column(
|
|
|
+ // crossAxisAlignment: CrossAxisAlignment.start,
|
|
|
+ // children: [
|
|
|
+ // // 串口配置区域
|
|
|
+ // Card(
|
|
|
+ // child: Padding(
|
|
|
+ // padding: const EdgeInsets.all(16.0),
|
|
|
+ // child: Column(
|
|
|
+ // crossAxisAlignment: CrossAxisAlignment.start,
|
|
|
+ // children: [
|
|
|
+ // const Text(
|
|
|
+ // '串口配置',
|
|
|
+ // style: TextStyle(
|
|
|
+ // fontSize: 18,
|
|
|
+ // fontWeight: FontWeight.bold,
|
|
|
+ // ),
|
|
|
+ // ),
|
|
|
+ // const SizedBox(height: 10),
|
|
|
+ // Row(
|
|
|
+ // children: [
|
|
|
+ // const Text('串口:'),
|
|
|
+ // const SizedBox(width: 10),
|
|
|
+ // DropdownButton<String>(
|
|
|
+ // value: _selectedPort,
|
|
|
+ // items: _availablePorts.map((port) {
|
|
|
+ // return DropdownMenuItem(
|
|
|
+ // value: port,
|
|
|
+ // child: Text(port),
|
|
|
+ // );
|
|
|
+ // }).toList(),
|
|
|
+ // onChanged: (value) {
|
|
|
+ // setState(() {
|
|
|
+ // _selectedPort = value;
|
|
|
+ // });
|
|
|
+ // },
|
|
|
+ // hint: const Text('选择串口'),
|
|
|
+ // ),
|
|
|
+ // const SizedBox(width: 20),
|
|
|
+ // SizedBox(
|
|
|
+ // height: 30,
|
|
|
+ // child: ElevatedButton(
|
|
|
+ // onPressed: _loadAvailablePorts,
|
|
|
+ // child: const Text('刷新'),
|
|
|
+ // ),
|
|
|
+ // ),
|
|
|
+
|
|
|
+ // const SizedBox(width: 20),
|
|
|
+
|
|
|
+ // const Text('波特率:'),
|
|
|
+ // const SizedBox(width: 10),
|
|
|
+ // SizedBox(
|
|
|
+ // width: 100,
|
|
|
+ // height: 40,
|
|
|
+ // child: TextField(
|
|
|
+ // controller: _baudRateController,
|
|
|
+ // keyboardType: TextInputType.number,
|
|
|
+ // decoration: const InputDecoration(
|
|
|
+ // border: OutlineInputBorder(),
|
|
|
+ // ),
|
|
|
+ // ),
|
|
|
+ // ),
|
|
|
+ // const SizedBox(width: 20),
|
|
|
+ // SizedBox(
|
|
|
+ // height: 30,
|
|
|
+ // child: ElevatedButton(
|
|
|
+ // onPressed: _toggleConnection,
|
|
|
+ // child: Text(
|
|
|
+ // _isConnected ? '断开' : '连接',
|
|
|
+ // style: TextStyle(
|
|
|
+ // color: _isConnected
|
|
|
+ // ? Colors.red
|
|
|
+ // : Colors.green,
|
|
|
+ // ),
|
|
|
+ // ),
|
|
|
+ // ),
|
|
|
+ // ),
|
|
|
+ // ],
|
|
|
+ // ),
|
|
|
+ // ],
|
|
|
+ // ),
|
|
|
+ // ),
|
|
|
+ // ),
|
|
|
+
|
|
|
+ // const SizedBox(height: 20),
|
|
|
+
|
|
|
+ // // 数据发送区域
|
|
|
+ // Card(
|
|
|
+ // child: Padding(
|
|
|
+ // padding: const EdgeInsets.all(16.0),
|
|
|
+ // child: Column(
|
|
|
+ // crossAxisAlignment: CrossAxisAlignment.start,
|
|
|
+ // children: [
|
|
|
+ // const Text(
|
|
|
+ // '数据发送',
|
|
|
+ // style: TextStyle(
|
|
|
+ // fontSize: 18,
|
|
|
+ // fontWeight: FontWeight.bold,
|
|
|
+ // ),
|
|
|
+ // ),
|
|
|
+ // const SizedBox(height: 10),
|
|
|
+ // Row(
|
|
|
+ // children: [
|
|
|
+ // Expanded(
|
|
|
+ // child: SizedBox(
|
|
|
+ // height: 50,
|
|
|
+ // child: TextField(
|
|
|
+ // controller: _dataController,
|
|
|
+ // decoration: const InputDecoration(
|
|
|
+ // hintText: '输入要发送的数据',
|
|
|
+ // border: OutlineInputBorder(),
|
|
|
+ // ),
|
|
|
+ // ),
|
|
|
+ // ),
|
|
|
+ // ),
|
|
|
+ // const SizedBox(width: 10),
|
|
|
+ // SizedBox(
|
|
|
+ // height: 50,
|
|
|
+ // child: ElevatedButton(
|
|
|
+ // onPressed: _sendData,
|
|
|
+ // child: const Text('发送'),
|
|
|
+ // ),
|
|
|
+ // ),
|
|
|
+ // ],
|
|
|
+ // ),
|
|
|
+ // ],
|
|
|
+ // ),
|
|
|
+ // ),
|
|
|
+ // ),
|
|
|
+
|
|
|
+ // const SizedBox(height: 20),
|
|
|
+
|
|
|
+ // // 数据接收区域
|
|
|
+ // Card(
|
|
|
+ // child: Padding(
|
|
|
+ // padding: const EdgeInsets.all(16.0),
|
|
|
+ // child: Column(
|
|
|
+ // crossAxisAlignment: CrossAxisAlignment.start,
|
|
|
+ // children: [
|
|
|
+ // Row(
|
|
|
+ // mainAxisAlignment: MainAxisAlignment.spaceBetween,
|
|
|
+ // children: [
|
|
|
+ // const Text(
|
|
|
+ // '数据接收',
|
|
|
+ // style: TextStyle(
|
|
|
+ // fontSize: 18,
|
|
|
+ // fontWeight: FontWeight.bold,
|
|
|
+ // ),
|
|
|
+ // ),
|
|
|
+ // ElevatedButton(
|
|
|
+ // onPressed: _clearReceivedData,
|
|
|
+ // child: const Text('清空'),
|
|
|
+ // ),
|
|
|
+ // ],
|
|
|
+ // ),
|
|
|
+ // const SizedBox(height: 10),
|
|
|
+ // Container(
|
|
|
+ // height: 200,
|
|
|
+ // decoration: BoxDecoration(
|
|
|
+ // border: Border.all(color: Colors.grey),
|
|
|
+ // ),
|
|
|
+ // child: ListView.builder(
|
|
|
+ // itemCount: _receivedData.length,
|
|
|
+ // itemBuilder: (context, index) {
|
|
|
+ // return Padding(
|
|
|
+ // padding: const EdgeInsets.symmetric(
|
|
|
+ // horizontal: 8.0,
|
|
|
+ // vertical: 4.0,
|
|
|
+ // ),
|
|
|
+ // child: Text(_receivedData[index]),
|
|
|
+ // );
|
|
|
+ // },
|
|
|
+ // ),
|
|
|
+ // ),
|
|
|
+ // ],
|
|
|
+ // ),
|
|
|
+ // ),
|
|
|
+ // ),
|
|
|
+ // ],
|
|
|
+ // ),
|
|
|
+ // ),
|
|
|
+ ),
|
|
|
+ );
|
|
|
+ }
|
|
|
+}
|