为什么识别到图像后数据通过串口发不到stm32上去
-
请在这里粘贴代码import sensor, image, time import json from machine import UART, Pin import struct # 硬件初始化 sensor.reset() sensor.set_pixformat(sensor.RGB565) sensor.set_framesize(sensor.QQVGA) sensor.skip_frames(2000) sensor.set_auto_gain(False) sensor.set_auto_whitebal(False) # 颜色阈值 (LAB格式) red_threshold = (56, 14, 70, 37, -30, 46) # 红色 yellow_threshold = (46, 100, -68, 72, 58, 92) # 黄色 # 外设初始化 uart = UART(1, baudrate=115200, timeout_char=100) led = Pin("LED_RED", Pin.OUT) # 使用板载LED作为传输指示灯 # 传输统计变量 success_count = 0 fail_count = 0 last_status = None def send_to_stm32(obj_type, x, y): """发送协议帧并返回状态""" global success_count, fail_count, last_status try: # 数据打包(小端格式) packet = struct.pack('<BiiB', obj_type, # 1字节类型 int(x), # 4字节X坐标 int(y), # 4字节Y坐标 0xAA) # 结束符 # 清空缓冲区 while uart.any(): uart.read() # 发送数据(LED亮起) led.on() uart.write(packet) # 等待确认(300ms超时) start = time.ticks_ms() while time.ticks_diff(time.ticks_ms(), start) < 300: if uart.any() and uart.read(1) == b'\x55': success_count += 1 last_status = True led.off() return True # 发送失败处理 fail_count += 1 last_status = False led.off() return False except Exception as e: print("Send error:", e) fail_count += 1 last_status = False led.off() return False def display_status(img): """在图像上显示传输状态""" # 状态文字颜色 color = (0, 255, 0) if last_status else (255, 0, 0) # 绘制状态信息 img.draw_string(0, 0, f"TX: {'OK' if last_status else 'FAIL'}", color=color, scale=2) img.draw_string(0, 20, f"Success: {success_count}", color=(0, 255, 0), scale=1) img.draw_string(0, 40, f"Fail: {fail_count}", color=(255, 0, 0), scale=1) img.draw_string(0, 60, f"FPS: {clock.fps():.1f}", color=(255, 255, 0), scale=1) clock = time.clock() while True: clock.tick() img = sensor.snapshot().lens_corr(1.8) # 红色圆形检测与发送 red_sent = False for c in img.find_circles(threshold=3500, r_min=2, r_max=100): area = (c.x()-c.r(), c.y()-c.r(), 2*c.r(), 2*c.r()) stats = img.get_statistics(roi=area) if (0 < stats.l_mode() < 100 and 0 < stats.a_mode() < 127 and 0 < stats.b_mode() < 127): img.draw_circle(c.x(), c.y(), c.r(), color=(255,0,0)) if not red_sent: # 避免重复发送 send_to_stm32(0x01, c.x(), c.y()) red_sent = True # 黄色块检测与发送 yellow_sent = False for b in img.find_blobs([yellow_threshold]): img.draw_rectangle(b.rect(), color=(255,255,0)) img.draw_cross(b.cx(), b.cy(), color=(255,255,0)) if not yellow_sent: send_to_stm32(0x02, b.cx(), b.cy()) yellow_sent = True # 显示传输状态 display_status(img) # 调试输出 if clock.fps() > 10: # 控制打印频率 status = "Success" if last_status else "Fail" print(f"{status} | Success: {success_count} Fail: {fail_count} FPS: {clock.fps():.1f}")
-
单独写一个uart.write的程序,然后用示波器看波形,然后在stm32上看有没有收到