logo

openmv接受字符串时,为啥会报错

2021-02-08 10:25
B
krfa
5f1fa1e2-330c-4515-8997-7a434cbedc85.png
import sensor, image, time, math
from pyb import UART
import json
import ustruct
uart = UART(1,115200)
uart.init(115200, bits=8, parity=None, stop=1)
thresholdss = (4, 15, 4, 28, 1, 19)
thresholds = [(12, 50, 30, 79, 12, 127), # generic_red_thresholds
              (4, 94, -87, -20, -28, 27), # generic_green_thresholds
              (21, 54, -37, 51, -87, -13)] # generic_blue_thresholds
sensor.reset()
sensor.set_pixformat(sensor.RGB565)
sensor.set_framesize(sensor.QQVGA)
sensor.skip_frames(time = 2000)
sensor.set_auto_gain(False)
sensor.set_auto_whitebal(False)
clock = time.clock()
b=0
red_cx = 1    #必须要定义,否则会有语法错误
green_cx = 2
blue_cx = 24
def sending_data(cx,cy):
	global uart;
	data = ustruct.pack("<bbhhb",
				   0x2C,
				   0x12,
				   int(cx),
				   int(cy),
				   0x5B)
	uart.write(data);
def find_max(circles):
	max_size=0
	for circle in circles:
		if circle.magnitude() > max_size:
			max_circle=circle
			max_size = circle.magnitude()
	return max_circle
while(True):
    if uart.any ():
        a=uart.readline().decode()
        b=int(a)
        print(a)
    if(b==1):
        clock.tick()
        img = sensor.snapshot()
        for blob in img.find_blobs([thresholdss], merge=True, pixels_threshold=200, area_threshold=200, merge=True):
            ratio = blob.w() / blob.h()
            if (ratio >= 0.5) and (ratio <= 1.5):
                img.draw_rectangle(blob.rect())
                img.draw_cross(blob.cx(), blob.cy())
                sending_data(blob.cx(), blob.cy());
                
    if(b==0):    
        clock.tick()
        img = sensor.snapshot()
        for blob in img.find_blobs([thresholds[0]], pixels_threshold=200, area_threshold=200, merge=True):
                        img.draw_rectangle(blob.rect())
                        img.draw_cross(blob.cx(), blob.cy())
                        red_cx = blob.cx()
        for blob in img.find_blobs([thresholds[1]], pixels_threshold=200, area_threshold=200, merge=True):
                        img.draw_rectangle(blob.rect())
                        img.draw_cross(blob.cx(), blob.cy())
                        green_cx = blob.cx()
        for blob in img.find_blobs([thresholds[2]], pixels_threshold=200, area_threshold=200, merge=True):
                        img.draw_rectangle(blob.rect())
                        img.draw_cross(blob.cx(), blob.cy())
                        blue_cx = blob.cx()
        if red_cx > green_cx and green_cx > blue_cx :
                                    print("123")
                                    uart.write("a\r\n")
        elif  red_cx > blue_cx and blue_cx > green_cx :
                                    print("132")
                                    uart.write("b\r\n")
        elif  green_cx > blue_cx and blue_cx > red_cx :
                                    print("231")
                                    uart.write("c\r\n")
        elif  green_cx > red_cx and red_cx > blue_cx :
                                    print("213")
                                    uart.write("d\r\n")
        elif  blue_cx > green_cx and green_cx > red_cx :
                                    print("321")
                                    uart.write("e\r\n")
        elif  blue_cx > red_cx and red_cx > green_cx :
                                    print("312")
                                    uart.write("f\r\n")
2021-02-19 14:31
I
网红小智智 星瞳科技

因为接收到的不是unicode字符串,你在第40行上看看读取的是啥。

a = uart.readline()
print(a)
a = a.decode()