-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSimpleSensorSync.html
More file actions
1190 lines (1045 loc) · 41 KB
/
SimpleSensorSync.html
File metadata and controls
1190 lines (1045 loc) · 41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0"/>
<title>SimpleSensorSync Studio — 多传感器同步板配置工具</title>
<style>
*{
margin:0;
padding:0;
box-sizing:border-box;
}
body{
font-family:-apple-system,BlinkMacSystemFont,"Segoe UI",Roboto,sans-serif;
background:#f3f5f9;
color:#222;
padding:20px;
}
.container{
max-width:1400px;
margin:auto;
}
.header{
background:white;
border-radius:16px;
padding:20px;
margin-bottom:20px;
display:flex;
justify-content:space-between;
align-items:center;
box-shadow:0 2px 10px rgba(0,0,0,0.06);
flex-wrap:wrap;
gap:12px;
}
.title-group{
display:flex;
flex-direction:column;
}
.title{
font-size:28px;
font-weight:700;
}
.subtitle{
font-size:13px;
color:#888;
margin-top:4px;
}
.connection{
display:flex;
align-items:center;
gap:10px;
flex-wrap:wrap;
}
.status{
padding:8px 16px;
border-radius:999px;
font-size:14px;
font-weight:600;
}
.connected{
background:#d4f7df;
color:#0d8b3e;
animation:pulse 0.5s ease-in-out 3;
}
@keyframes pulse{
0%,100%{transform:scale(1);}
50%{transform:scale(1.1);box-shadow:0 0 20px rgba(13,139,62,0.5);}
}
.disconnected{
background:#ffe0e0;
color:#c62828;
}
button{
border:none;
border-radius:10px;
padding:10px 18px;
cursor:pointer;
font-size:14px;
font-weight:600;
transition:0.2s;
}
button:hover{
transform:translateY(-1px);
}
button:disabled{
opacity:0.5;
cursor:not-allowed;
transform:none;
}
.primary{background:#2563eb;color:white;}
.danger{background:#dc2626;color:white;}
.success{background:#16a34a;color:white;}
.gray{background:#64748b;color:white;}
.warning{background:#f59e0b;color:white;}
.main-grid{
display:grid;
grid-template-columns:1fr 1fr;
gap:20px;
margin-bottom:20px;
}
.card{
background:white;
border-radius:16px;
padding:20px;
box-shadow:0 2px 10px rgba(0,0,0,0.06);
}
.card-title{
font-size:22px;
font-weight:700;
margin-bottom:20px;
display:flex;
align-items:center;
gap:8px;
}
.card-title .icon{
font-size:22px;
}
.status-grid{
display:grid;
grid-template-columns:1fr 1fr;
gap:15px;
margin-bottom:20px;
}
.status-item{
background:#f8fafc;
padding:14px;
border-radius:12px;
}
.status-label{
font-size:13px;
color:#666;
margin-bottom:6px;
}
.status-value{
font-size:24px;
font-weight:700;
}
.section{
margin-bottom:20px;
}
.section-title{
font-size:16px;
font-weight:700;
margin-bottom:12px;
color:#333;
border-bottom:1px solid #eee;
padding-bottom:6px;
}
.row{
display:flex;
align-items:center;
gap:12px;
margin-bottom:12px;
}
.row label{
width:140px;
font-weight:600;
font-size:14px;
flex-shrink:0;
}
.row label .hint{
font-weight:400;
font-size:11px;
color:#999;
display:block;
}
input[type="number"]{
flex:1;
padding:10px;
border:1px solid #ddd;
border-radius:10px;
font-size:14px;
min-width:0;
}
input[type="range"]{
flex:1;
}
select{
flex:1;
padding:10px;
border:1px solid #ddd;
border-radius:10px;
font-size:14px;
min-width:0;
}
.toggle-row{
display:flex;
align-items:center;
justify-content:space-between;
margin-bottom:12px;
padding:10px 14px;
background:#f8fafc;
border-radius:10px;
}
.toggle-row label{
font-weight:600;
font-size:14px;
}
.toggle-row label .hint{
font-weight:400;
font-size:11px;
color:#999;
}
.switch{
position:relative;
width:50px;
height:26px;
}
.switch input{
opacity:0;
width:0;
height:0;
}
.slider{
position:absolute;
cursor:pointer;
top:0;left:0;right:0;bottom:0;
background:#ccc;
transition:0.3s;
border-radius:26px;
}
.slider:before{
position:absolute;
content:"";
height:20px;
width:20px;
left:3px;
bottom:3px;
background:white;
transition:0.3s;
border-radius:50%;
}
.switch input:checked + .slider{
background:#2563eb;
}
.switch input:checked + .slider:before{
transform:translateX(24px);
}
.ip-input-group{
display:flex;
align-items:center;
gap:4px;
flex:1;
}
.ip-input-group input[type="number"]{
width:70px;
text-align:center;
padding:8px 4px;
flex:none;
}
.ip-dot{
font-size:20px;
font-weight:700;
color:#666;
}
.config-grid{
display:grid;
grid-template-columns:1fr 1fr;
gap:15px;
}
.btn-group{
display:flex;
gap:10px;
flex-wrap:wrap;
}
.log-area{
width:100%;
height:280px;
border:none;
background:#0f172a;
color:#00ff90;
border-radius:12px;
padding:15px;
font-family:'Courier New',monospace;
font-size:13px;
resize:vertical;
}
.footer{
display:grid;
grid-template-columns:1fr;
gap:20px;
}
.badge{
padding:4px 10px;
border-radius:999px;
font-size:12px;
font-weight:700;
display:inline-block;
}
.badge-blue{background:#dbeafe;color:#2563eb;}
.badge-green{background:#d4f7df;color:#0d8b3e;}
.badge-orange{background:#ffedd5;color:#ea580c;}
.badge-red{background:#ffe0e0;color:#c62828;}
.quick-send{
margin-bottom:20px;
}
.quick-send-title{
font-size:16px;
font-weight:700;
margin-bottom:10px;
}
.quick-input-row{
display:flex;
gap:10px;
align-items:center;
}
.quick-input-row input[type="text"]{
flex:1;
padding:10px;
border:2px solid #ddd;
border-radius:10px;
font-family:monospace;
font-size:13px;
}
.quick-input-row input[type="text"]:focus{
outline:none;
border-color:#2563eb;
}
.info-box{
background:#eff6ff;
border:1px solid #bfdbfe;
border-radius:12px;
padding:14px;
font-size:13px;
color:#1e40af;
margin-bottom:16px;
line-height:1.6;
}
.info-box strong{
color:#1e3a8a;
}
.hidden{
display:none;
}
@media(max-width:1000px){
.main-grid{
grid-template-columns:1fr;
}
.footer{
grid-template-columns:1fr;
}
.config-grid{
grid-template-columns:1fr;
}
}
</style>
</head>
<body>
<div class="container">
<!-- HEADER -->
<div class="header">
<div class="title-group">
<div class="title">SimpleSensorSync Studio</div>
<div class="subtitle">多传感器同步板配置工具 · <a href="https://github.com/InfiniteSenseLab/SimpleSensorSync" target="_blank" style="color:#64748b;text-decoration:none;">🔗 GitHub</a></div>
<a href="index.html" style="display:inline-block;margin-top:6px;font-size:12px;color:#2563eb;text-decoration:none;">← 返回主页</a>
</div>
<div class="connection">
<select id="baudRate" style="padding:8px 12px;border-radius:8px;border:1px solid #ddd;font-size:14px;">
<option value="9600">9600</option>
<option value="19200">19200</option>
<option value="38400">38400</option>
<option value="57600">57600</option>
<option value="115200">115200</option>
<option value="230400">230400</option>
<option value="460800">460800</option>
<option value="921600" selected>921600</option>
</select>
<div id="status" class="status disconnected">
未连接
</div>
<button class="primary" id="connectBtn">
连接串口
</button>
<button class="danger" id="disconnectBtn">
断开
</button>
</div>
</div>
<!-- QUICK SEND -->
<div class="card" style="margin-bottom:20px;">
<div class="quick-send-title">
<span class="icon">⚡</span> 快速发送 JSON 配置
</div>
<div class="info-box">
<strong>说明:</strong>粘贴完整 JSON 配置到输入框后按回车或点击"发送"。发送后设备会重启,串口断开属于正常现象。请等待约 5 秒后重新点击「连接串口」选择设备即可。
</div>
<div class="quick-input-row">
<input type="text" id="quickJsonInput" placeholder='粘贴 JSON 配置,例如: {"f":"cfg","port":8888,...}'>
<button class="primary" onclick="sendQuickJson()">发送</button>
</div>
</div>
<!-- MAIN CONFIG GRID -->
<div class="main-grid">
<!-- LEFT: NETWORK + TRIGGER FREQ + PULSE WIDTH -->
<div class="card">
<div class="card-title"><span class="icon">📡</span> 网络与触发配置</div>
<!-- IP -->
<div class="section">
<div class="section-title">IP 地址</div>
<div class="ip-input-group">
<input type="number" id="cfgIp0" value="192" min="0" max="255">
<span class="ip-dot">.</span>
<input type="number" id="cfgIp1" value="168" min="0" max="255">
<span class="ip-dot">.</span>
<input type="number" id="cfgIp2" value="1" min="0" max="255">
<span class="ip-dot">.</span>
<input type="number" id="cfgIp3" value="188" min="0" max="255">
</div>
</div>
<!-- Subnet -->
<div class="section">
<div class="section-title">子网掩码</div>
<div class="ip-input-group">
<input type="number" id="cfgSubnet0" value="255" min="0" max="255">
<span class="ip-dot">.</span>
<input type="number" id="cfgSubnet1" value="255" min="0" max="255">
<span class="ip-dot">.</span>
<input type="number" id="cfgSubnet2" value="255" min="0" max="255">
<span class="ip-dot">.</span>
<input type="number" id="cfgSubnet3" value="0" min="0" max="255">
</div>
</div>
<!-- Port -->
<div class="section">
<div class="section-title">网络端口</div>
<div class="row">
<label>Port <span class="hint">UDP端口号</span></label>
<input type="number" id="cfgPort" value="8888" min="1" max="65535">
</div>
</div>
<!-- Trigger Freq -->
<div class="section">
<div class="section-title">触发频率 (Hz)</div>
<div class="row">
<label>CAM1 <span class="hint">相机1, 建议≤200</span></label>
<input type="number" id="cfgHzCam1" value="20" min="1" max="200">
</div>
<div class="row">
<label>CAM2 <span class="hint">相机2, 建议≤200</span></label>
<input type="number" id="cfgHzCam2" value="30" min="1" max="200">
</div>
<div class="row">
<label>CAM3 <span class="hint">相机3, 建议≤200</span></label>
<input type="number" id="cfgHzCam3" value="40" min="1" max="200">
</div>
<div class="row">
<label>CAM4 <span class="hint">相机4, 建议≤200</span></label>
<input type="number" id="cfgHzCam4" value="50" min="1" max="200">
</div>
<div class="row">
<label>IMU2 <span class="hint">外部IMU, 建议≤200</span></label>
<input type="number" id="cfgHzImu2" value="10" min="1" max="200">
</div>
</div>
<!-- Pulse Width -->
<div class="section">
<div class="section-title">触发信号高电平持续时间 (ms)</div>
<div class="info-box" style="margin-bottom:10px;">
触发周期必须大于持续时间。例如 20Hz 的周期为 50ms,持续时间应小于 50ms。
</div>
<div class="row">
<label>CAM1 MS <span class="hint">相机1脉宽</span></label>
<input type="number" id="cfgCam1Ms" value="50" min="1" step="1">
</div>
<div class="row">
<label>CAM2 MS <span class="hint">相机2脉宽</span></label>
<input type="number" id="cfgCam2Ms" value="50" min="1" step="1">
</div>
<div class="row">
<label>CAM3 MS <span class="hint">相机3脉宽</span></label>
<input type="number" id="cfgCam3Ms" value="50" min="1" step="1">
</div>
<div class="row">
<label>CAM4 MS <span class="hint">相机4脉宽</span></label>
<input type="number" id="cfgCam4Ms" value="50" min="1" step="1">
</div>
<div class="row">
<label>IMU2 MS <span class="hint">IMU2脉宽</span></label>
<input type="number" id="cfgImu2Ms" value="50" min="1" step="1">
</div>
<div class="row">
<label>PPS MS <span class="hint">PPS脉宽</span></label>
<input type="number" id="cfgPpsMs" value="100" min="1" step="1">
</div>
</div>
</div>
<!-- RIGHT: UART + VERSION + SWITCHES -->
<div class="card">
<div class="card-title"><span class="icon">⚙</span> 串口与系统配置</div>
<!-- UART Baud Rates -->
<div class="section">
<div class="section-title">串口波特率</div>
<div class="row">
<label>UART0 <span class="hint">Type-C通讯</span></label>
<select id="cfgUart0Baud">
<option value="9600">9600</option>
<option value="19200">19200</option>
<option value="38400">38400</option>
<option value="57600">57600</option>
<option value="115200">115200</option>
<option value="230400">230400</option>
<option value="460800">460800</option>
<option value="921600" selected>921600</option>
</select>
</div>
<div class="row">
<label>UART1 <span class="hint">PPS/Lidar</span></label>
<select id="cfgUart1Baud">
<option value="9600" selected>9600</option>
<option value="19200">19200</option>
<option value="38400">38400</option>
<option value="57600">57600</option>
<option value="115200">115200</option>
<option value="230400">230400</option>
<option value="460800">460800</option>
<option value="921600">921600</option>
</select>
</div>
<div class="row">
<label>UART2 <span class="hint">GPS/RTK</span></label>
<select id="cfgUart2Baud">
<option value="9600">9600</option>
<option value="19200">19200</option>
<option value="38400">38400</option>
<option value="57600">57600</option>
<option value="115200" selected>115200</option>
<option value="230400">230400</option>
<option value="460800">460800</option>
<option value="921600">921600</option>
</select>
</div>
</div>
<!-- Version & Xtal -->
<div class="section">
<div class="section-title">硬件版本与晶振</div>
<div class="row">
<label>Version <span class="hint">V3/MINI:300, V4:400</span></label>
<select id="cfgVersion">
<option value="300">300 — V3/MINI</option>
<option value="400" selected>400 — V4</option>
</select>
</div>
<div class="row">
<label>Xtal Diff <span class="hint">晶振偏差修正</span></label>
<input type="number" id="cfgXtalDiff" value="0" step="1">
</div>
</div>
<!-- Switches -->
<div class="section">
<div class="section-title">功能开关</div>
<div class="toggle-row">
<label>USE GPS <span class="hint">启用GPS模块</span></label>
<label class="switch">
<input type="checkbox" id="cfgUseGps" checked>
<span class="slider"></span>
</label>
</div>
<div class="toggle-row">
<label>USE PPS <span class="hint">启用PPS精确时钟同步</span></label>
<label class="switch">
<input type="checkbox" id="cfgUsePps" checked>
<span class="slider"></span>
</label>
</div>
<div class="toggle-row">
<label>IMU <span class="hint">发送自带IMU数据(默认开)</span></label>
<label class="switch">
<input type="checkbox" id="cfgImu" checked>
<span class="slider"></span>
</label>
</div>
<div class="toggle-row">
<label>MID360 <span class="hint">与MID360雷达同步(默认开)</span></label>
<label class="switch">
<input type="checkbox" id="cfgMid360" checked>
<span class="slider"></span>
</label>
</div>
</div>
<!-- Sleep -->
<div class="section">
<div class="section-title">启动延迟</div>
<div class="row">
<label>Sleep (s) <span class="hint">网络启动等待时间</span></label>
<input type="number" id="cfgSleep" value="5" min="1" max="60" step="1">
</div>
</div>
<!-- Status Monitor -->
<div class="section">
<div class="section-title">设备状态</div>
<div class="status-grid">
<div class="status-item">
<div class="status-label">连接状态</div>
<div class="status-value" id="statusConnVal"><span class="badge badge-red">断开</span></div>
</div>
<div class="status-item">
<div class="status-label">设备版本</div>
<div class="status-value" id="statusVersion">—</div>
</div>
<div class="status-item">
<div class="status-label">晶振修正</div>
<div class="status-value" id="statusXtalDiff">—</div>
</div>
<div class="status-item">
<div class="status-label">配置时间</div>
<div class="status-value" id="statusLastCfg" style="font-size:16px;">—</div>
</div>
</div>
</div>
</div>
</div>
<!-- CONFIG ACTIONS -->
<div class="card" style="margin-bottom:20px;">
<div class="card-title"><span class="icon">💾</span> 配置操作</div>
<div class="info-box">
<strong>操作流程:</strong>
<ol style="margin:6px 0 0 18px;">
<li>点击「连接串口」,在浏览器弹窗中选择同步板设备</li>
<li>连接后点击「加载配置」从设备读取当前参数并填充到界面</li>
<li>修改参数后点击「上传配置」发送到设备,设备将自动重启</li>
<li>发送后串口会断开(正常现象),等待约 5 秒后重新点击「连接串口」</li>
<li>重新连接后会看到大量反馈数据,说明配置成功</li>
</ol>
</div>
<div class="btn-group">
<button class="gray" onclick="loadConfig()">加载配置</button>
<button class="primary" onclick="sendConfig()">上传配置</button>
<button class="gray" onclick="exportConfig()">导出配置 (JSON)</button>
<button class="gray" onclick="document.getElementById('importConfigFile').click()">导入配置 (JSON)</button>
<input type="file" id="importConfigFile" accept=".json" style="display:none" onchange="importConfig(event)">
</div>
</div>
<!-- LOG -->
<div class="card">
<div class="card-title"><span class="icon">📝</span> 系统日志</div>
<textarea class="log-area" id="logArea" readonly></textarea>
</div>
</div>
<script>
// ========== STATE ==========
let port = null;
let reader = null;
let reading = false;
let receiveBuffer = '';
let writeQueue = [];
let isWriting = false;
let pendingLogs = [];
let logUpdateScheduled = false;
// ========== UI HELPERS ==========
function setConnectedUI(connected){
const statusEl = document.getElementById('status');
const connBadge = document.getElementById('statusConnVal');
if(connected){
statusEl.className = 'status connected';
statusEl.textContent = '已连接';
connBadge.innerHTML = '<span class="badge badge-green">已连接</span>';
document.getElementById('connectBtn').disabled = true;
document.getElementById('disconnectBtn').disabled = false;
} else {
statusEl.className = 'status disconnected';
statusEl.textContent = '未连接';
connBadge.innerHTML = '<span class="badge badge-red">断开</span>';
document.getElementById('connectBtn').disabled = false;
document.getElementById('disconnectBtn').disabled = true;
}
}
// ========== SERIAL ==========
async function connectSerial(){
try{
log('正在请求串口...');
port = await navigator.serial.requestPort();
log('串口已选择,正在打开...');
const baudRate = parseInt(document.getElementById('baudRate').value);
await port.open({ baudRate });
log('串口已打开 (波特率: ' + baudRate + ')');
setConnectedUI(true);
startReading();
playBeep();
// 连接后自动加载当前设备参数(不保存不重启,仅读取EEPROM中的配置)
setTimeout(() => {
if (port) {
log('自动获取设备当前参数...');
loadConfig();
}
}, 500);
} catch(e){
log('连接失败: ' + e);
port = null;
}
}
async function disconnectSerial(){
reading = false;
if(reader){
try{ await reader.cancel(); } catch(e){}
reader = null;
}
if(port){
try{
if(port.readable) await port.readable.closed;
} catch(e){}
try{ await port.close(); } catch(e){}
port = null;
}
receiveBuffer = '';
writeQueue = [];
isWriting = false;
setConnectedUI(false);
}
// 监听设备意外断开(拔出/重启)
function setupDisconnectListener(){
if(!port) return;
port.addEventListener('disconnect', () => {
reading = false;
reader = null;
port = null;
receiveBuffer = '';
writeQueue = [];
isWriting = false;
setConnectedUI(false);
log('设备已断开(设备可能已重启),请等待约5秒后重新点击「连接串口」');
});
}
async function startReading(){
reading = true;
// 注册断开监听(必须在 port 打开后注册)
setupDisconnectListener();
const decoder = new TextDecoder();
while(reading && port && port.readable){
try{
reader = port.readable.getReader();
const { value, done } = await reader.read();
if(done){
reader.releaseLock();
reader = null;
break;
}
if(value){
const text = decoder.decode(value, { stream: true });
handleReceive(text);
}
// 每次读取成功后释放锁,以便下次循环重新获取
reader.releaseLock();
reader = null;
} catch(e){
// reader 已在 try 外释放,此处需确保释放
if(reader){
try{ reader.releaseLock(); } catch(ex){}
reader = null;
}
// 设备已断开(拔出/重启)
if(!port || !port.readable){
reading = false;
// disconnect 事件会处理 UI 更新
if(!port){
setConnectedUI(false);
log('设备已断开,请等待约5秒后重新连接');
}
break;
}
// 用户主动取消(disconnectSerial 调用 reader.cancel)
if(e.name === 'AbortError'){
break;
}
// 其他读取错误,短暂等待后重试
if(reading){
log('读取错误: ' + e.message);
await new Promise(r => setTimeout(r, 100));
}
}
}
// 循环结束后,若仍是 reading 状态且 port 不可读,说明设备已断开
if(reading && port && !port.readable){
reading = false;
port = null;
setConnectedUI(false);
log('设备已断开,请等待约5秒后重新连接');
}
}
function handleReceive(text){
receiveBuffer += text;
const lines = receiveBuffer.split('\n');
receiveBuffer = lines.pop() || '';
for(const line of lines){
if(line.trim()){
try {
const data = JSON.parse(line.trim());
handleData(data);
} catch(e) {
// 解析失败(双核并发交叉等),静默丢弃
}
}
}
}
function handleData(data){
if(data.f === 'cfg'){
applyConfigToUI(data);
log('配置加载完成 (来自设备)');
} else if(data.f === 'log'){
const level = data.l || 'INFO';
const msg = data.msg || '';
log('[设备 ' + level + '] ' + msg);
}
// IMU、t、GPS 等实时数据不再打印到日志区
}
// ========== BUILD CONFIG ==========
function buildConfigCmd(){
return {
f: "cfg",
port: parseInt(document.getElementById('cfgPort').value) || 8888,
ip: [
parseInt(document.getElementById('cfgIp0').value) || 0,
parseInt(document.getElementById('cfgIp1').value) || 0,
parseInt(document.getElementById('cfgIp2').value) || 0,
parseInt(document.getElementById('cfgIp3').value) || 0
],
subnet: [
parseInt(document.getElementById('cfgSubnet0').value) || 0,
parseInt(document.getElementById('cfgSubnet1').value) || 0,
parseInt(document.getElementById('cfgSubnet2').value) || 0,
parseInt(document.getElementById('cfgSubnet3').value) || 0
],
hz_cam_1: parseInt(document.getElementById('cfgHzCam1').value) || 20,
hz_cam_2: parseInt(document.getElementById('cfgHzCam2').value) || 30,
hz_cam_3: parseInt(document.getElementById('cfgHzCam3').value) || 40,
hz_cam_4: parseInt(document.getElementById('cfgHzCam4').value) || 50,
hz_imu_2: parseInt(document.getElementById('cfgHzImu2').value) || 10,
xtal_diff: parseInt(document.getElementById('cfgXtalDiff').value) || 0,
uart_0_baud_rate: parseInt(document.getElementById('cfgUart0Baud').value) || 921600,
uart_1_baud_rate: parseInt(document.getElementById('cfgUart1Baud').value) || 9600,
uart_2_baud_rate: parseInt(document.getElementById('cfgUart2Baud').value) || 115200,
use_gps: document.getElementById('cfgUseGps').checked,
use_pps: document.getElementById('cfgUsePps').checked,
cam1_ms: parseInt(document.getElementById('cfgCam1Ms').value) || 50,
cam2_ms: parseInt(document.getElementById('cfgCam2Ms').value) || 50,
cam3_ms: parseInt(document.getElementById('cfgCam3Ms').value) || 50,
cam4_ms: parseInt(document.getElementById('cfgCam4Ms').value) || 50,
imu2_ms: parseInt(document.getElementById('cfgImu2Ms').value) || 50,
pps_ms: parseInt(document.getElementById('cfgPpsMs').value) || 100,
version: parseInt(document.getElementById('cfgVersion').value) || 400,
imu: document.getElementById('cfgImu').checked,
mid360: document.getElementById('cfgMid360').checked,
sleep: parseInt(document.getElementById('cfgSleep').value) || 5
};
}
// ========== APPLY CONFIG TO UI ==========
function applyConfigToUI(data){
if(data.ip && Array.isArray(data.ip) && data.ip.length === 4){
document.getElementById('cfgIp0').value = data.ip[0];
document.getElementById('cfgIp1').value = data.ip[1];
document.getElementById('cfgIp2').value = data.ip[2];
document.getElementById('cfgIp3').value = data.ip[3];
}
if(data.subnet && Array.isArray(data.subnet) && data.subnet.length === 4){
document.getElementById('cfgSubnet0').value = data.subnet[0];
document.getElementById('cfgSubnet1').value = data.subnet[1];
document.getElementById('cfgSubnet2').value = data.subnet[2];
document.getElementById('cfgSubnet3').value = data.subnet[3];
}
if(data.port !== undefined){
document.getElementById('cfgPort').value = data.port;
}
if(data.hz_cam_1 !== undefined) document.getElementById('cfgHzCam1').value = data.hz_cam_1;
if(data.hz_cam_2 !== undefined) document.getElementById('cfgHzCam2').value = data.hz_cam_2;
if(data.hz_cam_3 !== undefined) document.getElementById('cfgHzCam3').value = data.hz_cam_3;
if(data.hz_cam_4 !== undefined) document.getElementById('cfgHzCam4').value = data.hz_cam_4;
if(data.hz_imu_2 !== undefined) document.getElementById('cfgHzImu2').value = data.hz_imu_2;
if(data.uart_0_baud_rate !== undefined){
setSelectValue('cfgUart0Baud', data.uart_0_baud_rate);
}
if(data.uart_1_baud_rate !== undefined){
setSelectValue('cfgUart1Baud', data.uart_1_baud_rate);
}
if(data.uart_2_baud_rate !== undefined){
setSelectValue('cfgUart2Baud', data.uart_2_baud_rate);
}
if(data.version !== undefined){
setSelectValue('cfgVersion', data.version);
document.getElementById('statusVersion').textContent = data.version;
}
if(data.xtal_diff !== undefined){
document.getElementById('cfgXtalDiff').value = data.xtal_diff;