baoshiwei
2025-05-21 832991a036bcb0d99a66d2d1f059253136ddd622
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
<script setup lang="ts">
import { ref, onMounted, onUnmounted } from 'vue';
import { sendToPipe } from '../pipe_client';
 
const props = defineProps<{
  pipeName: string;
}>();
 
interface ForceData {
  timestamp: string;
  fx: number;
  fy: number;
  fz: number;
  mx: number;
  my: number;
  mz: number;
}
 
const tableData = ref<ForceData[]>([]);
const maxRows = 100; // 最多显示100条记录
 
// 更新表格数据
function updateTable(forceData: number[]) {
  const now = new Date();
  const timeStr = `${now.getHours().toString().padStart(2, '0')}:${now.getMinutes().toString().padStart(2, '0')}:${now.getSeconds().toString().padStart(2, '0')}`;
  
  const newData: ForceData = {
    timestamp: timeStr,
    fx: forceData[0],
    fy: forceData[1],
    fz: forceData[2],
    mx: forceData[3],
    my: forceData[4],
    mz: forceData[5]
  };
  
  tableData.value.unshift(newData);
  
  // 限制数据行数
  if (tableData.value.length > maxRows) {
    tableData.value.pop();
  }
}
 
// 状态变量,用于控制错误显示频率
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;
    
    // 解析数据
    try {
      const forceData = JSON.parse(response);
      if (Array.isArray(forceData) && forceData.length === 6) {
        updateTable(forceData);
      }
    } catch (parseErr) {
      console.error('数据解析错误:', parseErr);
    }
  } catch (err) {
    // 增加错误计数
    errorCount.value++;
    
    // 只在连续错误达到阈值且尚未显示错误时显示
    if (errorCount.value >= maxConsecutiveErrors && !showingError.value) {
      console.error('管道通信失败:', err);
      showingError.value = true;
    }
  }
}
 
let dataInterval: number | null = null;
 
onMounted(() => {
  // 每500ms获取一次数据
  dataInterval = window.setInterval(receiveData, 500);
});
 
onUnmounted(() => {
  if (dataInterval !== null) {
    clearInterval(dataInterval);
  }
});
</script>
 
<template>
  <div class="table-container">
    <table class="force-table">
      <thead>
        <tr>
          <th>时间</th>
          <th>Fx (N)</th>
          <th>Fy (N)</th>
          <th>Fz (N)</th>
          <th>Mx (Nm)</th>
          <th>My (Nm)</th>
          <th>Mz (Nm)</th>
        </tr>
      </thead>
      <tbody>
        <tr v-for="(item, index) in tableData" :key="index">
          <td>{{ item.timestamp }}</td>
          <td>{{ item.fx.toFixed(2) }}</td>
          <td>{{ item.fy.toFixed(2) }}</td>
          <td>{{ item.fz.toFixed(2) }}</td>
          <td>{{ item.mx.toFixed(2) }}</td>
          <td>{{ item.my.toFixed(2) }}</td>
          <td>{{ item.mz.toFixed(2) }}</td>
        </tr>
        <tr v-if="tableData.length === 0">
          <td colspan="7" class="no-data">暂无数据</td>
        </tr>
      </tbody>
    </table>
  </div>
</template>
 
<style scoped>
.table-container {
  width: 98%;
  height: 97%;
  overflow-y: auto;
  margin: 10px;
}
 
.force-table {
  width: 100%;
  border-collapse: collapse;
  font-size: 14px;
}
 
.force-table th,
.force-table td {
  border: 1px solid #ddd;
  padding: 8px;
  text-align: center;
}
 
.force-table th {
  background-color: #f2f2f2;
  position: sticky;
  top: 0;
  z-index: 1;
}
 
.force-table tr:nth-child(even) {
  background-color: #f9f9f9;
}
 
.force-table tr:hover {
  background-color: #f0f0f0;
}
 
.no-data {
  text-align: center;
  padding: 20px;
  color: #999;
}
</style>