| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323 |
- // ignore_for_file: unused_import, unused_field, prefer_final_fields
- 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]),
- // );
- // },
- // ),
- // ),
- // ],
- // ),
- // ),
- // ),
- // ],
- // ),
- // ),
- ),
- );
- }
- }
|