在循环中,串口应如何关闭?
-
我想用OPEN MV串口3接收外界发送的一个信号,判断此信号是否为“PIC”,然后是就通过串口发送一个字符串“Finish1”,不是就发送“Unfinish1”,用串口调试助手调试时发送一个“PIC”,返回一个“Finish1”,但接着返回了很多“Unfinish1”.我猜测是代码中循环时应该在第一个else里把串口关闭一次或者是我的判定有问题。代码如下:
import sensor, image, pyb,time from pyb import UART RED_LED_PIN = 1 BLUE_LED_PIN = 3 uart = UART(3, 115200, timeout_char=1000) sensor.reset() sensor.set_pixformat(sensor.RGB565) sensor.set_framesize(sensor.VGA) sensor.skip_frames(time = 500) pyb.LED(RED_LED_PIN).off() pyb.LED(BLUE_LED_PIN).on() sensor.skip_frames(time = 500) print("You're on camera!") count = 0 while(True): if uart.any(): a = uart.readline().decode() if a == "PIC": count = count + 1 sensor.snapshot().save("%d.jpg"%(count)) pyb.LED(1).on() pyb.LED(3).off() print("Done! Reset the camera to see the saved image.") uart.write("Finish2") print("Send Finish2.") time.sleep(100) pyb.LED(1).off() pyb.LED(3).on() a = "\n" else: uart.write("Unfinish2") print("Send Unfinish2.") a = "\n" else: sensor.snapshot() time.sleep(100)
-
在第一个else那里应该a=None或者a=0