serial_setting_page.dart 12 KB

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