serial_setting_page.dart 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  1. // ignore_for_file: unused_import, unused_field, prefer_final_fields
  2. import 'dart:typed_data';
  3. import 'package:chicken_farm/components/vb_app_bar.dart';
  4. import 'package:chicken_farm/core/utils/toast.dart';
  5. import 'package:flutter/material.dart';
  6. class SerialSettingPage extends StatefulWidget {
  7. const SerialSettingPage({super.key});
  8. @override
  9. State<SerialSettingPage> createState() => _SerialSettingPageState();
  10. }
  11. class _SerialSettingPageState extends State<SerialSettingPage> {
  12. // final SerialPortService _serialService = SerialPortService();
  13. List<String> _availablePorts = [];
  14. String? _selectedPort;
  15. final TextEditingController _baudRateController = TextEditingController(
  16. text: '9600',
  17. );
  18. final TextEditingController _dataController = TextEditingController();
  19. final List<String> _receivedData = [];
  20. bool _isConnected = false;
  21. @override
  22. void initState() {
  23. super.initState();
  24. // _loadAvailablePorts();
  25. // _listenToSerialEvents();
  26. }
  27. // /// 加载可用串口列表
  28. // void _loadAvailablePorts() {
  29. // setState(() {
  30. // _availablePorts = SerialPortService.getAvailablePorts();
  31. // if (_availablePorts.isNotEmpty) {
  32. // _selectedPort = _availablePorts.first;
  33. // }
  34. // });
  35. // }
  36. // /// 监听串口事件
  37. // void _listenToSerialEvents() {
  38. // _serialService.events.listen((event) {
  39. // switch (event.type) {
  40. // case VbSerialPortEventType.statusChanged:
  41. // final status = event.data as VbSerialPortStatus;
  42. // setState(() {
  43. // _isConnected = status == VbSerialPortStatus.opened;
  44. // });
  45. // break;
  46. // case VbSerialPortEventType.dataReceived:
  47. // final data = event.data as Uint8List;
  48. // final text = String.fromCharCodes(data);
  49. // setState(() {
  50. // _receivedData.add('RX: $text');
  51. // if (_receivedData.length > 100) {
  52. // _receivedData.removeAt(0);
  53. // }
  54. // });
  55. // break;
  56. // case VbSerialPortEventType.errorOccurred:
  57. // final errorMsg = event.errorMessage ?? '未知错误';
  58. // setState(() {
  59. // _receivedData.add('ERR: $errorMsg');
  60. // if (_receivedData.length > 100) {
  61. // _receivedData.removeAt(0);
  62. // }
  63. // });
  64. // break;
  65. // }
  66. // });
  67. // }
  68. // /// 连接/断开串口
  69. // void _toggleConnection() async {
  70. // if (!_isConnected) {
  71. // // 连接串口
  72. // if (_selectedPort == null) {
  73. // ToastUtil.warning("请选择串口");
  74. // return;
  75. // }
  76. // final baudRate = int.tryParse(_baudRateController.text) ?? 9600;
  77. // final config = VbSerialPortConfig(baudRate: baudRate);
  78. // final result = await _serialService.openPort(_selectedPort!, config);
  79. // if (!result) {
  80. // ToastUtil.errorB("连接串口失败");
  81. // }
  82. // } else {
  83. // // 断开串口
  84. // await _serialService.closePort();
  85. // }
  86. // }
  87. // /// 发送数据
  88. // void _sendData() async {
  89. // if (!_isConnected) {
  90. // ToastUtil.errorB("请先连接串口");
  91. // return;
  92. // }
  93. // final data = _dataController.text;
  94. // if (data.isEmpty) {
  95. // ToastUtil.warning("请输入要发送的数据");
  96. // return;
  97. // }
  98. // final result = await _serialService.sendString(data);
  99. // if (result) {
  100. // setState(() {
  101. // _receivedData.add('TX: $data');
  102. // if (_receivedData.length > 100) {
  103. // _receivedData.removeAt(0);
  104. // }
  105. // });
  106. // _dataController.clear();
  107. // } else {
  108. // ToastUtil.errorB("发送数据失败");
  109. // }
  110. // }
  111. // /// 清空接收区
  112. // void _clearReceivedData() {
  113. // setState(() {
  114. // _receivedData.clear();
  115. // });
  116. // }
  117. @override
  118. Widget build(BuildContext context) {
  119. return Scaffold(
  120. appBar: VberAppBar(title: '串口调试工具'),
  121. body: Padding(
  122. padding: const EdgeInsets.all(16.0),
  123. // child: SingleChildScrollView(
  124. // physics: const BouncingScrollPhysics(),
  125. // child: Column(
  126. // crossAxisAlignment: CrossAxisAlignment.start,
  127. // children: [
  128. // // 串口配置区域
  129. // Card(
  130. // child: Padding(
  131. // padding: const EdgeInsets.all(16.0),
  132. // child: Column(
  133. // crossAxisAlignment: CrossAxisAlignment.start,
  134. // children: [
  135. // const Text(
  136. // '串口配置',
  137. // style: TextStyle(
  138. // fontSize: 18,
  139. // fontWeight: FontWeight.bold,
  140. // ),
  141. // ),
  142. // const SizedBox(height: 10),
  143. // Row(
  144. // children: [
  145. // const Text('串口:'),
  146. // const SizedBox(width: 10),
  147. // DropdownButton<String>(
  148. // value: _selectedPort,
  149. // items: _availablePorts.map((port) {
  150. // return DropdownMenuItem(
  151. // value: port,
  152. // child: Text(port),
  153. // );
  154. // }).toList(),
  155. // onChanged: (value) {
  156. // setState(() {
  157. // _selectedPort = value;
  158. // });
  159. // },
  160. // hint: const Text('选择串口'),
  161. // ),
  162. // const SizedBox(width: 20),
  163. // SizedBox(
  164. // height: 30,
  165. // child: ElevatedButton(
  166. // onPressed: _loadAvailablePorts,
  167. // child: const Text('刷新'),
  168. // ),
  169. // ),
  170. // const SizedBox(width: 20),
  171. // const Text('波特率:'),
  172. // const SizedBox(width: 10),
  173. // SizedBox(
  174. // width: 100,
  175. // height: 40,
  176. // child: TextField(
  177. // controller: _baudRateController,
  178. // keyboardType: TextInputType.number,
  179. // decoration: const InputDecoration(
  180. // border: OutlineInputBorder(),
  181. // ),
  182. // ),
  183. // ),
  184. // const SizedBox(width: 20),
  185. // SizedBox(
  186. // height: 30,
  187. // child: ElevatedButton(
  188. // onPressed: _toggleConnection,
  189. // child: Text(
  190. // _isConnected ? '断开' : '连接',
  191. // style: TextStyle(
  192. // color: _isConnected
  193. // ? Colors.red
  194. // : Colors.green,
  195. // ),
  196. // ),
  197. // ),
  198. // ),
  199. // ],
  200. // ),
  201. // ],
  202. // ),
  203. // ),
  204. // ),
  205. // const SizedBox(height: 20),
  206. // // 数据发送区域
  207. // Card(
  208. // child: Padding(
  209. // padding: const EdgeInsets.all(16.0),
  210. // child: Column(
  211. // crossAxisAlignment: CrossAxisAlignment.start,
  212. // children: [
  213. // const Text(
  214. // '数据发送',
  215. // style: TextStyle(
  216. // fontSize: 18,
  217. // fontWeight: FontWeight.bold,
  218. // ),
  219. // ),
  220. // const SizedBox(height: 10),
  221. // Row(
  222. // children: [
  223. // Expanded(
  224. // child: SizedBox(
  225. // height: 50,
  226. // child: TextField(
  227. // controller: _dataController,
  228. // decoration: const InputDecoration(
  229. // hintText: '输入要发送的数据',
  230. // border: OutlineInputBorder(),
  231. // ),
  232. // ),
  233. // ),
  234. // ),
  235. // const SizedBox(width: 10),
  236. // SizedBox(
  237. // height: 50,
  238. // child: ElevatedButton(
  239. // onPressed: _sendData,
  240. // child: const Text('发送'),
  241. // ),
  242. // ),
  243. // ],
  244. // ),
  245. // ],
  246. // ),
  247. // ),
  248. // ),
  249. // const SizedBox(height: 20),
  250. // // 数据接收区域
  251. // Card(
  252. // child: Padding(
  253. // padding: const EdgeInsets.all(16.0),
  254. // child: Column(
  255. // crossAxisAlignment: CrossAxisAlignment.start,
  256. // children: [
  257. // Row(
  258. // mainAxisAlignment: MainAxisAlignment.spaceBetween,
  259. // children: [
  260. // const Text(
  261. // '数据接收',
  262. // style: TextStyle(
  263. // fontSize: 18,
  264. // fontWeight: FontWeight.bold,
  265. // ),
  266. // ),
  267. // ElevatedButton(
  268. // onPressed: _clearReceivedData,
  269. // child: const Text('清空'),
  270. // ),
  271. // ],
  272. // ),
  273. // const SizedBox(height: 10),
  274. // Container(
  275. // height: 200,
  276. // decoration: BoxDecoration(
  277. // border: Border.all(color: Colors.grey),
  278. // ),
  279. // child: ListView.builder(
  280. // itemCount: _receivedData.length,
  281. // itemBuilder: (context, index) {
  282. // return Padding(
  283. // padding: const EdgeInsets.symmetric(
  284. // horizontal: 8.0,
  285. // vertical: 4.0,
  286. // ),
  287. // child: Text(_receivedData[index]),
  288. // );
  289. // },
  290. // ),
  291. // ),
  292. // ],
  293. // ),
  294. // ),
  295. // ),
  296. // ],
  297. // ),
  298. // ),
  299. ),
  300. );
  301. }
  302. }