serial_setting_page.dart 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565
  1. // ignore_for_file: unused_import, unused_field, prefer_final_fields
  2. import 'dart:async';
  3. import 'dart:typed_data';
  4. import 'package:chicken_farm/components/vb_app_bar.dart';
  5. import 'package:chicken_farm/core/services/win/win_reader_channel.dart';
  6. import 'package:chicken_farm/core/services/win/win_reader_manager.dart';
  7. import 'package:chicken_farm/core/utils/logger.dart';
  8. import 'package:chicken_farm/core/utils/toast.dart';
  9. import 'package:flutter/material.dart';
  10. class SerialSettingPage extends StatefulWidget {
  11. const SerialSettingPage({super.key});
  12. @override
  13. State<SerialSettingPage> createState() => _SerialSettingPageState();
  14. }
  15. class _SerialSettingPageState extends State<SerialSettingPage> {
  16. final WinReaderManager _readerManager = WinReaderManager();
  17. List<String> _availablePorts = [];
  18. String? _selectedPort;
  19. final TextEditingController _dataController = TextEditingController();
  20. final List<String> _receivedData = [];
  21. bool _isConnected = false;
  22. bool _isReading = false;
  23. // 设备信息
  24. String _deviceSN = '';
  25. String _softVersion = '';
  26. String _hardVersion = '';
  27. // 功率相关状态
  28. int? _currentPower;
  29. int _selectedPower = 20; // 默认值
  30. @override
  31. void initState() {
  32. super.initState();
  33. // 注册回调函数
  34. _readerManager.registerCallbacks(
  35. onConnect: _onConnect,
  36. onDataReceived: _onDataReceived,
  37. onError: _onError,
  38. );
  39. _loadUsbPorts();
  40. final connected = _readerManager.isConnected;
  41. if (connected) {
  42. _readerManager.disconnect();
  43. }
  44. _readerManager.autoConnect();
  45. }
  46. /// 加载可用Usb列表
  47. void _loadUsbPorts() async {
  48. try {
  49. final ports = await _readerManager.scanUsb();
  50. if (mounted) {
  51. // 检查组件是否仍然挂载
  52. setState(() {
  53. _availablePorts = ports ?? [];
  54. if (_availablePorts.isNotEmpty) {
  55. _selectedPort = _availablePorts.first;
  56. }
  57. });
  58. }
  59. } catch (e) {
  60. if (mounted) {
  61. // 检查组件是否仍然挂载
  62. logger.e(e);
  63. ToastUtil.error("扫描USB设备失败: $e");
  64. }
  65. }
  66. }
  67. /// 连接状态回调
  68. void _onConnect(bool connected) {
  69. if (mounted) {
  70. setState(() {
  71. _isConnected = connected;
  72. if (!connected) {
  73. _isReading = false;
  74. _deviceSN = '';
  75. _softVersion = '';
  76. _hardVersion = '';
  77. _currentPower = null;
  78. }
  79. });
  80. if (connected) {
  81. _addLog("成功连接到设备: $_selectedPort");
  82. // 连接成功后自动查询设备信息
  83. _autoQueryDeviceInfo();
  84. // 连接成功后自动查询功率
  85. _autoQueryPower();
  86. } else {
  87. _addLog("已断开设备连接");
  88. }
  89. }
  90. }
  91. /// 自动查询设备信息
  92. void _autoQueryDeviceInfo() async {
  93. String? deviceInfo = await _readerManager.getDeviceInfo();
  94. if (deviceInfo != null && mounted) {
  95. // 解析设备信息
  96. _parseDeviceInfo(deviceInfo);
  97. _addLog("设备信息查询成功: $deviceInfo");
  98. } else {
  99. _addLog("设备信息查询失败");
  100. }
  101. }
  102. /// 自动查询功率
  103. void _autoQueryPower() async {
  104. int? power = await _readerManager.getPower();
  105. if (power != null && mounted) {
  106. setState(() {
  107. _currentPower = power;
  108. _selectedPower = power; // 设置下拉框的默认值为当前功率
  109. });
  110. _addLog("功率查询成功: ${power}dBm");
  111. } else {
  112. _addLog("功率查询失败");
  113. }
  114. }
  115. /// 解析设备信息字符串
  116. void _parseDeviceInfo(String deviceInfo) {
  117. if (mounted) {
  118. // deviceInfo 格式: "SN:1234567890ABCDEF,SoftVer:1.2,HardVer:3.4"
  119. List<String> parts = deviceInfo.split(',');
  120. for (String part in parts) {
  121. if (part.startsWith('SN:')) {
  122. setState(() {
  123. _deviceSN = part.substring(3); // 去掉'SN:'前缀
  124. });
  125. } else if (part.startsWith('SoftVer:')) {
  126. setState(() {
  127. _softVersion = part.substring(8); // 去掉'SoftVer:'前缀
  128. });
  129. } else if (part.startsWith('HardVer:')) {
  130. setState(() {
  131. _hardVersion = part.substring(8); // 去掉'HardVer:'前缀
  132. });
  133. }
  134. }
  135. }
  136. }
  137. /// 数据接收回调
  138. void _onDataReceived(List<String> data) {
  139. if (mounted && data.isNotEmpty) {
  140. for (var element in data) {
  141. _addLog("接收数据: $element");
  142. }
  143. }
  144. }
  145. /// 错误回调
  146. void _onError(String error) {
  147. if (mounted) {
  148. _addLog("错误: $error");
  149. ToastUtil.error("操作失败: $error");
  150. }
  151. }
  152. /// 连接USB
  153. void _connectUsbPort() async {
  154. if (_selectedPort == null) {
  155. ToastUtil.error("请先选择一个USB设备");
  156. return;
  157. }
  158. if (!_isConnected) {
  159. // 获取当前选中设备的索引
  160. int deviceIndex = _availablePorts.indexOf(_selectedPort!);
  161. bool success = await _readerManager.connect(deviceIndex);
  162. if (!success) {
  163. ToastUtil.error("连接设备失败");
  164. }
  165. } else {
  166. await _readerManager.disconnect();
  167. }
  168. }
  169. /// 开始读取
  170. void _startRead() async {
  171. bool success = await _readerManager.startRead();
  172. if (success && mounted) {
  173. setState(() {
  174. _isReading = true;
  175. });
  176. _addLog("开始读取数据");
  177. }
  178. }
  179. /// 停止读取
  180. void _stopRead() async {
  181. await _readerManager.stopRead();
  182. if (mounted) {
  183. setState(() {
  184. _isReading = false;
  185. });
  186. _addLog("已停止读取数据");
  187. }
  188. }
  189. /// 查询功率
  190. void _queryPower() async {
  191. if (!_isConnected) {
  192. ToastUtil.error("请先连接设备");
  193. return;
  194. }
  195. int? power = await _readerManager.getPower();
  196. if (power != null && mounted) {
  197. setState(() {
  198. _currentPower = power;
  199. _selectedPower = power;
  200. });
  201. _addLog("功率查询成功: ${power}dBm");
  202. } else {
  203. _addLog("功率查询失败");
  204. }
  205. }
  206. /// 设置功率
  207. void _setPower() async {
  208. if (!_isConnected) {
  209. ToastUtil.error("请先连接设备");
  210. return;
  211. }
  212. bool success = await _readerManager.setPower(_selectedPower);
  213. if (success) {
  214. ToastUtil.success("功率设置成功");
  215. if (mounted) {
  216. _addLog("功率设置成功: ${_selectedPower}dBm");
  217. }
  218. Future.delayed(const Duration(milliseconds: 500), () {
  219. _queryPower();
  220. });
  221. } else {
  222. _addLog("功率设置失败");
  223. }
  224. }
  225. /// 添加日志
  226. void _addLog(String log) {
  227. if (mounted) {
  228. setState(() {
  229. _receivedData.add("${DateTime.now()}: $log");
  230. });
  231. }
  232. }
  233. /// 清空日志
  234. void _clearLogs() {
  235. if (mounted) {
  236. setState(() {
  237. _receivedData.clear();
  238. });
  239. }
  240. }
  241. @override
  242. Widget build(BuildContext context) {
  243. return Scaffold(
  244. appBar: VberAppBar(title: 'USB测试工具'),
  245. body: Padding(
  246. padding: const EdgeInsets.all(16.0),
  247. child: SingleChildScrollView(
  248. physics: const BouncingScrollPhysics(),
  249. child: Column(
  250. crossAxisAlignment: CrossAxisAlignment.start,
  251. children: [
  252. // 设备信息卡片
  253. Card(
  254. child: Padding(
  255. padding: const EdgeInsets.all(16.0),
  256. child: Column(
  257. crossAxisAlignment: CrossAxisAlignment.start,
  258. children: [
  259. Row(
  260. children: [
  261. const Text('USB设备:'),
  262. const SizedBox(width: 10),
  263. Expanded(
  264. child: DropdownButton<String>(
  265. value: _selectedPort,
  266. items: _availablePorts.map((port) {
  267. return DropdownMenuItem(
  268. value: port,
  269. child: Text(
  270. port,
  271. overflow: TextOverflow.ellipsis,
  272. ),
  273. );
  274. }).toList(),
  275. onChanged: (value) {
  276. if (!_isConnected) {
  277. setState(() {
  278. _selectedPort = value;
  279. });
  280. }
  281. },
  282. hint: const Text('选择USB'),
  283. isExpanded: true,
  284. ),
  285. ),
  286. const SizedBox(width: 20),
  287. SizedBox(
  288. height: 30,
  289. child: ElevatedButton(
  290. onPressed: _loadUsbPorts,
  291. child: const Text('刷新'),
  292. ),
  293. ),
  294. const SizedBox(width: 20),
  295. if (_selectedPort != null) ...[
  296. SizedBox(
  297. height: 30,
  298. child: ElevatedButton(
  299. onPressed: _connectUsbPort,
  300. child: Text(_isConnected ? '关闭连接' : '打开连接'),
  301. ),
  302. ),
  303. ],
  304. const SizedBox(width: 20),
  305. if (_isConnected) ...[
  306. SizedBox(
  307. height: 30,
  308. child: ElevatedButton(
  309. onPressed: _isReading ? _stopRead : _startRead,
  310. child: Text(_isReading ? '停止读取' : '开始读取'),
  311. ),
  312. ),
  313. ],
  314. ],
  315. ),
  316. if (_isConnected) ...[
  317. const SizedBox(height: 10),
  318. Row(
  319. children: [
  320. const Text(
  321. '设备信息: ',
  322. style: TextStyle(
  323. fontSize: 16,
  324. fontWeight: FontWeight.bold,
  325. ),
  326. ),
  327. Expanded(
  328. child: _deviceSN.isNotEmpty
  329. ? Row(
  330. children: [
  331. const Text(
  332. 'SN: ',
  333. style: TextStyle(
  334. fontWeight: FontWeight.bold,
  335. ),
  336. ),
  337. Text(
  338. _deviceSN,
  339. style: const TextStyle(
  340. fontWeight: FontWeight.w500,
  341. ),
  342. ),
  343. const SizedBox(width: 16),
  344. const Text(
  345. 'SoftVer: ',
  346. style: TextStyle(
  347. fontWeight: FontWeight.bold,
  348. ),
  349. ),
  350. Text(
  351. _softVersion,
  352. style: const TextStyle(
  353. fontWeight: FontWeight.w500,
  354. ),
  355. ),
  356. const SizedBox(width: 16),
  357. const Text(
  358. 'HardVer: ',
  359. style: TextStyle(
  360. fontWeight: FontWeight.bold,
  361. ),
  362. ),
  363. Text(
  364. _hardVersion,
  365. style: const TextStyle(
  366. fontWeight: FontWeight.w500,
  367. ),
  368. ),
  369. ],
  370. )
  371. : const Text(
  372. '未连接',
  373. style: TextStyle(
  374. fontWeight: FontWeight.w500,
  375. ),
  376. ),
  377. ),
  378. const SizedBox(width: 20),
  379. SizedBox(
  380. height: 30,
  381. child: ElevatedButton(
  382. onPressed: _autoQueryDeviceInfo,
  383. child: const Text('刷新设备信息'),
  384. ),
  385. ),
  386. ],
  387. ),
  388. const SizedBox(height: 10),
  389. // 功率设置卡片
  390. Row(
  391. children: [
  392. const Text(
  393. '当前功率: ',
  394. style: TextStyle(
  395. fontSize: 14,
  396. fontWeight: FontWeight.bold,
  397. ),
  398. ),
  399. const SizedBox(width: 10),
  400. Text(
  401. _currentPower != null
  402. ? '${_currentPower}dBm'
  403. : '未查询',
  404. style: const TextStyle(
  405. fontSize: 14,
  406. fontWeight: FontWeight.w500,
  407. ),
  408. ),
  409. const SizedBox(width: 30),
  410. SizedBox(
  411. height: 30,
  412. child: ElevatedButton(
  413. onPressed: _queryPower,
  414. child: const Text('查询功率'),
  415. ),
  416. ),
  417. const SizedBox(width: 50),
  418. const Text(
  419. '功率设置: ',
  420. style: TextStyle(
  421. fontSize: 16,
  422. fontWeight: FontWeight.bold,
  423. ),
  424. ),
  425. const SizedBox(width: 10),
  426. Expanded(
  427. child: DropdownButton<int>(
  428. value: _selectedPower,
  429. items:
  430. List.generate(
  431. 26,
  432. (index) => index + 5,
  433. ) // 5-30的值
  434. .map((value) {
  435. return DropdownMenuItem(
  436. value: value,
  437. child: Text('${value}dBm'),
  438. );
  439. })
  440. .toList(),
  441. onChanged: (value) {
  442. if (value != null && _isConnected) {
  443. setState(() {
  444. _selectedPower = value;
  445. });
  446. }
  447. },
  448. isExpanded: true,
  449. ),
  450. ),
  451. const SizedBox(width: 20),
  452. SizedBox(
  453. height: 30,
  454. child: ElevatedButton(
  455. onPressed: _setPower,
  456. child: const Text('设置功率'),
  457. ),
  458. ),
  459. ],
  460. ),
  461. const SizedBox(height: 5),
  462. ],
  463. ],
  464. ),
  465. ),
  466. ),
  467. const SizedBox(height: 20),
  468. Card(
  469. child: Padding(
  470. padding: const EdgeInsets.all(16.0),
  471. child: Column(
  472. crossAxisAlignment: CrossAxisAlignment.start,
  473. children: [
  474. const Text(
  475. '数据日志',
  476. style: TextStyle(
  477. fontSize: 16,
  478. fontWeight: FontWeight.bold,
  479. ),
  480. ),
  481. const SizedBox(height: 10),
  482. Row(
  483. children: [
  484. ElevatedButton(
  485. onPressed: _clearLogs,
  486. child: const Text('清空日志'),
  487. ),
  488. const SizedBox(width: 10),
  489. Text(
  490. '日志数量: ${_receivedData.length}',
  491. style: const TextStyle(fontWeight: FontWeight.bold),
  492. ),
  493. ],
  494. ),
  495. const SizedBox(height: 10),
  496. Container(
  497. height: 250,
  498. decoration: BoxDecoration(
  499. border: Border.all(color: Colors.grey),
  500. borderRadius: BorderRadius.circular(4),
  501. ),
  502. child: _receivedData.isEmpty
  503. ? const Center(child: Text('暂无日志数据'))
  504. : ListView.builder(
  505. itemCount: _receivedData.length,
  506. itemBuilder: (context, index) {
  507. return Padding(
  508. padding: const EdgeInsets.all(8.0),
  509. child: Text(
  510. _receivedData[_receivedData.length -
  511. 1 -
  512. index],
  513. style: const TextStyle(fontSize: 12),
  514. ),
  515. );
  516. },
  517. ),
  518. ),
  519. ],
  520. ),
  521. ),
  522. ),
  523. ],
  524. ),
  525. ),
  526. ),
  527. );
  528. }
  529. @override
  530. void dispose() {
  531. // _readerManager.clearCallbacks();
  532. // _readerManager.dispose();
  533. // _dataController.dispose();
  534. super.dispose();
  535. }
  536. }