python
import serial
import pyautogui
import time
# 设置串口参数
port = 'COM5' # 串口号
baudrate = 115200 # 波特率
# 打开串口
ser = serial.Serial(port, baudrate)
while True:
if ser.in_waiting > 0:
# 读取串口数据
data = ser.readline()
if float(data.strip().decode('utf-8')) > 6: # 左
pyautogui.press('left')
if float(data.strip().decode('utf-8')) < -6: # 右
pyautogui.press('right')
# 处理数据
# TODO: 添加你的处理逻辑
# 输出数据
print(data.strip().decode('utf-8'))
# time.sleep(1);
ser.reset_input_buffer() # 清空缓冲区
matlab
delete(instrfindall); % 删除所有打开的串口
% 创建串口对象
s = serial('COM8', 'BaudRate', 115200, 'DataBits', 8, 'StopBits', 1, 'Parity', 'none', 'FlowControl', 'none');
s.ReadAsyncMode = 'continuous';
fopen(s); % 打开串口
fwrite(s, 'm');
% 循环读取串口数据
numReadings = 4096; % 设置读取数据的次数
data = zeros(numReadings, 1); % 存储读取的数据
for i = 1:numReadings
data(i) = str2double(fgetl(s)); % 读取数据并转换为数值
end
disp(data)
% 关闭串口连接
fclose(s);
delete(s);
clear s;
% 绘制波形图
figure;
plot(data);
title('波形图');
xlabel('样本');
% ylabel('电压值');
ylabel('ADC值');
Fs = 2000000; % 采样频率
N = 4096; % 采样点数
% 执行FFT
X = fft(data,N); % 对信号进行FFT
% 计算频谱幅值
X_mag = abs(X)/N; % 将FFT结果归一化
f_axis = (0:N-1)*Fs/N; % 计算频率向量
% 绘制频谱图
figure;
plot(f_axis,X_mag)
xlabel('Frequency (Hz)')
ylabel('Magnitude')
title('Spectrum of a 1kHz sine wave')