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/GaugeDisplay.vue | 74 ++++++++----------------------------- 1 files changed, 16 insertions(+), 58 deletions(-) diff --git a/src/components/GaugeDisplay.vue b/src/components/GaugeDisplay.vue index a322a3f..2338932 100644 --- a/src/components/GaugeDisplay.vue +++ b/src/components/GaugeDisplay.vue @@ -1,11 +1,7 @@ <script setup lang="ts"> import { ref, onMounted, onUnmounted } from 'vue'; import * as echarts from 'echarts'; -import { sendToPipe } from '../pipe_client'; - -const props = defineProps<{ - pipeName: string; -}>(); +import { createDataReceiver } from '../utils/dataFetcher'; const chartContainer = ref<HTMLElement | null>(null); let chart: echarts.ECharts | null = null; @@ -38,8 +34,8 @@ { name: 'Fx', type: 'gauge', - min: -100, - max: 100, + min: 1000, + max: 4000, splitNumber: 10, radius: '30%', center: ['16.7%', '30%'], @@ -61,8 +57,8 @@ { name: 'Fy', type: 'gauge', - min: -100, - max: 100, + min: 1000, + max: 4000, splitNumber: 10, radius: '30%', center: ['50%', '30%'], @@ -84,8 +80,8 @@ { name: 'Fz', type: 'gauge', - min: -100, - max: 100, + min: 1000, + max: 4000, splitNumber: 10, radius: '30%', center: ['83.3%', '30%'], @@ -107,8 +103,8 @@ { name: 'Mx', type: 'gauge', - min: -10, - max: 10, + min: 1000, + max: 4000, splitNumber: 10, radius: '30%', center: ['16.7%', '75%'], @@ -130,8 +126,8 @@ { name: 'My', type: 'gauge', - min: -10, - max: 10, + min: 1000, + max: 4000, splitNumber: 10, radius: '30%', center: ['50%', '75%'], @@ -153,8 +149,8 @@ { name: 'Mz', type: 'gauge', - min: -10, - max: 10, + min: 1000, + max: 4000, splitNumber: 10, radius: '30%', center: ['83.3%', '75%'], @@ -209,57 +205,19 @@ }); } -// 鐘舵�佸彉閲忥紝鐢ㄤ簬鎺у埗閿欒鏄剧ず棰戠巼 -const errorCount = ref(0); -const maxConsecutiveErrors = 5; -const showingError = ref(false); - -// 鎺ユ敹绠¢亾鏁版嵁鐨勫嚱鏁� -async function receiveData() { - try { - const response = await sendToPipe(props.pipeName, 'GET_FORCE_DATA'); - - // 閲嶇疆閿欒璁℃暟 - errorCount.value = 0; - showingError.value = false; - - // 瑙f瀽鏁版嵁 - try { - const forceData = JSON.parse(response); - if (Array.isArray(forceData) && forceData.length === 6) { - updateChart(forceData); - } - } catch (parseErr) { - console.error('鏁版嵁瑙f瀽閿欒:', parseErr); - } - } catch (err) { - // 澧炲姞閿欒璁℃暟 - errorCount.value++; - - // 鍙湪杩炵画閿欒杈惧埌闃堝�间笖灏氭湭鏄剧ず閿欒鏃舵樉绀� - if (errorCount.value >= maxConsecutiveErrors && !showingError.value) { - console.error('绠¢亾閫氫俊澶辫触:', err); - showingError.value = true; - } - } -} - -let dataInterval: number | null = null; onMounted(() => { initChart(); - // 姣�200ms鑾峰彇涓�娆℃暟鎹� - dataInterval = window.setInterval(receiveData, 200); + createDataReceiver( (forceData) => { + updateChart(forceData); + }); // 浼犻�� isSerialPortConnected 鍑芥暟 // 鐩戝惉绐楀彛澶у皬鍙樺寲锛岃皟鏁村浘琛ㄥぇ灏� window.addEventListener('resize', handleResize); }); onUnmounted(() => { - if (dataInterval !== null) { - clearInterval(dataInterval); - } if (chart) { chart.dispose(); -- Gitblit v1.9.3