serial_setting_page.dart 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563
  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(String data) {
  139. if (mounted) {
  140. _addLog("读取标签: $data");
  141. }
  142. }
  143. /// 错误回调
  144. void _onError(String error) {
  145. if (mounted) {
  146. _addLog("错误: $error");
  147. ToastUtil.error("操作失败: $error");
  148. }
  149. }
  150. /// 连接USB
  151. void _connectUsbPort() async {
  152. if (_selectedPort == null) {
  153. ToastUtil.error("请先选择一个USB设备");
  154. return;
  155. }
  156. if (!_isConnected) {
  157. // 获取当前选中设备的索引
  158. int deviceIndex = _availablePorts.indexOf(_selectedPort!);
  159. bool success = await _readerManager.connect(deviceIndex);
  160. if (!success) {
  161. ToastUtil.error("连接设备失败");
  162. }
  163. } else {
  164. await _readerManager.disconnect();
  165. }
  166. }
  167. /// 开始读取
  168. void _startRead() async {
  169. bool success = await _readerManager.startRead();
  170. if (success && mounted) {
  171. setState(() {
  172. _isReading = true;
  173. });
  174. _addLog("开始读取数据");
  175. }
  176. }
  177. /// 停止读取
  178. void _stopRead() async {
  179. await _readerManager.stopRead();
  180. if (mounted) {
  181. setState(() {
  182. _isReading = false;
  183. });
  184. _addLog("已停止读取数据");
  185. }
  186. }
  187. /// 查询功率
  188. void _queryPower() async {
  189. if (!_isConnected) {
  190. ToastUtil.error("请先连接设备");
  191. return;
  192. }
  193. int? power = await _readerManager.getPower();
  194. if (power != null && mounted) {
  195. setState(() {
  196. _currentPower = power;
  197. _selectedPower = power;
  198. });
  199. _addLog("功率查询成功: ${power}dBm");
  200. } else {
  201. _addLog("功率查询失败");
  202. }
  203. }
  204. /// 设置功率
  205. void _setPower() async {
  206. if (!_isConnected) {
  207. ToastUtil.error("请先连接设备");
  208. return;
  209. }
  210. bool success = await _readerManager.setPower(_selectedPower);
  211. if (success) {
  212. ToastUtil.success("功率设置成功");
  213. if (mounted) {
  214. _addLog("功率设置成功: ${_selectedPower}dBm");
  215. }
  216. Future.delayed(const Duration(milliseconds: 500), () {
  217. _queryPower();
  218. });
  219. } else {
  220. _addLog("功率设置失败");
  221. }
  222. }
  223. /// 添加日志
  224. void _addLog(String log) {
  225. if (mounted) {
  226. setState(() {
  227. _receivedData.add("${DateTime.now()}: $log");
  228. });
  229. }
  230. }
  231. /// 清空日志
  232. void _clearLogs() {
  233. if (mounted) {
  234. setState(() {
  235. _receivedData.clear();
  236. });
  237. }
  238. }
  239. @override
  240. Widget build(BuildContext context) {
  241. return Scaffold(
  242. appBar: VberAppBar(title: 'USB测试工具'),
  243. body: Padding(
  244. padding: const EdgeInsets.all(16.0),
  245. child: SingleChildScrollView(
  246. physics: const BouncingScrollPhysics(),
  247. child: Column(
  248. crossAxisAlignment: CrossAxisAlignment.start,
  249. children: [
  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. children: [
  259. const Text('USB设备:'),
  260. const SizedBox(width: 10),
  261. Expanded(
  262. child: DropdownButton<String>(
  263. value: _selectedPort,
  264. items: _availablePorts.map((port) {
  265. return DropdownMenuItem(
  266. value: port,
  267. child: Text(
  268. port,
  269. overflow: TextOverflow.ellipsis,
  270. ),
  271. );
  272. }).toList(),
  273. onChanged: (value) {
  274. if (!_isConnected) {
  275. setState(() {
  276. _selectedPort = value;
  277. });
  278. }
  279. },
  280. hint: const Text('选择USB'),
  281. isExpanded: true,
  282. ),
  283. ),
  284. const SizedBox(width: 20),
  285. SizedBox(
  286. height: 30,
  287. child: ElevatedButton(
  288. onPressed: _loadUsbPorts,
  289. child: const Text('刷新'),
  290. ),
  291. ),
  292. const SizedBox(width: 20),
  293. if (_selectedPort != null) ...[
  294. SizedBox(
  295. height: 30,
  296. child: ElevatedButton(
  297. onPressed: _connectUsbPort,
  298. child: Text(_isConnected ? '关闭连接' : '打开连接'),
  299. ),
  300. ),
  301. ],
  302. const SizedBox(width: 20),
  303. if (_isConnected) ...[
  304. SizedBox(
  305. height: 30,
  306. child: ElevatedButton(
  307. onPressed: _isReading ? _stopRead : _startRead,
  308. child: Text(_isReading ? '停止读取' : '开始读取'),
  309. ),
  310. ),
  311. ],
  312. ],
  313. ),
  314. if (_isConnected) ...[
  315. const SizedBox(height: 10),
  316. Row(
  317. children: [
  318. const Text(
  319. '设备信息: ',
  320. style: TextStyle(
  321. fontSize: 16,
  322. fontWeight: FontWeight.bold,
  323. ),
  324. ),
  325. Expanded(
  326. child: _deviceSN.isNotEmpty
  327. ? Row(
  328. children: [
  329. const Text(
  330. 'SN: ',
  331. style: TextStyle(
  332. fontWeight: FontWeight.bold,
  333. ),
  334. ),
  335. Text(
  336. _deviceSN,
  337. style: const TextStyle(
  338. fontWeight: FontWeight.w500,
  339. ),
  340. ),
  341. const SizedBox(width: 16),
  342. const Text(
  343. 'SoftVer: ',
  344. style: TextStyle(
  345. fontWeight: FontWeight.bold,
  346. ),
  347. ),
  348. Text(
  349. _softVersion,
  350. style: const TextStyle(
  351. fontWeight: FontWeight.w500,
  352. ),
  353. ),
  354. const SizedBox(width: 16),
  355. const Text(
  356. 'HardVer: ',
  357. style: TextStyle(
  358. fontWeight: FontWeight.bold,
  359. ),
  360. ),
  361. Text(
  362. _hardVersion,
  363. style: const TextStyle(
  364. fontWeight: FontWeight.w500,
  365. ),
  366. ),
  367. ],
  368. )
  369. : const Text(
  370. '未连接',
  371. style: TextStyle(
  372. fontWeight: FontWeight.w500,
  373. ),
  374. ),
  375. ),
  376. const SizedBox(width: 20),
  377. SizedBox(
  378. height: 30,
  379. child: ElevatedButton(
  380. onPressed: _autoQueryDeviceInfo,
  381. child: const Text('刷新设备信息'),
  382. ),
  383. ),
  384. ],
  385. ),
  386. const SizedBox(height: 10),
  387. // 功率设置卡片
  388. Row(
  389. children: [
  390. const Text(
  391. '当前功率: ',
  392. style: TextStyle(
  393. fontSize: 14,
  394. fontWeight: FontWeight.bold,
  395. ),
  396. ),
  397. const SizedBox(width: 10),
  398. Text(
  399. _currentPower != null
  400. ? '${_currentPower}dBm'
  401. : '未查询',
  402. style: const TextStyle(
  403. fontSize: 14,
  404. fontWeight: FontWeight.w500,
  405. ),
  406. ),
  407. const SizedBox(width: 30),
  408. SizedBox(
  409. height: 30,
  410. child: ElevatedButton(
  411. onPressed: _queryPower,
  412. child: const Text('查询功率'),
  413. ),
  414. ),
  415. const SizedBox(width: 50),
  416. const Text(
  417. '功率设置: ',
  418. style: TextStyle(
  419. fontSize: 16,
  420. fontWeight: FontWeight.bold,
  421. ),
  422. ),
  423. const SizedBox(width: 10),
  424. Expanded(
  425. child: DropdownButton<int>(
  426. value: _selectedPower,
  427. items:
  428. List.generate(
  429. 26,
  430. (index) => index + 5,
  431. ) // 5-30的值
  432. .map((value) {
  433. return DropdownMenuItem(
  434. value: value,
  435. child: Text('${value}dBm'),
  436. );
  437. })
  438. .toList(),
  439. onChanged: (value) {
  440. if (value != null && _isConnected) {
  441. setState(() {
  442. _selectedPower = value;
  443. });
  444. }
  445. },
  446. isExpanded: true,
  447. ),
  448. ),
  449. const SizedBox(width: 20),
  450. SizedBox(
  451. height: 30,
  452. child: ElevatedButton(
  453. onPressed: _setPower,
  454. child: const Text('设置功率'),
  455. ),
  456. ),
  457. ],
  458. ),
  459. const SizedBox(height: 5),
  460. ],
  461. ],
  462. ),
  463. ),
  464. ),
  465. const SizedBox(height: 20),
  466. Card(
  467. child: Padding(
  468. padding: const EdgeInsets.all(16.0),
  469. child: Column(
  470. crossAxisAlignment: CrossAxisAlignment.start,
  471. children: [
  472. const Text(
  473. '数据日志',
  474. style: TextStyle(
  475. fontSize: 16,
  476. fontWeight: FontWeight.bold,
  477. ),
  478. ),
  479. const SizedBox(height: 10),
  480. Row(
  481. children: [
  482. ElevatedButton(
  483. onPressed: _clearLogs,
  484. child: const Text('清空日志'),
  485. ),
  486. const SizedBox(width: 10),
  487. Text(
  488. '日志数量: ${_receivedData.length}',
  489. style: const TextStyle(fontWeight: FontWeight.bold),
  490. ),
  491. ],
  492. ),
  493. const SizedBox(height: 10),
  494. Container(
  495. height: 250,
  496. decoration: BoxDecoration(
  497. border: Border.all(color: Colors.grey),
  498. borderRadius: BorderRadius.circular(4),
  499. ),
  500. child: _receivedData.isEmpty
  501. ? const Center(child: Text('暂无日志数据'))
  502. : ListView.builder(
  503. itemCount: _receivedData.length,
  504. itemBuilder: (context, index) {
  505. return Padding(
  506. padding: const EdgeInsets.all(8.0),
  507. child: Text(
  508. _receivedData[_receivedData.length -
  509. 1 -
  510. index],
  511. style: const TextStyle(fontSize: 12),
  512. ),
  513. );
  514. },
  515. ),
  516. ),
  517. ],
  518. ),
  519. ),
  520. ),
  521. ],
  522. ),
  523. ),
  524. ),
  525. );
  526. }
  527. @override
  528. void dispose() {
  529. // _readerManager.clearCallbacks();
  530. // _readerManager.dispose();
  531. // _dataController.dispose();
  532. super.dispose();
  533. }
  534. }