-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_raw_display.py
More file actions
40 lines (29 loc) · 1.15 KB
/
test_raw_display.py
File metadata and controls
40 lines (29 loc) · 1.15 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
#!/usr/bin/env python3
"""測試:將原始資料直接顯示為波形"""
import serial
import time
import struct
PORT = '/dev/cu.usbserial-0001'
BAUDRATE = 9600
def read_and_display(duration=10):
"""讀取資料並嘗試解析為數值"""
ser = serial.Serial(port=PORT, baudrate=BAUDRATE, timeout=1.0)
print("開始讀取資料...")
print("假設:每個 byte 代表一個 EMG 樣本值")
start_time = time.time()
sample_count = 0
while time.time() - start_time < duration:
if ser.in_waiting > 0:
data = ser.read(ser.in_waiting)
for byte in data:
# 將 byte 轉換為有符號整數 (-128 to 127)
signed_value = byte - 128 if byte > 127 else byte
sample_count += 1
if sample_count % 100 == 0:
print(f"樣本 {sample_count}: {signed_value} (原始: 0x{byte:02x})")
time.sleep(0.01)
ser.close()
print(f"\n共收到 {sample_count} 個樣本")
print(f"採樣率約: {sample_count / duration:.1f} Hz")
if __name__ == '__main__':
read_and_display(5)