问题:在使用K210时使用官方介绍的串口通信,发送的数据为八位的数据,但是在使用中需要十六位的,因为所需数据可能涉及到百位。
解决方法:将数据打包后发送。
一下为打包函数:
def sending_data(cx,cy,ch):global uart;#frame=[0x2C,18,cx%0xff,int(cx/0xff),cy%0xff,int(cy/0xff),0x5B];#data = bytearray(frame)data = ustruct.pack("<bbhhhb", #格式为俩个字符俩个短整型(2字节)0x2C, #帧头10x12, #帧头2int(cx), # up sample by 4 #数据1int(cy), # up sample by 4 #数据2int(ch),0x5B)uart = UART(UART.UART1, 115200, 8, 1, 0, timeout=1000, read_buf_len=4096)uart.write(data); #必须要传入一个字节数组
MAIX BIT K210端完整代码
# object detector boot.py
# generated by maixhub.comimport sensor, image, lcd, time
import KPU as kpu
import gc, sysimport ustruct#LED
from fpioa_manager import fm
from Maix import GPIO
io_led_red = 13
io_led_bule = 14
io_led_green = 12
fm.register(io_led_red, fm.fpioa.GPIO0)
fm.register(io_led_bule, fm.fpioa.GPIO1)
fm.register(io_led_green, fm.fpioa.GPIO2)
led_r=GPIO(GPIO.GPIO0, GPIO.OUT)
led_b=GPIO(GPIO.GPIO1, GPIO.OUT)
led_g=GPIO(GPIO.GPIO2, GPIO.OUT)#串口
from fpioa_manager import fm
from machine import UART
# need your connect hardware IO 10/11 to loopback
fm.register(10, fm.fpioa.UART1_TX, force=True)
fm.register(9, fm.fpioa.UART1_RX, force=True)def sending_data(cx,cy,ch):global uart;#frame=[0x2C,18,cx%0xff,int(cx/0xff),cy%0xff,int(cy/0xff),0x5B];#data = bytearray(frame)data = ustruct.pack("<bbhhhb", #格式为俩个字符俩个短整型(2字节)0x2C, #帧头10x12, #帧头2int(cx), # up sample by 4 #数据1int(cy), # up sample by 4 #数据2int(ch),0x5B)uart = UART(UART.UART1, 115200, 8, 1, 0, timeout=1000, read_buf_len=4096)uart.write(data); #必须要传入一个字节数组def lcd_show_except(e):import uioerr_str = uio.StringIO()sys.print_exception(e, err_str)err_str = err_str.getvalue()img = image.Image(size=(224,224))img.draw_string(0, 10, err_str, scale=1, color=(0xff,0x00,0x00))lcd.display(img)def main(anchors, labels = None, model_addr="/sd/m.kmodel", sensor_window=(240, 240), lcd_rotation=0, sensor_hmirror=False, sensor_vflip=False):sensor.reset()sensor.set_pixformat(sensor.RGB565)sensor.set_framesize(sensor.QVGA)sensor.set_windowing(sensor_window)sensor.set_hmirror(sensor_hmirror)sensor.set_vflip(sensor_vflip)sensor.run(1)lcd.init(type=1)lcd.rotation(lcd_rotation)lcd.clear(lcd.WHITE)#uart = UART(UART.UART1, 500000, 8, 1, 0, timeout=1000, read_buf_len=4096)#白灯开机指示led_r.value(0)led_b.value(0)led_g.value(0)if not labels:with open('labels.txt','r') as f:exec(f.read())if not labels:print("no labels.txt")img = image.Image(size=(320, 240))img.draw_string(90, 110, "no labels.txt", color=(255, 0, 0), scale=2)lcd.display(img)return 1try:img = image.Image("startup.jpg")lcd.display(img)except Exception:img = image.Image(size=(320, 240))img.draw_string(90, 110, "loading model...", color=(255, 255, 255), scale=2)lcd.display(img)try:task = Nonetask = kpu.load(model_addr)kpu.init_yolo2(task, 0.5, 0.3, 5, anchors) # threshold:[0,1], nms_value: [0, 1]while(True):img = sensor.snapshot()t = time.ticks_ms()objects = kpu.run_yolo2(task, img)t = time.ticks_ms() - tif objects:for obj in objects:pos = obj.rect()img.draw_rectangle(pos)img.draw_string(pos[0], pos[1], "%s : %.2f" %(labels[obj.classid()], obj.value()), scale=2, color=(255, 0, 0))#串口发送数据objx = (obj.x()+obj.w())/2objy = (obj.y()+obj.h())/2#uart.write("AB%sD%dE%dF" %(labels[obj.classid()], objx, objy))#print("AB%sD%dE%dF" %(labels[obj.classid()], objx, objy))if labels[obj.classid()] == "WWW":sending_data(1,objx,objy)led_r.value(0)if labels[obj.classid()] == "CAR":sending_data(2,objx,objy)led_b.value(0)if labels[obj.classid()] == "HHH":sending_data(3,objx,objy)led_g.value(0)else:sending_data(0,0,0)led_r.value(1)led_b.value(1)led_g.value(1)#print(00000)img.draw_string(0, 200, "t:%dms" %(t), scale=2, color=(255, 0, 0))lcd.display(img)except Exception as e:raise efinally:if not task is None:kpu.deinit(task)if __name__ == "__main__":try:labels = ["CAR", "WWW", "HHH"]anchors = [5.5, 6.90625, 5.0, 5.0, 4.1875, 4.125, 6.875, 5.25, 10.5, 9.9375]# main(anchors = anchors, labels=labels, model_addr=0x300000, lcd_rotation=0)main(anchors = anchors, labels=labels, model_addr="/sd/m.kmodel")except Exception as e:sys.print_exception(e)lcd_show_except(e)finally:gc.collect()
单片机端的数据处理代码
//Maix字节处理
void Maix_Byte_Get(u8 bytedata){static u8 rec_sta;//u8 check_val=0;maix_buf[rec_sta] = bytedata;if(rec_sta==0){if(bytedata==0x2C){rec_sta=1;}else{rec_sta=0;}}else if(rec_sta==1){if(bytedata==0x12){rec_sta=2;}else{rec_sta=0;}}else if(rec_sta==2){rec_sta=3;}else if(rec_sta == 3){maix.clas = maix_buf[3]<<8|maix_buf[2];rec_sta=4;}else if(rec_sta ==4){rec_sta=5;}else if(rec_sta == 5){maix.pos_x = maix_buf[5]<<8|maix_buf[4];rec_sta=6;}else if(rec_sta == 6){rec_sta=7;}else if(rec_sta == 7){maix.pos_y = maix_buf[7]<<8|maix_buf[6];rec_sta = 8;}else if(rec_sta == 8){rec_sta = 9;}else if(rec_sta ==9){rec_sta = 0;}}