From 2ad852ee08e21ee681950f1d6058499248baf88e Mon Sep 17 00:00:00 2001 From: baoshiwei <baoshiwei@shlanbao.cn> Date: 星期五, 18 七月 2025 15:04:22 +0800 Subject: [PATCH] 完成串口读取数据和处理 --- src/components/ForceChart.vue | 79 ++++++++++----------------------------- 1 files changed, 20 insertions(+), 59 deletions(-) diff --git a/src/components/ForceChart.vue b/src/components/ForceChart.vue index 138a1c3..b3b414c 100644 --- a/src/components/ForceChart.vue +++ b/src/components/ForceChart.vue @@ -1,11 +1,8 @@ <script setup lang="ts"> -import { ref, onMounted, onUnmounted} from 'vue'; +import { ref, onMounted, onUnmounted } from 'vue'; import * as echarts from 'echarts'; -import { sendToPipe } from '../pipe_client'; +import { createDataReceiver } from '../utils/dataFetcher'; -const props = defineProps<{ - pipeName: string; -}>(); const chartContainer = ref<HTMLElement | null>(null); let chart: echarts.ECharts | null = null; @@ -64,37 +61,43 @@ name: 'Fx', type: 'line', data: dataHistory.value[0], - smooth: true + smooth: true, + showSymbol: false }, { name: 'Fy', type: 'line', data: dataHistory.value[1], - smooth: true + smooth: true, + showSymbol: false }, { name: 'Fz', type: 'line', data: dataHistory.value[2], - smooth: true + smooth: true, + showSymbol: false }, { name: 'Mx', type: 'line', data: dataHistory.value[3], - smooth: true + smooth: true, + showSymbol: false }, { name: 'My', type: 'line', data: dataHistory.value[4], - smooth: true + smooth: true, + showSymbol: false }, { name: 'Mz', type: 'line', data: dataHistory.value[5], - smooth: true + smooth: true, + showSymbol: false } ] }; @@ -150,60 +153,18 @@ chart?.resize(); } -// 鐘舵�佸彉閲忥紝鐢ㄤ簬鎺у埗閿欒鏄剧ず棰戠巼 -const errorCount = ref(0); -const maxConsecutiveErrors = 5; -const showingError = ref(false); - -// 鎺ユ敹绠¢亾鏁版嵁鐨勫嚱鏁� -async function receiveForceData() { - try { - // 浠庣閬撴帴鏀舵暟鎹� - const response = await sendToPipe(props.pipeName, 'GET_FORCE_DATA'); - - // 鎴愬姛鎺ユ敹鏁版嵁锛岄噸缃敊璇鏁� - errorCount.value = 0; - if (showingError.value) { - showingError.value = false; - console.log('绠¢亾閫氫俊宸叉仮澶�'); - } - - // 瑙f瀽鎺ユ敹鍒扮殑鏁版嵁 - try { - const forceData = JSON.parse(response); - if (Array.isArray(forceData) && forceData.length === 6) { - updateChart(forceData); - } - } catch (e) { - console.warn('瑙f瀽鏁版嵁澶辫触:', e); - } - } catch (err) { - // 澧炲姞閿欒璁℃暟 - errorCount.value++; - - // 鍙湪杩炵画閿欒杈惧埌闃堝�兼椂鏄剧ず閿欒淇℃伅锛岄伩鍏嶆棩蹇楀埛灞� - if (errorCount.value >= maxConsecutiveErrors && !showingError.value) { - showingError.value = true; - console.error('绠¢亾閫氫俊鎸佺画澶辫触锛岃妫�鏌ユ湇鍔$鐘舵��:', err); - } - } -} - -// 瀹氭椂鑾峰彇鏁版嵁 -let dataTimer: number | null = null; - onMounted(() => { initChart(); + createDataReceiver( (forceData) => { + console.log('forceData', forceData); + updateChart(forceData); + }); // 浼犻�� isSerialPortConnected 鍑芥暟 - // 姣忕鑾峰彇涓�娆℃暟鎹� - dataTimer = window.setInterval(receiveForceData, 1000); + // 鐩戝惉绐楀彛澶у皬鍙樺寲锛岃皟鏁村浘琛ㄥぇ灏� + window.addEventListener('resize', handleResize); }); onUnmounted(() => { - // 娓呯悊瀹氭椂鍣ㄥ拰浜嬩欢鐩戝惉 - if (dataTimer !== null) { - clearInterval(dataTimer); - } window.removeEventListener('resize', handleResize); -- Gitblit v1.9.3